-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprometheus_publish_test.cpp
124 lines (107 loc) · 5.17 KB
/
prometheus_publish_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <catch2/catch_all.hpp>
#include <sstream>
#include <cxxmetrics_prometheus/prometheus_publisher.hpp>
#include <cxxmetrics/simple_reservoir.hpp>
using namespace cxxmetrics;
using namespace cxxmetrics_literals;
using namespace cxxmetrics_prometheus;
TEST_CASE("Prometheus Publisher can publish counter values", "[prometheus]")
{
metrics_registry<> r;
prometheus_publisher<decltype(r)::repository_type> subject(r);
*r.counter("1MyCounter%#@#(@#:][\\"/"COUNTER"_m, {{"tag_name2^$", "tag_value\"with quotes\""}, {"x2", 123523}}) += 1200100;
std::stringstream stream;
subject.write(stream);
auto out = stream.str();
WARN(out);
REQUIRE_THAT(out, Catch::Matchers::StartsWith( "# TYPE ") &&
Catch::Matchers::ContainsSubstring(" untyped") &&
Catch::Matchers::ContainsSubstring("1MyCounter") &&
Catch::Matchers::ContainsSubstring("1200100") &&
Catch::Matchers::ContainsSubstring(":COUNTER") &&
Catch::Matchers::ContainsSubstring("tag_value\\\"with quotes\\\"") &&
Catch::Matchers::ContainsSubstring("x2=\"123523\"") &&
Catch::Matchers::ContainsSubstring("tag_name2__"));
}
TEST_CASE("Prometheus Publisher can publish gauge values", "[prometheus]")
{
double metric_value = 923.005;
metrics_registry<> r;
prometheus_publisher<decltype(r)::repository_type> subject(r);
r.gauge("MyGauge"/"value"_m, metric_value, {{"tag_name2", "tag_value"}, {"x2", 123523}});
std::stringstream stream;
subject.write(stream);
auto out = stream.str();
WARN(out);
REQUIRE_THAT(out, Catch::Matchers::StartsWith( "# TYPE MyGauge:value gauge") &&
Catch::Matchers::ContainsSubstring("MyGauge:value") &&
Catch::Matchers::ContainsSubstring("tag_name2=\"tag_value\"") &&
Catch::Matchers::ContainsSubstring("923.005") &&
Catch::Matchers::ContainsSubstring("x2=\"123523\""));
}
TEST_CASE("Prometheus Publisher can publish meter values", "[prometheus]")
{
metrics_registry<> r;
prometheus_publisher<decltype(r)::repository_type> subject(r);
auto& m = *r.meter<1_sec, 1_min, 1_sec, 5_min>("MyMeter", {{"tag_name2", "tag_value"}, {"x2", 123523}});
for (int i = 0; i < 10; i++)
m.mark(10000);
std::stringstream stream;
subject.write(stream);
auto out = stream.str();
WARN(out);
REQUIRE_THAT(out, Catch::Matchers::StartsWith( "# TYPE MyMeter gauge") &&
Catch::Matchers::ContainsSubstring("MyMeter") &&
Catch::Matchers::ContainsSubstring("tag_name2=\"tag_value\"") &&
Catch::Matchers::ContainsSubstring("mean") &&
Catch::Matchers::ContainsSubstring("1sec") &&
Catch::Matchers::ContainsSubstring("1min") &&
Catch::Matchers::ContainsSubstring("5min") &&
Catch::Matchers::ContainsSubstring("x2=\"123523\""));
}
TEST_CASE("Prometheus Publisher can publish histogram values", "[prometheus]")
{
metrics_registry<> r;
prometheus_publisher<decltype(r)::repository_type> subject(r);
auto& hist = *r.histogram("MyHistogram"_m, cxxmetrics::simple_reservoir<int64_t, 100>(), {{"mytag","tagvalue2"}});
for (int i = 1; i <= 100; i++)
hist.update(i * 97);
std::stringstream stream;
subject.write(stream);
auto out = stream.str();
WARN(out);
REQUIRE_THAT(out, Catch::Matchers::ContainsSubstring( "# TYPE MyHistogram summary") &&
Catch::Matchers::ContainsSubstring("MyHistogram_count") &&
Catch::Matchers::ContainsSubstring("mytag=\"tagvalue2\"") &&
Catch::Matchers::ContainsSubstring(".5") &&
Catch::Matchers::ContainsSubstring(".9") &&
Catch::Matchers::ContainsSubstring(".99"));
}
TEST_CASE("Prometheus Publisher can publish timer values", "[prometheus]")
{
using reservoir_type = simple_reservoir<std::chrono::system_clock::duration, 4>;
metrics_registry<> r;
prometheus_publisher<decltype(r)::repository_type> subject(r);
auto& t = *r.timer<100_micro, std::chrono::system_clock, reservoir_type, true, 5_min, 1_min, 10_sec>("MyTimer", reservoir_type(), {{"tag_name2", "tag_value"}, {"x2", 123523}});
t.update(std::chrono::microseconds(1000));
t.update(std::chrono::microseconds(10));
t.update(std::chrono::microseconds(20));
t.update(std::chrono::microseconds(40));
t.update(std::chrono::microseconds(80));
std::stringstream stream;
subject.write(stream);
auto out = stream.str();
WARN(out);
REQUIRE_THAT(out, Catch::Matchers::ContainsSubstring( "# TYPE MyTimer summary") &&
Catch::Matchers::ContainsSubstring("MyTimer_count") &&
Catch::Matchers::ContainsSubstring(".5") &&
Catch::Matchers::ContainsSubstring(".9") &&
Catch::Matchers::ContainsSubstring(".99") &&
Catch::Matchers::ContainsSubstring("tag_name2=\"tag_value\"") &&
Catch::Matchers::ContainsSubstring("mean") &&
Catch::Matchers::ContainsSubstring("10sec") &&
Catch::Matchers::ContainsSubstring("1usec") &&
Catch::Matchers::ContainsSubstring("1min") &&
Catch::Matchers::ContainsSubstring("5min") &&
Catch::Matchers::ContainsSubstring("x2=\"123523\""));
}