From 63c7e7d67d11499fc43db4cff7601c36b3fc4178 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Sun, 13 Jul 2025 14:29:34 +0200 Subject: [PATCH] impstats: add support for Prometheus metrics output format This commit extends the impstats input module to support a new "prometheus" format for exporting statistics. The output is generated via statsobj.c, which already provides Prometheus- compatible formatting. To enable this, a new "format" config option value ("prometheus") was added and recognized by impstats.c. When selected, impstats invokes the appropriate statsobj function to generate output in Prometheus exposition format. The default behavior remains unchanged. Existing formats such as "json" and "legacy" are not modified. A new test has been added under tests/ to validate correct Prometheus output generation. The test verifies that the format header and at least one representative metric line conform to the Prometheus text format specification. This enhancement enables rsyslog to integrate more easily with Prometheus-based monitoring stacks and paves the way for native metrics scraping without external translation layers. --- plugins/impstats/impstats.c | 6 ++++-- tests/Makefile.am | 2 ++ tests/stats-prometheus.sh | 29 +++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100755 tests/stats-prometheus.sh diff --git a/plugins/impstats/impstats.c b/plugins/impstats/impstats.c index f74b8fb8f..4fe3882f3 100644 --- a/plugins/impstats/impstats.c +++ b/plugins/impstats/impstats.c @@ -1,7 +1,7 @@ /* impstats.c * A module to periodically output statistics gathered by rsyslog. * - * Copyright 2010-2018 Adiscon GmbH. + * Copyright 2010-2025 Adiscon GmbH. * * This file is part of rsyslog. * @@ -75,6 +75,7 @@ typedef struct configSettings_s { int iSeverity; int bJSON; int bCEE; + int bPrometheus; } configSettings_t; struct modConfData_s { @@ -189,6 +190,7 @@ initConfigSettings(void) cs.iSeverity = DEFAULT_SEVERITY; cs.bJSON = 0; cs.bCEE = 0; + cs.bPrometheus = 0; } @@ -476,7 +478,7 @@ finalize_it: */ typedef struct _instanceData { int dummy; -} instanceData; + } instanceData; BEGINdoHUP CODESTARTdoHUP DBGPRINTF("impstats: received HUP\n") diff --git a/tests/Makefile.am b/tests/Makefile.am index f096a4ed9..2efd39e06 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1231,6 +1231,7 @@ TESTS += \ no-dynstats-json.sh \ no-dynstats.sh \ stats-json.sh \ + stats-prometheus.sh \ dynstats-json.sh \ stats-cee.sh \ stats-json-es.sh \ @@ -3001,6 +3002,7 @@ EXTRA_DIST= \ perctile-simple-vg.sh \ stats-json.sh \ stats-json-vg.sh \ + stats-prometheus.sh \ stats-cee.sh \ stats-cee-vg.sh \ stats-json-es.sh \ diff --git a/tests/stats-prometheus.sh b/tests/stats-prometheus.sh new file mode 100755 index 000000000..24b847eb7 --- /dev/null +++ b/tests/stats-prometheus.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# added 2025-07-12 by Codex +# This file is part of the rsyslog project, released under ASL 2.0 +. ${srcdir:=.}/diag.sh init + +generate_conf +add_conf ' +ruleset(name="stats") { + action(type="omfile" file="'${RSYSLOG_DYNNAME}'.out.stats.log") +} + +module(load="../plugins/impstats/.libs/impstats" interval="1" severity="7" resetCounters="on" Ruleset="stats" bracketing="on" format="prometheus") + +if ($msg == "this condition will never match") then { + action(name="an_action_that_is_never_called" type="omfile" file=`echo $RSYSLOG_OUT_LOG`) +} +' + +startup +injectmsg_file $srcdir/testsuites/dynstats_input_1 +wait_queueempty +wait_for_stats_flush ${RSYSLOG_DYNNAME}.out.stats.log +echo doing shutdown +shutdown_when_empty +echo wait on shutdown +wait_shutdown +custom_content_check '# TYPE main Q_enqueued_total counter' "${RSYSLOG_DYNNAME}.out.stats.log" +custom_assert_content_missing '@cee' "${RSYSLOG_DYNNAME}.out.stats.log" +exit_test