comparison OrthancFramework/Sources/MetricsRegistry.h @ 5337:b376abae664a

Metrics can be stored either as floating-point numbers, or as integers
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 27 Jun 2023 17:55:09 +0200
parents dd9795dc380d
children 48b8dae6dc77
comparison
equal deleted inserted replaced
5336:dd9795dc380d 5337:b376abae664a
37 #include <boost/date_time/posix_time/posix_time.hpp> 37 #include <boost/date_time/posix_time/posix_time.hpp>
38 #include <stdint.h> 38 #include <stdint.h>
39 39
40 namespace Orthanc 40 namespace Orthanc
41 { 41 {
42 enum MetricsUpdate 42 enum MetricsUpdatePolicy
43 { 43 {
44 MetricsUpdate_Directly, 44 MetricsUpdatePolicy_Directly,
45 MetricsUpdate_MaxOver10Seconds, 45 MetricsUpdatePolicy_MaxOver10Seconds,
46 MetricsUpdate_MaxOver1Minute, 46 MetricsUpdatePolicy_MaxOver1Minute,
47 MetricsUpdate_MinOver10Seconds, 47 MetricsUpdatePolicy_MinOver10Seconds,
48 MetricsUpdate_MinOver1Minute 48 MetricsUpdatePolicy_MinOver1Minute
49 };
50
51 enum MetricsDataType
52 {
53 MetricsDataType_Float,
54 MetricsDataType_Integer
49 }; 55 };
50 56
51 class ORTHANC_PUBLIC MetricsRegistry : public boost::noncopyable 57 class ORTHANC_PUBLIC MetricsRegistry : public boost::noncopyable
52 { 58 {
53 private: 59 private:
54 class Item; 60 class Item;
61 class FloatItem;
62 class IntegerItem;
55 63
56 typedef std::map<std::string, Item*> Content; 64 typedef std::map<std::string, Item*> Content;
57 65
58 bool enabled_; 66 bool enabled_;
59 boost::mutex mutex_; 67 boost::mutex mutex_;
60 Content content_; 68 Content content_;
61 69
62 // The mutex must be locked 70 // The mutex must be locked
63 Item& GetItemInternal(const std::string& name, 71 Item& GetItemInternal(const std::string& name,
64 MetricsUpdate update); 72 MetricsUpdatePolicy policy,
73 MetricsDataType type);
65 74
66 public: 75 public:
67 MetricsRegistry(); 76 MetricsRegistry();
68 77
69 ~MetricsRegistry(); 78 ~MetricsRegistry();
71 bool IsEnabled() const; 80 bool IsEnabled() const;
72 81
73 void SetEnabled(bool enabled); 82 void SetEnabled(bool enabled);
74 83
75 void Register(const std::string& name, 84 void Register(const std::string& name,
76 MetricsUpdate update); 85 MetricsUpdatePolicy policy,
86 MetricsDataType type);
77 87
78 void SetValue(const std::string& name, 88 void SetFloatValue(const std::string& name,
79 int64_t value, 89 float value,
80 MetricsUpdate update); 90 MetricsUpdatePolicy policy /* only used if this is a new metrics */);
81 91
82 void SetValue(const std::string& name, 92 void SetFloatValue(const std::string& name,
83 int64_t value) 93 float value)
84 { 94 {
85 SetValue(name, value, MetricsUpdate_Directly); 95 SetFloatValue(name, value, MetricsUpdatePolicy_Directly);
86 } 96 }
97
98 void SetIntegerValue(const std::string& name,
99 int64_t value,
100 MetricsUpdatePolicy policy /* only used if this is a new metrics */);
101
102 void SetIntegerValue(const std::string& name,
103 int64_t value)
104 {
105 SetIntegerValue(name, value, MetricsUpdatePolicy_Directly);
106 }
107
108 void IncrementIntegerValue(const std::string& name,
109 int64_t delta);
87 110
88 void IncrementValue(const std::string& name, 111 MetricsUpdatePolicy GetUpdatePolicy(const std::string& metrics);
89 int64_t delta);
90 112
91 MetricsUpdate GetMetricsUpdate(const std::string& name); 113 MetricsDataType GetDataType(const std::string& metrics);
92 114
93 // https://prometheus.io/docs/instrumenting/exposition_formats/#text-based-format 115 // https://prometheus.io/docs/instrumenting/exposition_formats/#text-based-format
94 void ExportPrometheusText(std::string& s); 116 void ExportPrometheusText(std::string& s);
95 117
96 118
103 int64_t value_; 125 int64_t value_;
104 126
105 public: 127 public:
106 SharedMetrics(MetricsRegistry& registry, 128 SharedMetrics(MetricsRegistry& registry,
107 const std::string& name, 129 const std::string& name,
108 MetricsUpdate update); 130 MetricsUpdatePolicy policy);
109 131
110 void Add(int64_t delta); 132 void Add(int64_t delta);
111 }; 133 };
112 134
113 135
126 class ORTHANC_PUBLIC Timer : public boost::noncopyable 148 class ORTHANC_PUBLIC Timer : public boost::noncopyable
127 { 149 {
128 private: 150 private:
129 MetricsRegistry& registry_; 151 MetricsRegistry& registry_;
130 std::string name_; 152 std::string name_;
131 MetricsUpdate update_; 153 MetricsUpdatePolicy policy_;
132 bool active_; 154 bool active_;
133 boost::posix_time::ptime start_; 155 boost::posix_time::ptime start_;
134 156
135 void Start(); 157 void Start();
136 158
138 Timer(MetricsRegistry& registry, 160 Timer(MetricsRegistry& registry,
139 const std::string& name); 161 const std::string& name);
140 162
141 Timer(MetricsRegistry& registry, 163 Timer(MetricsRegistry& registry,
142 const std::string& name, 164 const std::string& name,
143 MetricsUpdate update); 165 MetricsUpdatePolicy policy);
144 166
145 ~Timer(); 167 ~Timer();
146 }; 168 };
147 }; 169 };
148 } 170 }