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.
This commit is contained in:
Rainer Gerhards 2025-07-13 14:29:34 +02:00
parent f62dfb6d2c
commit 63c7e7d67d
3 changed files with 35 additions and 2 deletions

View File

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

View File

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

29
tests/stats-prometheus.sh Executable file
View File

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