feat(core): Hackmation - Add last activity metric (#13237)

This commit is contained in:
Juuso Tapaninen 2025-02-20 14:46:29 +02:00 committed by GitHub
parent b5e2f331cc
commit 272f55b80f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 37 additions and 10 deletions

View file

@ -38,6 +38,13 @@ describe('PrometheusMetricsService', () => {
includeApiStatusCodeLabel: false, includeApiStatusCodeLabel: false,
includeQueueMetrics: false, includeQueueMetrics: false,
}, },
rest: 'rest',
form: 'form',
formTest: 'form-test',
formWaiting: 'form-waiting',
webhook: 'webhook',
webhookTest: 'webhook-test',
webhookWaiting: 'webhook-waiting',
}, },
}); });
@ -145,10 +152,16 @@ describe('PrometheusMetricsService', () => {
includeStatusCode: false, includeStatusCode: false,
}); });
expect(promClient.Gauge).toHaveBeenNthCalledWith(2, {
name: 'n8n_last_activity',
help: 'last instance activity (backend request).',
labelNames: ['timestamp'],
});
expect(app.use).toHaveBeenCalledWith( expect(app.use).toHaveBeenCalledWith(
[ [
'/rest/',
'/api/', '/api/',
'/rest/',
'/webhook/', '/webhook/',
'/webhook-waiting/', '/webhook-waiting/',
'/webhook-test/', '/webhook-test/',

View file

@ -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) { private initRouteMetrics(app: express.Application) {
if (!this.includes.metrics.routes) return; if (!this.includes.metrics.routes) return;
@ -130,18 +131,31 @@ export class PrometheusMetricsService {
includeStatusCode: this.includes.labels.apiStatusCode, 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( app.use(
[ [
'/rest/',
'/api/', '/api/',
'/webhook/', `/${this.globalConfig.endpoints.rest}/`,
'/webhook-waiting/', `/${this.globalConfig.endpoints.webhook}/`,
'/webhook-test/', `/${this.globalConfig.endpoints.webhookWaiting}/`,
'/form/', `/${this.globalConfig.endpoints.webhookTest}/`,
'/form-waiting/', `/${this.globalConfig.endpoints.form}/`,
'/form-test/', `/${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);
},
); );
} }