From a35b79f4f5921b45ced7527c96632817352aaeef Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Fri, 3 Feb 2023 19:17:31 +0100 Subject: [PATCH] add proxy_group --- README.md | 6 +++++- prometheus-proxy/index.ts | 13 +++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e3689d7..74b943b 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,9 @@ services: - net environment: # proxy our own metrics as an example + # use proxy_group "default", if the job_name + # is unique enough. If not, you can specify + # e.g. the stack name in here PROMETHEUS_PROXY_CONFIG: | { "apps": [ @@ -38,7 +41,8 @@ services: ], "metrics_path": "/metrics" } - ] + ], + "proxy_group": "default" } deploy: mode: replicated diff --git a/prometheus-proxy/index.ts b/prometheus-proxy/index.ts index 1e96856..c233053 100644 --- a/prometheus-proxy/index.ts +++ b/prometheus-proxy/index.ts @@ -16,7 +16,7 @@ collectDefaultMetrics({ register: metricsRegistry }); const fetchErrors = new client.Counter({ name: 'prometheus_proxy_fetch_errors', help: 'Error count', - labelNames: ['proxy_error_instance', 'proxy_error_job'] + labelNames: ['proxy_group', 'proxy_error_instance', 'proxy_error_job'] }); metricsRegistry.registerMetric(fetchErrors); @@ -44,7 +44,8 @@ type Config = { port: string | number }[], metrics_path?: string - }[] + }[], + proxy_group: string | null }; let config: Config; @@ -63,6 +64,8 @@ app.get('/metrics', async (req, res) => { const promises: Promise[] = []; + const proxy_group = config.proxy_group || 'default'; + for (const app of config.apps) { try { const instances = new Set(); @@ -85,12 +88,13 @@ app.get('/metrics', async (req, res) => { const response: string = remoteMetrics.data; const augmented = - response.replace(regexWithLabels, `$1{proxy_instance="${instance}",proxy_job="${app.job_name}",$2} $3`) - .replace(regexWithoutLabels, `$1{proxy_instance="${instance}",proxy_job="${app.job_name}"} $2`); + response.replace(regexWithLabels, `$1{proxy_instance="${instance}",proxy_job="${app.job_name}",proxy_group="${proxy_group}",$2} $3`) + .replace(regexWithoutLabels, `$1{proxy_instance="${instance}",proxy_job="${app.job_name}",proxy_group="${proxy_group}"} $2`); return augmented; } catch (error) { console.error(JSON.stringify(error)); fetchErrors.labels({ + proxy_group: proxy_group, proxy_error_job: app.job_name, proxy_error_instance: instance }).inc(); @@ -101,6 +105,7 @@ app.get('/metrics', async (req, res) => { } catch (error) { console.error(JSON.stringify(error)); fetchErrors.labels({ + proxy_group: proxy_group, proxy_error_job: app.job_name }).inc(); }