Skip to content

Commit

Permalink
add proxy_group
Browse files Browse the repository at this point in the history
  • Loading branch information
s4ke committed Feb 3, 2023
1 parent 6be2698 commit a35b79f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -38,7 +41,8 @@ services:
],
"metrics_path": "/metrics"
}
]
],
"proxy_group": "default"
}
deploy:
mode: replicated
Expand Down
13 changes: 9 additions & 4 deletions prometheus-proxy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -44,7 +44,8 @@ type Config = {
port: string | number
}[],
metrics_path?: string
}[]
}[],
proxy_group: string | null
};

let config: Config;
Expand All @@ -63,6 +64,8 @@ app.get('/metrics', async (req, res) => {

const promises: Promise<string>[] = [];

const proxy_group = config.proxy_group || 'default';

for (const app of config.apps) {
try {
const instances = new Set<string>();
Expand All @@ -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();
Expand All @@ -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();
}
Expand Down

0 comments on commit a35b79f

Please sign in to comment.