changeset 5431:4be5f117aa0d

metrics
author Alain Mazy <am@osimis.io>
date Tue, 21 Nov 2023 10:32:42 +0100
parents b83192e7ad10
children 59e3b6f8c5be 6f2b11bfee8d
files NEWS OrthancFramework/Sources/Cache/MemoryStringCache.cpp OrthancFramework/Sources/Cache/MemoryStringCache.h OrthancFramework/Sources/FileStorage/StorageCache.cpp OrthancFramework/Sources/FileStorage/StorageCache.h OrthancServer/Sources/OrthancRestApi/OrthancRestSystem.cpp OrthancServer/Sources/ServerContext.cpp OrthancServer/Sources/ServerContext.h
diffstat 8 files changed, 48 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Mon Nov 20 17:01:48 2023 +0100
+++ b/NEWS	Tue Nov 21 10:32:42 2023 +0100
@@ -45,6 +45,9 @@
 * All 'expand' GET arguments now accepts expand=true and expand=false values.
   The /studies/../instances and sibling routes are the only whose expand is true if not specified.
   These routes now accepts expand=false to simply list the child resources ids.
+* In /tools/metrics-prometheus:
+  - 'orthanc_dicom_cache_size' renamed into 'orthanc_dicom_cache_size_mb'
+  - added 'orthanc_storage_cache_count' and 'orthanc_storage_cache_size_mb'
 
 
 Maintenance
--- a/OrthancFramework/Sources/Cache/MemoryStringCache.cpp	Mon Nov 20 17:01:48 2023 +0100
+++ b/OrthancFramework/Sources/Cache/MemoryStringCache.cpp	Tue Nov 21 10:32:42 2023 +0100
@@ -268,6 +268,20 @@
     }
 
     // Post-condition: "currentSize_ <= targetSize"
+  }
+
+  size_t MemoryStringCache::GetCurrentSize() const
+  {
+    boost::mutex::scoped_lock cacheLock(cacheMutex_);
+
+    return currentSize_;
+  }
+    
+  size_t MemoryStringCache::GetNumberOfItems() const
+  {
+    boost::mutex::scoped_lock cacheLock(cacheMutex_);
+    return content_.GetSize();
 
   }
+
 }
--- a/OrthancFramework/Sources/Cache/MemoryStringCache.h	Mon Nov 20 17:01:48 2023 +0100
+++ b/OrthancFramework/Sources/Cache/MemoryStringCache.h	Tue Nov 21 10:32:42 2023 +0100
@@ -70,7 +70,7 @@
   private:
     class StringValue;
 
-    boost::mutex              cacheMutex_;  // note: we can not use recursive_mutex with condition_variable
+    mutable boost::mutex      cacheMutex_;  // note: we can not use recursive_mutex with condition_variable
     boost::condition_variable cacheCond_;
     std::set<std::string>     itemsBeingLoaded_;
 
@@ -91,6 +91,10 @@
 
     void Invalidate(const std::string& key);
 
+    size_t GetCurrentSize() const;
+    
+    size_t GetNumberOfItems() const;
+
   private:
     void Add(const std::string& key,
              const std::string& value);
--- a/OrthancFramework/Sources/FileStorage/StorageCache.cpp	Mon Nov 20 17:01:48 2023 +0100
+++ b/OrthancFramework/Sources/FileStorage/StorageCache.cpp	Tue Nov 21 10:32:42 2023 +0100
@@ -185,4 +185,16 @@
 
     return false;
   }
+
+
+  size_t StorageCache::GetCurrentSize() const
+  {
+    return cache_.GetCurrentSize();
+  }
+  
+  size_t StorageCache::GetNumberOfItems() const
+  {
+    return cache_.GetNumberOfItems();
+  }
+
 }
--- a/OrthancFramework/Sources/FileStorage/StorageCache.h	Mon Nov 20 17:01:48 2023 +0100
+++ b/OrthancFramework/Sources/FileStorage/StorageCache.h	Tue Nov 21 10:32:42 2023 +0100
@@ -93,6 +93,10 @@
       void Invalidate(const std::string& uuid,
                       FileContentType contentType);
 
+      size_t GetCurrentSize() const;
+      
+      size_t GetNumberOfItems() const;
+
     private:
       void Add(const std::string& uuid, 
                FileContentType contentType,
--- a/OrthancServer/Sources/OrthancRestApi/OrthancRestSystem.cpp	Mon Nov 20 17:01:48 2023 +0100
+++ b/OrthancServer/Sources/OrthancRestApi/OrthancRestSystem.cpp	Tue Nov 21 10:32:42 2023 +0100
@@ -955,6 +955,8 @@
     registry.SetIntegerValue("orthanc_up_time_s", serverUpTime);
     registry.SetIntegerValue("orthanc_last_change", lastChange["Last"].asInt64());
 
+    context.PublishCacheMetrics();
+
     std::string s;
     registry.ExportPrometheusText(s);
 
--- a/OrthancServer/Sources/ServerContext.cpp	Mon Nov 20 17:01:48 2023 +0100
+++ b/OrthancServer/Sources/ServerContext.cpp	Tue Nov 21 10:32:42 2023 +0100
@@ -328,11 +328,15 @@
   }
 
 
-  void ServerContext::PublishDicomCacheMetrics()
+  void ServerContext::PublishCacheMetrics()
   {
-    metricsRegistry_->SetFloatValue("orthanc_dicom_cache_size",
+    metricsRegistry_->SetFloatValue("orthanc_dicom_cache_size_mb",
                                     static_cast<float>(dicomCache_.GetCurrentSize()) / static_cast<float>(1024 * 1024));
     metricsRegistry_->SetIntegerValue("orthanc_dicom_cache_count", dicomCache_.GetNumberOfItems());
+
+    metricsRegistry_->SetFloatValue("orthanc_storage_cache_size_mb",
+                                    static_cast<float>(storageCache_.GetCurrentSize()) / static_cast<float>(1024 * 1024));
+    metricsRegistry_->SetIntegerValue("orthanc_storage_cache_count", storageCache_.GetNumberOfItems());
   }
 
 
@@ -668,7 +672,6 @@
       // Remove the file from the DicomCache (useful if
       // "OverwriteInstances" is set to "true")
       dicomCache_.Invalidate(resultPublicId);
-      PublishDicomCacheMetrics();
 
       // TODO Should we use "gzip" instead?
       CompressionType compression = (compressionEnabled_ ? CompressionType_ZlibWithSize : CompressionType_None);
@@ -1328,7 +1331,6 @@
       try
       {
         context_.dicomCache_.Acquire(instancePublicId_, dicom_.release(), dicomSize_);
-        context_.PublishDicomCacheMetrics();
       }
       catch (OrthancException&)
       {
@@ -1406,7 +1408,6 @@
     {
       // remove the file from the DicomCache
       dicomCache_.Invalidate(uuid);
-      PublishDicomCacheMetrics();
     }
 
     return index_.DeleteResource(remainingAncestor, uuid, expectedType);
@@ -1419,7 +1420,6 @@
         change.GetChangeType() == ChangeType_Deleted)
     {
       dicomCache_.Invalidate(change.GetPublicId());
-      PublishDicomCacheMetrics();
     }
     
     pendingChanges_.Enqueue(change.Clone());
--- a/OrthancServer/Sources/ServerContext.h	Mon Nov 20 17:01:48 2023 +0100
+++ b/OrthancServer/Sources/ServerContext.h	Tue Nov 21 10:32:42 2023 +0100
@@ -282,8 +282,6 @@
                                       StoreInstanceMode mode,
                                       bool isReconstruct);
 
-    void PublishDicomCacheMetrics();
-
     // This method must only be called from "ServerIndex"!
     void RemoveFile(const std::string& fileUuid,
                     FileContentType type);
@@ -615,5 +613,7 @@
     }
 
     int64_t GetServerUpTime() const;
+
+    void PublishCacheMetrics();
   };
 }