12 Metrics global_metrics;
16 string serialize_name(const string &name, const vector<pair<string, string>> &labels)
19 return "nageru_" + name;
23 for (const pair<string, string> &label : labels) {
24 if (!label_str.empty()) {
27 label_str += label.first + "=\"" + label.second + "\"";
29 return "nageru_" + name + "{" + label_str + "}";
34 void Metrics::add(const string &name, const vector<pair<string, string>> &labels, atomic<int64_t> *location, Metrics::Type type)
37 metric.data_type = DATA_TYPE_INT64;
39 metric.labels = labels;
40 metric.location_int64 = location;
42 lock_guard<mutex> lock(mu);
43 metrics.push_back(metric);
44 assert(types.count(name) == 0 || types[name] == type);
48 void Metrics::add(const string &name, const vector<pair<string, string>> &labels, atomic<double> *location, Metrics::Type type)
51 metric.data_type = DATA_TYPE_DOUBLE;
53 metric.labels = labels;
54 metric.location_double = location;
56 lock_guard<mutex> lock(mu);
57 metrics.push_back(metric);
58 assert(types.count(name) == 0 || types[name] == type);
62 void Metrics::add(const string &name, const vector<pair<string, string>> &labels, Histogram *location)
65 metric.data_type = DATA_TYPE_HISTOGRAM;
67 metric.labels = labels;
68 metric.location_histogram = location;
70 lock_guard<mutex> lock(mu);
71 metrics.push_back(metric);
72 assert(types.count(name) == 0 || types[name] == TYPE_HISTOGRAM);
73 types[name] = TYPE_HISTOGRAM;
76 string Metrics::serialize() const
79 ss.imbue(locale("C"));
82 lock_guard<mutex> lock(mu);
83 for (const auto &name_and_type : types) {
84 if (name_and_type.second == TYPE_GAUGE) {
85 ss << "# TYPE nageru_" << name_and_type.first << " gauge\n";
86 } else if (name_and_type.second == TYPE_HISTOGRAM) {
87 ss << "# TYPE nageru_" << name_and_type.first << " histogram\n";
90 for (const Metric &metric : metrics) {
91 string name = serialize_name(metric.name, metric.labels);
93 if (metric.data_type == DATA_TYPE_INT64) {
94 ss << name << " " << metric.location_int64->load() << "\n";
95 } else if (metric.data_type == DATA_TYPE_DOUBLE) {
96 ss << name << " " << metric.location_double->load() << "\n";
98 ss << metric.location_histogram->serialize(metric.name, metric.labels);
105 void Histogram::init(const vector<double> &bucket_vals)
107 this->num_buckets = bucket_vals.size();
108 buckets.reset(new Bucket[num_buckets]);
109 for (size_t i = 0; i < num_buckets; ++i) {
110 buckets[i].val = bucket_vals[i];
114 void Histogram::init_uniform(size_t num_buckets)
116 this->num_buckets = num_buckets;
117 buckets.reset(new Bucket[num_buckets]);
118 for (size_t i = 0; i < num_buckets; ++i) {
123 void Histogram::init_geometric(double min, double max, 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) {
128 buckets[i].val = min * pow(max / min, double(i) / (num_buckets - 1));
132 void Histogram::count_event(double val)
135 ref_bucket.val = val;
136 auto it = lower_bound(buckets.get(), buckets.get() + num_buckets, ref_bucket,
137 [](const Bucket &a, const Bucket &b) { return a.val < b.val; });
138 if (it == buckets.get() + num_buckets) {
139 ++count_after_last_bucket;
143 // Non-atomic add, but that's fine, since there are no concurrent writers.
147 string Histogram::serialize(const string &name, const vector<pair<string, string>> &labels) const
150 ss.imbue(locale("C"));
154 for (size_t bucket_idx = 0; bucket_idx < num_buckets; ++bucket_idx) {
156 le_ss.imbue(locale("C"));
158 le_ss << buckets[bucket_idx].val;
159 vector<pair<string, string>> bucket_labels = labels;
160 bucket_labels.emplace_back("le", le_ss.str());
162 count += buckets[bucket_idx].count.load();
163 ss << serialize_name(name + "_bucket", bucket_labels) << " " << count << "\n";
166 count += count_after_last_bucket.load();
168 ss << serialize_name(name + "_sum", labels) << " " << sum.load() << "\n";
169 ss << serialize_name(name + "_count", labels) << " " << count << "\n";