metrics_test.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2018 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
20 #include "glog/logging.h"
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 #include "prometheus/exposer.h"
24 
25 namespace cartographer {
26 namespace cloud {
27 namespace metrics {
28 namespace prometheus {
29 namespace {
30 
31 static auto* kCounter = ::cartographer::metrics::Counter::Null();
32 static auto* kGauge = ::cartographer::metrics::Gauge::Null();
33 static auto* kScoresMetric = ::cartographer::metrics::Histogram::Null();
34 
35 const char kLabelKey[] = "kind";
36 const char kLabelValue[] = "score";
37 const std::array<double, 5> kObserveScores = {{-1, 0.11, 0.2, 0.5, 2}};
38 
39 class Algorithm {
40  public:
41  static void RegisterMetrics(::cartographer::metrics::FamilyFactory* factory) {
42  auto boundaries = ::cartographer::metrics::Histogram::FixedWidth(0.05, 20);
43  auto* scores_family = factory->NewHistogramFamily(
44  "/algorithm/scores", "Scores achieved", boundaries);
45  kScoresMetric = scores_family->Add({{kLabelKey, kLabelValue}});
46  }
47  void Run() {
48  for (double score : kObserveScores) {
49  kScoresMetric->Observe(score);
50  }
51  }
52 };
53 
54 TEST(MetricsTest, CollectCounter) {
55  FamilyFactory factory;
56  auto* counter_family = factory.NewCounterFamily("/test/hits", "Hits");
57  kCounter = counter_family->Add({{kLabelKey, kLabelValue}});
58  kCounter->Increment();
59  kCounter->Increment(5);
60  double expected_value = 1 + 5;
61  std::vector<::io::prometheus::client::MetricFamily> collected;
62  {
63  std::shared_ptr<::prometheus::Collectable> collectable;
64  CHECK(collectable = factory.GetCollectable().lock());
65  collected = collectable->Collect();
66  }
67  ASSERT_EQ(collected.size(), 1);
68  ASSERT_EQ(collected[0].metric_size(), 1);
69  EXPECT_THAT(
70  collected[0].metric(0).label(),
71  testing::AllOf(
72  testing::ElementsAre(testing::Property(
73  &io::prometheus::client::LabelPair::name, kLabelKey)),
74  testing::ElementsAre(testing::Property(
75  &io::prometheus::client::LabelPair::value, kLabelValue))));
76  EXPECT_THAT(collected[0].metric(0).counter().value(),
77  testing::DoubleEq(expected_value));
78 }
79 
80 TEST(MetricsTest, CollectGauge) {
81  FamilyFactory factory;
82  auto* gauge_family =
83  factory.NewGaugeFamily("/test/queue/length", "Length of some queue");
84  kGauge = gauge_family->Add({{kLabelKey, kLabelValue}});
85  kGauge->Increment();
86  kGauge->Increment(5);
87  kGauge->Decrement();
88  kGauge->Decrement(2);
89  double expected_value = 1 + 5 - 1 - 2;
90  std::vector<::io::prometheus::client::MetricFamily> collected;
91  {
92  std::shared_ptr<::prometheus::Collectable> collectable;
93  CHECK(collectable = factory.GetCollectable().lock());
94  collected = collectable->Collect();
95  }
96  ASSERT_EQ(collected.size(), 1);
97  ASSERT_EQ(collected[0].metric_size(), 1);
98  EXPECT_THAT(
99  collected[0].metric(0).label(),
100  testing::AllOf(
101  testing::ElementsAre(testing::Property(
102  &io::prometheus::client::LabelPair::name, kLabelKey)),
103  testing::ElementsAre(testing::Property(
104  &io::prometheus::client::LabelPair::value, kLabelValue))));
105  EXPECT_THAT(collected[0].metric(0).gauge().value(),
106  testing::DoubleEq(expected_value));
107 }
108 
109 TEST(MetricsTest, CollectHistogram) {
110  FamilyFactory registry;
111  Algorithm::RegisterMetrics(&registry);
112 
113  Algorithm algorithm;
114  algorithm.Run();
115  std::vector<::io::prometheus::client::MetricFamily> collected;
116  {
117  std::shared_ptr<::prometheus::Collectable> collectable;
118  CHECK(collectable = registry.GetCollectable().lock());
119  collected = collectable->Collect();
120  }
121  ASSERT_EQ(collected.size(), 1);
122  ASSERT_EQ(collected[0].metric_size(), 1);
123  EXPECT_THAT(
124  collected[0].metric(0).label(),
125  testing::AllOf(
126  testing::ElementsAre(testing::Property(
127  &io::prometheus::client::LabelPair::name, kLabelKey)),
128  testing::ElementsAre(testing::Property(
129  &io::prometheus::client::LabelPair::value, kLabelValue))));
130  EXPECT_THAT(collected[0].metric(0).histogram().sample_count(),
131  testing::Eq(kObserveScores.size()));
132  EXPECT_EQ(collected[0].metric(0).histogram().bucket(0).cumulative_count(), 1);
133 }
134 
135 TEST(MetricsTest, RunExposerServer) {
136  FamilyFactory registry;
137  Algorithm::RegisterMetrics(&registry);
139  ::prometheus::Exposer exposer("0.0.0.0:9100");
140  exposer.RegisterCollectable(registry.GetCollectable());
141 
142  Algorithm algorithm;
143  algorithm.Run();
144 }
145 
146 } // namespace
147 } // namespace prometheus
148 } // namespace metrics
149 } // namespace cloud
150 } // namespace cartographer
virtual Family< Counter > * NewCounterFamily(const std::string &name, const std::string &description)=0
virtual Family< Histogram > * NewHistogramFamily(const std::string &name, const std::string &description, const Histogram::BucketBoundaries &boundaries)=0
void RegisterAllMetrics(FamilyFactory *registry)
Definition: register.cc:28
static BucketBoundaries FixedWidth(double width, int num_finite_buckets)
static Counter * Null()
Definition: counter.cc:33
void Run(const std::string &configuration_directory, const std::string &configuration_basename)
TEST(TrajectoryConnectivityStateTest, UnknownTrajectory)
static Gauge * Null()
Definition: gauge.cc:36


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58