From 7fc3b25d21c6c4f1802f34b1ae065a65cac3001b Mon Sep 17 00:00:00 2001 From: Danny Martini Date: Fri, 25 Oct 2024 10:24:39 +0200 Subject: [PATCH] fix(core): Fix Message Event Bus Metrics not counting up for labeled metrics (#11396) --- packages/cli/jest.config.js | 1 + ...rometheus-metrics.service.unmocked.test.ts | 109 ++++++++++++++++++ .../src/metrics/prometheus-metrics.service.ts | 5 +- 3 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 packages/cli/src/metrics/__tests__/prometheus-metrics.service.unmocked.test.ts diff --git a/packages/cli/jest.config.js b/packages/cli/jest.config.js index 72a653e07d..5420435df2 100644 --- a/packages/cli/jest.config.js +++ b/packages/cli/jest.config.js @@ -14,4 +14,5 @@ module.exports = { ], coveragePathIgnorePatterns: ['/src/databases/migrations/'], testTimeout: 10_000, + prettierPath: null, }; diff --git a/packages/cli/src/metrics/__tests__/prometheus-metrics.service.unmocked.test.ts b/packages/cli/src/metrics/__tests__/prometheus-metrics.service.unmocked.test.ts new file mode 100644 index 0000000000..e485bbe435 --- /dev/null +++ b/packages/cli/src/metrics/__tests__/prometheus-metrics.service.unmocked.test.ts @@ -0,0 +1,109 @@ +import { GlobalConfig } from '@n8n/config'; +import type express from 'express'; +import { mock } from 'jest-mock-extended'; +import type { InstanceSettings } from 'n8n-core'; +import promClient from 'prom-client'; + +import { EventMessageWorkflow } from '@/eventbus/event-message-classes/event-message-workflow'; +import type { EventService } from '@/events/event.service'; +import { mockInstance } from '@test/mocking'; + +import { MessageEventBus } from '../../eventbus/message-event-bus/message-event-bus'; +import { PrometheusMetricsService } from '../prometheus-metrics.service'; + +jest.unmock('@/eventbus/message-event-bus/message-event-bus'); + +const customPrefix = 'custom_'; + +const eventService = mock(); +const instanceSettings = mock({ instanceType: 'main' }); +const app = mock(); +const eventBus = new MessageEventBus( + mock(), + mock(), + mock(), + mock(), + mock(), + mock(), + mock(), + mock(), +); + +describe('workflow_success_total', () => { + test('support workflow id labels', async () => { + // ARRANGE + const globalConfig = mockInstance(GlobalConfig, { + endpoints: { + metrics: { + prefix: '', + includeMessageEventBusMetrics: true, + includeWorkflowIdLabel: true, + }, + }, + }); + + const prometheusMetricsService = new PrometheusMetricsService( + mock(), + eventBus, + globalConfig, + eventService, + instanceSettings, + ); + + await prometheusMetricsService.init(app); + + // ACT + const event = new EventMessageWorkflow({ + eventName: 'n8n.workflow.success', + payload: { workflowId: '1234' }, + }); + + eventBus.emit('metrics.eventBus.event', event); + + // ASSERT + const workflowSuccessCounter = + await promClient.register.getSingleMetricAsString('workflow_success_total'); + + expect(workflowSuccessCounter).toMatchInlineSnapshot(` +"# HELP workflow_success_total Total number of n8n.workflow.success events. +# TYPE workflow_success_total counter +workflow_success_total{workflow_id="1234"} 1" +`); + }); + + test('support a custom prefix', async () => { + // ARRANGE + const globalConfig = mockInstance(GlobalConfig, { + endpoints: { + metrics: { + prefix: customPrefix, + }, + }, + }); + + const prometheusMetricsService = new PrometheusMetricsService( + mock(), + eventBus, + globalConfig, + eventService, + instanceSettings, + ); + + await prometheusMetricsService.init(app); + + // ACT + const event = new EventMessageWorkflow({ + eventName: 'n8n.workflow.success', + payload: { workflowId: '1234' }, + }); + + eventBus.emit('metrics.eventBus.event', event); + + // ASSERT + const versionInfoMetric = promClient.register.getSingleMetric(`${customPrefix}version_info`); + + if (!versionInfoMetric) { + fail(`Could not find a metric called "${customPrefix}version_info"`); + } + }); +}); diff --git a/packages/cli/src/metrics/prometheus-metrics.service.ts b/packages/cli/src/metrics/prometheus-metrics.service.ts index 2565b0a6b1..41714d25ad 100644 --- a/packages/cli/src/metrics/prometheus-metrics.service.ts +++ b/packages/cli/src/metrics/prometheus-metrics.service.ts @@ -211,7 +211,6 @@ export class PrometheusMetricsService { help: `Total number of ${eventName} events.`, labelNames: Object.keys(labels), }); - counter.labels(labels).inc(0); this.counters[eventName] = counter; } @@ -224,7 +223,9 @@ export class PrometheusMetricsService { this.eventBus.on('metrics.eventBus.event', (event: EventMessageTypes) => { const counter = this.toCounter(event); if (!counter) return; - counter.inc(1); + + const labels = this.toLabels(event); + counter.inc(labels, 1); }); }