]> git.sesse.net Git - nageru/blobdiff - metrics.cpp
Mark the appropriate metrics as gauges.
[nageru] / metrics.cpp
index 78f1e9854d62aef89797987865580681429dd3a3..dd2ff690a1aeac351364a1000670c6f392020969 100644 (file)
@@ -7,16 +7,16 @@ using namespace std;
 
 Metrics global_metrics;
 
-void Metrics::register_int_metric(const string &name, atomic<int64_t> *location)
+void Metrics::register_int_metric(const string &name, atomic<int64_t> *location, Metrics::Type type)
 {
        lock_guard<mutex> lock(mu);
-       int_metrics.emplace(name, location);
+       int_metrics.emplace(name, Metric<int64_t>{ type, location });
 }
 
-void Metrics::register_double_metric(const string &name, atomic<double> *location)
+void Metrics::register_double_metric(const string &name, atomic<double> *location, Metrics::Type type)
 {
        lock_guard<mutex> lock(mu);
-       double_metrics.emplace(name, location);
+       double_metrics.emplace(name, Metric<double>{ type, location });
 }
 
 string Metrics::serialize() const
@@ -28,10 +28,16 @@ string Metrics::serialize() const
 
        lock_guard<mutex> lock(mu);
        for (const auto &key_and_value : int_metrics) {
-               ss << "nageru_" << key_and_value.first.c_str() << " " << key_and_value.second->load() << "\n";
+               if (key_and_value.second.type == TYPE_GAUGE) {
+                       ss << "# TYPE nageru_" << key_and_value.first << " gauge\n";
+               }
+               ss << "nageru_" << key_and_value.first << " " << key_and_value.second.location->load() << "\n";
        }
        for (const auto &key_and_value : double_metrics) {
-               ss << "nageru_" << key_and_value.first.c_str() << " " << key_and_value.second->load() << "\n";
+               if (key_and_value.second.type == TYPE_GAUGE) {
+                       ss << "# TYPE nageru_" << key_and_value.first << " gauge\n";
+               }
+               ss << "nageru_" << key_and_value.first << " " << key_and_value.second.location->load() << "\n";
        }
 
        return ss.str();