]> git.sesse.net Git - nageru/blob - metrics.cpp
Pre-serialize only the labels for metrics; the ordering constraints did not really...
[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 string Metrics::serialize() const
79 {
80         stringstream ss;
81         ss.imbue(locale("C"));
82         ss.precision(20);
83
84         lock_guard<mutex> lock(mu);
85         auto type_it = types.cbegin();
86         for (const auto &key_and_metric : metrics) {
87                 string name = "nageru_" + key_and_metric.first.name + key_and_metric.first.serialized_labels;
88                 const Metric &metric = key_and_metric.second;
89
90                 if (type_it != types.cend() &&
91                     key_and_metric.first.name == type_it->first) {
92                         // It's the first time we print out any metric with this name,
93                         // so add the type header.
94                         if (type_it->second == TYPE_GAUGE) {
95                                 ss << "# TYPE nageru_" << type_it->first << " gauge\n";
96                         } else if (type_it->second == TYPE_HISTOGRAM) {
97                                 ss << "# TYPE nageru_" << type_it->first << " histogram\n";
98                         }
99                         ++type_it;
100                 }
101
102                 if (metric.data_type == DATA_TYPE_INT64) {
103                         ss << name << " " << metric.location_int64->load() << "\n";
104                 } else if (metric.data_type == DATA_TYPE_DOUBLE) {
105                         ss << name << " " << metric.location_double->load() << "\n";
106                 } else {
107                         ss << metric.location_histogram->serialize(key_and_metric.first.name, key_and_metric.first.labels);
108                 }
109         }
110
111         return ss.str();
112 }
113
114 void Histogram::init(const vector<double> &bucket_vals)
115 {
116         this->num_buckets = bucket_vals.size();
117         buckets.reset(new Bucket[num_buckets]);
118         for (size_t i = 0; i < num_buckets; ++i) {
119                 buckets[i].val = bucket_vals[i];
120         }
121 }
122
123 void Histogram::init_uniform(size_t num_buckets)
124 {
125         this->num_buckets = num_buckets;
126         buckets.reset(new Bucket[num_buckets]);
127         for (size_t i = 0; i < num_buckets; ++i) {
128                 buckets[i].val = i;
129         }
130 }
131
132 void Histogram::init_geometric(double min, double max, size_t num_buckets)
133 {
134         this->num_buckets = num_buckets;
135         buckets.reset(new Bucket[num_buckets]);
136         for (size_t i = 0; i < num_buckets; ++i) {
137                 buckets[i].val = min * pow(max / min, double(i) / (num_buckets - 1));
138         }
139 }
140
141 void Histogram::count_event(double val)
142 {
143         Bucket ref_bucket;
144         ref_bucket.val = val;
145         auto it = lower_bound(buckets.get(), buckets.get() + num_buckets, ref_bucket,
146                 [](const Bucket &a, const Bucket &b) { return a.val < b.val; });
147         if (it == buckets.get() + num_buckets) {
148                 ++count_after_last_bucket;
149         } else {
150                 ++it->count;
151         }
152         // Non-atomic add, but that's fine, since there are no concurrent writers.
153         sum = sum + val;
154 }
155
156 string Histogram::serialize(const string &name, const vector<pair<string, string>> &labels) const
157 {
158         stringstream ss;
159         ss.imbue(locale("C"));
160         ss.precision(20);
161
162         int64_t count = 0;
163         for (size_t bucket_idx = 0; bucket_idx < num_buckets; ++bucket_idx) {
164                 stringstream le_ss;
165                 le_ss.imbue(locale("C"));
166                 le_ss.precision(20);
167                 le_ss << buckets[bucket_idx].val;
168                 vector<pair<string, string>> bucket_labels = labels;
169                 bucket_labels.emplace_back("le", le_ss.str());
170
171                 count += buckets[bucket_idx].count.load();
172                 ss << Metrics::serialize_name(name + "_bucket", bucket_labels) << " " << count << "\n";
173         }
174
175         count += count_after_last_bucket.load();
176
177         ss << Metrics::serialize_name(name + "_sum", labels) << " " << sum.load() << "\n";
178         ss << Metrics::serialize_name(name + "_count", labels) << " " << count << "\n";
179
180         return ss.str();
181 }