comparison OrthancFramework/Sources/MetricsRegistry.h @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Core/MetricsRegistry.h@05a363186da6
children bf7b9edf6b81
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #pragma once
35
36 #include "OrthancFramework.h"
37
38 #if !defined(ORTHANC_SANDBOXED)
39 # error The macro ORTHANC_SANDBOXED must be defined
40 #endif
41
42 #if ORTHANC_SANDBOXED == 1
43 # error The class MetricsRegistry cannot be used in sandboxed environments
44 #endif
45
46 #include <boost/thread/mutex.hpp>
47 #include <boost/date_time/posix_time/posix_time.hpp>
48
49 namespace Orthanc
50 {
51 enum MetricsType
52 {
53 MetricsType_Default,
54 MetricsType_MaxOver10Seconds,
55 MetricsType_MaxOver1Minute,
56 MetricsType_MinOver10Seconds,
57 MetricsType_MinOver1Minute
58 };
59
60 class ORTHANC_PUBLIC MetricsRegistry : public boost::noncopyable
61 {
62 private:
63 class Item;
64
65 typedef std::map<std::string, Item*> Content;
66
67 bool enabled_;
68 boost::mutex mutex_;
69 Content content_;
70
71 void SetValueInternal(const std::string& name,
72 float value,
73 MetricsType type);
74
75 public:
76 MetricsRegistry() :
77 enabled_(true)
78 {
79 }
80
81 ~MetricsRegistry();
82
83 bool IsEnabled() const
84 {
85 return enabled_;
86 }
87
88 void SetEnabled(bool enabled);
89
90 void Register(const std::string& name,
91 MetricsType type);
92
93 void SetValue(const std::string& name,
94 float value,
95 MetricsType type)
96 {
97 // Inlining to avoid loosing time if metrics are disabled
98 if (enabled_)
99 {
100 SetValueInternal(name, value, type);
101 }
102 }
103
104 void SetValue(const std::string& name,
105 float value)
106 {
107 SetValue(name, value, MetricsType_Default);
108 }
109
110 MetricsType GetMetricsType(const std::string& name);
111
112 // https://prometheus.io/docs/instrumenting/exposition_formats/#text-based-format
113 void ExportPrometheusText(std::string& s);
114
115
116 class ORTHANC_PUBLIC SharedMetrics : public boost::noncopyable
117 {
118 private:
119 boost::mutex mutex_;
120 MetricsRegistry& registry_;
121 std::string name_;
122 float value_;
123
124 public:
125 SharedMetrics(MetricsRegistry& registry,
126 const std::string& name,
127 MetricsType type) :
128 registry_(registry),
129 name_(name),
130 value_(0)
131 {
132 }
133
134 void Add(float delta);
135 };
136
137
138 class ORTHANC_PUBLIC ActiveCounter : public boost::noncopyable
139 {
140 private:
141 SharedMetrics& metrics_;
142
143 public:
144 ActiveCounter(SharedMetrics& metrics) :
145 metrics_(metrics)
146 {
147 metrics_.Add(1);
148 }
149
150 ~ActiveCounter()
151 {
152 metrics_.Add(-1);
153 }
154 };
155
156
157 class ORTHANC_PUBLIC Timer : public boost::noncopyable
158 {
159 private:
160 MetricsRegistry& registry_;
161 std::string name_;
162 MetricsType type_;
163 bool active_;
164 boost::posix_time::ptime start_;
165
166 void Start();
167
168 public:
169 Timer(MetricsRegistry& registry,
170 const std::string& name) :
171 registry_(registry),
172 name_(name),
173 type_(MetricsType_MaxOver10Seconds)
174 {
175 Start();
176 }
177
178 Timer(MetricsRegistry& registry,
179 const std::string& name,
180 MetricsType type) :
181 registry_(registry),
182 name_(name),
183 type_(type)
184 {
185 Start();
186 }
187
188 ~Timer();
189 };
190 };
191 }