diff OrthancFramework/Sources/MetricsRegistry.cpp @ 4300:b30a8de92ad9

abi continued
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 05 Nov 2020 19:33:18 +0100
parents 4d42408da117
children d9473bd5ed43
line wrap: on
line diff
--- a/OrthancFramework/Sources/MetricsRegistry.cpp	Thu Nov 05 18:24:50 2020 +0100
+++ b/OrthancFramework/Sources/MetricsRegistry.cpp	Thu Nov 05 19:33:18 2020 +0100
@@ -175,6 +175,11 @@
     }
   }
 
+  bool MetricsRegistry::IsEnabled() const
+  {
+    return enabled_;
+  }
+
 
   void MetricsRegistry::SetEnabled(bool enabled)
   {
@@ -208,7 +213,6 @@
     }    
   }
 
-
   void MetricsRegistry::SetValueInternal(const std::string& name,
                                          float value,
                                          MetricsType type)
@@ -230,6 +234,29 @@
     }
   }
 
+  MetricsRegistry::MetricsRegistry() :
+    enabled_(true)
+  {
+  }
+
+
+  void MetricsRegistry::SetValue(const std::string &name,
+                                 float value,
+                                 MetricsType type)
+  {
+    // Inlining to avoid loosing time if metrics are disabled
+    if (enabled_)
+    {
+      SetValueInternal(name, value, type);
+    }
+  }
+
+
+  void MetricsRegistry::SetValue(const std::string &name, float value)
+  {
+    SetValue(name, value, MetricsType_Default);
+  }
+
 
   MetricsType MetricsRegistry::GetMetricsType(const std::string& name)
   {
@@ -286,6 +313,15 @@
   }
 
 
+  MetricsRegistry::SharedMetrics::SharedMetrics(MetricsRegistry &registry,
+                                                const std::string &name,
+                                                MetricsType type) :
+    registry_(registry),
+    name_(name),
+    value_(0)
+  {
+  }
+
   void MetricsRegistry::SharedMetrics::Add(float delta)
   {
     boost::mutex::scoped_lock lock(mutex_);
@@ -294,6 +330,18 @@
   }
 
 
+  MetricsRegistry::ActiveCounter::ActiveCounter(MetricsRegistry::SharedMetrics &metrics) :
+    metrics_(metrics)
+  {
+    metrics_.Add(1);
+  }
+
+  MetricsRegistry::ActiveCounter::~ActiveCounter()
+  {
+    metrics_.Add(-1);
+  }
+
+
   void  MetricsRegistry::Timer::Start()
   {
     if (registry_.IsEnabled())
@@ -308,13 +356,34 @@
   }
 
 
+  MetricsRegistry::Timer::Timer(MetricsRegistry &registry,
+                                const std::string &name) :
+    registry_(registry),
+    name_(name),
+    type_(MetricsType_MaxOver10Seconds)
+  {
+    Start();
+  }
+
+
+  MetricsRegistry::Timer::Timer(MetricsRegistry &registry,
+                                const std::string &name,
+                                MetricsType type) :
+    registry_(registry),
+    name_(name),
+    type_(type)
+  {
+    Start();
+  }
+
+
   MetricsRegistry::Timer::~Timer()
   {
     if (active_)
-    {   
+    {
       boost::posix_time::time_duration diff = GetNow() - start_;
       registry_.SetValue(
-        name_, static_cast<float>(diff.total_milliseconds()), type_);
+            name_, static_cast<float>(diff.total_milliseconds()), type_);
     }
   }
 }