feat(core): Introduce worker metrics (#10850)
This commit is contained in:
@@ -2,6 +2,7 @@ import { GlobalConfig } from '@n8n/config';
|
||||
import type express from 'express';
|
||||
import promBundle from 'express-prom-bundle';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import type { InstanceSettings } from 'n8n-core';
|
||||
import promClient from 'prom-client';
|
||||
|
||||
import config from '@/config';
|
||||
@@ -43,11 +44,13 @@ describe('PrometheusMetricsService', () => {
|
||||
const app = mock<express.Application>();
|
||||
const eventBus = mock<MessageEventBus>();
|
||||
const eventService = mock<EventService>();
|
||||
const instanceSettings = mock<InstanceSettings>({ instanceType: 'main' });
|
||||
const prometheusMetricsService = new PrometheusMetricsService(
|
||||
mock(),
|
||||
eventBus,
|
||||
globalConfig,
|
||||
eventService,
|
||||
instanceSettings,
|
||||
);
|
||||
|
||||
afterEach(() => {
|
||||
@@ -64,6 +67,7 @@ describe('PrometheusMetricsService', () => {
|
||||
mock(),
|
||||
customGlobalConfig,
|
||||
mock(),
|
||||
instanceSettings,
|
||||
);
|
||||
|
||||
await customPrometheusMetricsService.init(app);
|
||||
@@ -204,5 +208,18 @@ describe('PrometheusMetricsService', () => {
|
||||
expect(promClient.Counter).toHaveBeenCalledTimes(0); // cache metrics
|
||||
expect(eventService.on).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not set up queue metrics if enabled and on scaling mode but instance is not main', async () => {
|
||||
config.set('executions.mode', 'queue');
|
||||
prometheusMetricsService.enableMetric('queue');
|
||||
// @ts-expect-error private field
|
||||
instanceSettings.instanceType = 'worker';
|
||||
|
||||
await prometheusMetricsService.init(app);
|
||||
|
||||
expect(promClient.Gauge).toHaveBeenCalledTimes(1); // version metric
|
||||
expect(promClient.Counter).toHaveBeenCalledTimes(0); // cache metrics
|
||||
expect(eventService.on).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { GlobalConfig } from '@n8n/config';
|
||||
import type express from 'express';
|
||||
import promBundle from 'express-prom-bundle';
|
||||
import { InstanceSettings } from 'n8n-core';
|
||||
import { EventMessageTypeNames } from 'n8n-workflow';
|
||||
import promClient, { type Counter, type Gauge } from 'prom-client';
|
||||
import semverParse from 'semver/functions/parse';
|
||||
@@ -22,6 +23,7 @@ export class PrometheusMetricsService {
|
||||
private readonly eventBus: MessageEventBus,
|
||||
private readonly globalConfig: GlobalConfig,
|
||||
private readonly eventService: EventService,
|
||||
private readonly instanceSettings: InstanceSettings,
|
||||
) {}
|
||||
|
||||
private readonly counters: { [key: string]: Counter<string> | null } = {};
|
||||
@@ -227,7 +229,13 @@ export class PrometheusMetricsService {
|
||||
}
|
||||
|
||||
private initQueueMetrics() {
|
||||
if (!this.includes.metrics.queue || config.getEnv('executions.mode') !== 'queue') return;
|
||||
if (
|
||||
!this.includes.metrics.queue ||
|
||||
config.getEnv('executions.mode') !== 'queue' ||
|
||||
this.instanceSettings.instanceType !== 'main'
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.gauges.waiting = new promClient.Gauge({
|
||||
name: this.prefix + 'scaling_mode_queue_jobs_waiting',
|
||||
|
||||
Reference in New Issue
Block a user