Mercurial > hg > orthanc
comparison Core/MetricsRegistry.h @ 3174:8ea7c4546c3a
primitives to collect metrics in Orthanc
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 29 Jan 2019 15:15:48 +0100 |
parents | |
children | 784bbb03fb54 |
comparison
equal
deleted
inserted
replaced
3173:096f4a29f223 | 3174:8ea7c4546c3a |
---|---|
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-2019 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 #if !defined(ORTHANC_SANDBOXED) | |
37 # error The macro ORTHANC_SANDBOXED must be defined | |
38 #endif | |
39 | |
40 #if ORTHANC_SANDBOXED == 1 | |
41 # error The class MetricsRegistry cannot be used in sandboxed environments | |
42 #endif | |
43 | |
44 #include <boost/thread/mutex.hpp> | |
45 #include <boost/date_time/posix_time/posix_time.hpp> | |
46 | |
47 namespace Orthanc | |
48 { | |
49 enum MetricsType | |
50 { | |
51 MetricsType_Default, | |
52 MetricsType_MaxOver10Seconds, | |
53 MetricsType_MaxOver1Minute, | |
54 MetricsType_MinOver10Seconds, | |
55 MetricsType_MinOver1Minute | |
56 }; | |
57 | |
58 class MetricsRegistry : public boost::noncopyable | |
59 { | |
60 private: | |
61 class Item; | |
62 | |
63 typedef std::map<std::string, Item*> Content; | |
64 | |
65 bool enabled_; | |
66 boost::mutex mutex_; | |
67 Content content_; | |
68 | |
69 void SetValueInternal(const std::string& name, | |
70 float value, | |
71 MetricsType type); | |
72 | |
73 public: | |
74 MetricsRegistry() : | |
75 enabled_(true) | |
76 { | |
77 } | |
78 | |
79 ~MetricsRegistry(); | |
80 | |
81 bool IsEnabled() const | |
82 { | |
83 return enabled_; | |
84 } | |
85 | |
86 void SetEnabled(bool enabled); | |
87 | |
88 void Register(const std::string& name, | |
89 MetricsType type); | |
90 | |
91 void SetValue(const std::string& name, | |
92 float value, | |
93 MetricsType type) | |
94 { | |
95 // Inlining to avoid loosing time if metrics are disabled | |
96 if (enabled_) | |
97 { | |
98 SetValueInternal(name, value, type); | |
99 } | |
100 } | |
101 | |
102 void SetValue(const std::string& name, | |
103 float value) | |
104 { | |
105 SetValue(name, value, MetricsType_Default); | |
106 } | |
107 | |
108 MetricsType GetMetricsType(const std::string& name); | |
109 | |
110 // https://prometheus.io/docs/instrumenting/exposition_formats/#text-based-format | |
111 void ExportPrometheusText(std::string& s); | |
112 | |
113 | |
114 class Timer : public boost::noncopyable | |
115 { | |
116 private: | |
117 MetricsRegistry& registry_; | |
118 std::string name_; | |
119 MetricsType type_; | |
120 bool active_; | |
121 boost::posix_time::ptime start_; | |
122 | |
123 void Start(); | |
124 | |
125 public: | |
126 Timer(MetricsRegistry& registry, | |
127 const std::string& name) : | |
128 registry_(registry), | |
129 name_(name), | |
130 type_(MetricsType_MaxOver10Seconds) | |
131 { | |
132 Start(); | |
133 } | |
134 | |
135 Timer(MetricsRegistry& registry, | |
136 const std::string& name, | |
137 MetricsType type) : | |
138 registry_(registry), | |
139 name_(name), | |
140 type_(type) | |
141 { | |
142 Start(); | |
143 } | |
144 | |
145 ~Timer(); | |
146 }; | |
147 }; | |
148 } |