aboutsummaryrefslogtreecommitdiff
path: root/src/util/metricsbackend.cpp
diff options
context:
space:
mode:
authorsfan5 <sfan5@live.de>2022-05-09 21:20:58 +0200
committerGitHub <noreply@github.com>2022-05-09 21:20:58 +0200
commitf5a8593b11382b70ee969dc93b71f34fb0cad5df (patch)
treebb3b782a61147b9052bac3de578b808f7abd315f /src/util/metricsbackend.cpp
parentc2898f53bc3eb1f22daf93b37608156885fe5c5a (diff)
downloadminetest-f5a8593b11382b70ee969dc93b71f34fb0cad5df.tar.gz
minetest-f5a8593b11382b70ee969dc93b71f34fb0cad5df.tar.bz2
minetest-f5a8593b11382b70ee969dc93b71f34fb0cad5df.zip
Add more Prometheus metrics (#12274)
Diffstat (limited to 'src/util/metricsbackend.cpp')
-rw-r--r--src/util/metricsbackend.cpp93
1 files changed, 79 insertions, 14 deletions
diff --git a/src/util/metricsbackend.cpp b/src/util/metricsbackend.cpp
index c3b7def62..63b49ac0a 100644
--- a/src/util/metricsbackend.cpp
+++ b/src/util/metricsbackend.cpp
@@ -18,6 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/
#include "metricsbackend.h"
+#include "util/thread.h"
#if USE_PROMETHEUS
#include <prometheus/exposer.h>
#include <prometheus/registry.h>
@@ -27,18 +28,78 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "settings.h"
#endif
+/* Plain implementation */
+
+class SimpleMetricCounter : public MetricCounter
+{
+public:
+ SimpleMetricCounter() : MetricCounter(), m_counter(0.0) {}
+
+ virtual ~SimpleMetricCounter() {}
+
+ void increment(double number) override
+ {
+ MutexAutoLock lock(m_mutex);
+ m_counter += number;
+ }
+ double get() const override
+ {
+ MutexAutoLock lock(m_mutex);
+ return m_counter;
+ }
+
+private:
+ mutable std::mutex m_mutex;
+ double m_counter;
+};
+
+class SimpleMetricGauge : public MetricGauge
+{
+public:
+ SimpleMetricGauge() : MetricGauge(), m_gauge(0.0) {}
+
+ virtual ~SimpleMetricGauge() {}
+
+ void increment(double number) override
+ {
+ MutexAutoLock lock(m_mutex);
+ m_gauge += number;
+ }
+ void decrement(double number) override
+ {
+ MutexAutoLock lock(m_mutex);
+ m_gauge -= number;
+ }
+ void set(double number) override
+ {
+ MutexAutoLock lock(m_mutex);
+ m_gauge = number;
+ }
+ double get() const override
+ {
+ MutexAutoLock lock(m_mutex);
+ return m_gauge;
+ }
+
+private:
+ mutable std::mutex m_mutex;
+ double m_gauge;
+};
+
MetricCounterPtr MetricsBackend::addCounter(
- const std::string &name, const std::string &help_str)
+ const std::string &name, const std::string &help_str, Labels labels)
{
- return std::make_shared<SimpleMetricCounter>(name, help_str);
+ return std::make_shared<SimpleMetricCounter>();
}
MetricGaugePtr MetricsBackend::addGauge(
- const std::string &name, const std::string &help_str)
+ const std::string &name, const std::string &help_str, Labels labels)
{
- return std::make_shared<SimpleMetricGauge>(name, help_str);
+ return std::make_shared<SimpleMetricGauge>();
}
+/* Prometheus backend */
+
#if USE_PROMETHEUS
class PrometheusMetricCounter : public MetricCounter
@@ -47,13 +108,14 @@ public:
PrometheusMetricCounter() = delete;
PrometheusMetricCounter(const std::string &name, const std::string &help_str,
+ MetricsBackend::Labels labels,
std::shared_ptr<prometheus::Registry> registry) :
MetricCounter(),
m_family(prometheus::BuildCounter()
.Name(name)
.Help(help_str)
.Register(*registry)),
- m_counter(m_family.Add({}))
+ m_counter(m_family.Add(labels))
{
}
@@ -73,13 +135,14 @@ public:
PrometheusMetricGauge() = delete;
PrometheusMetricGauge(const std::string &name, const std::string &help_str,
+ MetricsBackend::Labels labels,
std::shared_ptr<prometheus::Registry> registry) :
MetricGauge(),
m_family(prometheus::BuildGauge()
.Name(name)
.Help(help_str)
.Register(*registry)),
- m_gauge(m_family.Add({}))
+ m_gauge(m_family.Add(labels))
{
}
@@ -107,10 +170,12 @@ public:
virtual ~PrometheusMetricsBackend() {}
- virtual MetricCounterPtr addCounter(
- const std::string &name, const std::string &help_str);
- virtual MetricGaugePtr addGauge(
- const std::string &name, const std::string &help_str);
+ MetricCounterPtr addCounter(
+ const std::string &name, const std::string &help_str,
+ Labels labels = {}) override;
+ MetricGaugePtr addGauge(
+ const std::string &name, const std::string &help_str,
+ Labels labels = {}) override;
private:
std::unique_ptr<prometheus::Exposer> m_exposer;
@@ -118,15 +183,15 @@ private:
};
MetricCounterPtr PrometheusMetricsBackend::addCounter(
- const std::string &name, const std::string &help_str)
+ const std::string &name, const std::string &help_str, Labels labels)
{
- return std::make_shared<PrometheusMetricCounter>(name, help_str, m_registry);
+ return std::make_shared<PrometheusMetricCounter>(name, help_str, labels, m_registry);
}
MetricGaugePtr PrometheusMetricsBackend::addGauge(
- const std::string &name, const std::string &help_str)
+ const std::string &name, const std::string &help_str, Labels labels)
{
- return std::make_shared<PrometheusMetricGauge>(name, help_str, m_registry);
+ return std::make_shared<PrometheusMetricGauge>(name, help_str, labels, m_registry);
}
MetricsBackend *createPrometheusMetricsBackend()