Skip to content

Commit

Permalink
Add error as a counter
Browse files Browse the repository at this point in the history
  • Loading branch information
ausias-armesto committed Sep 12, 2024
1 parent 4b6efb2 commit 2f5a86a
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Settings = {
intervalMs: number;
offsetMs: number;
metricLabels: Record<string, string>;
metrics: Record<string, prom.Summary>;
metrics: Record<string, prom.Summary | prom.Counter>;
};

// if this file is the entrypoint of the nodejs process
Expand Down Expand Up @@ -104,9 +104,9 @@ if (require.main === module) {
};

const labelNames = Object.keys(settings.metricLabels);
settings.metrics['errorSum'] = new prom.Summary({
settings.metrics['errorSum'] = new prom.Counter({
name: `uhttp_error`,
help: 'Latency measure not possible due to error',
help: 'Error counter measuring latency',
labelNames,
});

Expand Down Expand Up @@ -166,8 +166,10 @@ function tick(uClient: Routing.Client, uHTTPsettings: UHTTPsettings, settings: S
log.info('Executing latency tick - scheduled to execute every %dms', settings.intervalMs);
runner
.once(uClient, uHTTPsettings.rpcProvider)
.then(collectMetrics(settings.metrics, settings.metricLabels))
.catch(reportError(settings.metrics, settings.metricLabels))
.then(
collectMetrics(settings.metrics as Record<string, prom.Summary>, settings.metricLabels),
)
.catch(reportError(settings.metrics['errorSum'] as prom.Counter, settings.metricLabels))
.finally(pushMetrics(settings.pushGateway));
}

Expand All @@ -184,10 +186,10 @@ function collectMetrics(
};
}

function reportError(metrics: Record<string, prom.Summary>, metricLabels: Record<string, string>) {
function reportError(errorCounter: prom.Counter, metricLabels: Record<string, string>) {
return function (err: Error) {
log.error('Error trying to check latency: %s', err);
metrics['errorSum'].observe(metricLabels, 0);
errorCounter.inc(metricLabels);
};
}

Expand Down

0 comments on commit 2f5a86a

Please sign in to comment.