Mercurial > hg > orthanc
comparison OrthancFramework/Sources/MetricsRegistry.cpp @ 5336:dd9795dc380d
renamed MetricsType as MetricsUpdate to clarify
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 27 Jun 2023 15:56:04 +0200 |
parents | c103b0e70d75 |
children | b376abae664a |
comparison
equal
deleted
inserted
replaced
5335:c103b0e70d75 | 5336:dd9795dc380d |
---|---|
33 static const boost::posix_time::ptime GetNow() | 33 static const boost::posix_time::ptime GetNow() |
34 { | 34 { |
35 return boost::posix_time::microsec_clock::universal_time(); | 35 return boost::posix_time::microsec_clock::universal_time(); |
36 } | 36 } |
37 | 37 |
38 class MetricsRegistry::Item | 38 class MetricsRegistry::Item : public boost::noncopyable |
39 { | 39 { |
40 private: | 40 private: |
41 MetricsType type_; | 41 MetricsUpdate update_; |
42 boost::posix_time::ptime time_; | 42 boost::posix_time::ptime time_; |
43 bool hasValue_; | 43 bool hasValue_; |
44 int64_t value_; | 44 int64_t value_; |
45 | 45 |
46 void SetValue(int64_t value, | 46 void SetValue(int64_t value, |
80 return true; // No value yet | 80 return true; // No value yet |
81 } | 81 } |
82 } | 82 } |
83 | 83 |
84 public: | 84 public: |
85 explicit Item(MetricsType type) : | 85 explicit Item(MetricsUpdate update) : |
86 type_(type), | 86 update_(update), |
87 hasValue_(false), | 87 hasValue_(false), |
88 value_(0) | 88 value_(0) |
89 { | 89 { |
90 } | 90 } |
91 | 91 |
92 MetricsType GetType() const | 92 MetricsUpdate GetUpdate() const |
93 { | 93 { |
94 return type_; | 94 return update_; |
95 } | 95 } |
96 | 96 |
97 void Update(int64_t value) | 97 void Update(int64_t value) |
98 { | 98 { |
99 const boost::posix_time::ptime now = GetNow(); | 99 const boost::posix_time::ptime now = GetNow(); |
100 | 100 |
101 switch (type_) | 101 switch (update_) |
102 { | 102 { |
103 case MetricsType_Default: | 103 case MetricsUpdate_Directly: |
104 SetValue(value, now); | 104 SetValue(value, now); |
105 break; | 105 break; |
106 | 106 |
107 case MetricsType_MaxOver10Seconds: | 107 case MetricsUpdate_MaxOver10Seconds: |
108 if (IsLargerOverPeriod(value, 10, now)) | 108 if (IsLargerOverPeriod(value, 10, now)) |
109 { | 109 { |
110 SetValue(value, now); | 110 SetValue(value, now); |
111 } | 111 } |
112 break; | 112 break; |
113 | 113 |
114 case MetricsType_MaxOver1Minute: | 114 case MetricsUpdate_MaxOver1Minute: |
115 if (IsLargerOverPeriod(value, 60, now)) | 115 if (IsLargerOverPeriod(value, 60, now)) |
116 { | 116 { |
117 SetValue(value, now); | 117 SetValue(value, now); |
118 } | 118 } |
119 break; | 119 break; |
120 | 120 |
121 case MetricsType_MinOver10Seconds: | 121 case MetricsUpdate_MinOver10Seconds: |
122 if (IsSmallerOverPeriod(value, 10, now)) | 122 if (IsSmallerOverPeriod(value, 10, now)) |
123 { | 123 { |
124 SetValue(value, now); | 124 SetValue(value, now); |
125 } | 125 } |
126 break; | 126 break; |
127 | 127 |
128 case MetricsType_MinOver1Minute: | 128 case MetricsUpdate_MinOver1Minute: |
129 if (IsSmallerOverPeriod(value, 60, now)) | 129 if (IsSmallerOverPeriod(value, 60, now)) |
130 { | 130 { |
131 SetValue(value, now); | 131 SetValue(value, now); |
132 } | 132 } |
133 break; | 133 break; |
189 enabled_ = enabled; | 189 enabled_ = enabled; |
190 } | 190 } |
191 | 191 |
192 | 192 |
193 void MetricsRegistry::Register(const std::string& name, | 193 void MetricsRegistry::Register(const std::string& name, |
194 MetricsType type) | 194 MetricsUpdate update) |
195 { | 195 { |
196 boost::mutex::scoped_lock lock(mutex_); | 196 boost::mutex::scoped_lock lock(mutex_); |
197 | 197 |
198 Content::iterator found = content_.find(name); | 198 Content::iterator found = content_.find(name); |
199 | 199 |
200 if (found == content_.end()) | 200 if (found == content_.end()) |
201 { | 201 { |
202 content_[name] = new Item(type); | 202 content_[name] = new Item(update); |
203 } | 203 } |
204 else | 204 else |
205 { | 205 { |
206 assert(found->second != NULL); | 206 assert(found->second != NULL); |
207 | 207 |
208 // This metrics already exists: Only recreate it if there is a | 208 // This metrics already exists: Only recreate it if there is a |
209 // mismatch in the type of metrics | 209 // mismatch in the type of metrics |
210 if (found->second->GetType() != type) | 210 if (found->second->GetUpdate() != update) |
211 { | 211 { |
212 delete found->second; | 212 delete found->second; |
213 found->second = new Item(type); | 213 found->second = new Item(update); |
214 } | 214 } |
215 } | 215 } |
216 } | 216 } |
217 | 217 |
218 | 218 |
219 MetricsRegistry::Item& MetricsRegistry::GetItemInternal(const std::string& name, | 219 MetricsRegistry::Item& MetricsRegistry::GetItemInternal(const std::string& name, |
220 MetricsType type) | 220 MetricsUpdate update) |
221 { | 221 { |
222 Content::iterator found = content_.find(name); | 222 Content::iterator found = content_.find(name); |
223 | 223 |
224 if (found == content_.end()) | 224 if (found == content_.end()) |
225 { | 225 { |
226 Item* item = new Item(type); | 226 Item* item = new Item(update); |
227 content_[name] = item; | 227 content_[name] = item; |
228 return *item; | 228 return *item; |
229 } | 229 } |
230 else | 230 else |
231 { | 231 { |
240 } | 240 } |
241 | 241 |
242 | 242 |
243 void MetricsRegistry::SetValue(const std::string &name, | 243 void MetricsRegistry::SetValue(const std::string &name, |
244 int64_t value, | 244 int64_t value, |
245 MetricsType type) | 245 MetricsUpdate update) |
246 { | 246 { |
247 // Inlining to avoid loosing time if metrics are disabled | 247 // Inlining to avoid loosing time if metrics are disabled |
248 if (enabled_) | 248 if (enabled_) |
249 { | 249 { |
250 boost::mutex::scoped_lock lock(mutex_); | 250 boost::mutex::scoped_lock lock(mutex_); |
251 GetItemInternal(name, type).Update(value); | 251 GetItemInternal(name, update).Update(value); |
252 } | 252 } |
253 } | 253 } |
254 | 254 |
255 | 255 |
256 void MetricsRegistry::IncrementValue(const std::string &name, | 256 void MetricsRegistry::IncrementValue(const std::string &name, |
258 { | 258 { |
259 // Inlining to avoid loosing time if metrics are disabled | 259 // Inlining to avoid loosing time if metrics are disabled |
260 if (enabled_) | 260 if (enabled_) |
261 { | 261 { |
262 boost::mutex::scoped_lock lock(mutex_); | 262 boost::mutex::scoped_lock lock(mutex_); |
263 Item& item = GetItemInternal(name, MetricsType_Default); | 263 Item& item = GetItemInternal(name, MetricsUpdate_Directly); |
264 | 264 |
265 if (item.HasValue()) | 265 if (item.HasValue()) |
266 { | 266 { |
267 item.Update(item.GetValue() + delta); | 267 item.Update(item.GetValue() + delta); |
268 } | 268 } |
272 } | 272 } |
273 } | 273 } |
274 } | 274 } |
275 | 275 |
276 | 276 |
277 MetricsType MetricsRegistry::GetMetricsType(const std::string& name) | 277 MetricsUpdate MetricsRegistry::GetMetricsUpdate(const std::string& name) |
278 { | 278 { |
279 boost::mutex::scoped_lock lock(mutex_); | 279 boost::mutex::scoped_lock lock(mutex_); |
280 | 280 |
281 Content::const_iterator found = content_.find(name); | 281 Content::const_iterator found = content_.find(name); |
282 | 282 |
285 throw OrthancException(ErrorCode_InexistentItem); | 285 throw OrthancException(ErrorCode_InexistentItem); |
286 } | 286 } |
287 else | 287 else |
288 { | 288 { |
289 assert(found->second != NULL); | 289 assert(found->second != NULL); |
290 return found->second->GetType(); | 290 return found->second->GetUpdate(); |
291 } | 291 } |
292 } | 292 } |
293 | 293 |
294 | 294 |
295 void MetricsRegistry::ExportPrometheusText(std::string& s) | 295 void MetricsRegistry::ExportPrometheusText(std::string& s) |
329 } | 329 } |
330 | 330 |
331 | 331 |
332 MetricsRegistry::SharedMetrics::SharedMetrics(MetricsRegistry ®istry, | 332 MetricsRegistry::SharedMetrics::SharedMetrics(MetricsRegistry ®istry, |
333 const std::string &name, | 333 const std::string &name, |
334 MetricsType type) : | 334 MetricsUpdate update) : |
335 registry_(registry), | 335 registry_(registry), |
336 name_(name), | 336 name_(name), |
337 value_(0) | 337 value_(0) |
338 { | 338 { |
339 } | 339 } |
374 | 374 |
375 MetricsRegistry::Timer::Timer(MetricsRegistry ®istry, | 375 MetricsRegistry::Timer::Timer(MetricsRegistry ®istry, |
376 const std::string &name) : | 376 const std::string &name) : |
377 registry_(registry), | 377 registry_(registry), |
378 name_(name), | 378 name_(name), |
379 type_(MetricsType_MaxOver10Seconds) | 379 update_(MetricsUpdate_MaxOver10Seconds) |
380 { | 380 { |
381 Start(); | 381 Start(); |
382 } | 382 } |
383 | 383 |
384 | 384 |
385 MetricsRegistry::Timer::Timer(MetricsRegistry ®istry, | 385 MetricsRegistry::Timer::Timer(MetricsRegistry ®istry, |
386 const std::string &name, | 386 const std::string &name, |
387 MetricsType type) : | 387 MetricsUpdate update) : |
388 registry_(registry), | 388 registry_(registry), |
389 name_(name), | 389 name_(name), |
390 type_(type) | 390 update_(update) |
391 { | 391 { |
392 Start(); | 392 Start(); |
393 } | 393 } |
394 | 394 |
395 | 395 |
397 { | 397 { |
398 if (active_) | 398 if (active_) |
399 { | 399 { |
400 boost::posix_time::time_duration diff = GetNow() - start_; | 400 boost::posix_time::time_duration diff = GetNow() - start_; |
401 registry_.SetValue( | 401 registry_.SetValue( |
402 name_, static_cast<int64_t>(diff.total_milliseconds()), type_); | 402 name_, static_cast<int64_t>(diff.total_milliseconds()), update_); |
403 } | 403 } |
404 } | 404 } |
405 } | 405 } |