refactor(core): Clean up Prometheus service (no-changelog) (#10068)
This commit is contained in:
@@ -1,83 +0,0 @@
|
||||
import { Container } from 'typedi';
|
||||
import { parse as semverParse } from 'semver';
|
||||
import request from 'supertest';
|
||||
|
||||
import config from '@/config';
|
||||
import { N8N_VERSION } from '@/constants';
|
||||
import { PrometheusMetricsService } from '@/metrics/prometheus-metrics.service';
|
||||
import { ExecutionRecoveryService } from '@/executions/execution-recovery.service';
|
||||
|
||||
import { setupTestServer } from './shared/utils';
|
||||
import { mockInstance } from '../shared/mocking';
|
||||
|
||||
mockInstance(ExecutionRecoveryService);
|
||||
jest.unmock('@/eventbus/MessageEventBus/MessageEventBus');
|
||||
config.set('endpoints.metrics.enable', true);
|
||||
config.set('endpoints.metrics.includeDefaultMetrics', false);
|
||||
config.set('endpoints.metrics.prefix', 'n8n_test_');
|
||||
const testServer = setupTestServer({ endpointGroups: ['metrics'] });
|
||||
|
||||
let testAgent = request.agent(testServer.app);
|
||||
|
||||
async function getMetricsResponseAsLines() {
|
||||
const response = await testAgent.get('/metrics');
|
||||
expect(response.status).toEqual(200);
|
||||
expect(response.type).toEqual('text/plain');
|
||||
|
||||
const lines = response.text.trim().split('\n');
|
||||
return lines;
|
||||
}
|
||||
|
||||
describe('Metrics', () => {
|
||||
it('should return n8n version', async () => {
|
||||
const n8nVersion = semverParse(N8N_VERSION || '0.0.0');
|
||||
expect(n8nVersion).toBeTruthy();
|
||||
const lines = await getMetricsResponseAsLines();
|
||||
expect(lines).toContain(
|
||||
`n8n_test_version_info{version="v${n8nVersion!.version}",major="${
|
||||
n8nVersion!.major
|
||||
}",minor="${n8nVersion!.minor}",patch="${n8nVersion!.patch}"} 1`,
|
||||
);
|
||||
});
|
||||
|
||||
it('should return cache metrics when enabled', async () => {
|
||||
config.set('endpoints.metrics.includeCacheMetrics', true);
|
||||
await Container.get(PrometheusMetricsService).configureMetrics(testServer.app);
|
||||
const lines = await getMetricsResponseAsLines();
|
||||
expect(lines).toContain('n8n_test_cache_hits_total 0');
|
||||
expect(lines).toContain('n8n_test_cache_misses_total 0');
|
||||
expect(lines).toContain('n8n_test_cache_updates_total 0');
|
||||
});
|
||||
|
||||
// TODO: Commented out due to flakiness in CI
|
||||
// it('should return event metrics when enabled', async () => {
|
||||
// config.set('endpoints.metrics.includeMessageEventBusMetrics', true);
|
||||
// await Container.get(MetricsService).configureMetrics(testServer.app);
|
||||
// await eventBus.initialize();
|
||||
// await eventBus.send(
|
||||
// new EventMessageGeneric({
|
||||
// eventName: 'n8n.destination.test',
|
||||
// }),
|
||||
// );
|
||||
// const lines = await getMetricsResponseAsLines();
|
||||
// expect(lines).toContain('n8n_test_destination_test_total 1');
|
||||
// await eventBus.close();
|
||||
// jest.mock('@/eventbus/MessageEventBus/MessageEventBus');
|
||||
// });
|
||||
|
||||
it('should return default metrics', async () => {
|
||||
config.set('endpoints.metrics.includeDefaultMetrics', true);
|
||||
await Container.get(PrometheusMetricsService).configureMetrics(testServer.app);
|
||||
const lines = await getMetricsResponseAsLines();
|
||||
expect(lines).toContain('nodejs_heap_space_size_total_bytes{space="read_only"} 0');
|
||||
config.set('endpoints.metrics.includeDefaultMetrics', false);
|
||||
});
|
||||
|
||||
it('should not return default metrics only when disabled', async () => {
|
||||
config.set('endpoints.metrics.includeDefaultMetrics', false);
|
||||
await Container.get(PrometheusMetricsService).configureMetrics(testServer.app);
|
||||
const lines = await getMetricsResponseAsLines();
|
||||
expect(lines).not.toContain('nodejs_heap_space_size_total_bytes{space="read_only"} 0');
|
||||
config.set('endpoints.metrics.includeDefaultMetrics', true);
|
||||
});
|
||||
});
|
||||
149
packages/cli/test/integration/prometheus-metrics.test.ts
Normal file
149
packages/cli/test/integration/prometheus-metrics.test.ts
Normal file
@@ -0,0 +1,149 @@
|
||||
import { Container } from 'typedi';
|
||||
import { parse as semverParse } from 'semver';
|
||||
import request, { type Response } from 'supertest';
|
||||
|
||||
import config from '@/config';
|
||||
import { N8N_VERSION } from '@/constants';
|
||||
import { PrometheusMetricsService } from '@/metrics/prometheus-metrics.service';
|
||||
import { setupTestServer } from './shared/utils';
|
||||
|
||||
jest.unmock('@/eventbus/MessageEventBus/MessageEventBus');
|
||||
|
||||
const toLines = (response: Response) => response.text.trim().split('\n');
|
||||
|
||||
config.set('endpoints.metrics.enable', true);
|
||||
config.set('endpoints.metrics.prefix', 'n8n_test_');
|
||||
|
||||
const server = setupTestServer({ endpointGroups: ['metrics'] });
|
||||
const agent = request.agent(server.app);
|
||||
|
||||
let prometheusService: PrometheusMetricsService;
|
||||
|
||||
describe('Metrics', () => {
|
||||
beforeAll(() => {
|
||||
prometheusService = Container.get(PrometheusMetricsService);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
prometheusService.disableAllMetrics();
|
||||
});
|
||||
|
||||
it('should return n8n version', async () => {
|
||||
/**
|
||||
* Arrange
|
||||
*/
|
||||
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 n8nVersion = semverParse(N8N_VERSION);
|
||||
|
||||
if (!n8nVersion) fail('Failed to parse n8n version');
|
||||
|
||||
const { version, major, minor, patch } = n8nVersion;
|
||||
|
||||
expect(toLines(response)).toContain(
|
||||
`n8n_test_version_info{version="v${version}",major="${major}",minor="${minor}",patch="${patch}"} 1`,
|
||||
);
|
||||
});
|
||||
|
||||
it('should return default metrics if enabled', async () => {
|
||||
/**
|
||||
* Arrange
|
||||
*/
|
||||
prometheusService.enableMetric('default');
|
||||
await prometheusService.init(server.app);
|
||||
|
||||
/**
|
||||
* Act
|
||||
*/
|
||||
const response = await agent.get('/metrics');
|
||||
|
||||
/**
|
||||
* Assert
|
||||
*/
|
||||
expect(response.status).toEqual(200);
|
||||
expect(response.type).toEqual('text/plain');
|
||||
expect(toLines(response)).toContain('nodejs_heap_space_size_total_bytes{space="read_only"} 0');
|
||||
});
|
||||
|
||||
it('should not return default metrics if disabled', async () => {
|
||||
/**
|
||||
* Arrange
|
||||
*/
|
||||
prometheusService.disableMetric('default');
|
||||
await prometheusService.init(server.app);
|
||||
|
||||
/**
|
||||
* Act
|
||||
*/
|
||||
const response = await agent.get('/metrics');
|
||||
|
||||
/**
|
||||
* Assert
|
||||
*/
|
||||
expect(response.status).toEqual(200);
|
||||
expect(response.type).toEqual('text/plain');
|
||||
expect(toLines(response)).not.toContain(
|
||||
'nodejs_heap_space_size_total_bytes{space="read_only"} 0',
|
||||
);
|
||||
});
|
||||
|
||||
it('should return cache metrics if enabled', async () => {
|
||||
/**
|
||||
* Arrange
|
||||
*/
|
||||
prometheusService.enableMetric('cache');
|
||||
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_cache_hits_total 0');
|
||||
expect(lines).toContain('n8n_test_cache_misses_total 0');
|
||||
expect(lines).toContain('n8n_test_cache_updates_total 0');
|
||||
});
|
||||
|
||||
it('should not return cache metrics if disabled', async () => {
|
||||
/**
|
||||
* Arrange
|
||||
*/
|
||||
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).not.toContain('n8n_test_cache_hits_total 0');
|
||||
expect(lines).not.toContain('n8n_test_cache_misses_total 0');
|
||||
expect(lines).not.toContain('n8n_test_cache_updates_total 0');
|
||||
});
|
||||
});
|
||||
@@ -148,7 +148,7 @@ export const setupTestServer = ({
|
||||
const { PrometheusMetricsService } = await import(
|
||||
'@/metrics/prometheus-metrics.service'
|
||||
);
|
||||
await Container.get(PrometheusMetricsService).configureMetrics(app);
|
||||
await Container.get(PrometheusMetricsService).init(app);
|
||||
break;
|
||||
|
||||
case 'eventBus':
|
||||
|
||||
Reference in New Issue
Block a user