]> git.lizzy.rs Git - minetest.git/blob - src/util/metricsbackend.cpp
Clean up serialization
[minetest.git] / src / util / metricsbackend.cpp
1 /*
2 Minetest
3 Copyright (C) 2013-2020 Minetest core developers team
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "metricsbackend.h"
21 #if USE_PROMETHEUS
22 #include <prometheus/exposer.h>
23 #include <prometheus/registry.h>
24 #include <prometheus/counter.h>
25 #include <prometheus/gauge.h>
26 #include "log.h"
27 #include "settings.h"
28 #endif
29
30 MetricCounterPtr MetricsBackend::addCounter(
31                 const std::string &name, const std::string &help_str)
32 {
33         return std::make_shared<SimpleMetricCounter>(name, help_str);
34 }
35
36 MetricGaugePtr MetricsBackend::addGauge(
37                 const std::string &name, const std::string &help_str)
38 {
39         return std::make_shared<SimpleMetricGauge>(name, help_str);
40 }
41
42 #if USE_PROMETHEUS
43
44 class PrometheusMetricCounter : public MetricCounter
45 {
46 public:
47         PrometheusMetricCounter() = delete;
48
49         PrometheusMetricCounter(const std::string &name, const std::string &help_str,
50                         std::shared_ptr<prometheus::Registry> registry) :
51                         MetricCounter(),
52                         m_family(prometheus::BuildCounter()
53                                                         .Name(name)
54                                                         .Help(help_str)
55                                                         .Register(*registry)),
56                         m_counter(m_family.Add({}))
57         {
58         }
59
60         virtual ~PrometheusMetricCounter() {}
61
62         virtual void increment(double number) { m_counter.Increment(number); }
63         virtual double get() const { return m_counter.Value(); }
64
65 private:
66         prometheus::Family<prometheus::Counter> &m_family;
67         prometheus::Counter &m_counter;
68 };
69
70 class PrometheusMetricGauge : public MetricGauge
71 {
72 public:
73         PrometheusMetricGauge() = delete;
74
75         PrometheusMetricGauge(const std::string &name, const std::string &help_str,
76                         std::shared_ptr<prometheus::Registry> registry) :
77                         MetricGauge(),
78                         m_family(prometheus::BuildGauge()
79                                                         .Name(name)
80                                                         .Help(help_str)
81                                                         .Register(*registry)),
82                         m_gauge(m_family.Add({}))
83         {
84         }
85
86         virtual ~PrometheusMetricGauge() {}
87
88         virtual void increment(double number) { m_gauge.Increment(number); }
89         virtual void decrement(double number) { m_gauge.Decrement(number); }
90         virtual void set(double number) { m_gauge.Set(number); }
91         virtual double get() const { return m_gauge.Value(); }
92
93 private:
94         prometheus::Family<prometheus::Gauge> &m_family;
95         prometheus::Gauge &m_gauge;
96 };
97
98 class PrometheusMetricsBackend : public MetricsBackend
99 {
100 public:
101         PrometheusMetricsBackend(const std::string &addr) :
102                         MetricsBackend(), m_exposer(std::unique_ptr<prometheus::Exposer>(
103                                                           new prometheus::Exposer(addr))),
104                         m_registry(std::make_shared<prometheus::Registry>())
105         {
106                 m_exposer->RegisterCollectable(m_registry);
107         }
108
109         virtual ~PrometheusMetricsBackend() {}
110
111         virtual MetricCounterPtr addCounter(
112                         const std::string &name, const std::string &help_str);
113         virtual MetricGaugePtr addGauge(
114                         const std::string &name, const std::string &help_str);
115
116 private:
117         std::unique_ptr<prometheus::Exposer> m_exposer;
118         std::shared_ptr<prometheus::Registry> m_registry;
119 };
120
121 MetricCounterPtr PrometheusMetricsBackend::addCounter(
122                 const std::string &name, const std::string &help_str)
123 {
124         return std::make_shared<PrometheusMetricCounter>(name, help_str, m_registry);
125 }
126
127 MetricGaugePtr PrometheusMetricsBackend::addGauge(
128                 const std::string &name, const std::string &help_str)
129 {
130         return std::make_shared<PrometheusMetricGauge>(name, help_str, m_registry);
131 }
132
133 MetricsBackend *createPrometheusMetricsBackend()
134 {
135         std::string addr;
136         g_settings->getNoEx("prometheus_listener_address", addr);
137         return new PrometheusMetricsBackend(addr);
138 }
139
140 #endif