1 /*
2  * Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/net/prometheus/histogram.h>
8 
9 #include <stdlib.h>
10 #include <string.h>
11 
12 #include <zephyr/kernel.h>
13 
14 #include <zephyr/logging/log.h>
15 LOG_MODULE_REGISTER(pm_histogram, CONFIG_PROMETHEUS_LOG_LEVEL);
16 
prometheus_histogram_observe(struct prometheus_histogram * histogram,double value)17 int prometheus_histogram_observe(struct prometheus_histogram *histogram, double value)
18 {
19 	if (!histogram) {
20 		return -EINVAL;
21 	}
22 
23 	/* increment count */
24 	histogram->count++;
25 
26 	/* update sum */
27 	histogram->sum += value;
28 
29 	/* find appropriate bucket */
30 	for (size_t i = 0; i < histogram->num_buckets; ++i) {
31 		if (value <= histogram->buckets[i].upper_bound) {
32 			/* increment count for the bucket */
33 			histogram->buckets[i].count++;
34 
35 			LOG_DBG("value: %f, bucket: %f, count: %lu", value,
36 				histogram->buckets[i].upper_bound, histogram->buckets[i].count);
37 
38 			break;
39 		}
40 	}
41 
42 	return 0;
43 }
44