forked from jupp0r/prometheus-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_serializer.cc
More file actions
190 lines (171 loc) · 5.31 KB
/
text_serializer.cc
File metadata and controls
190 lines (171 loc) · 5.31 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "prometheus/text_serializer.h"
#include <cmath>
#include <limits>
namespace prometheus {
namespace {
// Write a double as a string, with proper formatting for infinity and NaN
std::string ToString(double v) {
if (std::isnan(v)) {
return "Nan";
}
if (std::isinf(v)) {
return (v < 0 ? "-Inf" : "+Inf");
}
return std::to_string(v);
}
const std::string& EscapeLabelValue(const std::string& value,
std::string* tmp) {
bool copy = false;
for (size_t i = 0; i < value.size(); ++i) {
auto c = value[i];
if (c == '\\' || c == '"' || c == '\n') {
if (!copy) {
tmp->reserve(value.size() + 1);
tmp->assign(value, 0, i);
copy = true;
}
if (c == '\\') {
tmp->append("\\\\");
} else if (c == '"') {
tmp->append("\\\"");
} else {
tmp->append("\\\n");
}
} else if (copy) {
tmp->push_back(c);
}
}
return copy ? *tmp : value;
}
// Write a line header: metric name and labels
void WriteHead(std::ostream& out, const MetricFamily& family,
const ClientMetric& metric, const std::string& suffix = "",
const std::string& extraLabelName = "",
const std::string& extraLabelValue = "") {
out << family.name << suffix;
if (!metric.label.empty() || !extraLabelName.empty()) {
out << "{";
const char* prefix = "";
std::string tmp;
for (auto& lp : metric.label) {
out << prefix << lp.name << "=\"" << EscapeLabelValue(lp.value, &tmp)
<< "\"";
prefix = ",";
}
if (!extraLabelName.empty()) {
out << prefix << extraLabelName << "=\""
<< EscapeLabelValue(extraLabelValue, &tmp) << "\"";
}
out << "}";
}
out << " ";
}
// Write a line trailer: timestamp
void WriteTail(std::ostream& out, const ClientMetric& metric) {
if (metric.timestamp_ms != 0) {
out << " " << metric.timestamp_ms;
}
out << "\n";
}
void SerializeCounter(std::ostream& out, const MetricFamily& family,
const ClientMetric& metric) {
WriteHead(out, family, metric);
out << ToString(metric.counter.value);
WriteTail(out, metric);
}
void SerializeGauge(std::ostream& out, const MetricFamily& family,
const ClientMetric& metric) {
WriteHead(out, family, metric);
out << ToString(metric.gauge.value);
WriteTail(out, metric);
}
void SerializeSummary(std::ostream& out, const MetricFamily& family,
const ClientMetric& metric) {
auto& sum = metric.summary;
WriteHead(out, family, metric, "_count");
out << sum.sample_count;
WriteTail(out, metric);
WriteHead(out, family, metric, "_sum");
out << ToString(sum.sample_sum);
WriteTail(out, metric);
for (auto& q : sum.quantile) {
WriteHead(out, family, metric, "", "quantile", ToString(q.quantile));
out << ToString(q.value);
WriteTail(out, metric);
}
}
void SerializeUntyped(std::ostream& out, const MetricFamily& family,
const ClientMetric& metric) {
WriteHead(out, family, metric);
out << ToString(metric.untyped.value);
WriteTail(out, metric);
}
void SerializeHistogram(std::ostream& out, const MetricFamily& family,
const ClientMetric& metric) {
auto& hist = metric.histogram;
WriteHead(out, family, metric, "_count");
out << hist.sample_count;
WriteTail(out, metric);
WriteHead(out, family, metric, "_sum");
out << ToString(hist.sample_sum);
WriteTail(out, metric);
double last = -std::numeric_limits<double>::infinity();
for (auto& b : hist.bucket) {
WriteHead(out, family, metric, "_bucket", "le", ToString(b.upper_bound));
last = b.upper_bound;
out << b.cumulative_count;
WriteTail(out, metric);
}
if (last != std::numeric_limits<double>::infinity()) {
WriteHead(out, family, metric, "_bucket", "le", "+Inf");
out << hist.sample_count;
WriteTail(out, metric);
}
}
void SerializeFamily(std::ostream& out, const MetricFamily& family) {
if (!family.help.empty()) {
out << "# HELP " << family.name << " " << family.help << "\n";
}
switch (family.type) {
case MetricType::Counter:
out << "# TYPE " << family.name << " counter\n";
for (auto& metric : family.metric) {
SerializeCounter(out, family, metric);
}
break;
case MetricType::Gauge:
out << "# TYPE " << family.name << " gauge\n";
for (auto& metric : family.metric) {
SerializeGauge(out, family, metric);
}
break;
case MetricType::Summary:
out << "# TYPE " << family.name << " summary\n";
for (auto& metric : family.metric) {
SerializeSummary(out, family, metric);
}
break;
case MetricType::Untyped:
out << "# TYPE " << family.name << " untyped\n";
for (auto& metric : family.metric) {
SerializeUntyped(out, family, metric);
}
break;
case MetricType::Histogram:
out << "# TYPE " << family.name << " histogram\n";
for (auto& metric : family.metric) {
SerializeHistogram(out, family, metric);
}
break;
default:
break;
}
}
} // namespace
void TextSerializer::Serialize(std::ostream& out,
const std::vector<MetricFamily>& metrics) const {
for (auto& family : metrics) {
SerializeFamily(out, family);
}
}
} // namespace prometheus