Skip to content

Commit

Permalink
feat(core): Hackmation - Add last activity metric (#13237)
Browse files Browse the repository at this point in the history
  • Loading branch information
juusotn8n authored Feb 20, 2025
1 parent b5e2f33 commit 272f55b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ describe('PrometheusMetricsService', () => {
includeApiStatusCodeLabel: false,
includeQueueMetrics: false,
},
rest: 'rest',
form: 'form',
formTest: 'form-test',
formWaiting: 'form-waiting',
webhook: 'webhook',
webhookTest: 'webhook-test',
webhookWaiting: 'webhook-waiting',
},
});

Expand Down Expand Up @@ -145,10 +152,16 @@ describe('PrometheusMetricsService', () => {
includeStatusCode: false,
});

expect(promClient.Gauge).toHaveBeenNthCalledWith(2, {
name: 'n8n_last_activity',
help: 'last instance activity (backend request).',
labelNames: ['timestamp'],
});

expect(app.use).toHaveBeenCalledWith(
[
'/rest/',
'/api/',
'/rest/',
'/webhook/',
'/webhook-waiting/',
'/webhook-test/',
Expand Down
32 changes: 23 additions & 9 deletions packages/cli/src/metrics/prometheus-metrics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ export class PrometheusMetricsService {
}

/**
* Set up metrics for server routes with `express-prom-bundle`
* Set up metrics for server routes with `express-prom-bundle`. The same
* middleware is also utilized for an instance activity metric
*/
private initRouteMetrics(app: express.Application) {
if (!this.includes.metrics.routes) return;
Expand All @@ -130,18 +131,31 @@ export class PrometheusMetricsService {
includeStatusCode: this.includes.labels.apiStatusCode,
});

const activityGauge = new promClient.Gauge({
name: this.prefix + 'last_activity',
help: 'last instance activity (backend request).',
labelNames: ['timestamp'],
});

activityGauge.set({ timestamp: new Date().toISOString() }, 1);

app.use(
[
'/rest/',
'/api/',
'/webhook/',
'/webhook-waiting/',
'/webhook-test/',
'/form/',
'/form-waiting/',
'/form-test/',
`/${this.globalConfig.endpoints.rest}/`,
`/${this.globalConfig.endpoints.webhook}/`,
`/${this.globalConfig.endpoints.webhookWaiting}/`,
`/${this.globalConfig.endpoints.webhookTest}/`,
`/${this.globalConfig.endpoints.form}/`,
`/${this.globalConfig.endpoints.formWaiting}/`,
`/${this.globalConfig.endpoints.formTest}/`,
],
metricsMiddleware,
(req, res, next) => {
activityGauge.reset();
activityGauge.set({ timestamp: new Date().toISOString() }, 1);

metricsMiddleware(req, res, next);
},
);
}

Expand Down

0 comments on commit 272f55b

Please sign in to comment.