mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 01:56:46 +00:00
feat(core): Expose queue metrics for Prometheus (#10559)
This commit is contained in:
@@ -6,11 +6,14 @@ import { N8N_VERSION } from '@/constants';
|
||||
import { PrometheusMetricsService } from '@/metrics/prometheus-metrics.service';
|
||||
import { setupTestServer } from './shared/utils';
|
||||
import { GlobalConfig } from '@n8n/config';
|
||||
import config from '@/config';
|
||||
import { EventService } from '@/events/event.service';
|
||||
|
||||
jest.unmock('@/eventbus/message-event-bus/message-event-bus');
|
||||
|
||||
const toLines = (response: Response) => response.text.trim().split('\n');
|
||||
|
||||
const eventService = Container.get(EventService);
|
||||
const globalConfig = Container.get(GlobalConfig);
|
||||
globalConfig.endpoints.metrics = {
|
||||
enable: true,
|
||||
@@ -25,6 +28,8 @@ globalConfig.endpoints.metrics = {
|
||||
includeApiPathLabel: true,
|
||||
includeApiMethodLabel: true,
|
||||
includeApiStatusCodeLabel: true,
|
||||
includeQueueMetrics: true,
|
||||
queueMetricsInterval: 20,
|
||||
};
|
||||
|
||||
const server = setupTestServer({ endpointGroups: ['metrics'] });
|
||||
@@ -32,7 +37,7 @@ const agent = request.agent(server.app);
|
||||
|
||||
let prometheusService: PrometheusMetricsService;
|
||||
|
||||
describe('Metrics', () => {
|
||||
describe('PrometheusMetricsService', () => {
|
||||
beforeAll(() => {
|
||||
prometheusService = Container.get(PrometheusMetricsService);
|
||||
});
|
||||
@@ -222,4 +227,60 @@ describe('Metrics', () => {
|
||||
expect(lines).toContainEqual(expect.stringContaining('path="/webhook-test/some-uuid"'));
|
||||
expect(lines).toContainEqual(expect.stringContaining('status_code="404"'));
|
||||
});
|
||||
|
||||
it('should return queue metrics if enabled', async () => {
|
||||
/**
|
||||
* Arrange
|
||||
*/
|
||||
prometheusService.enableMetric('queue');
|
||||
config.set('executions.mode', 'queue');
|
||||
await prometheusService.init(server.app);
|
||||
|
||||
/**
|
||||
* Act
|
||||
*/
|
||||
const response = await agent.get('/metrics');
|
||||
|
||||
/**
|
||||
* Assert
|
||||
*/
|
||||
expect(response.status).toEqual(200);
|
||||
expect(response.type).toEqual('text/plain');
|
||||
|
||||
const lines = toLines(response);
|
||||
|
||||
expect(lines).toContain('n8n_test_scaling_mode_queue_jobs_waiting 0');
|
||||
expect(lines).toContain('n8n_test_scaling_mode_queue_jobs_active 0');
|
||||
expect(lines).toContain('n8n_test_scaling_mode_queue_jobs_completed 0');
|
||||
expect(lines).toContain('n8n_test_scaling_mode_queue_jobs_failed 0');
|
||||
});
|
||||
|
||||
it('should set queue metrics in response to `job-counts-updated` event', async () => {
|
||||
/**
|
||||
* Arrange
|
||||
*/
|
||||
prometheusService.enableMetric('queue');
|
||||
config.set('executions.mode', 'queue');
|
||||
await prometheusService.init(server.app);
|
||||
|
||||
/**
|
||||
* Act
|
||||
*/
|
||||
eventService.emit('job-counts-updated', { waiting: 1, active: 2, completed: 0, failed: 0 });
|
||||
|
||||
/**
|
||||
* Assert
|
||||
*/
|
||||
const response = await agent.get('/metrics');
|
||||
|
||||
expect(response.status).toEqual(200);
|
||||
expect(response.type).toEqual('text/plain');
|
||||
|
||||
const lines = toLines(response);
|
||||
|
||||
expect(lines).toContain('n8n_test_scaling_mode_queue_jobs_waiting 1');
|
||||
expect(lines).toContain('n8n_test_scaling_mode_queue_jobs_active 2');
|
||||
expect(lines).toContain('n8n_test_scaling_mode_queue_jobs_completed 0');
|
||||
expect(lines).toContain('n8n_test_scaling_mode_queue_jobs_failed 0');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user