10 Metrics global_metrics;
12 void Metrics::add(const string &name, const vector<pair<string, string>> &labels, atomic<int64_t> *location, Metrics::Type type)
15 metric.data_type = DATA_TYPE_INT64;
17 metric.labels = labels;
18 metric.location_int64 = location;
20 lock_guard<mutex> lock(mu);
21 metrics.push_back(metric);
22 assert(types.count(name) == 0 || types[name] == type);
26 void Metrics::add(const string &name, const vector<pair<string, string>> &labels, atomic<double> *location, Metrics::Type type)
29 metric.data_type = DATA_TYPE_DOUBLE;
31 metric.labels = labels;
32 metric.location_double = location;
34 lock_guard<mutex> lock(mu);
35 metrics.push_back(metric);
36 assert(types.count(name) == 0 || types[name] == type);
40 string Metrics::serialize() const
43 ss.imbue(locale("C"));
46 lock_guard<mutex> lock(mu);
47 for (const auto &name_and_type : types) {
48 if (name_and_type.second == TYPE_GAUGE) {
49 ss << "# TYPE nageru_" << name_and_type.first << " gauge\n";
52 for (const Metric &metric : metrics) {
54 if (metric.labels.empty()) {
55 name = "nageru_" + metric.name;
57 name = "nageru_" + metric.name + "{";
59 for (const pair<string, string> &label : metric.labels) {
64 name += label.first + "=\"" + label.second + "\"";
69 if (metric.data_type == DATA_TYPE_INT64) {
70 ss << "nageru_" << name << " " << metric.location_int64->load() << "\n";
72 ss << "nageru_" << name << " " << metric.location_double->load() << "\n";