]> git.lizzy.rs Git - minetest.git/blob - src/util/metricsbackend.h
Add MetricsBackend with prometheus counter support
[minetest.git] / src / util / metricsbackend.h
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 #pragma once
21 #include <memory>
22 #include "config.h"
23 #include "util/thread.h"
24
25 class MetricCounter
26 {
27 public:
28         MetricCounter() = default;
29
30         virtual ~MetricCounter() {}
31
32         virtual void increment(double number = 1.0) = 0;
33         virtual double get() const = 0;
34 };
35
36 typedef std::shared_ptr<MetricCounter> MetricCounterPtr;
37
38 class SimpleMetricCounter : public MetricCounter
39 {
40 public:
41         SimpleMetricCounter() = delete;
42
43         virtual ~SimpleMetricCounter() {}
44
45         SimpleMetricCounter(const std::string &name, const std::string &help_str) :
46                         MetricCounter(), m_name(name), m_help_str(help_str),
47                         m_counter(0.0)
48         {
49         }
50
51         virtual void increment(double number)
52         {
53                 MutexAutoLock lock(m_mutex);
54                 m_counter += number;
55         }
56         virtual double get() const
57         {
58                 MutexAutoLock lock(m_mutex);
59                 return m_counter;
60         }
61
62 private:
63         std::string m_name;
64         std::string m_help_str;
65
66         mutable std::mutex m_mutex;
67         double m_counter;
68 };
69
70 class MetricGauge
71 {
72 public:
73         MetricGauge() = default;
74         virtual ~MetricGauge() {}
75
76         virtual void increment(double number = 1.0) = 0;
77         virtual void decrement(double number = 1.0) = 0;
78         virtual void set(double number) = 0;
79         virtual double get() const = 0;
80 };
81
82 typedef std::shared_ptr<MetricGauge> MetricGaugePtr;
83
84 class SimpleMetricGauge : public MetricGauge
85 {
86 public:
87         SimpleMetricGauge() = delete;
88
89         SimpleMetricGauge(const std::string &name, const std::string &help_str) :
90                         MetricGauge(), m_name(name), m_help_str(help_str), m_gauge(0.0)
91         {
92         }
93
94         virtual ~SimpleMetricGauge() {}
95
96         virtual void increment(double number)
97         {
98                 MutexAutoLock lock(m_mutex);
99                 m_gauge += number;
100         }
101         virtual void decrement(double number)
102         {
103                 MutexAutoLock lock(m_mutex);
104                 m_gauge -= number;
105         }
106         virtual void set(double number)
107         {
108                 MutexAutoLock lock(m_mutex);
109                 m_gauge = number;
110         }
111         virtual double get() const
112         {
113                 MutexAutoLock lock(m_mutex);
114                 return m_gauge;
115         }
116
117 private:
118         std::string m_name;
119         std::string m_help_str;
120
121         mutable std::mutex m_mutex;
122         double m_gauge;
123 };
124
125 class MetricsBackend
126 {
127 public:
128         MetricsBackend() = default;
129
130         virtual ~MetricsBackend() {}
131
132         virtual MetricCounterPtr addCounter(
133                         const std::string &name, const std::string &help_str);
134         virtual MetricGaugePtr addGauge(
135                         const std::string &name, const std::string &help_str);
136 };
137
138 #if USE_PROMETHEUS
139 MetricsBackend *createPrometheusMetricsBackend();
140 #endif