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:
Jukka Rissanen 2024-11-20 15:05:50 +02:00 committed by Anas Nashif
commit d3efcbd316
15 changed files with 132 additions and 198 deletions

View file

@ -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");

View file

@ -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

View file

@ -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);

View file

@ -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

View file

@ -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

View file

@ -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