]> git.sesse.net Git - nageru/blob - metrics.cpp
Add support for bus audio level metrics, which includes support for removing metrics.
[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                         ss << name << " " << metric.location_double->load() << "\n";
122                 } else {
123                         ss << metric.location_histogram->serialize(key_and_metric.first.name, key_and_metric.first.labels);
124                 }
125         }
126
127         return ss.str();
128 }
129
130 void Histogram::init(const vector<double> &bucket_vals)
131 {
132         this->num_buckets = bucket_vals.size();
133         buckets.reset(new Bucket[num_buckets]);
134         for (size_t i = 0; i < num_buckets; ++i) {
135                 buckets[i].val = bucket_vals[i];
136         }
137 }
138
139 void Histogram::init_uniform(size_t num_buckets)
140 {
141         this->num_buckets = num_buckets;
142         buckets.reset(new Bucket[num_buckets]);
143         for (size_t i = 0; i < num_buckets; ++i) {
144                 buckets[i].val = i;
145         }
146 }
147
148 void Histogram::init_geometric(double min, double max, size_t num_buckets)
149 {
150         this->num_buckets = num_buckets;
151         buckets.reset(new Bucket[num_buckets]);
152         for (size_t i = 0; i < num_buckets; ++i) {
153                 buckets[i].val = min * pow(max / min, double(i) / (num_buckets - 1));
154         }
155 }
156
157 void Histogram::count_event(double val)
158 {
159         Bucket ref_bucket;
160         ref_bucket.val = val;
161         auto it = lower_bound(buckets.get(), buckets.get() + num_buckets, ref_bucket,
162                 [](const Bucket &a, const Bucket &b) { return a.val < b.val; });
163         if (it == buckets.get() + num_buckets) {
164                 ++count_after_last_bucket;
165         } else {
166                 ++it->count;
167         }
168         // Non-atomic add, but that's fine, since there are no concurrent writers.
169         sum = sum + val;
170 }
171
172 string Histogram::serialize(const string &name, const vector<pair<string, string>> &labels) const
173 {
174         stringstream ss;
175         ss.imbue(locale("C"));
176         ss.precision(20);
177
178         int64_t count = 0;
179         for (size_t bucket_idx = 0; bucket_idx < num_buckets; ++bucket_idx) {
180                 stringstream le_ss;
181                 le_ss.imbue(locale("C"));
182                 le_ss.precision(20);
183                 le_ss << buckets[bucket_idx].val;
184                 vector<pair<string, string>> bucket_labels = labels;
185                 bucket_labels.emplace_back("le", le_ss.str());
186
187                 count += buckets[bucket_idx].count.load();
188                 ss << Metrics::serialize_name(name + "_bucket", bucket_labels) << " " << count << "\n";
189         }
190
191         count += count_after_last_bucket.load();
192
193         ss << Metrics::serialize_name(name + "_sum", labels) << " " << sum.load() << "\n";
194         ss << Metrics::serialize_name(name + "_count", labels) << " " << count << "\n";
195
196         return ss.str();
197 }