Zephyr API Documentation 4.0.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
collector.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
3 * Copyright (c) 2024 Nordic Semiconductor
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef ZEPHYR_INCLUDE_PROMETHEUS_COLLECTOR_H_
9#define ZEPHYR_INCLUDE_PROMETHEUS_COLLECTOR_H_
10
23#include <zephyr/kernel.h>
26
27#include <stddef.h>
28
30
41typedef int (*prometheus_scrape_cb_t)(struct prometheus_collector *collector,
43 void *user_data);
44
64
76#define PROMETHEUS_COLLECTOR_DEFINE(_name, ...) \
77 STRUCT_SECTION_ITERABLE(prometheus_collector, _name) = { \
78 .name = STRINGIFY(_name), \
79 .metrics = SYS_SLIST_STATIC_INIT(&_name.metrics), \
80 .lock = Z_MUTEX_INITIALIZER(_name.lock), \
81 .user_cb = COND_CODE_0( \
82 NUM_VA_ARGS_LESS_1( \
83 LIST_DROP_EMPTY(__VA_ARGS__, _)), \
84 (NULL), \
85 (GET_ARG_N(1, __VA_ARGS__))), \
86 .user_data = COND_CODE_0( \
87 NUM_VA_ARGS_LESS_1(__VA_ARGS__), (NULL), \
88 (GET_ARG_N(1, \
89 GET_ARGS_LESS_N(1, __VA_ARGS__)))), \
90 }
91
105 struct prometheus_metric *metric);
106
117 const char *name);
118
121enum prometheus_walk_state {
122 PROMETHEUS_WALK_START,
123 PROMETHEUS_WALK_CONTINUE,
124 PROMETHEUS_WALK_STOP,
125};
126
127struct prometheus_collector_walk_context {
128 struct prometheus_collector *collector;
129 struct prometheus_metric *metric;
130 struct prometheus_metric *tmp;
131 enum prometheus_walk_state state;
132};
133
147int prometheus_collector_walk_metrics(struct prometheus_collector_walk_context *ctx,
148 uint8_t *buffer, size_t buffer_size);
149
158static inline int prometheus_collector_walk_init(struct prometheus_collector_walk_context *ctx,
160{
161 if (collector == NULL) {
162 return -EINVAL;
163 }
164
165 ctx->collector = collector;
166 ctx->state = PROMETHEUS_WALK_START;
167 ctx->metric = NULL;
168 ctx->tmp = NULL;
169
170 return 0;
171}
172
177#endif /* ZEPHYR_INCLUDE_PROMETHEUS_COLLECTOR_H_ */
int prometheus_collector_walk_metrics(struct prometheus_collector_walk_context *ctx, uint8_t *buffer, size_t buffer_size)
Walk through all metrics in a Prometheus collector and format them into a buffer.
static int prometheus_collector_walk_init(struct prometheus_collector_walk_context *ctx, struct prometheus_collector *collector)
Initialize the walker context to walk through all metrics.
Definition collector.h:158
int(* prometheus_scrape_cb_t)(struct prometheus_collector *collector, struct prometheus_metric *metric, void *user_data)
Callback used to scrape a collector for a specific metric.
Definition collector.h:41
const void * prometheus_collector_get_metric(struct prometheus_collector *collector, const char *name)
Get a metric from a Prometheus collector.
int prometheus_collector_register_metric(struct prometheus_collector *collector, struct prometheus_metric *metric)
Register a metric with a Prometheus collector.
struct _slist sys_slist_t
Single-linked list structure.
Definition slist.h:49
#define EINVAL
Invalid argument.
Definition errno.h:60
Public kernel APIs.
Prometheus metric interface.
state
Definition parser_state.h:29
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
Mutex Structure.
Definition kernel.h:3004
Prometheus collector definition.
Definition collector.h:50
const char * name
Name of the collector.
Definition collector.h:52
sys_slist_t metrics
Array of metrics associated with the collector.
Definition collector.h:54
struct k_mutex lock
Mutex to protect the metrics list manipulation.
Definition collector.h:56
void * user_data
User data.
Definition collector.h:62
prometheus_scrape_cb_t user_cb
User callback function.
Definition collector.h:60
Type used to represent a Prometheus metric base.
Definition metric.h:47
struct prometheus_collector * collector
Back pointer to the collector that this metric belongs to.
Definition metric.h:51
void * metric
Back pointer to the actual metric (counter, gauge, etc.).
Definition metric.h:56
void * user_data
User defined data.
Definition metric.h:68