/
text_serializer_test.cc
103 lines (82 loc) · 3.25 KB
/
text_serializer_test.cc
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
#include "prometheus/text_serializer.h"
#include <gmock/gmock.h>
#include <cmath>
#include <limits>
#include "prometheus/family.h"
#include "prometheus/gauge.h"
#include "prometheus/histogram.h"
#include "prometheus/summary.h"
namespace prometheus {
namespace {
class TextSerializerTest : public testing::Test {
public:
std::string Serialize(MetricType type) const {
MetricFamily metricFamily;
metricFamily.name = name;
metricFamily.help = "my metric help text";
metricFamily.type = type;
metricFamily.metric = std::vector<ClientMetric>{metric};
std::vector<MetricFamily> families{metricFamily};
return textSerializer.Serialize(families);
}
const std::string name = "my_metric";
ClientMetric metric;
TextSerializer textSerializer;
};
TEST_F(TextSerializerTest, shouldSerializeNotANumber) {
metric.gauge.value = std::nan("");
EXPECT_THAT(Serialize(MetricType::Gauge), testing::HasSubstr(name + " Nan"));
}
TEST_F(TextSerializerTest, shouldSerializeNegativeInfinity) {
metric.gauge.value = -std::numeric_limits<double>::infinity();
EXPECT_THAT(Serialize(MetricType::Gauge), testing::HasSubstr(name + " -Inf"));
}
TEST_F(TextSerializerTest, shouldSerializePositiveInfinity) {
metric.gauge.value = std::numeric_limits<double>::infinity();
EXPECT_THAT(Serialize(MetricType::Gauge), testing::HasSubstr(name + " +Inf"));
}
TEST_F(TextSerializerTest, shouldEscapeBackslash) {
metric.label.resize(1, ClientMetric::Label{"k", "v\\v"});
EXPECT_THAT(Serialize(MetricType::Gauge),
testing::HasSubstr(name + "{k=\"v\\\\v\"}"));
}
TEST_F(TextSerializerTest, shouldEscapeNewline) {
metric.label.resize(1, ClientMetric::Label{"k", "v\nv"});
EXPECT_THAT(Serialize(MetricType::Gauge),
testing::HasSubstr(name + "{k=\"v\\\nv\"}"));
}
TEST_F(TextSerializerTest, shouldEscapeDoubleQuote) {
metric.label.resize(1, ClientMetric::Label{"k", "v\"v"});
EXPECT_THAT(Serialize(MetricType::Gauge),
testing::HasSubstr(name + "{k=\"v\\\"v\"}"));
}
TEST_F(TextSerializerTest, shouldSerializeUntyped) {
metric.untyped.value = 64.0;
const auto serialized = Serialize(MetricType::Untyped);
EXPECT_THAT(serialized, testing::HasSubstr(name + " 64.000000"));
}
TEST_F(TextSerializerTest, shouldSerializeHistogram) {
Histogram histogram{{1}};
histogram.Observe(0);
histogram.Observe(200);
metric = histogram.Collect();
const auto serialized = Serialize(MetricType::Histogram);
EXPECT_THAT(serialized, testing::HasSubstr(name + "_count 2"));
EXPECT_THAT(serialized, testing::HasSubstr(name + "_sum 200.00000"));
EXPECT_THAT(serialized,
testing::HasSubstr(name + "_bucket{le=\"1.000000\"} 1"));
EXPECT_THAT(serialized, testing::HasSubstr(name + "_bucket{le=\"+Inf\"} 2"));
}
TEST_F(TextSerializerTest, shouldSerializeSummary) {
Summary summary{Summary::Quantiles{{0.5, 0.05}}};
summary.Observe(0);
summary.Observe(200);
metric = summary.Collect();
const auto serialized = Serialize(MetricType::Summary);
EXPECT_THAT(serialized, testing::HasSubstr(name + "_count 2"));
EXPECT_THAT(serialized, testing::HasSubstr(name + "_sum 200.00000"));
EXPECT_THAT(serialized,
testing::HasSubstr(name + "{quantile=\"0.500000\"} 0.000000"));
}
} // namespace
} // namespace prometheus