Skip to content

Commit d3efcbd

Browse files
jukkarnashif
authored andcommitted
net: prometheus: Remove need to have separate metric struct
Embed "struct prometheus_metric" to individual metric like counter, gauge, histogram and summary. This way we avoid having a separate base pointer in specific metrict struct. We also do not need to search the specific metric from base metric as we can simply use CONTAINER_OF() macro to get the base metric. This embedding means that the counter, gauge, histogram and summary metric define macros are changed as user does not need to create a separate "struct prometheus_metric". Convert the tests and sample to use the new macros. Remove also the static from metric creation macros so that user can decide whether it needs collector to be static or not. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 5282cca commit d3efcbd

File tree

15 files changed

+132
-198
lines changed

15 files changed

+132
-198
lines changed

include/zephyr/net/prometheus/collector.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
3+
* Copyright (c) 2024 Nordic Semiconductor
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
@@ -47,7 +48,7 @@ struct prometheus_collector {
4748
* @param _name The collector's name.
4849
*/
4950
#define PROMETHEUS_COLLECTOR_DEFINE(_name) \
50-
static STRUCT_SECTION_ITERABLE(prometheus_collector, _name) = { \
51+
STRUCT_SECTION_ITERABLE(prometheus_collector, _name) = { \
5152
.name = STRINGIFY(_name), \
5253
.metrics = SYS_SLIST_STATIC_INIT(&_name.metrics), \
5354
.lock = Z_MUTEX_INITIALIZER(_name.lock), \

include/zephyr/net/prometheus/counter.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
3+
* Copyright (c) 2024 Nordic Semiconductor
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
@@ -29,38 +30,37 @@
2930
*/
3031
struct prometheus_counter {
3132
/** Base of the Prometheus counter metric */
32-
struct prometheus_metric *base;
33+
struct prometheus_metric base;
3334
/** Value of the Prometheus counter metric */
3435
uint64_t value;
3536
};
3637

3738
/**
3839
* @brief Prometheus Counter definition.
3940
*
40-
* This macro defines a Counter metric.
41+
* This macro defines a Counter metric. If you want to make the counter static,
42+
* then add "static" keyword before the PROMETHEUS_COUNTER_DEFINE.
4143
*
42-
* @param _name The channel's name.
43-
* @param _detail The metric base.
44+
* @param _name The counter metric name
45+
* @param _desc Counter description
46+
* @param _label Label for the metric. Additional labels can be added at runtime.
4447
*
4548
* Example usage:
4649
* @code{.c}
4750
*
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);
51+
* PROMETHEUS_COUNTER_DEFINE(http_request_counter, "HTTP request counter",
52+
* ({ .key = "http_request", .value = "request_count" }));
5953
* @endcode
6054
*/
61-
#define PROMETHEUS_COUNTER_DEFINE(_name, _detail) \
62-
static STRUCT_SECTION_ITERABLE(prometheus_counter, _name) = {.base = (void *)(_detail), \
63-
.value = 0}
55+
#define PROMETHEUS_COUNTER_DEFINE(_name, _desc, _label) \
56+
STRUCT_SECTION_ITERABLE(prometheus_counter, _name) = { \
57+
.base.name = STRINGIFY(_name), \
58+
.base.type = PROMETHEUS_COUNTER, \
59+
.base.description = _desc, \
60+
.base.labels[0] = __DEBRACKET _label, \
61+
.base.num_labels = 1, \
62+
.value = 0ULL, \
63+
}
6464

6565
/**
6666
* @brief Increment the value of a Prometheus counter metric

include/zephyr/net/prometheus/gauge.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
3+
* Copyright (c) 2024 Nordic Semiconductor
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
@@ -27,39 +28,38 @@
2728
*/
2829
struct prometheus_gauge {
2930
/** Base of the Prometheus gauge metric */
30-
struct prometheus_metric *base;
31+
struct prometheus_metric base;
3132
/** Value of the Prometheus gauge metric */
3233
double value;
3334
};
3435

3536
/**
3637
* @brief Prometheus Gauge definition.
3738
*
38-
* This macro defines a Gauge metric.
39+
* This macro defines a Gauge metric. If you want to make the gauge static,
40+
* then add "static" keyword before the PROMETHEUS_GAUGE_DEFINE.
3941
*
40-
* @param _name The channel's name.
41-
* @param _detail The metric base.
42+
* @param _name The gauge metric name.
43+
* @param _desc Gauge description
44+
* @param _label Label for the metric. Additional labels can be added at runtime.
4245
*
4346
* Example usage:
4447
* @code{.c}
4548
*
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);
49+
* PROMETHEUS_GAUGE_DEFINE(http_request_gauge, "HTTP request gauge",
50+
* ({ .key = "http_request", .value = "request_count" }));
5751
*
5852
* @endcode
5953
*/
60-
#define PROMETHEUS_GAUGE_DEFINE(_name, _detail) \
61-
static STRUCT_SECTION_ITERABLE(prometheus_gauge, _name) = {.base = (void *)(_detail), \
62-
.value = 0}
54+
#define PROMETHEUS_GAUGE_DEFINE(_name, _desc, _label) \
55+
STRUCT_SECTION_ITERABLE(prometheus_gauge, _name) = { \
56+
.base.name = STRINGIFY(_name), \
57+
.base.type = PROMETHEUS_GAUGE, \
58+
.base.description = _desc, \
59+
.base.labels[0] = __DEBRACKET _label, \
60+
.base.num_labels = 1, \
61+
.value = 0.0, \
62+
}
6363

6464
/**
6565
* @brief Set the value of a Prometheus gauge metric

include/zephyr/net/prometheus/histogram.h

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
3+
* Copyright (c) 2024 Nordic Semiconductor
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
@@ -41,7 +42,7 @@ struct prometheus_histogram_bucket {
4142
*/
4243
struct prometheus_histogram {
4344
/** Base of the Prometheus histogram metric */
44-
struct prometheus_metric *base;
45+
struct prometheus_metric base;
4546
/** Array of buckets in the histogram */
4647
struct prometheus_histogram_bucket *buckets;
4748
/** Number of buckets in the histogram */
@@ -55,34 +56,33 @@ struct prometheus_histogram {
5556
/**
5657
* @brief Prometheus Histogram definition.
5758
*
58-
* This macro defines a Histogram metric.
59+
* This macro defines a Histogram metric. If you want to make the histogram static,
60+
* then add "static" keyword before the PROMETHEUS_HISTOGRAM_DEFINE.
5961
*
60-
* @param _name The channel's name.
61-
* @param _detail The metric base.
62+
* @param _name The histogram metric name.
63+
* @param _desc Histogram description
64+
* @param _label Label for the metric. Additional labels can be added at runtime.
6265
*
6366
* Example usage:
6467
* @code{.c}
6568
*
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);
69+
* PROMETHEUS_HISTOGRAM_DEFINE(http_request_histogram, "HTTP request histogram",
70+
* ({ .key = "request_latency", .value = "request_latency_seconds" }));
7771
*
7872
* @endcode
7973
*/
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}
74+
#define PROMETHEUS_HISTOGRAM_DEFINE(_name, _desc, _label) \
75+
STRUCT_SECTION_ITERABLE(prometheus_histogram, _name) = { \
76+
.base.name = STRINGIFY(_name), \
77+
.base.type = PROMETHEUS_HISTOGRAM, \
78+
.base.description = _desc, \
79+
.base.labels[0] = __DEBRACKET _label, \
80+
.base.num_labels = 1, \
81+
.buckets = NULL, \
82+
.num_buckets = 0, \
83+
.sum = 0.0, \
84+
.count = 0U, \
85+
}
8686

8787
/**
8888
* @brief Observe a value in a Prometheus histogram metric

include/zephyr/net/prometheus/metric.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
3+
* Copyright (c) 2024 Nordic Semiconductor
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/

include/zephyr/net/prometheus/summary.h

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
3+
* Copyright (c) 2024 Nordic Semiconductor
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
@@ -41,7 +42,7 @@ struct prometheus_summary_quantile {
4142
*/
4243
struct prometheus_summary {
4344
/** Base of the Prometheus summary metric */
44-
struct prometheus_metric *base;
45+
struct prometheus_metric base;
4546
/** Array of quantiles associated with the Prometheus summary metric */
4647
struct prometheus_summary_quantile *quantiles;
4748
/** Number of quantiles associated with the Prometheus summary metric */
@@ -55,35 +56,36 @@ struct prometheus_summary {
5556
/**
5657
* @brief Prometheus Summary definition.
5758
*
58-
* This macro defines a Summary metric.
59+
* This macro defines a Summary metric. If you want to make the summary static,
60+
* then add "static" keyword before the PROMETHEUS_SUMMARY_DEFINE.
61+
*
62+
* @param _name The summary metric name.
63+
* @param _desc Summary description
64+
* @param _label Label for the metric. Additional labels can be added at runtime.
5965
*
60-
* @param _name The channel's name.
61-
* @param _detail The metric base.
6266
*
6367
* Example usage:
6468
* @code{.c}
6569
*
66-
* struct prometheus_metric http_request_gauge = {
67-
* .type = PROMETHEUS_SUMMARY,
68-
* .name = "http_request_summary",
69-
* .description = "HTTP request summary",
70-
* .num_labels = 1,
71-
* .labels = {
72-
* { .key = "request_latency", .value = "request_latency_seconds",}
73-
* },
74-
* };
75-
*
76-
* PROMETHEUS_SUMMARY_DEFINE(test_summary, &test_summary_metric);
70+
* PROMETHEUS_SUMMARY_DEFINE(http_request_summary, "HTTP request summary",
71+
* ({ .key = "request_latency",
72+
* .value = "request_latency_seconds" }));
7773
*
7874
* @endcode
7975
*/
8076

81-
#define PROMETHEUS_SUMMARY_DEFINE(_name, _detail) \
82-
static STRUCT_SECTION_ITERABLE(prometheus_summary, _name) = {.base = (void *)(_detail), \
83-
.quantiles = NULL, \
84-
.num_quantiles = 0, \
85-
.sum = 0, \
86-
.count = 0}
77+
#define PROMETHEUS_SUMMARY_DEFINE(_name, _desc, _label) \
78+
STRUCT_SECTION_ITERABLE(prometheus_summary, _name) = { \
79+
.base.name = STRINGIFY(_name), \
80+
.base.type = PROMETHEUS_SUMMARY, \
81+
.base.description = _desc, \
82+
.base.labels[0] = __DEBRACKET _label, \
83+
.base.num_labels = 1, \
84+
.quantiles = NULL, \
85+
.num_quantiles = 0, \
86+
.sum = 0.0, \
87+
.count = 0U, \
88+
}
8789

8890
/**
8991
* @brief Observes a value in a Prometheus summary metric

samples/net/prometheus/src/main.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,8 @@ static void setup_tls(void)
145145
#endif /* defined(CONFIG_NET_SAMPLE_HTTPS_SERVICE) */
146146
}
147147

148-
struct prometheus_metric http_request_counter = {
149-
.type = PROMETHEUS_COUNTER,
150-
.name = "http_request_counter",
151-
.description = "HTTP request counter",
152-
.num_labels = 1,
153-
.labels = {{
154-
.key = "http_request",
155-
.value = "request_count",
156-
}},
157-
};
158-
159-
PROMETHEUS_COUNTER_DEFINE(test_counter, &http_request_counter);
148+
PROMETHEUS_COUNTER_DEFINE(http_request_counter, "HTTP request counter",
149+
({ .key = "http_request", .value = "request_count" }));
160150

161151
PROMETHEUS_COLLECTOR_DEFINE(test_collector);
162152

@@ -165,10 +155,10 @@ int main(void)
165155
/* Create a mock collector with different types of metrics */
166156
prom_context.collector = &test_collector;
167157

168-
prom_context.counter = &test_counter;
158+
prom_context.counter = &http_request_counter;
169159
prometheus_counter_inc(prom_context.counter);
170160

171-
prometheus_collector_register_metric(prom_context.collector, prom_context.counter->base);
161+
prometheus_collector_register_metric(prom_context.collector, &prom_context.counter->base);
172162

173163
setup_tls();
174164

0 commit comments

Comments
 (0)