net: prometheus: counter: Add function to add a value

Add function to increase the counter value with arbitrary value.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
This commit is contained in:
Jukka Rissanen 2024-11-18 11:54:12 +02:00 committed by Anas Nashif
commit d94bcd2566
2 changed files with 17 additions and 6 deletions

View file

@ -62,13 +62,26 @@ struct prometheus_counter {
static STRUCT_SECTION_ITERABLE(prometheus_counter, _name) = {.base = (void *)(_detail), \
.value = 0}
/**
* @brief Increment the value of a Prometheus counter metric
* Increments the value of the specified counter metric by arbitrary amount.
* @param counter Pointer to the counter metric to increment.
* @param value Amount to increment the counter by.
* @return 0 on success, negative errno on error.
*/
int prometheus_counter_add(struct prometheus_counter *counter, uint64_t value);
/**
* @brief Increment the value of a Prometheus counter metric
* Increments the value of the specified counter metric by one.
* @param counter Pointer to the counter metric to increment.
* @return 0 on success, negative errno on error.
*/
int prometheus_counter_inc(struct prometheus_counter *counter);
static inline int prometheus_counter_inc(struct prometheus_counter *counter)
{
return prometheus_counter_add(counter, 1ULL);
}
/**
* @}

View file

@ -14,15 +14,13 @@
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(pm_counter, CONFIG_PROMETHEUS_LOG_LEVEL);
int prometheus_counter_inc(struct prometheus_counter *counter)
int prometheus_counter_add(struct prometheus_counter *counter, uint64_t value)
{
if (!counter) {
if (counter == NULL) {
return -EINVAL;
}
if (counter) {
counter->value++;
}
counter->value += value;
return 0;
}