From 790cc6f79e027273312f282fe27ce5c54f075151 Mon Sep 17 00:00:00 2001 From: Alex Gustafsson <89969483+alexg-axis@users.noreply.github.com> Date: Wed, 18 Dec 2024 23:50:38 +0100 Subject: [PATCH] Add parameter for specifying prometheus path (#1602) Add a `--prometheus-path` parameter which allows users to specify at what path the metrics should be exposed. This simplifies serving metrics on a specific path behind some restrictive reverse proxies that expect the upstream server to serve URLs with paths matching the requested path. Co-authored-by: Pavel Punsky --- examples/run_tests_prom.sh | 7 +++++++ src/apps/relay/mainrelay.c | 7 +++++++ src/apps/relay/mainrelay.h | 1 + src/apps/relay/prom_server.c | 2 +- 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/examples/run_tests_prom.sh b/examples/run_tests_prom.sh index ea43419..8be5dae 100755 --- a/examples/run_tests_prom.sh +++ b/examples/run_tests_prom.sh @@ -64,3 +64,10 @@ turnserver_pid="$!" sleep 2 assert_prom_response "http://127.0.0.1:8080/metrics" kill "$turnserver_pid" + +echo "Running turnserver with prometheus, using custom path" +$BINDIR/turnserver --prometheus --prometheus-path="/coturn/metrics" > /dev/null & +turnserver_pid="$!" +sleep 2 +assert_prom_response "http://localhost:9641/coturn/metrics" +kill "$turnserver_pid" diff --git a/src/apps/relay/mainrelay.c b/src/apps/relay/mainrelay.c index 61455f4..af741c1 100644 --- a/src/apps/relay/mainrelay.c +++ b/src/apps/relay/mainrelay.c @@ -209,6 +209,7 @@ turn_params_t turn_params = { 0, /* prometheus disabled by default */ DEFAULT_PROM_SERVER_PORT, /* prometheus port */ "", /* prometheus address */ + "/metrics", /* prometheus path */ 0, /* prometheus username labelling disabled by default when prometheus is enabled */ ///////////// Users DB ////////////// @@ -1137,6 +1138,7 @@ static char Usage[] = " also the path / on this port can be used as a health check\n" " --prometheus-port Prometheus metrics port (Default: 9641).\n" " --prometheus-address
Prometheus listening address (Default: any).\n" + " --prometheus-path Prometheus serve path (Default: /metrics).\n" " --prometheus-username-labels When metrics are enabled, add labels with client usernames.\n" #endif " --use-auth-secret TURN REST API flag.\n" @@ -1436,6 +1438,7 @@ enum EXTRA_OPTS { PROMETHEUS_OPT, PROMETHEUS_PORT_OPT, PROMETHEUS_ADDRESS_OPT, + PROMETHEUS_PATH_OPT, PROMETHEUS_ENABLE_USERNAMES_OPT, AUTH_SECRET_OPT, NO_AUTH_PINGS_OPT, @@ -1555,6 +1558,7 @@ static const struct myoption long_options[] = { {"prometheus", optional_argument, NULL, PROMETHEUS_OPT}, {"prometheus-port", optional_argument, NULL, PROMETHEUS_PORT_OPT}, {"prometheus-address", optional_argument, NULL, PROMETHEUS_ADDRESS_OPT}, + {"prometheus-path", optional_argument, NULL, PROMETHEUS_PATH_OPT}, {"prometheus-username-labels", optional_argument, NULL, PROMETHEUS_ENABLE_USERNAMES_OPT}, #endif {"use-auth-secret", optional_argument, NULL, AUTH_SECRET_OPT}, @@ -2207,6 +2211,9 @@ static void set_option(int c, char *value) { case PROMETHEUS_ADDRESS_OPT: STRCPY(turn_params.prometheus_address, value); break; + case PROMETHEUS_PATH_OPT: + STRCPY(turn_params.prometheus_path, value); + break; case PROMETHEUS_ENABLE_USERNAMES_OPT: turn_params.prometheus_username_labels = 1; break; diff --git a/src/apps/relay/mainrelay.h b/src/apps/relay/mainrelay.h index 261e22e..e43da39 100644 --- a/src/apps/relay/mainrelay.h +++ b/src/apps/relay/mainrelay.h @@ -311,6 +311,7 @@ typedef struct _turn_params_ { int prometheus; int prometheus_port; char prometheus_address[INET6_ADDRSTRLEN]; + char prometheus_path[1025]; int prometheus_username_labels; /////// Users DB /////////// diff --git a/src/apps/relay/prom_server.c b/src/apps/relay/prom_server.c index c35780c..f4bfb56 100644 --- a/src/apps/relay/prom_server.c +++ b/src/apps/relay/prom_server.c @@ -52,7 +52,7 @@ MHD_RESULT promhttp_handler(void *cls, struct MHD_Connection *connection, const if (strcmp(method, "GET") != 0) { status = MHD_HTTP_METHOD_NOT_ALLOWED; body = "method not allowed"; - } else if (strcmp(url, "/metrics") == 0) { + } else if (strcmp(url, turn_params.prometheus_path) == 0) { body = prom_collector_registry_bridge(PROM_COLLECTOR_REGISTRY_DEFAULT); mode = MHD_RESPMEM_MUST_FREE; status = MHD_HTTP_OK;