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 <jukka.rissanen@nordicsemi.no>
This commit is contained in:
parent
5282ccaa7c
commit
d3efcbd316
15 changed files with 132 additions and 198 deletions
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
|
||||
* Copyright (c) 2024 Nordic Semiconductor
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
@ -47,7 +48,7 @@ struct prometheus_collector {
|
|||
* @param _name The collector's name.
|
||||
*/
|
||||
#define PROMETHEUS_COLLECTOR_DEFINE(_name) \
|
||||
static STRUCT_SECTION_ITERABLE(prometheus_collector, _name) = { \
|
||||
STRUCT_SECTION_ITERABLE(prometheus_collector, _name) = { \
|
||||
.name = STRINGIFY(_name), \
|
||||
.metrics = SYS_SLIST_STATIC_INIT(&_name.metrics), \
|
||||
.lock = Z_MUTEX_INITIALIZER(_name.lock), \
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
|
||||
* Copyright (c) 2024 Nordic Semiconductor
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
@ -29,7 +30,7 @@
|
|||
*/
|
||||
struct prometheus_counter {
|
||||
/** Base of the Prometheus counter metric */
|
||||
struct prometheus_metric *base;
|
||||
struct prometheus_metric base;
|
||||
/** Value of the Prometheus counter metric */
|
||||
uint64_t value;
|
||||
};
|
||||
|
@ -37,30 +38,29 @@ struct prometheus_counter {
|
|||
/**
|
||||
* @brief Prometheus Counter definition.
|
||||
*
|
||||
* This macro defines a Counter metric.
|
||||
* This macro defines a Counter metric. If you want to make the counter static,
|
||||
* then add "static" keyword before the PROMETHEUS_COUNTER_DEFINE.
|
||||
*
|
||||
* @param _name The channel's name.
|
||||
* @param _detail The metric base.
|
||||
* @param _name The counter metric name
|
||||
* @param _desc Counter description
|
||||
* @param _label Label for the metric. Additional labels can be added at runtime.
|
||||
*
|
||||
* Example usage:
|
||||
* @code{.c}
|
||||
*
|
||||
* struct prometheus_metric http_request_counter = {
|
||||
* .type = PROMETHEUS_COUNTER,
|
||||
* .name = "http_request_counter",
|
||||
* .description = "HTTP request counter",
|
||||
* .num_labels = 1,
|
||||
* .labels = {
|
||||
* { .key = "http_request", .value = "request_count",}
|
||||
* },
|
||||
*};
|
||||
*
|
||||
* PROMETHEUS_COUNTER_DEFINE(test_counter, &test_counter_metric);
|
||||
* PROMETHEUS_COUNTER_DEFINE(http_request_counter, "HTTP request counter",
|
||||
* ({ .key = "http_request", .value = "request_count" }));
|
||||
* @endcode
|
||||
*/
|
||||
#define PROMETHEUS_COUNTER_DEFINE(_name, _detail) \
|
||||
static STRUCT_SECTION_ITERABLE(prometheus_counter, _name) = {.base = (void *)(_detail), \
|
||||
.value = 0}
|
||||
#define PROMETHEUS_COUNTER_DEFINE(_name, _desc, _label) \
|
||||
STRUCT_SECTION_ITERABLE(prometheus_counter, _name) = { \
|
||||
.base.name = STRINGIFY(_name), \
|
||||
.base.type = PROMETHEUS_COUNTER, \
|
||||
.base.description = _desc, \
|
||||
.base.labels[0] = __DEBRACKET _label, \
|
||||
.base.num_labels = 1, \
|
||||
.value = 0ULL, \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Increment the value of a Prometheus counter metric
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
|
||||
* Copyright (c) 2024 Nordic Semiconductor
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
@ -27,7 +28,7 @@
|
|||
*/
|
||||
struct prometheus_gauge {
|
||||
/** Base of the Prometheus gauge metric */
|
||||
struct prometheus_metric *base;
|
||||
struct prometheus_metric base;
|
||||
/** Value of the Prometheus gauge metric */
|
||||
double value;
|
||||
};
|
||||
|
@ -35,31 +36,30 @@ struct prometheus_gauge {
|
|||
/**
|
||||
* @brief Prometheus Gauge definition.
|
||||
*
|
||||
* This macro defines a Gauge metric.
|
||||
* This macro defines a Gauge metric. If you want to make the gauge static,
|
||||
* then add "static" keyword before the PROMETHEUS_GAUGE_DEFINE.
|
||||
*
|
||||
* @param _name The channel's name.
|
||||
* @param _detail The metric base.
|
||||
* @param _name The gauge metric name.
|
||||
* @param _desc Gauge description
|
||||
* @param _label Label for the metric. Additional labels can be added at runtime.
|
||||
*
|
||||
* Example usage:
|
||||
* @code{.c}
|
||||
*
|
||||
* struct prometheus_metric http_request_gauge = {
|
||||
* .type = PROMETHEUS_GAUGE,
|
||||
* .name = "http_request_gauge",
|
||||
* .description = "HTTP request gauge",
|
||||
* .num_labels = 1,
|
||||
* .labels = {
|
||||
* { .key = "http_request", .value = "request_count",}
|
||||
* },
|
||||
* };
|
||||
*
|
||||
* PROMETHEUS_GAUGE_DEFINE(test_gauge, &test_gauge_metric);
|
||||
* PROMETHEUS_GAUGE_DEFINE(http_request_gauge, "HTTP request gauge",
|
||||
* ({ .key = "http_request", .value = "request_count" }));
|
||||
*
|
||||
* @endcode
|
||||
*/
|
||||
#define PROMETHEUS_GAUGE_DEFINE(_name, _detail) \
|
||||
static STRUCT_SECTION_ITERABLE(prometheus_gauge, _name) = {.base = (void *)(_detail), \
|
||||
.value = 0}
|
||||
#define PROMETHEUS_GAUGE_DEFINE(_name, _desc, _label) \
|
||||
STRUCT_SECTION_ITERABLE(prometheus_gauge, _name) = { \
|
||||
.base.name = STRINGIFY(_name), \
|
||||
.base.type = PROMETHEUS_GAUGE, \
|
||||
.base.description = _desc, \
|
||||
.base.labels[0] = __DEBRACKET _label, \
|
||||
.base.num_labels = 1, \
|
||||
.value = 0.0, \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the value of a Prometheus gauge metric
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
|
||||
* Copyright (c) 2024 Nordic Semiconductor
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
@ -41,7 +42,7 @@ struct prometheus_histogram_bucket {
|
|||
*/
|
||||
struct prometheus_histogram {
|
||||
/** Base of the Prometheus histogram metric */
|
||||
struct prometheus_metric *base;
|
||||
struct prometheus_metric base;
|
||||
/** Array of buckets in the histogram */
|
||||
struct prometheus_histogram_bucket *buckets;
|
||||
/** Number of buckets in the histogram */
|
||||
|
@ -55,34 +56,33 @@ struct prometheus_histogram {
|
|||
/**
|
||||
* @brief Prometheus Histogram definition.
|
||||
*
|
||||
* This macro defines a Histogram metric.
|
||||
* This macro defines a Histogram metric. If you want to make the histogram static,
|
||||
* then add "static" keyword before the PROMETHEUS_HISTOGRAM_DEFINE.
|
||||
*
|
||||
* @param _name The channel's name.
|
||||
* @param _detail The metric base.
|
||||
* @param _name The histogram metric name.
|
||||
* @param _desc Histogram description
|
||||
* @param _label Label for the metric. Additional labels can be added at runtime.
|
||||
*
|
||||
* Example usage:
|
||||
* @code{.c}
|
||||
*
|
||||
* struct prometheus_metric http_request_gauge = {
|
||||
* .type = PROMETHEUS_HISTOGRAM,
|
||||
* .name = "http_request_histograms",
|
||||
* .description = "HTTP request histogram",
|
||||
* .num_labels = 1,
|
||||
* .labels = {
|
||||
* { .key = "request_latency", .value = "request_latency_seconds",}
|
||||
* },
|
||||
* };
|
||||
*
|
||||
* PROMETHEUS_HISTOGRAM_DEFINE(test_histogram, &test_histogram_metric);
|
||||
* PROMETHEUS_HISTOGRAM_DEFINE(http_request_histogram, "HTTP request histogram",
|
||||
* ({ .key = "request_latency", .value = "request_latency_seconds" }));
|
||||
*
|
||||
* @endcode
|
||||
*/
|
||||
#define PROMETHEUS_HISTOGRAM_DEFINE(_name, _detail) \
|
||||
static STRUCT_SECTION_ITERABLE(prometheus_histogram, _name) = {.base = (void *)(_detail), \
|
||||
.buckets = NULL, \
|
||||
.num_buckets = 0, \
|
||||
.sum = 0, \
|
||||
.count = 0}
|
||||
#define PROMETHEUS_HISTOGRAM_DEFINE(_name, _desc, _label) \
|
||||
STRUCT_SECTION_ITERABLE(prometheus_histogram, _name) = { \
|
||||
.base.name = STRINGIFY(_name), \
|
||||
.base.type = PROMETHEUS_HISTOGRAM, \
|
||||
.base.description = _desc, \
|
||||
.base.labels[0] = __DEBRACKET _label, \
|
||||
.base.num_labels = 1, \
|
||||
.buckets = NULL, \
|
||||
.num_buckets = 0, \
|
||||
.sum = 0.0, \
|
||||
.count = 0U, \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Observe a value in a Prometheus histogram metric
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
|
||||
* Copyright (c) 2024 Nordic Semiconductor
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
|
||||
* Copyright (c) 2024 Nordic Semiconductor
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
@ -41,7 +42,7 @@ struct prometheus_summary_quantile {
|
|||
*/
|
||||
struct prometheus_summary {
|
||||
/** Base of the Prometheus summary metric */
|
||||
struct prometheus_metric *base;
|
||||
struct prometheus_metric base;
|
||||
/** Array of quantiles associated with the Prometheus summary metric */
|
||||
struct prometheus_summary_quantile *quantiles;
|
||||
/** Number of quantiles associated with the Prometheus summary metric */
|
||||
|
@ -55,35 +56,36 @@ struct prometheus_summary {
|
|||
/**
|
||||
* @brief Prometheus Summary definition.
|
||||
*
|
||||
* This macro defines a Summary metric.
|
||||
* This macro defines a Summary metric. If you want to make the summary static,
|
||||
* then add "static" keyword before the PROMETHEUS_SUMMARY_DEFINE.
|
||||
*
|
||||
* @param _name The summary metric name.
|
||||
* @param _desc Summary description
|
||||
* @param _label Label for the metric. Additional labels can be added at runtime.
|
||||
*
|
||||
* @param _name The channel's name.
|
||||
* @param _detail The metric base.
|
||||
*
|
||||
* Example usage:
|
||||
* @code{.c}
|
||||
*
|
||||
* struct prometheus_metric http_request_gauge = {
|
||||
* .type = PROMETHEUS_SUMMARY,
|
||||
* .name = "http_request_summary",
|
||||
* .description = "HTTP request summary",
|
||||
* .num_labels = 1,
|
||||
* .labels = {
|
||||
* { .key = "request_latency", .value = "request_latency_seconds",}
|
||||
* },
|
||||
* };
|
||||
*
|
||||
* PROMETHEUS_SUMMARY_DEFINE(test_summary, &test_summary_metric);
|
||||
* PROMETHEUS_SUMMARY_DEFINE(http_request_summary, "HTTP request summary",
|
||||
* ({ .key = "request_latency",
|
||||
* .value = "request_latency_seconds" }));
|
||||
*
|
||||
* @endcode
|
||||
*/
|
||||
|
||||
#define PROMETHEUS_SUMMARY_DEFINE(_name, _detail) \
|
||||
static STRUCT_SECTION_ITERABLE(prometheus_summary, _name) = {.base = (void *)(_detail), \
|
||||
.quantiles = NULL, \
|
||||
.num_quantiles = 0, \
|
||||
.sum = 0, \
|
||||
.count = 0}
|
||||
#define PROMETHEUS_SUMMARY_DEFINE(_name, _desc, _label) \
|
||||
STRUCT_SECTION_ITERABLE(prometheus_summary, _name) = { \
|
||||
.base.name = STRINGIFY(_name), \
|
||||
.base.type = PROMETHEUS_SUMMARY, \
|
||||
.base.description = _desc, \
|
||||
.base.labels[0] = __DEBRACKET _label, \
|
||||
.base.num_labels = 1, \
|
||||
.quantiles = NULL, \
|
||||
.num_quantiles = 0, \
|
||||
.sum = 0.0, \
|
||||
.count = 0U, \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Observes a value in a Prometheus summary metric
|
||||
|
|
|
@ -145,18 +145,8 @@ static void setup_tls(void)
|
|||
#endif /* defined(CONFIG_NET_SAMPLE_HTTPS_SERVICE) */
|
||||
}
|
||||
|
||||
struct prometheus_metric http_request_counter = {
|
||||
.type = PROMETHEUS_COUNTER,
|
||||
.name = "http_request_counter",
|
||||
.description = "HTTP request counter",
|
||||
.num_labels = 1,
|
||||
.labels = {{
|
||||
.key = "http_request",
|
||||
.value = "request_count",
|
||||
}},
|
||||
};
|
||||
|
||||
PROMETHEUS_COUNTER_DEFINE(test_counter, &http_request_counter);
|
||||
PROMETHEUS_COUNTER_DEFINE(http_request_counter, "HTTP request counter",
|
||||
({ .key = "http_request", .value = "request_count" }));
|
||||
|
||||
PROMETHEUS_COLLECTOR_DEFINE(test_collector);
|
||||
|
||||
|
@ -165,10 +155,10 @@ int main(void)
|
|||
/* Create a mock collector with different types of metrics */
|
||||
prom_context.collector = &test_collector;
|
||||
|
||||
prom_context.counter = &test_counter;
|
||||
prom_context.counter = &http_request_counter;
|
||||
prometheus_counter_inc(prom_context.counter);
|
||||
|
||||
prometheus_collector_register_metric(prom_context.collector, prom_context.counter->base);
|
||||
prometheus_collector_register_metric(prom_context.collector, &prom_context.counter->base);
|
||||
|
||||
setup_tls();
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
|
||||
* Copyright (c) 2024 Nordic Semiconductor
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
@ -46,10 +47,10 @@ int prometheus_collector_register_metric(struct prometheus_collector *collector,
|
|||
const struct prometheus_counter *prometheus_get_counter_metric(const char *name)
|
||||
{
|
||||
STRUCT_SECTION_FOREACH(prometheus_counter, entry) {
|
||||
LOG_DBG("entry->name: %s", entry->base->name);
|
||||
LOG_DBG("entry->name: %s", entry->base.name);
|
||||
|
||||
if (strncmp(entry->base->name, name, sizeof(entry->base->name)) == 0) {
|
||||
LOG_DBG("Counter found %s=%s", entry->base->name, name);
|
||||
if (strncmp(entry->base.name, name, strlen(entry->base.name)) == 0) {
|
||||
LOG_DBG("Counter found %s", entry->base.name);
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
|
@ -62,10 +63,10 @@ const struct prometheus_counter *prometheus_get_counter_metric(const char *name)
|
|||
const struct prometheus_gauge *prometheus_get_gauge_metric(const char *name)
|
||||
{
|
||||
STRUCT_SECTION_FOREACH(prometheus_gauge, entry) {
|
||||
LOG_DBG("entry->name: %s", entry->base->name);
|
||||
LOG_DBG("entry->name: %s", entry->base.name);
|
||||
|
||||
if (strncmp(entry->base->name, name, sizeof(entry->base->name)) == 0) {
|
||||
LOG_DBG("Counter found %s=%s", entry->base->name, name);
|
||||
if (strncmp(entry->base.name, name, strlen(entry->base.name)) == 0) {
|
||||
LOG_DBG("Counter found %s", entry->base.name);
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
|
@ -78,10 +79,10 @@ const struct prometheus_gauge *prometheus_get_gauge_metric(const char *name)
|
|||
const struct prometheus_histogram *prometheus_get_histogram_metric(const char *name)
|
||||
{
|
||||
STRUCT_SECTION_FOREACH(prometheus_histogram, entry) {
|
||||
LOG_DBG("entry->name: %s", entry->base->name);
|
||||
LOG_DBG("entry->name: %s", entry->base.name);
|
||||
|
||||
if (strncmp(entry->base->name, name, sizeof(entry->base->name)) == 0) {
|
||||
LOG_DBG("Counter found %s=%s", entry->base->name, name);
|
||||
if (strncmp(entry->base.name, name, strlen(entry->base.name)) == 0) {
|
||||
LOG_DBG("Counter found %s", entry->base.name);
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
|
@ -94,10 +95,10 @@ const struct prometheus_histogram *prometheus_get_histogram_metric(const char *n
|
|||
const struct prometheus_summary *prometheus_get_summary_metric(const char *name)
|
||||
{
|
||||
STRUCT_SECTION_FOREACH(prometheus_summary, entry) {
|
||||
LOG_DBG("entry->name: %s", entry->base->name);
|
||||
LOG_DBG("entry->name: %s", entry->base.name);
|
||||
|
||||
if (strncmp(entry->base->name, name, sizeof(entry->base->name)) == 0) {
|
||||
LOG_DBG("Counter found %s=%s", entry->base->name, name);
|
||||
if (strncmp(entry->base.name, name, strlen(entry->base.name)) == 0) {
|
||||
LOG_DBG("Counter found %s", entry->base.name);
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
|
@ -123,7 +124,7 @@ const void *prometheus_collector_get_metric(struct prometheus_collector *collect
|
|||
|
||||
SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&collector->metrics, metric, tmp, node) {
|
||||
|
||||
if (strncmp(metric->name, name, sizeof(metric->name)) == 0) {
|
||||
if (strncmp(metric->name, name, strlen(metric->name)) == 0) {
|
||||
type = metric->type;
|
||||
is_found = true;
|
||||
|
||||
|
@ -136,22 +137,23 @@ const void *prometheus_collector_get_metric(struct prometheus_collector *collect
|
|||
|
||||
if (!is_found) {
|
||||
LOG_ERR("Metric %s not found", name);
|
||||
return NULL;
|
||||
goto out;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case PROMETHEUS_COUNTER:
|
||||
return prometheus_get_counter_metric(name);
|
||||
return CONTAINER_OF(metric, struct prometheus_counter, base);
|
||||
case PROMETHEUS_GAUGE:
|
||||
return prometheus_get_gauge_metric(name);
|
||||
return CONTAINER_OF(metric, struct prometheus_gauge, base);
|
||||
case PROMETHEUS_HISTOGRAM:
|
||||
return prometheus_get_histogram_metric(name);
|
||||
return CONTAINER_OF(metric, struct prometheus_histogram, base);
|
||||
case PROMETHEUS_SUMMARY:
|
||||
return prometheus_get_summary_metric(name);
|
||||
return CONTAINER_OF(metric, struct prometheus_summary, base);
|
||||
default:
|
||||
LOG_ERR("Invalid metric type");
|
||||
return NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
out:
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
@ -131,8 +131,7 @@ int prometheus_format_exposition(struct prometheus_collector *collector, char *b
|
|||
switch (metric->type) {
|
||||
case PROMETHEUS_COUNTER: {
|
||||
const struct prometheus_counter *counter =
|
||||
(const struct prometheus_counter *)prometheus_collector_get_metric(
|
||||
collector, metric->name);
|
||||
CONTAINER_OF(metric, struct prometheus_counter, base);
|
||||
|
||||
LOG_DBG("counter->value: %llu", counter->value);
|
||||
|
||||
|
@ -152,8 +151,7 @@ int prometheus_format_exposition(struct prometheus_collector *collector, char *b
|
|||
|
||||
case PROMETHEUS_GAUGE: {
|
||||
const struct prometheus_gauge *gauge =
|
||||
(const struct prometheus_gauge *)prometheus_collector_get_metric(
|
||||
collector, metric->name);
|
||||
CONTAINER_OF(metric, struct prometheus_gauge, base);
|
||||
|
||||
LOG_DBG("gauge->value: %f", gauge->value);
|
||||
|
||||
|
@ -173,8 +171,7 @@ int prometheus_format_exposition(struct prometheus_collector *collector, char *b
|
|||
|
||||
case PROMETHEUS_HISTOGRAM: {
|
||||
const struct prometheus_histogram *histogram =
|
||||
(const struct prometheus_histogram *)
|
||||
prometheus_collector_get_metric(collector, metric->name);
|
||||
CONTAINER_OF(metric, struct prometheus_histogram, base);
|
||||
|
||||
LOG_DBG("histogram->count: %lu", histogram->count);
|
||||
|
||||
|
@ -210,8 +207,7 @@ int prometheus_format_exposition(struct prometheus_collector *collector, char *b
|
|||
|
||||
case PROMETHEUS_SUMMARY: {
|
||||
const struct prometheus_summary *summary =
|
||||
(const struct prometheus_summary *)prometheus_collector_get_metric(
|
||||
collector, metric->name);
|
||||
CONTAINER_OF(metric, struct prometheus_summary, base);
|
||||
|
||||
LOG_DBG("summary->count: %lu", summary->count);
|
||||
|
||||
|
|
|
@ -9,18 +9,8 @@
|
|||
#include <zephyr/net/prometheus/counter.h>
|
||||
#include <zephyr/net/prometheus/collector.h>
|
||||
|
||||
struct prometheus_metric test_counter_metric = {
|
||||
.type = PROMETHEUS_COUNTER,
|
||||
.name = "test_counter",
|
||||
.description = "Test counter",
|
||||
.num_labels = 1,
|
||||
.labels = {{
|
||||
.key = "test",
|
||||
.value = "counter",
|
||||
}},
|
||||
};
|
||||
|
||||
PROMETHEUS_COUNTER_DEFINE(test_counter_m, &test_counter_metric);
|
||||
PROMETHEUS_COUNTER_DEFINE(test_counter_m, "Test counter",
|
||||
({ .key = "test_counter", .value = "test" }));
|
||||
|
||||
PROMETHEUS_COLLECTOR_DEFINE(test_custom_collector);
|
||||
|
||||
|
@ -38,7 +28,7 @@ ZTEST(test_collector, test_prometheus_collector_register)
|
|||
int ret;
|
||||
struct prometheus_counter *counter;
|
||||
|
||||
prometheus_collector_register_metric(&test_custom_collector, test_counter_m.base);
|
||||
prometheus_collector_register_metric(&test_custom_collector, &test_counter_m.base);
|
||||
|
||||
counter = (struct prometheus_counter *)prometheus_collector_get_metric(
|
||||
&test_custom_collector, "test_counter");
|
||||
|
|
|
@ -8,18 +8,8 @@
|
|||
|
||||
#include <zephyr/net/prometheus/counter.h>
|
||||
|
||||
struct prometheus_metric test_counter_metric = {
|
||||
.type = PROMETHEUS_COUNTER,
|
||||
.name = "test_counter",
|
||||
.description = "Test counter",
|
||||
.num_labels = 1,
|
||||
.labels = {{
|
||||
.key = "test",
|
||||
.value = "counter",
|
||||
}},
|
||||
};
|
||||
|
||||
PROMETHEUS_COUNTER_DEFINE(test_counter_m, &test_counter_metric);
|
||||
PROMETHEUS_COUNTER_DEFINE(test_counter_m, "Test counter",
|
||||
({ .key = "test_counter", .value = "test" }));
|
||||
|
||||
/**
|
||||
* @brief Test prometheus_counter_inc
|
||||
|
|
|
@ -12,18 +12,8 @@
|
|||
|
||||
#define MAX_BUFFER_SIZE 256
|
||||
|
||||
struct prometheus_metric test_counter_metric = {
|
||||
.type = PROMETHEUS_COUNTER,
|
||||
.name = "test_counter",
|
||||
.description = "Test counter",
|
||||
.num_labels = 1,
|
||||
.labels = {{
|
||||
.key = "test",
|
||||
.value = "counter",
|
||||
}},
|
||||
};
|
||||
|
||||
PROMETHEUS_COUNTER_DEFINE(test_counter_m, &test_counter_metric);
|
||||
PROMETHEUS_COUNTER_DEFINE(test_counter, "Test counter",
|
||||
({ .key = "test", .value = "counter" }));
|
||||
|
||||
PROMETHEUS_COLLECTOR_DEFINE(test_custom_collector);
|
||||
|
||||
|
@ -42,16 +32,16 @@ ZTEST(test_formatter, test_prometheus_formatter_simple)
|
|||
"# TYPE test_counter counter\n"
|
||||
"test_counter{test=\"counter\"} 1\n";
|
||||
|
||||
prometheus_collector_register_metric(&test_custom_collector, test_counter_m.base);
|
||||
prometheus_collector_register_metric(&test_custom_collector, &test_counter.base);
|
||||
|
||||
counter = (struct prometheus_counter *)prometheus_collector_get_metric(
|
||||
&test_custom_collector, "test_counter");
|
||||
|
||||
zassert_equal(counter, &test_counter_m, "Counter not found in collector");
|
||||
zassert_equal(counter, &test_counter, "Counter not found in collector");
|
||||
|
||||
zassert_equal(test_counter_m.value, 0, "Counter value is not 0");
|
||||
zassert_equal(test_counter.value, 0, "Counter value is not 0");
|
||||
|
||||
ret = prometheus_counter_inc(&test_counter_m);
|
||||
ret = prometheus_counter_inc(&test_counter);
|
||||
zassert_ok(ret, "Error incrementing counter");
|
||||
|
||||
zassert_equal(counter->value, 1, "Counter value is not 1");
|
||||
|
@ -59,7 +49,9 @@ ZTEST(test_formatter, test_prometheus_formatter_simple)
|
|||
ret = prometheus_format_exposition(&test_custom_collector, formatted, sizeof(formatted));
|
||||
zassert_ok(ret, "Error formatting exposition data");
|
||||
|
||||
zassert_equal(strcmp(formatted, exposed), 0, "Exposition format is not as expected");
|
||||
zassert_equal(strcmp(formatted, exposed), 0,
|
||||
"Exposition format is not as expected (expected \"%s\", got \"%s\")",
|
||||
exposed, formatted);
|
||||
}
|
||||
|
||||
ZTEST_SUITE(test_formatter, NULL, NULL, NULL, NULL, NULL);
|
||||
|
|
|
@ -8,18 +8,8 @@
|
|||
|
||||
#include <zephyr/net/prometheus/gauge.h>
|
||||
|
||||
struct prometheus_metric test_gauge_metric = {
|
||||
.type = PROMETHEUS_GAUGE,
|
||||
.name = "test_gauge",
|
||||
.description = "Test gauge",
|
||||
.num_labels = 1,
|
||||
.labels = {{
|
||||
.key = "test",
|
||||
.value = "gauge",
|
||||
}},
|
||||
};
|
||||
|
||||
PROMETHEUS_GAUGE_DEFINE(test_gauge_m, &test_gauge_metric);
|
||||
PROMETHEUS_GAUGE_DEFINE(test_gauge_m, "Test gauge",
|
||||
({ .key = "test", .value = "gauge" }));
|
||||
|
||||
/**
|
||||
* @brief Test prometheus_gauge_set
|
||||
|
|
|
@ -8,18 +8,8 @@
|
|||
|
||||
#include <zephyr/net/prometheus/histogram.h>
|
||||
|
||||
struct prometheus_metric test_histogram_metric = {
|
||||
.type = PROMETHEUS_HISTOGRAM,
|
||||
.name = "test_histogram",
|
||||
.description = "Test histogram",
|
||||
.num_labels = 1,
|
||||
.labels = {{
|
||||
.key = "test",
|
||||
.value = "histogram",
|
||||
}},
|
||||
};
|
||||
|
||||
PROMETHEUS_HISTOGRAM_DEFINE(test_histogram_m, &test_histogram_metric);
|
||||
PROMETHEUS_HISTOGRAM_DEFINE(test_histogram_m, "Test histogram",
|
||||
({ .key = "test", .value = "histogram" }));
|
||||
|
||||
/**
|
||||
* @brief Test prometheus_histogram_observe
|
||||
|
|
|
@ -8,18 +8,8 @@
|
|||
|
||||
#include <zephyr/net/prometheus/summary.h>
|
||||
|
||||
struct prometheus_metric test_summary_metric = {
|
||||
.type = PROMETHEUS_SUMMARY,
|
||||
.name = "test_summary",
|
||||
.description = "Test summary",
|
||||
.num_labels = 1,
|
||||
.labels = {{
|
||||
.key = "test",
|
||||
.value = "summary",
|
||||
}},
|
||||
};
|
||||
|
||||
PROMETHEUS_SUMMARY_DEFINE(test_summary_m, &test_summary_metric);
|
||||
PROMETHEUS_SUMMARY_DEFINE(test_summary_m, "Test summary",
|
||||
({ .key = "test", .value = "summary" }));
|
||||
|
||||
/**
|
||||
* @brief Test prometheus_summary_observe
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue