Skip to content

Commit fbffaf7

Browse files
mustafaabdullahkkrish2718
authored andcommitted
[nrf fromtree] net: add initial prometheus client library
The library provides Prometheus metrics types, collector and exposion formatter. The library isn't thread-safe for now. The next first pull request will support that. Can be use exposion formatted output with Zephyr Http server. Signed-off-by: Mustafa Abdullah Kus <[email protected]> (cherry picked from commit d482e3d)
1 parent c21cc96 commit fbffaf7

File tree

19 files changed

+1159
-0
lines changed

19 files changed

+1159
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_INCLUDE_PROMETHEUS_COLLECTOR_H_
8+
#define ZEPHYR_INCLUDE_PROMETHEUS_COLLECTOR_H_
9+
10+
/**
11+
* @file
12+
*
13+
* @brief Prometheus collector APIs.
14+
*
15+
* @defgroup prometheus Prometheus API
16+
* @since 4.0
17+
* @version 0.1.0
18+
* @ingroup networking
19+
* @{
20+
*/
21+
22+
#include <zephyr/sys/iterable_sections.h>
23+
#include <zephyr/net/prometheus/metric.h>
24+
25+
#include <stddef.h>
26+
27+
/**
28+
* @brief Prometheus collector definition
29+
*
30+
* This structure defines a Prometheus collector.
31+
*/
32+
struct prometheus_collector {
33+
/** Name of the collector */
34+
const char *name;
35+
/** Array of metrics associated with the collector */
36+
struct prometheus_metric *metric[CONFIG_PROMETHEUS_MAX_METRICS];
37+
/** Number of metrics associated with the collector */
38+
size_t size;
39+
};
40+
41+
/**
42+
* @brief Prometheus Collector definition.
43+
*
44+
* This macro defines a Collector.
45+
*
46+
* @param _name The collector's name.
47+
*/
48+
#define PROMETHEUS_COLLECTOR_DEFINE(_name) \
49+
static STRUCT_SECTION_ITERABLE(prometheus_collector, _name) = { \
50+
.name = STRINGIFY(_name), .size = 0, .metric = {0}}
51+
52+
/**
53+
* @brief Register a metric with a Prometheus collector
54+
*
55+
* Registers the specified metric with the given collector.
56+
*
57+
* @param collector Pointer to the collector to register the metric with.
58+
* @param metric Pointer to the metric to register.
59+
*
60+
* @return 0 if successful, otherwise a negative error code.
61+
* @retval -EINVAL Invalid arguments.
62+
* @retval -ENOMEM Not enough memory to register the metric.
63+
*/
64+
int prometheus_collector_register_metric(struct prometheus_collector *collector,
65+
struct prometheus_metric *metric);
66+
67+
/**
68+
* @brief Get a metric from a Prometheus collector
69+
*
70+
* Retrieves the metric with the specified name from the given collector.
71+
*
72+
* @param collector Pointer to the collector to retrieve the metric from.
73+
* @param name Name of the metric to retrieve.
74+
* @return Pointer to the retrieved metric, or NULL if not found.
75+
*/
76+
const void *prometheus_collector_get_metric(const struct prometheus_collector *collector,
77+
const char *name);
78+
79+
/**
80+
* @}
81+
*/
82+
83+
#endif /* ZEPHYR_INCLUDE_PROMETHEUS_COLLECTOR_H_ */
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_INCLUDE_PROMETHEUS_COUNTER_H_
8+
#define ZEPHYR_INCLUDE_PROMETHEUS_COUNTER_H_
9+
10+
/**
11+
* @file
12+
*
13+
* @brief Prometheus counter APIs.
14+
*
15+
* @addtogroup prometheus
16+
* @{
17+
*/
18+
19+
#include <stdint.h>
20+
21+
#include <zephyr/sys/iterable_sections.h>
22+
#include <zephyr/net/prometheus/metric.h>
23+
24+
/**
25+
* @brief Type used to represent a Prometheus counter metric.
26+
*
27+
* * References
28+
* * See https://prometheus.io/docs/concepts/metric_types/#counter
29+
*/
30+
struct prometheus_counter {
31+
/** Base of the Prometheus counter metric */
32+
struct prometheus_metric *base;
33+
/** Value of the Prometheus counter metric */
34+
uint64_t value;
35+
};
36+
37+
/**
38+
* @brief Prometheus Counter definition.
39+
*
40+
* This macro defines a Counter metric.
41+
*
42+
* @param _name The channel's name.
43+
* @param _detail The metric base.
44+
*
45+
* Example usage:
46+
* @code{.c}
47+
*
48+
* struct prometheus_metric http_request_counter = {
49+
* .type = PROMETHEUS_COUNTER,
50+
* .name = "http_request_counter",
51+
* .description = "HTTP request counter",
52+
* .num_labels = 1,
53+
* .labels = {
54+
* { .key = "http_request", .value = "request_count",}
55+
* },
56+
*};
57+
*
58+
* PROMETHEUS_COUNTER_DEFINE(test_counter, &test_counter_metric);
59+
* @endcode
60+
*/
61+
#define PROMETHEUS_COUNTER_DEFINE(_name, _detail) \
62+
static STRUCT_SECTION_ITERABLE(prometheus_counter, _name) = {.base = (void *)(_detail), \
63+
.value = 0}
64+
65+
/**
66+
* @brief Increment the value of a Prometheus counter metric
67+
* Increments the value of the specified counter metric by one.
68+
* @param counter Pointer to the counter metric to increment.
69+
* @return 0 on success, negative errno on error.
70+
*/
71+
int prometheus_counter_inc(struct prometheus_counter *counter);
72+
73+
/**
74+
* @}
75+
*/
76+
77+
#endif /* ZEPHYR_INCLUDE_PROMETHEUS_COUNTER_H_ */
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_INCLUDE_PROMETHEUS_FORMATTER_H_
8+
#define ZEPHYR_INCLUDE_PROMETHEUS_FORMATTER_H_
9+
10+
/**
11+
* @file
12+
*
13+
* @brief Prometheus formatter APIs.
14+
*
15+
* @addtogroup prometheus
16+
* @{
17+
*/
18+
19+
#include <zephyr/net/prometheus/collector.h>
20+
21+
/**
22+
* @brief Format exposition data for Prometheus
23+
*
24+
* Formats the exposition data collected by the specified collector into the provided buffer.
25+
* Function to format metric data according to Prometheus text-based format
26+
*
27+
* @param collector Pointer to the collector containing the data to format.
28+
* @param buffer Pointer to the buffer where the formatted exposition data will be stored.
29+
* @param buffer_size Size of the buffer.
30+
*
31+
* @return 0 on success, negative errno on error.
32+
*/
33+
int prometheus_format_exposition(const struct prometheus_collector *collector, char *buffer,
34+
size_t buffer_size);
35+
36+
/**
37+
* @}
38+
*/
39+
40+
#endif /* ZEPHYR_INCLUDE_PROMETHEUS_FORMATTER_H_ */
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_INCLUDE_PROMETHEUS_GAUGE_H_
8+
#define ZEPHYR_INCLUDE_PROMETHEUS_GAUGE_H_
9+
10+
/**
11+
* @file
12+
*
13+
* @brief Prometheus gauge APIs.
14+
*
15+
* @addtogroup prometheus
16+
* @{
17+
*/
18+
19+
#include <zephyr/sys/iterable_sections.h>
20+
#include <zephyr/net/prometheus/metric.h>
21+
22+
/**
23+
* @brief Type used to represent a Prometheus gauge metric.
24+
*
25+
* * References
26+
* * See https://prometheus.io/docs/concepts/metric_types/#gauge
27+
*/
28+
struct prometheus_gauge {
29+
/** Base of the Prometheus gauge metric */
30+
struct prometheus_metric *base;
31+
/** Value of the Prometheus gauge metric */
32+
double value;
33+
};
34+
35+
/**
36+
* @brief Prometheus Gauge definition.
37+
*
38+
* This macro defines a Gauge metric.
39+
*
40+
* @param _name The channel's name.
41+
* @param _detail The metric base.
42+
*
43+
* Example usage:
44+
* @code{.c}
45+
*
46+
* struct prometheus_metric http_request_gauge = {
47+
* .type = PROMETHEUS_GAUGE,
48+
* .name = "http_request_gauge",
49+
* .description = "HTTP request gauge",
50+
* .num_labels = 1,
51+
* .labels = {
52+
* { .key = "http_request", .value = "request_count",}
53+
* },
54+
* };
55+
*
56+
* PROMETHEUS_GAUGE_DEFINE(test_gauge, &test_gauge_metric);
57+
*
58+
* @endcode
59+
*/
60+
#define PROMETHEUS_GAUGE_DEFINE(_name, _detail) \
61+
static STRUCT_SECTION_ITERABLE(prometheus_gauge, _name) = {.base = (void *)(_detail), \
62+
.value = 0}
63+
64+
/**
65+
* @brief Set the value of a Prometheus gauge metric
66+
*
67+
* Sets the value of the specified gauge metric to the given value.
68+
*
69+
* @param gauge Pointer to the gauge metric to set.
70+
* @param value Value to set the gauge metric to.
71+
*
72+
* @return 0 on success, -EINVAL if the value is negative.
73+
*/
74+
int prometheus_gauge_set(struct prometheus_gauge *gauge, double value);
75+
76+
/**
77+
* @}
78+
*/
79+
80+
#endif /* ZEPHYR_INCLUDE_PROMETHEUS_GAUGE_H_ */
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_INCLUDE_PROMETHEUS_HISTOGRAM_H_
8+
#define ZEPHYR_INCLUDE_PROMETHEUS_HISTOGRAM_H_
9+
10+
/**
11+
* @file
12+
*
13+
* @brief Prometheus histogram APIs.
14+
*
15+
* @addtogroup prometheus
16+
* @{
17+
*/
18+
19+
#include <zephyr/sys/iterable_sections.h>
20+
#include <zephyr/net/prometheus/metric.h>
21+
22+
#include <stddef.h>
23+
24+
/**
25+
* @brief Prometheus histogram bucket definition.
26+
*
27+
* This structure defines a Prometheus histogram bucket.
28+
*/
29+
struct prometheus_histogram_bucket {
30+
/** Upper bound value of bucket */
31+
double upper_bound;
32+
/** Cumulative count of observations in the bucket */
33+
unsigned long count;
34+
};
35+
36+
/**
37+
* @brief Type used to represent a Prometheus histogram metric.
38+
*
39+
* * References
40+
* * See https://prometheus.io/docs/concepts/metric_types/#histogram
41+
*/
42+
struct prometheus_histogram {
43+
/** Base of the Prometheus histogram metric */
44+
struct prometheus_metric *base;
45+
/** Array of buckets in the histogram */
46+
struct prometheus_histogram_bucket *buckets;
47+
/** Number of buckets in the histogram */
48+
size_t num_buckets;
49+
/** Sum of all observed values in the histogram */
50+
double sum;
51+
/** Total count of observations in the histogram */
52+
unsigned long count;
53+
};
54+
55+
/**
56+
* @brief Prometheus Histogram definition.
57+
*
58+
* This macro defines a Histogram metric.
59+
*
60+
* @param _name The channel's name.
61+
* @param _detail The metric base.
62+
*
63+
* Example usage:
64+
* @code{.c}
65+
*
66+
* struct prometheus_metric http_request_gauge = {
67+
* .type = PROMETHEUS_HISTOGRAM,
68+
* .name = "http_request_histograms",
69+
* .description = "HTTP request histogram",
70+
* .num_labels = 1,
71+
* .labels = {
72+
* { .key = "request_latency", .value = "request_latency_seconds",}
73+
* },
74+
* };
75+
*
76+
* PROMETHEUS_HISTOGRAM_DEFINE(test_histogram, &test_histogram_metric);
77+
*
78+
* @endcode
79+
*/
80+
#define PROMETHEUS_HISTOGRAM_DEFINE(_name, _detail) \
81+
static STRUCT_SECTION_ITERABLE(prometheus_histogram, _name) = {.base = (void *)(_detail), \
82+
.buckets = NULL, \
83+
.num_buckets = 0, \
84+
.sum = 0, \
85+
.count = 0}
86+
87+
/**
88+
* @brief Observe a value in a Prometheus histogram metric
89+
*
90+
* Observes the specified value in the given histogram metric.
91+
*
92+
* @param histogram Pointer to the histogram metric to observe.
93+
* @param value Value to observe in the histogram metric.
94+
* @return 0 on success, -EINVAL if the value is negative.
95+
*/
96+
int prometheus_histogram_observe(struct prometheus_histogram *histogram, double value);
97+
98+
/**
99+
* @}
100+
*/
101+
102+
#endif /* ZEPHYR_INCLUDE_PROMETHEUS_HISTOGRAM_H_ */

0 commit comments

Comments
 (0)