11 Metrics global_metrics;
15 string serialize_name(const string &name, const vector<pair<string, string>> &labels)
18 return "nageru_" + name;
22 for (const pair<string, string> &label : labels) {
23 if (!label_str.empty()) {
26 label_str += label.first + "=\"" + label.second + "\"";
28 return "nageru_" + name + "{" + label_str + "}";
33 void Metrics::add(const string &name, const vector<pair<string, string>> &labels, atomic<int64_t> *location, Metrics::Type type)
36 metric.data_type = DATA_TYPE_INT64;
38 metric.labels = labels;
39 metric.location_int64 = location;
41 lock_guard<mutex> lock(mu);
42 metrics.push_back(metric);
43 assert(types.count(name) == 0 || types[name] == type);
47 void Metrics::add(const string &name, const vector<pair<string, string>> &labels, atomic<double> *location, Metrics::Type type)
50 metric.data_type = DATA_TYPE_DOUBLE;
52 metric.labels = labels;
53 metric.location_double = location;
55 lock_guard<mutex> lock(mu);
56 metrics.push_back(metric);
57 assert(types.count(name) == 0 || types[name] == type);
61 void Metrics::add(const string &name, const vector<pair<string, string>> &labels, Histogram *location)
64 metric.data_type = DATA_TYPE_HISTOGRAM;
66 metric.labels = labels;
67 metric.location_histogram = location;
69 lock_guard<mutex> lock(mu);
70 metrics.push_back(metric);
71 assert(types.count(name) == 0 || types[name] == TYPE_HISTOGRAM);
72 types[name] = TYPE_HISTOGRAM;
75 string Metrics::serialize() const
78 ss.imbue(locale("C"));
81 lock_guard<mutex> lock(mu);
82 for (const auto &name_and_type : types) {
83 if (name_and_type.second == TYPE_GAUGE) {
84 ss << "# TYPE nageru_" << name_and_type.first << " gauge\n";
85 } else if (name_and_type.second == TYPE_HISTOGRAM) {
86 ss << "# TYPE nageru_" << name_and_type.first << " histogram\n";
89 for (const Metric &metric : metrics) {
90 string name = serialize_name(metric.name, metric.labels);
92 if (metric.data_type == DATA_TYPE_INT64) {
93 ss << name << " " << metric.location_int64->load() << "\n";
94 } else if (metric.data_type == DATA_TYPE_DOUBLE) {
95 ss << name << " " << metric.location_double->load() << "\n";
97 ss << metric.location_histogram->serialize(metric.name, metric.labels);
104 void Histogram::init(const vector<double> &bucket_vals)
106 this->num_buckets = bucket_vals.size();
107 buckets.reset(new Bucket[num_buckets]);
108 for (size_t i = 0; i < num_buckets; ++i) {
109 buckets[i].val = bucket_vals[i];
113 void Histogram::init_uniform(size_t num_buckets)
115 this->num_buckets = num_buckets;
116 buckets.reset(new Bucket[num_buckets]);
117 for (size_t i = 0; i < num_buckets; ++i) {
122 void Histogram::count_event(double val)
125 ref_bucket.val = val;
126 auto it = lower_bound(buckets.get(), buckets.get() + num_buckets, ref_bucket,
127 [](const Bucket &a, const Bucket &b) { return a.val < b.val; });
128 if (it == buckets.get() + num_buckets) {
129 ++count_after_last_bucket;
133 // Non-atomic add, but that's fine, since there are no concurrent writers.
137 string Histogram::serialize(const string &name, const vector<pair<string, string>> &labels) const
140 ss.imbue(locale("C"));
144 for (size_t bucket_idx = 0; bucket_idx < num_buckets; ++bucket_idx) {
146 le_ss.imbue(locale("C"));
148 le_ss << buckets[bucket_idx].val;
149 vector<pair<string, string>> bucket_labels = labels;
150 bucket_labels.emplace_back("le", le_ss.str());
152 count += buckets[bucket_idx].count.load();
153 ss << serialize_name(name + "_bucket", bucket_labels) << " " << count << "\n";
156 count += count_after_last_bucket.load();
158 ss << serialize_name(name + "_sum", labels) << " " << sum.load() << "\n";
159 ss << serialize_name(name + "_count", labels) << " " << count << "\n";