]> git.sesse.net Git - nageru/blob - metrics.cpp
fafc38915a9601732d27df973daf903f82412214
[nageru] / metrics.cpp
1 #include "metrics.h"
2
3 #include <assert.h>
4 #include <math.h>
5
6 #include <algorithm>
7 #include <chrono>
8 #include <locale>
9 #include <sstream>
10
11 using namespace std;
12 using namespace std::chrono;
13
14 Metrics global_metrics;
15
16 double get_timestamp_for_metrics()
17 {
18         return duration<double>(system_clock::now().time_since_epoch()).count();
19 }
20
21 string Metrics::serialize_name(const string &name, const vector<pair<string, string>> &labels)
22 {
23         return "nageru_" + name + serialize_labels(labels);
24 }
25
26 string Metrics::serialize_labels(const vector<pair<string, string>> &labels)
27 {
28         if (labels.empty()) {
29                 return "";
30         }
31
32         string label_str;
33         for (const pair<string, string> &label : labels) {
34                 if (!label_str.empty()) {
35                         label_str += ",";
36                 }
37                 label_str += label.first + "=\"" + label.second + "\"";
38         }
39         return "{" + label_str + "}";
40 }
41
42 void Metrics::add(const string &name, const vector<pair<string, string>> &labels, atomic<int64_t> *location, Metrics::Type type)
43 {
44         Metric metric;
45         metric.data_type = DATA_TYPE_INT64;
46         metric.location_int64 = location;
47
48         lock_guard<mutex> lock(mu);
49         metrics.emplace(MetricKey(name, labels), metric);
50         assert(types.count(name) == 0 || types[name] == type);
51         types[name] = type;
52 }
53
54 void Metrics::add(const string &name, const vector<pair<string, string>> &labels, atomic<double> *location, Metrics::Type type)
55 {
56         Metric metric;
57         metric.data_type = DATA_TYPE_DOUBLE;
58         metric.location_double = location;
59
60         lock_guard<mutex> lock(mu);
61         metrics.emplace(MetricKey(name, labels), metric);
62         assert(types.count(name) == 0 || types[name] == type);
63         types[name] = type;
64 }
65
66 void Metrics::add(const string &name, const vector<pair<string, string>> &labels, Histogram *location)
67 {
68         Metric metric;
69         metric.data_type = DATA_TYPE_HISTOGRAM;
70         metric.location_histogram = location;
71
72         lock_guard<mutex> lock(mu);
73         metrics.emplace(MetricKey(name, labels), metric);
74         assert(types.count(name) == 0 || types[name] == TYPE_HISTOGRAM);
75         types[name] = TYPE_HISTOGRAM;
76 }
77
78 void Metrics::remove(const string &name, const vector<pair<string, string>> &labels)
79 {
80         lock_guard<mutex> lock(mu);
81
82         auto it = metrics.find(MetricKey(name, labels));
83         assert(it != metrics.end());
84
85         // If this is the last metric with this name, remove the type as well.
86         if (!((it != metrics.begin() && prev(it)->first.name == name) ||
87               (it != metrics.end() && next(it)->first.name == name))) {
88                 types.erase(name);
89         }
90
91         metrics.erase(it);
92 }
93
94 string Metrics::serialize() const
95 {
96         stringstream ss;
97         ss.imbue(locale("C"));
98         ss.precision(20);
99
100         lock_guard<mutex> lock(mu);
101         auto type_it = types.cbegin();
102         for (const auto &key_and_metric : metrics) {
103                 string name = "nageru_" + key_and_metric.first.name + key_and_metric.first.serialized_labels;
104                 const Metric &metric = key_and_metric.second;
105
106                 if (type_it != types.cend() &&
107                     key_and_metric.first.name == type_it->first) {
108                         // It's the first time we print out any metric with this name,
109                         // so add the type header.
110                         if (type_it->second == TYPE_GAUGE) {
111                                 ss << "# TYPE nageru_" << type_it->first << " gauge\n";
112                         } else if (type_it->second == TYPE_HISTOGRAM) {
113                                 ss << "# TYPE nageru_" << type_it->first << " histogram\n";
114                         }
115                         ++type_it;
116                 }
117
118                 if (metric.data_type == DATA_TYPE_INT64) {
119                         ss << name << " " << metric.location_int64->load() << "\n";
120                 } else if (metric.data_type == DATA_TYPE_DOUBLE) {
121                         double val = metric.location_double->load();
122                         if (isnan(val)) {
123                                 // Prometheus can't handle “-nan”.
124                                 ss << name << " NaN\n";
125                         } else {
126                                 ss << name << " " << val << "\n";
127                         }
128                 } else {
129                         ss << metric.location_histogram->serialize(key_and_metric.first.name, key_and_metric.first.labels);
130                 }
131         }
132
133         return ss.str();
134 }
135
136 void Histogram::init(const vector<double> &bucket_vals)
137 {
138         this->num_buckets = bucket_vals.size();
139         buckets.reset(new Bucket[num_buckets]);
140         for (size_t i = 0; i < num_buckets; ++i) {
141                 buckets[i].val = bucket_vals[i];
142         }
143 }
144
145 void Histogram::init_uniform(size_t num_buckets)
146 {
147         this->num_buckets = num_buckets;
148         buckets.reset(new Bucket[num_buckets]);
149         for (size_t i = 0; i < num_buckets; ++i) {
150                 buckets[i].val = i;
151         }
152 }
153
154 void Histogram::init_geometric(double min, double max, size_t num_buckets)
155 {
156         this->num_buckets = num_buckets;
157         buckets.reset(new Bucket[num_buckets]);
158         for (size_t i = 0; i < num_buckets; ++i) {
159                 buckets[i].val = min * pow(max / min, double(i) / (num_buckets - 1));
160         }
161 }
162
163 void Histogram::count_event(double val)
164 {
165         Bucket ref_bucket;
166         ref_bucket.val = val;
167         auto it = lower_bound(buckets.get(), buckets.get() + num_buckets, ref_bucket,
168                 [](const Bucket &a, const Bucket &b) { return a.val < b.val; });
169         if (it == buckets.get() + num_buckets) {
170                 ++count_after_last_bucket;
171         } else {
172                 ++it->count;
173         }
174         // Non-atomic add, but that's fine, since there are no concurrent writers.
175         sum = sum + val;
176 }
177
178 string Histogram::serialize(const string &name, const vector<pair<string, string>> &labels) const
179 {
180         stringstream ss;
181         ss.imbue(locale("C"));
182         ss.precision(20);
183
184         int64_t count = 0;
185         for (size_t bucket_idx = 0; bucket_idx < num_buckets; ++bucket_idx) {
186                 stringstream le_ss;
187                 le_ss.imbue(locale("C"));
188                 le_ss.precision(20);
189                 le_ss << buckets[bucket_idx].val;
190                 vector<pair<string, string>> bucket_labels = labels;
191                 bucket_labels.emplace_back("le", le_ss.str());
192
193                 count += buckets[bucket_idx].count.load();
194                 ss << Metrics::serialize_name(name + "_bucket", bucket_labels) << " " << count << "\n";
195         }
196
197         count += count_after_last_bucket.load();
198
199         ss << Metrics::serialize_name(name + "_sum", labels) << " " << sum.load() << "\n";
200         ss << Metrics::serialize_name(name + "_count", labels) << " " << count << "\n";
201
202         return ss.str();
203 }