20 #include "glog/logging.h" 21 #include "gmock/gmock.h" 22 #include "gtest/gtest.h" 23 #include "prometheus/exposer.h" 28 namespace prometheus {
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}};
44 "/algorithm/scores",
"Scores achieved", boundaries);
45 kScoresMetric = scores_family->Add({{kLabelKey, kLabelValue}});
48 for (
double score : kObserveScores) {
49 kScoresMetric->Observe(score);
54 TEST(MetricsTest, CollectCounter) {
55 FamilyFactory factory;
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;
63 std::shared_ptr<::prometheus::Collectable> collectable;
64 CHECK(collectable = factory.GetCollectable().lock());
65 collected = collectable->Collect();
67 ASSERT_EQ(collected.size(), 1);
68 ASSERT_EQ(collected[0].metric_size(), 1);
70 collected[0].metric(0).label(),
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));
80 TEST(MetricsTest, CollectGauge) {
81 FamilyFactory factory;
83 factory.NewGaugeFamily(
"/test/queue/length",
"Length of some queue");
84 kGauge = gauge_family->Add({{kLabelKey, kLabelValue}});
89 double expected_value = 1 + 5 - 1 - 2;
90 std::vector<::io::prometheus::client::MetricFamily> collected;
92 std::shared_ptr<::prometheus::Collectable> collectable;
93 CHECK(collectable = factory.GetCollectable().lock());
94 collected = collectable->Collect();
96 ASSERT_EQ(collected.size(), 1);
97 ASSERT_EQ(collected[0].metric_size(), 1);
99 collected[0].metric(0).label(),
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));
109 TEST(MetricsTest, CollectHistogram) {
110 FamilyFactory registry;
111 Algorithm::RegisterMetrics(®istry);
115 std::vector<::io::prometheus::client::MetricFamily> collected;
117 std::shared_ptr<::prometheus::Collectable> collectable;
118 CHECK(collectable = registry.GetCollectable().lock());
119 collected = collectable->Collect();
121 ASSERT_EQ(collected.size(), 1);
122 ASSERT_EQ(collected[0].metric_size(), 1);
124 collected[0].metric(0).label(),
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);
135 TEST(MetricsTest, RunExposerServer) {
136 FamilyFactory registry;
137 Algorithm::RegisterMetrics(®istry);
139 ::prometheus::Exposer exposer(
"0.0.0.0:9100");
140 exposer.RegisterCollectable(registry.GetCollectable());
static Histogram * Null()
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)
static BucketBoundaries FixedWidth(double width, int num_finite_buckets)
void Run(const std::string &configuration_directory, const std::string &configuration_basename)
TEST(TrajectoryConnectivityStateTest, UnknownTrajectory)