From 90c2ca4c55453ef6abbdc1aecf6a91a27409f21c Mon Sep 17 00:00:00 2001 From: "W.C.A. Wijngaards" Date: Fri, 30 Jan 2026 09:38:00 +0100 Subject: [PATCH] - add-prometheus-metrics, adjust configure.ac, Makefile.in and add daemon/metrics.c and daemon/metrics.h for statistics in prometheus metrics. --- Makefile.in | 9 +++++---- config.h.in | 10 ++++++++++ configure | 27 +++++++++++++++++++++++++++ configure.ac | 9 +++++++++ daemon/metrics.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ daemon/metrics.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 daemon/metrics.c create mode 100644 daemon/metrics.h diff --git a/Makefile.in b/Makefile.in index b28ed34ae..4d8f98845 100644 --- a/Makefile.in +++ b/Makefile.in @@ -187,9 +187,9 @@ unittcpreuse.lo unitdoq.lo unitinfra.lo UNITTEST_OBJ_LINK=$(UNITTEST_OBJ) worker_cb.lo $(COMMON_OBJ) $(SLDNS_OBJ) \ $(COMPAT_OBJ) DAEMON_SRC=daemon/acl_list.c daemon/cachedump.c daemon/daemon.c \ -daemon/remote.c daemon/stats.c daemon/unbound.c daemon/worker.c @WIN_DAEMON_SRC@ +daemon/remote.c daemon/stats.c daemon/metrics.c daemon/unbound.c daemon/worker.c @WIN_DAEMON_SRC@ DAEMON_OBJ=acl_list.lo cachedump.lo daemon.lo \ -shm_main.lo remote.lo stats.lo unbound.lo \ +shm_main.lo remote.lo stats.lo metrics.lo unbound.lo \ worker.lo @WIN_DAEMON_OBJ@ DAEMON_OBJ_LINK=$(DAEMON_OBJ) $(COMMON_OBJ_ALL_SYMBOLS) $(SLDNS_OBJ) \ $(COMPAT_OBJ) @WIN_DAEMON_OBJ_LINK@ @@ -210,11 +210,11 @@ UBANCHOR_OBJ_LINK=$(UBANCHOR_OBJ) parseutil.lo \ $(COMPAT_OBJ_WITHOUT_CTIME) $(SLDNS_ALLOCCHECK_EXTRA_OBJ) @WIN_UBANCHOR_OBJ_LINK@ TESTBOUND_SRC=testcode/testbound.c testcode/testpkts.c \ daemon/worker.c daemon/acl_list.c \ -daemon/daemon.c daemon/stats.c \ +daemon/daemon.c daemon/stats.c daemon/metrics.c \ testcode/replay.c testcode/fake_event.c TESTBOUND_OBJ=testbound.lo replay.lo fake_event.lo TESTBOUND_OBJ_LINK=$(TESTBOUND_OBJ) testpkts.lo worker.lo acl_list.lo \ -daemon.lo stats.lo shm_main.lo $(COMMON_OBJ_WITHOUT_NETCALL) ub_event.lo $(SLDNS_OBJ) \ +daemon.lo stats.lo metrics.lo shm_main.lo $(COMMON_OBJ_WITHOUT_NETCALL) ub_event.lo $(SLDNS_OBJ) \ $(COMPAT_OBJ) LOCKVERIFY_SRC=testcode/lock_verify.c LOCKVERIFY_OBJ=lock_verify.lo @@ -721,6 +721,7 @@ depend: ipset.lo ipset.o: $(srcdir)/ipset/ipset.c doqclient.lo doqclient.o: $(srcdir)/testcode/doqclient.c unitdoq.lo unitdoq.o: $(srcdir)/testcode/unitdoq.c +metrics.lo metrics.o: $(srcdir)/daemon/metrics.c # Dependencies dns.lo dns.o: $(srcdir)/services/cache/dns.c config.h $(srcdir)/iterator/iter_delegpt.h $(srcdir)/util/log.h \ diff --git a/config.h.in b/config.h.in index 59fa60745..dd5cb2dd0 100644 --- a/config.h.in +++ b/config.h.in @@ -237,6 +237,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_EVENT_H +/* Define to 1 if you have the `evhttp_free' function. */ +#undef HAVE_EVHTTP_FREE + /* Define to 1 if you have the `EVP_aes_256_cbc' function. */ #undef HAVE_EVP_AES_256_CBC @@ -980,6 +983,9 @@ /* define this to enable debug checks. */ #undef UNBOUND_DEBUG +/* Define the default metrics HTTP endpoint port. */ +#undef UNBOUND_METRICS_PORT + /* Define to 1 to use cachedb support */ #undef USE_CACHEDB @@ -1027,6 +1033,10 @@ distributions) the use of non-ephemeral ports. */ #undef USE_LINUX_IP_LOCAL_PORT_RANGE +/* Define this to expose Unbound statistics via a prometheus metrics HTTP + endpoint. */ +#undef USE_METRICS + /* Define if you want to use internal select based events */ #undef USE_MINI_EVENT diff --git a/configure b/configure index b5ca5e4a7..600e4bffc 100755 --- a/configure +++ b/configure @@ -22165,6 +22165,31 @@ else $as_nop fi printf "%s\n" "#define HAVE_DECL_EVSIGNAL_ASSIGN $ac_have_decl" >>confdefs.h + # prometheus metrics depend on libevent 2.0 and later, and is therefore + # only enabled when the required version is found and used + + for ac_func in evhttp_free +do : + ac_fn_c_check_func "$LINENO" "evhttp_free" "ac_cv_func_evhttp_free" +if test "x$ac_cv_func_evhttp_free" = xyes +then : + printf "%s\n" "#define HAVE_EVHTTP_FREE 1" >>confdefs.h + + +printf "%s\n" "#define USE_METRICS /**/" >>confdefs.h + + +printf "%s\n" "#define UNBOUND_METRICS_PORT 9100" >>confdefs.h + + +else $as_nop + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: disabling prometheus metrics" >&5 +printf "%s\n" "$as_me: disabling prometheus metrics" >&6;} + +fi + +done PC_LIBEVENT_DEPENDENCY="libevent" if test -n "$BAK_LDFLAGS_SET"; then @@ -22174,6 +22199,8 @@ else printf "%s\n" "#define USE_MINI_EVENT 1" >>confdefs.h + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: Prometheus metrics are disabled with the builtin libevent alternative" >&5 +printf "%s\n" "$as_me: Prometheus metrics are disabled with the builtin libevent alternative" >&6;} fi # check for libexpat diff --git a/configure.ac b/configure.ac index 41eedc2fd..281c64cce 100644 --- a/configure.ac +++ b/configure.ac @@ -1498,6 +1498,14 @@ large outgoing port ranges. ]) # include "event2/event.h" #endif ]) + # prometheus metrics depend on libevent 2.0 and later, and is therefore + # only enabled when the required version is found and used + AC_CHECK_FUNCS([evhttp_free], [ + AC_DEFINE_UNQUOTED([USE_METRICS], [], [Define this to expose Unbound statistics via a prometheus metrics HTTP endpoint.]) + AC_DEFINE_UNQUOTED([UNBOUND_METRICS_PORT], [9100], [Define the default metrics HTTP endpoint port.]) + ], [ + AC_MSG_NOTICE([disabling prometheus metrics]) + ]) PC_LIBEVENT_DEPENDENCY="libevent" AC_SUBST(PC_LIBEVENT_DEPENDENCY) if test -n "$BAK_LDFLAGS_SET"; then @@ -1505,6 +1513,7 @@ large outgoing port ranges. ]) fi else AC_DEFINE(USE_MINI_EVENT, 1, [Define if you want to use internal select based events]) + AC_MSG_NOTICE([Prometheus metrics are disabled with the builtin libevent alternative]) fi # check for libexpat diff --git a/daemon/metrics.c b/daemon/metrics.c new file mode 100644 index 000000000..828e2ddd2 --- /dev/null +++ b/daemon/metrics.c @@ -0,0 +1,46 @@ +/* + * daemon/metrics.c - prometheus metrics output. + * + * Copyright (c) 2026, NLnet Labs. All rights reserved. + * + * This software is open source. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of the NLNET LABS nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * \file + * + * The statistics output provides metrics to prometheus. + */ +#include "config.h" +#include "daemon/metrics.h" + +#ifdef USE_METRICS + +#endif /* USE_METRICS */ diff --git a/daemon/metrics.h b/daemon/metrics.h new file mode 100644 index 000000000..ee7682ada --- /dev/null +++ b/daemon/metrics.h @@ -0,0 +1,45 @@ +/* + * daemon/metrics.h - prometheus metrics output. + * + * Copyright (c) 2026, NLnet Labs. All rights reserved. + * + * This software is open source. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of the NLNET LABS nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * \file + * + * The statistics output provides metrics to prometheus. + */ + +#ifndef DAEMON_METRICS_H +#define DAEMON_METRICS_H + +#endif /* DAEMON_METRICS_H */