Skip to content

Commit 460e2f8

Browse files
jukkarnashif
authored andcommitted
net: prometheus: summary: Add set function to summary metric
Add prometheus_summary_observe_set() function to summary metric. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 2a5c702 commit 460e2f8

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

include/zephyr/net/prometheus/summary.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ struct prometheus_summary {
9696
*/
9797
int prometheus_summary_observe(struct prometheus_summary *summary, double value);
9898

99+
/**
100+
* @brief Set the summary value to specific value.
101+
* The new value must be higher than the current value. This function can be used
102+
* if we cannot add individual increments to the summary but need to periodically
103+
* update the counter value. This function will add the difference between the
104+
* new value and the old value to the summary fields.
105+
* @param summary Pointer to the summary metric to increment.
106+
* @param value New value of the summary.
107+
* @param count New counter value of the summary.
108+
* @return 0 on success, negative errno on error.
109+
*/
110+
int prometheus_summary_observe_set(struct prometheus_summary *summary,
111+
double value, unsigned long count);
112+
99113
/**
100114
* @}
101115
*/

subsys/net/lib/prometheus/summary.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,31 @@ int prometheus_summary_observe(struct prometheus_summary *summary, double value)
2828

2929
return 0;
3030
}
31+
32+
int prometheus_summary_observe_set(struct prometheus_summary *summary,
33+
double value, unsigned long count)
34+
{
35+
unsigned long old_count;
36+
double old_sum;
37+
38+
if (summary == NULL) {
39+
return -EINVAL;
40+
}
41+
42+
if (value == summary->sum && count == summary->count) {
43+
return 0;
44+
}
45+
46+
old_count = summary->count;
47+
if (count < old_count) {
48+
LOG_DBG("Cannot set summary count to a lower value");
49+
return -EINVAL;
50+
}
51+
52+
summary->count += (count - old_count);
53+
54+
old_sum = summary->sum;
55+
summary->sum += (value - old_sum);
56+
57+
return 0;
58+
}

0 commit comments

Comments
 (0)