changeset 3176:784bbb03fb54

new metrics: orthanc_rest_api_active_requests
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 29 Jan 2019 18:07:41 +0100
parents 574890d14c92
children 053e72ff9fc5
files Core/MetricsRegistry.cpp Core/MetricsRegistry.h OrthancServer/OrthancRestApi/OrthancRestApi.cpp OrthancServer/OrthancRestApi/OrthancRestApi.h
diffstat 4 files changed, 60 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/Core/MetricsRegistry.cpp	Tue Jan 29 17:34:09 2019 +0100
+++ b/Core/MetricsRegistry.cpp	Tue Jan 29 18:07:41 2019 +0100
@@ -295,6 +295,14 @@
   }
 
 
+  void MetricsRegistry::SharedMetrics::Add(float delta)
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+    value_ += delta;
+    registry_.SetValue(name_, value_);
+  }
+
+
   void  MetricsRegistry::Timer::Start()
   {
     if (registry_.IsEnabled())
--- a/Core/MetricsRegistry.h	Tue Jan 29 17:34:09 2019 +0100
+++ b/Core/MetricsRegistry.h	Tue Jan 29 18:07:41 2019 +0100
@@ -111,6 +111,47 @@
     void ExportPrometheusText(std::string& s);
 
 
+    class SharedMetrics : public boost::noncopyable
+    {
+    private:
+      boost::mutex      mutex_;
+      MetricsRegistry&  registry_;
+      std::string       name_;
+      float             value_;
+
+    public:
+      SharedMetrics(MetricsRegistry& registry,
+                    const std::string& name,
+                    MetricsType type) :
+        registry_(registry),
+        name_(name),
+        value_(0)
+      {
+      }
+
+      void Add(float delta);
+    };
+
+
+    class ActiveCounter : public boost::noncopyable
+    {
+    private:
+      SharedMetrics&   metrics_;
+
+    public:
+      ActiveCounter(SharedMetrics& metrics) :
+        metrics_(metrics)
+      {
+        metrics_.Add(1);
+      }
+
+      ~ActiveCounter()
+      {
+        metrics_.Add(-1);
+      }
+    };
+
+
     class Timer : public boost::noncopyable
     {
     private:
--- a/OrthancServer/OrthancRestApi/OrthancRestApi.cpp	Tue Jan 29 17:34:09 2019 +0100
+++ b/OrthancServer/OrthancRestApi/OrthancRestApi.cpp	Tue Jan 29 18:07:41 2019 +0100
@@ -137,7 +137,10 @@
   OrthancRestApi::OrthancRestApi(ServerContext& context) : 
     context_(context),
     leaveBarrier_(false),
-    resetRequestReceived_(false)
+    resetRequestReceived_(false),
+    activeRequests_(context.GetMetricsRegistry(), 
+                    "orthanc_rest_api_active_requests", 
+                    MetricsType_MaxOver10Seconds)
   {
     RegisterSystem();
 
@@ -169,6 +172,7 @@
                               size_t bodySize)
   {
     MetricsRegistry::Timer timer(context_.GetMetricsRegistry(), "orthanc_rest_api_duration_ms");
+    MetricsRegistry::ActiveCounter counter(activeRequests_);
 
     return RestApi::Handle(output, origin, remoteIp, username, method,
                            uri, headers, getArguments, bodyData, bodySize);
--- a/OrthancServer/OrthancRestApi/OrthancRestApi.h	Tue Jan 29 17:34:09 2019 +0100
+++ b/OrthancServer/OrthancRestApi/OrthancRestApi.h	Tue Jan 29 18:07:41 2019 +0100
@@ -33,9 +33,10 @@
 
 #pragma once
 
+#include "../../Core/DicomParsing/DicomModification.h"
 #include "../../Core/JobsEngine/SetOfCommandsJob.h"
+#include "../../Core/MetricsRegistry.h"
 #include "../../Core/RestApi/RestApi.h"
-#include "../../Core/DicomParsing/DicomModification.h"
 #include "../ServerEnumerations.h"
 
 #include <set>
@@ -52,9 +53,10 @@
     typedef std::set<std::string> SetOfStrings;
 
   private:
-    ServerContext& context_;
-    bool leaveBarrier_;
-    bool resetRequestReceived_;
+    ServerContext&                  context_;
+    bool                            leaveBarrier_;
+    bool                            resetRequestReceived_;
+    MetricsRegistry::SharedMetrics  activeRequests_;
 
     void RegisterSystem();