metrics_test.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2018 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "cartographer/cloud/metrics/prometheus/family_factory.h"
00018 #include "cartographer/metrics/family_factory.h"
00019 #include "cartographer/metrics/register.h"
00020 #include "glog/logging.h"
00021 #include "gmock/gmock.h"
00022 #include "gtest/gtest.h"
00023 #include "prometheus/exposer.h"
00024 #include "prometheus/metric_family.h"
00025 
00026 namespace cartographer {
00027 namespace cloud {
00028 namespace metrics {
00029 namespace prometheus {
00030 namespace {
00031 
00032 using Label = ::prometheus::ClientMetric::Label;
00033 
00034 static auto* kCounter = ::cartographer::metrics::Counter::Null();
00035 static auto* kGauge = ::cartographer::metrics::Gauge::Null();
00036 static auto* kScoresMetric = ::cartographer::metrics::Histogram::Null();
00037 
00038 const char kLabelKey[] = "kind";
00039 const char kLabelValue[] = "score";
00040 const std::array<double, 5> kObserveScores = {{-1, 0.11, 0.2, 0.5, 2}};
00041 
00042 class Algorithm {
00043  public:
00044   static void RegisterMetrics(::cartographer::metrics::FamilyFactory* factory) {
00045     auto boundaries = ::cartographer::metrics::Histogram::FixedWidth(0.05, 20);
00046     auto* scores_family = factory->NewHistogramFamily(
00047         "/algorithm/scores", "Scores achieved", boundaries);
00048     kScoresMetric = scores_family->Add({{kLabelKey, kLabelValue}});
00049   }
00050   void Run() {
00051     for (double score : kObserveScores) {
00052       kScoresMetric->Observe(score);
00053     }
00054   }
00055 };
00056 
00057 TEST(MetricsTest, CollectCounter) {
00058   FamilyFactory factory;
00059   auto* counter_family = factory.NewCounterFamily("/test/hits", "Hits");
00060   kCounter = counter_family->Add({{kLabelKey, kLabelValue}});
00061   kCounter->Increment();
00062   kCounter->Increment(5);
00063   double expected_value = 1 + 5;
00064   std::vector<::prometheus::MetricFamily> collected;
00065   {
00066     std::shared_ptr<::prometheus::Collectable> collectable;
00067     CHECK(collectable = factory.GetCollectable().lock());
00068     collected = collectable->Collect();
00069   }
00070   ASSERT_EQ(collected.size(), 1);
00071   ASSERT_EQ(collected[0].metric.size(), 1);
00072   EXPECT_THAT(
00073       collected[0].metric.at(0).label,
00074       testing::AllOf(
00075           testing::ElementsAre(testing::Field(&Label::name, kLabelKey)),
00076           testing::ElementsAre(testing::Field(&Label::value, kLabelValue))));
00077   EXPECT_THAT(collected[0].metric.at(0).counter.value,
00078               testing::DoubleEq(expected_value));
00079 }
00080 
00081 TEST(MetricsTest, CollectGauge) {
00082   FamilyFactory factory;
00083   auto* gauge_family =
00084       factory.NewGaugeFamily("/test/queue/length", "Length of some queue");
00085   kGauge = gauge_family->Add({{kLabelKey, kLabelValue}});
00086   kGauge->Increment();
00087   kGauge->Increment(5);
00088   kGauge->Decrement();
00089   kGauge->Decrement(2);
00090   double expected_value = 1 + 5 - 1 - 2;
00091   std::vector<::prometheus::MetricFamily> collected;
00092   {
00093     std::shared_ptr<::prometheus::Collectable> collectable;
00094     CHECK(collectable = factory.GetCollectable().lock());
00095     collected = collectable->Collect();
00096   }
00097   ASSERT_EQ(collected.size(), 1);
00098   ASSERT_EQ(collected[0].metric.size(), 1);
00099   EXPECT_THAT(
00100       collected[0].metric.at(0).label,
00101       testing::AllOf(
00102           testing::ElementsAre(testing::Field(&Label::name, kLabelKey)),
00103           testing::ElementsAre(testing::Field(&Label::value, kLabelValue))));
00104   EXPECT_THAT(collected[0].metric.at(0).gauge.value,
00105               testing::DoubleEq(expected_value));
00106 }
00107 
00108 TEST(MetricsTest, CollectHistogram) {
00109   FamilyFactory registry;
00110   Algorithm::RegisterMetrics(&registry);
00111 
00112   Algorithm algorithm;
00113   algorithm.Run();
00114   std::vector<::prometheus::MetricFamily> collected;
00115   {
00116     std::shared_ptr<::prometheus::Collectable> collectable;
00117     CHECK(collectable = registry.GetCollectable().lock());
00118     collected = collectable->Collect();
00119   }
00120   ASSERT_EQ(collected.size(), 1);
00121   ASSERT_EQ(collected[0].metric.size(), 1);
00122   EXPECT_THAT(
00123       collected[0].metric.at(0).label,
00124       testing::AllOf(
00125           testing::ElementsAre(testing::Field(&Label::name, kLabelKey)),
00126           testing::ElementsAre(testing::Field(&Label::value, kLabelValue))));
00127   EXPECT_THAT(collected[0].metric.at(0).histogram.sample_count,
00128               testing::Eq(kObserveScores.size()));
00129   EXPECT_EQ(collected[0].metric.at(0).histogram.bucket.at(0).cumulative_count,
00130             1);
00131 }
00132 
00133 TEST(MetricsTest, RunExposerServer) {
00134   FamilyFactory registry;
00135   Algorithm::RegisterMetrics(&registry);
00136   ::cartographer::metrics::RegisterAllMetrics(&registry);
00137   ::prometheus::Exposer exposer("0.0.0.0:9100");
00138   exposer.RegisterCollectable(registry.GetCollectable());
00139 
00140   Algorithm algorithm;
00141   algorithm.Run();
00142 }
00143 
00144 }  // namespace
00145 }  // namespace prometheus
00146 }  // namespace metrics
00147 }  // namespace cloud
00148 }  // namespace cartographer


cartographer
Author(s): The Cartographer Authors
autogenerated on Thu May 9 2019 02:27:35