12 using namespace std::chrono;
14 Metrics global_metrics;
16 double get_timestamp_for_metrics()
18 return duration<double>(system_clock::now().time_since_epoch()).count();
21 string Metrics::serialize_name(const string &name, const vector<pair<string, string>> &labels)
23 return "nageru_" + name + serialize_labels(labels);
26 string Metrics::serialize_labels(const vector<pair<string, string>> &labels)
33 for (const pair<string, string> &label : labels) {
34 if (!label_str.empty()) {
37 label_str += label.first + "=\"" + label.second + "\"";
39 return "{" + label_str + "}";
42 void Metrics::add(const string &name, const vector<pair<string, string>> &labels, atomic<int64_t> *location, Metrics::Type type)
45 metric.data_type = DATA_TYPE_INT64;
46 metric.location_int64 = location;
48 lock_guard<mutex> lock(mu);
49 metrics.emplace(MetricKey(name, labels), metric);
50 assert(types.count(name) == 0 || types[name] == type);
54 void Metrics::add(const string &name, const vector<pair<string, string>> &labels, atomic<double> *location, Metrics::Type type)
57 metric.data_type = DATA_TYPE_DOUBLE;
58 metric.location_double = location;
60 lock_guard<mutex> lock(mu);
61 metrics.emplace(MetricKey(name, labels), metric);
62 assert(types.count(name) == 0 || types[name] == type);
66 void Metrics::add(const string &name, const vector<pair<string, string>> &labels, Histogram *location)
69 metric.data_type = DATA_TYPE_HISTOGRAM;
70 metric.location_histogram = location;
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;
78 void Metrics::remove(const string &name, const vector<pair<string, string>> &labels)
80 lock_guard<mutex> lock(mu);
82 auto it = metrics.find(MetricKey(name, labels));
83 assert(it != metrics.end());
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))) {
94 string Metrics::serialize() const
97 ss.imbue(locale("C"));
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;
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";
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";
123 ss << metric.location_histogram->serialize(key_and_metric.first.name, key_and_metric.first.labels);
130 void Histogram::init(const vector<double> &bucket_vals)
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];
139 void Histogram::init_uniform(size_t num_buckets)
141 this->num_buckets = num_buckets;
142 buckets.reset(new Bucket[num_buckets]);
143 for (size_t i = 0; i < num_buckets; ++i) {
148 void Histogram::init_geometric(double min, double max, size_t num_buckets)
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));
157 void Histogram::count_event(double val)
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;
168 // Non-atomic add, but that's fine, since there are no concurrent writers.
172 string Histogram::serialize(const string &name, const vector<pair<string, string>> &labels) const
175 ss.imbue(locale("C"));
179 for (size_t bucket_idx = 0; bucket_idx < num_buckets; ++bucket_idx) {
181 le_ss.imbue(locale("C"));
183 le_ss << buckets[bucket_idx].val;
184 vector<pair<string, string>> bucket_labels = labels;
185 bucket_labels.emplace_back("le", le_ss.str());
187 count += buckets[bucket_idx].count.load();
188 ss << Metrics::serialize_name(name + "_bucket", bucket_labels) << " " << count << "\n";
191 count += count_after_last_bucket.load();
193 ss << Metrics::serialize_name(name + "_sum", labels) << " " << sum.load() << "\n";
194 ss << Metrics::serialize_name(name + "_count", labels) << " " << count << "\n";