Skip to content

Commit 7e881d2

Browse files
jukkarnashif
authored andcommitted
net: stats: Add prometheus support
Allow user to update prometheus metrics from network statistics. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent f0736e0 commit 7e881d2

File tree

6 files changed

+785
-2
lines changed

6 files changed

+785
-2
lines changed

include/zephyr/net/net_if.h

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
#include <zephyr/net/ipv4_autoconf.h>
4343
#endif
4444

45+
#include <zephyr/net/prometheus/collector.h>
46+
4547
#ifdef __cplusplus
4648
extern "C" {
4749
#endif
@@ -694,6 +696,10 @@ struct net_if {
694696
#if defined(CONFIG_NET_STATISTICS_PER_INTERFACE)
695697
/** Network statistics related to this network interface */
696698
struct net_stats stats;
699+
700+
/** Promethus collector for this network interface */
701+
IF_ENABLED(CONFIG_NET_STATISTICS_VIA_PROMETHEUS,
702+
(struct prometheus_collector *collector);)
697703
#endif /* CONFIG_NET_STATISTICS_PER_INTERFACE */
698704

699705
/** Network interface instance configuration */
@@ -3198,12 +3204,24 @@ struct net_if_api {
31983204
NET_IF_DHCPV6_INIT \
31993205
}
32003206

3207+
#define NET_PROMETHEUS_GET_COLLECTOR_NAME(dev_id, sfx) \
3208+
net_stats_##dev_id##_##sfx##_collector
3209+
#define NET_PROMETHEUS_INIT(dev_id, sfx) \
3210+
IF_ENABLED(CONFIG_NET_STATISTICS_VIA_PROMETHEUS, \
3211+
(.collector = &NET_PROMETHEUS_GET_COLLECTOR_NAME(dev_id, sfx),))
3212+
32013213
#define NET_IF_GET_NAME(dev_id, sfx) __net_if_##dev_id##_##sfx
32023214
#define NET_IF_DEV_GET_NAME(dev_id, sfx) __net_if_dev_##dev_id##_##sfx
32033215

32043216
#define NET_IF_GET(dev_id, sfx) \
32053217
((struct net_if *)&NET_IF_GET_NAME(dev_id, sfx))
32063218

3219+
#if defined(CONFIG_NET_STATISTICS_VIA_PROMETHEUS)
3220+
extern int net_stats_prometheus_scrape(struct prometheus_collector *collector,
3221+
struct prometheus_metric *metric,
3222+
void *user_data);
3223+
#endif /* CONFIG_NET_STATISTICS_VIA_PROMETHEUS */
3224+
32073225
#define NET_IF_INIT(dev_id, sfx, _l2, _mtu, _num_configs) \
32083226
static STRUCT_SECTION_ITERABLE(net_if_dev, \
32093227
NET_IF_DEV_GET_NAME(dev_id, sfx)) = { \
@@ -3221,7 +3239,15 @@ struct net_if_api {
32213239
.if_dev = &(NET_IF_DEV_GET_NAME(dev_id, sfx)), \
32223240
NET_IF_CONFIG_INIT \
32233241
} \
3224-
}
3242+
}; \
3243+
IF_ENABLED(CONFIG_NET_STATISTICS_VIA_PROMETHEUS, \
3244+
(static PROMETHEUS_COLLECTOR_DEFINE( \
3245+
NET_PROMETHEUS_GET_COLLECTOR_NAME(dev_id, \
3246+
sfx), \
3247+
net_stats_prometheus_scrape, \
3248+
NET_IF_GET(dev_id, sfx)); \
3249+
NET_STATS_PROMETHEUS(NET_IF_GET(dev_id, sfx), \
3250+
dev_id, sfx);))
32253251

32263252
#define NET_IF_OFFLOAD_INIT(dev_id, sfx, _mtu) \
32273253
static STRUCT_SECTION_ITERABLE(net_if_dev, \
@@ -3239,7 +3265,16 @@ struct net_if_api {
32393265
.if_dev = &(NET_IF_DEV_GET_NAME(dev_id, sfx)), \
32403266
NET_IF_CONFIG_INIT \
32413267
} \
3242-
}
3268+
}; \
3269+
IF_ENABLED(CONFIG_NET_STATISTICS_VIA_PROMETHEUS, \
3270+
(static PROMETHEUS_COLLECTOR_DEFINE( \
3271+
NET_PROMETHEUS_GET_COLLECTOR_NAME(dev_id, \
3272+
sfx), \
3273+
net_stats_prometheus_scrape, \
3274+
NET_IF_GET(dev_id, sfx)); \
3275+
NET_STATS_PROMETHEUS(NET_IF_GET(dev_id, sfx), \
3276+
dev_id, sfx);))
3277+
32433278

32443279
/** @endcond */
32453280

0 commit comments

Comments
 (0)