refactor(core): Port endpoints config (no-changelog) (#10268)
This commit is contained in:
@@ -5,6 +5,8 @@ import { mock } from 'jest-mock-extended';
|
||||
import { PrometheusMetricsService } from '../prometheus-metrics.service';
|
||||
import type express from 'express';
|
||||
import type { MessageEventBus } from '@/eventbus/MessageEventBus/MessageEventBus';
|
||||
import { mockInstance } from '@test/mocking';
|
||||
import { GlobalConfig } from '@n8n/config';
|
||||
|
||||
const mockMiddleware = (
|
||||
_req: express.Request,
|
||||
@@ -16,13 +18,27 @@ jest.mock('prom-client');
|
||||
jest.mock('express-prom-bundle', () => jest.fn(() => mockMiddleware));
|
||||
|
||||
describe('PrometheusMetricsService', () => {
|
||||
beforeEach(() => {
|
||||
config.load(config.default);
|
||||
const globalConfig = mockInstance(GlobalConfig, {
|
||||
endpoints: {
|
||||
metrics: {
|
||||
prefix: 'n8n_',
|
||||
includeDefaultMetrics: true,
|
||||
includeApiEndpoints: true,
|
||||
includeCacheMetrics: true,
|
||||
includeMessageEventBusMetrics: true,
|
||||
includeCredentialTypeLabel: false,
|
||||
includeNodeTypeLabel: false,
|
||||
includeWorkflowIdLabel: false,
|
||||
includeApiPathLabel: true,
|
||||
includeApiMethodLabel: true,
|
||||
includeApiStatusCodeLabel: true,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
describe('init', () => {
|
||||
it('should set up `n8n_version_info`', async () => {
|
||||
const service = new PrometheusMetricsService(mock(), mock());
|
||||
const service = new PrometheusMetricsService(mock(), mock(), globalConfig);
|
||||
|
||||
await service.init(mock<express.Application>());
|
||||
|
||||
@@ -34,7 +50,7 @@ describe('PrometheusMetricsService', () => {
|
||||
});
|
||||
|
||||
it('should set up default metrics collection with `prom-client`', async () => {
|
||||
const service = new PrometheusMetricsService(mock(), mock());
|
||||
const service = new PrometheusMetricsService(mock(), mock(), globalConfig);
|
||||
|
||||
await service.init(mock<express.Application>());
|
||||
|
||||
@@ -43,7 +59,7 @@ describe('PrometheusMetricsService', () => {
|
||||
|
||||
it('should set up `n8n_cache_hits_total`', async () => {
|
||||
config.set('endpoints.metrics.includeCacheMetrics', true);
|
||||
const service = new PrometheusMetricsService(mock(), mock());
|
||||
const service = new PrometheusMetricsService(mock(), mock(), globalConfig);
|
||||
|
||||
await service.init(mock<express.Application>());
|
||||
|
||||
@@ -58,7 +74,7 @@ describe('PrometheusMetricsService', () => {
|
||||
|
||||
it('should set up `n8n_cache_misses_total`', async () => {
|
||||
config.set('endpoints.metrics.includeCacheMetrics', true);
|
||||
const service = new PrometheusMetricsService(mock(), mock());
|
||||
const service = new PrometheusMetricsService(mock(), mock(), globalConfig);
|
||||
|
||||
await service.init(mock<express.Application>());
|
||||
|
||||
@@ -73,7 +89,7 @@ describe('PrometheusMetricsService', () => {
|
||||
|
||||
it('should set up `n8n_cache_updates_total`', async () => {
|
||||
config.set('endpoints.metrics.includeCacheMetrics', true);
|
||||
const service = new PrometheusMetricsService(mock(), mock());
|
||||
const service = new PrometheusMetricsService(mock(), mock(), globalConfig);
|
||||
|
||||
await service.init(mock<express.Application>());
|
||||
|
||||
@@ -91,7 +107,7 @@ describe('PrometheusMetricsService', () => {
|
||||
config.set('endpoints.metrics.includeApiPathLabel', true);
|
||||
config.set('endpoints.metrics.includeApiMethodLabel', true);
|
||||
config.set('endpoints.metrics.includeApiStatusCodeLabel', true);
|
||||
const service = new PrometheusMetricsService(mock(), mock());
|
||||
const service = new PrometheusMetricsService(mock(), mock(), globalConfig);
|
||||
|
||||
const app = mock<express.Application>();
|
||||
|
||||
@@ -122,7 +138,7 @@ describe('PrometheusMetricsService', () => {
|
||||
|
||||
it('should set up event bus metrics', async () => {
|
||||
const eventBus = mock<MessageEventBus>();
|
||||
const service = new PrometheusMetricsService(mock(), eventBus);
|
||||
const service = new PrometheusMetricsService(mock(), eventBus, globalConfig);
|
||||
|
||||
await service.init(mock<express.Application>());
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import config from '@/config';
|
||||
import { N8N_VERSION } from '@/constants';
|
||||
import type express from 'express';
|
||||
import promBundle from 'express-prom-bundle';
|
||||
@@ -11,32 +10,34 @@ import { MessageEventBus } from '@/eventbus/MessageEventBus/MessageEventBus';
|
||||
import { EventMessageTypeNames } from 'n8n-workflow';
|
||||
import type { EventMessageTypes } from '@/eventbus';
|
||||
import type { Includes, MetricCategory, MetricLabel } from './types';
|
||||
import { GlobalConfig } from '@n8n/config';
|
||||
|
||||
@Service()
|
||||
export class PrometheusMetricsService {
|
||||
constructor(
|
||||
private readonly cacheService: CacheService,
|
||||
private readonly eventBus: MessageEventBus,
|
||||
private readonly globalConfig: GlobalConfig,
|
||||
) {}
|
||||
|
||||
private readonly counters: { [key: string]: Counter<string> | null } = {};
|
||||
|
||||
private readonly prefix = config.getEnv('endpoints.metrics.prefix');
|
||||
private readonly prefix = this.globalConfig.endpoints.metrics.prefix;
|
||||
|
||||
private readonly includes: Includes = {
|
||||
metrics: {
|
||||
default: config.getEnv('endpoints.metrics.includeDefaultMetrics'),
|
||||
routes: config.getEnv('endpoints.metrics.includeApiEndpoints'),
|
||||
cache: config.getEnv('endpoints.metrics.includeCacheMetrics'),
|
||||
logs: config.getEnv('endpoints.metrics.includeMessageEventBusMetrics'),
|
||||
default: this.globalConfig.endpoints.metrics.includeDefaultMetrics,
|
||||
routes: this.globalConfig.endpoints.metrics.includeApiEndpoints,
|
||||
cache: this.globalConfig.endpoints.metrics.includeCacheMetrics,
|
||||
logs: this.globalConfig.endpoints.metrics.includeMessageEventBusMetrics,
|
||||
},
|
||||
labels: {
|
||||
credentialsType: config.getEnv('endpoints.metrics.includeCredentialTypeLabel'),
|
||||
nodeType: config.getEnv('endpoints.metrics.includeNodeTypeLabel'),
|
||||
workflowId: config.getEnv('endpoints.metrics.includeWorkflowIdLabel'),
|
||||
apiPath: config.getEnv('endpoints.metrics.includeApiPathLabel'),
|
||||
apiMethod: config.getEnv('endpoints.metrics.includeApiMethodLabel'),
|
||||
apiStatusCode: config.getEnv('endpoints.metrics.includeApiStatusCodeLabel'),
|
||||
credentialsType: this.globalConfig.endpoints.metrics.includeCredentialTypeLabel,
|
||||
nodeType: this.globalConfig.endpoints.metrics.includeNodeTypeLabel,
|
||||
workflowId: this.globalConfig.endpoints.metrics.includeWorkflowIdLabel,
|
||||
apiPath: this.globalConfig.endpoints.metrics.includeApiPathLabel,
|
||||
apiMethod: this.globalConfig.endpoints.metrics.includeApiMethodLabel,
|
||||
apiStatusCode: this.globalConfig.endpoints.metrics.includeApiStatusCodeLabel,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user