fix(core): Respect prefix for all Prometheus metrics (#10130)
This commit is contained in:
@@ -86,7 +86,7 @@ describe('PrometheusMetricsService', () => {
|
||||
expect(service.counters.cacheUpdatesTotal?.inc).toHaveBeenCalledWith(0);
|
||||
});
|
||||
|
||||
it('should set up API metrics with `express-prom-bundle`', async () => {
|
||||
it('should set up route metrics with `express-prom-bundle`', async () => {
|
||||
config.set('endpoints.metrics.includeApiEndpoints', true);
|
||||
config.set('endpoints.metrics.includeApiPathLabel', true);
|
||||
config.set('endpoints.metrics.includeApiMethodLabel', true);
|
||||
|
||||
@@ -10,8 +10,7 @@ import { CacheService } from '@/services/cache/cache.service';
|
||||
import { MessageEventBus } from '@/eventbus/MessageEventBus/MessageEventBus';
|
||||
import { EventMessageTypeNames } from 'n8n-workflow';
|
||||
import type { EventMessageTypes } from '@/eventbus';
|
||||
|
||||
type MetricCategory = 'default' | 'api' | 'cache' | 'logs';
|
||||
import type { Includes, MetricCategory, MetricLabel } from './types';
|
||||
|
||||
@Service()
|
||||
export class PrometheusMetricsService {
|
||||
@@ -24,10 +23,10 @@ export class PrometheusMetricsService {
|
||||
|
||||
private readonly prefix = config.getEnv('endpoints.metrics.prefix');
|
||||
|
||||
private readonly includes = {
|
||||
private readonly includes: Includes = {
|
||||
metrics: {
|
||||
default: config.getEnv('endpoints.metrics.includeDefaultMetrics'),
|
||||
api: config.getEnv('endpoints.metrics.includeApiEndpoints'),
|
||||
routes: config.getEnv('endpoints.metrics.includeApiEndpoints'),
|
||||
cache: config.getEnv('endpoints.metrics.includeCacheMetrics'),
|
||||
logs: config.getEnv('endpoints.metrics.includeMessageEventBusMetrics'),
|
||||
},
|
||||
@@ -60,8 +59,20 @@ export class PrometheusMetricsService {
|
||||
}
|
||||
|
||||
disableAllMetrics() {
|
||||
for (const metric of Object.keys(this.includes.metrics) as MetricCategory[]) {
|
||||
this.includes.metrics[metric] = false;
|
||||
for (const metric in this.includes.metrics) {
|
||||
this.includes.metrics[metric as MetricCategory] = false;
|
||||
}
|
||||
}
|
||||
|
||||
enableLabels(labels: MetricLabel[]) {
|
||||
for (const label of labels) {
|
||||
this.includes.labels[label] = true;
|
||||
}
|
||||
}
|
||||
|
||||
disableAllLabels() {
|
||||
for (const label in this.includes.labels) {
|
||||
this.includes.labels[label as MetricLabel] = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,7 +109,7 @@ export class PrometheusMetricsService {
|
||||
* Set up metrics for server routes with `express-prom-bundle`
|
||||
*/
|
||||
private initRouteMetrics(app: express.Application) {
|
||||
if (!this.includes.metrics.api) return;
|
||||
if (!this.includes.metrics.routes) return;
|
||||
|
||||
const metricsMiddleware = promBundle({
|
||||
autoregister: false,
|
||||
@@ -126,11 +137,25 @@ export class PrometheusMetricsService {
|
||||
private mountMetricsEndpoint(app: express.Application) {
|
||||
app.get('/metrics', async (_req: express.Request, res: express.Response) => {
|
||||
const metrics = await promClient.register.metrics();
|
||||
const prefixedMetrics = this.addPrefixToMetrics(metrics);
|
||||
res.setHeader('Content-Type', promClient.register.contentType);
|
||||
res.send(metrics).end();
|
||||
res.send(prefixedMetrics).end();
|
||||
});
|
||||
}
|
||||
|
||||
private addPrefixToMetrics(metrics: string) {
|
||||
return metrics
|
||||
.split('\n')
|
||||
.map((rawLine) => {
|
||||
const line = rawLine.trim();
|
||||
|
||||
if (!line || line.startsWith('#') || line.startsWith(this.prefix)) return rawLine;
|
||||
|
||||
return this.prefix + line;
|
||||
})
|
||||
.join('\n');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up cache metrics: `n8n_cache_hits_total`, `n8n_cache_misses_total`, and
|
||||
* `n8n_cache_updates_total`
|
||||
|
||||
14
packages/cli/src/metrics/types.ts
Normal file
14
packages/cli/src/metrics/types.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
export type MetricCategory = 'default' | 'routes' | 'cache' | 'logs';
|
||||
|
||||
export type MetricLabel =
|
||||
| 'credentialsType'
|
||||
| 'nodeType'
|
||||
| 'workflowId'
|
||||
| 'apiPath'
|
||||
| 'apiMethod'
|
||||
| 'apiStatusCode';
|
||||
|
||||
export type Includes = {
|
||||
metrics: Record<MetricCategory, boolean>;
|
||||
labels: Record<MetricLabel, boolean>;
|
||||
};
|
||||
Reference in New Issue
Block a user