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 string Metrics::serialize() const
81 ss.imbue(locale("C"));
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;
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";
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";
107 ss << metric.location_histogram->serialize(key_and_metric.first.name, key_and_metric.first.labels);
114 void Histogram::init(const vector<double> &bucket_vals)
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];
123 void Histogram::init_uniform(size_t num_buckets)
125 this->num_buckets = num_buckets;
126 buckets.reset(new Bucket[num_buckets]);
127 for (size_t i = 0; i < num_buckets; ++i) {
132 void Histogram::init_geometric(double min, double max, size_t num_buckets)
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));
141 void Histogram::count_event(double val)
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;
152 // Non-atomic add, but that's fine, since there are no concurrent writers.
156 string Histogram::serialize(const string &name, const vector<pair<string, string>> &labels) const
159 ss.imbue(locale("C"));
163 for (size_t bucket_idx = 0; bucket_idx < num_buckets; ++bucket_idx) {
165 le_ss.imbue(locale("C"));
167 le_ss << buckets[bucket_idx].val;
168 vector<pair<string, string>> bucket_labels = labels;
169 bucket_labels.emplace_back("le", le_ss.str());
171 count += buckets[bucket_idx].count.load();
172 ss << Metrics::serialize_name(name + "_bucket", bucket_labels) << " " << count << "\n";
175 count += count_after_last_bucket.load();
177 ss << Metrics::serialize_name(name + "_sum", labels) << " " << sum.load() << "\n";
178 ss << Metrics::serialize_name(name + "_count", labels) << " " << count << "\n";