feat(core): Add Prometheus metrics for n8n events and api invocations (experimental) (#5177)
* create prometheus metrics from events * feat(core): Add more Prometheus metrics (experimental) (#5187) * refactor(core): Add Prometheus labels to relevant metrics * feat(core): Add more Prometheus metrics (experimental) * add 'v' prefix to value of version label Co-authored-by: Cornelius Suermann <cornelius@n8n.io>
This commit is contained in:
committed by
GitHub
parent
4ebf40c7a2
commit
9b032d68bc
@@ -35,6 +35,11 @@ export interface EventPayloadAudit extends AbstractEventPayload {
|
||||
userEmail?: string;
|
||||
firstName?: string;
|
||||
lastName?: string;
|
||||
credentialName?: string;
|
||||
credentialType?: string;
|
||||
credentialId?: string;
|
||||
workflowId?: string;
|
||||
workflowName?: string;
|
||||
}
|
||||
|
||||
export interface EventMessageAuditOptions extends AbstractEventMessageOptions {
|
||||
|
||||
@@ -11,6 +11,11 @@ export type EventNamesNodeType = typeof eventNamesNode[number];
|
||||
// --------------------------------------
|
||||
export interface EventPayloadNode extends AbstractEventPayload {
|
||||
msg?: string;
|
||||
executionId: string;
|
||||
nodeName: string;
|
||||
workflowId?: string;
|
||||
workflowName: string;
|
||||
nodeType?: string;
|
||||
}
|
||||
|
||||
export interface EventMessageNodeOptions extends AbstractEventMessageOptions {
|
||||
|
||||
@@ -6,7 +6,10 @@ import { MessageEventBusLogWriter } from '../MessageEventBusWriter/MessageEventB
|
||||
import EventEmitter from 'events';
|
||||
import config from '@/config';
|
||||
import * as Db from '@/Db';
|
||||
import { messageEventBusDestinationFromDb } from '../MessageEventBusDestination/Helpers.ee';
|
||||
import {
|
||||
messageEventBusDestinationFromDb,
|
||||
incrementPrometheusMetric,
|
||||
} from '../MessageEventBusDestination/Helpers.ee';
|
||||
import uniqby from 'lodash.uniqby';
|
||||
import { EventMessageConfirmSource } from '../EventMessageClasses/EventMessageConfirm';
|
||||
import {
|
||||
@@ -205,6 +208,10 @@ class MessageEventBus extends EventEmitter {
|
||||
}
|
||||
|
||||
private async emitMessage(msg: EventMessageTypes) {
|
||||
if (config.getEnv('endpoints.metrics.enable')) {
|
||||
await incrementPrometheusMetric(msg);
|
||||
}
|
||||
|
||||
// generic emit for external modules to capture events
|
||||
// this is for internal use ONLY and not for use with custom destinations!
|
||||
this.emit('message', msg);
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
/* eslint-disable import/no-cycle */
|
||||
import { MessageEventBusDestinationTypeNames } from 'n8n-workflow';
|
||||
import type { EventDestinations } from '@/databases/entities/MessageEventBusDestinationEntity';
|
||||
import { promClient } from '@/metrics';
|
||||
import {
|
||||
EventMessageTypeNames,
|
||||
LoggerProxy,
|
||||
MessageEventBusDestinationTypeNames,
|
||||
} from 'n8n-workflow';
|
||||
import config from '../../config';
|
||||
import type { EventMessageTypes } from '../EventMessageClasses';
|
||||
import type { MessageEventBusDestination } from './MessageEventBusDestination.ee';
|
||||
import { MessageEventBusDestinationSentry } from './MessageEventBusDestinationSentry.ee';
|
||||
import { MessageEventBusDestinationSyslog } from './MessageEventBusDestinationSyslog.ee';
|
||||
@@ -24,3 +31,85 @@ export function messageEventBusDestinationFromDb(
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const prometheusCounters: Record<string, promClient.Counter<string> | null> = {};
|
||||
|
||||
function getMetricNameForEvent(event: EventMessageTypes): string {
|
||||
const prefix = config.getEnv('endpoints.metrics.prefix');
|
||||
return prefix + event.eventName.replace('n8n.', '').replace(/\./g, '_') + '_total';
|
||||
}
|
||||
|
||||
function getLabelValueForNode(nodeType: string): string {
|
||||
return nodeType.replace('n8n-nodes-', '').replace(/\./g, '_');
|
||||
}
|
||||
|
||||
function getLabelValueForCredential(credentialType: string): string {
|
||||
return credentialType.replace(/\./g, '_');
|
||||
}
|
||||
|
||||
function getLabelsForEvent(event: EventMessageTypes): Record<string, string> {
|
||||
switch (event.__type) {
|
||||
case EventMessageTypeNames.audit:
|
||||
if (event.eventName.startsWith('n8n.audit.user.credentials')) {
|
||||
return config.getEnv('endpoints.metrics.includeCredentialTypeLabel')
|
||||
? {
|
||||
credential_type: getLabelValueForCredential(
|
||||
event.payload.credentialType ?? 'unknown',
|
||||
),
|
||||
}
|
||||
: {};
|
||||
}
|
||||
|
||||
if (event.eventName.startsWith('n8n.audit.workflow')) {
|
||||
return config.getEnv('endpoints.metrics.includeWorkflowIdLabel')
|
||||
? { workflow_id: event.payload.workflowId?.toString() ?? 'unknown' }
|
||||
: {};
|
||||
}
|
||||
break;
|
||||
|
||||
case EventMessageTypeNames.node:
|
||||
return config.getEnv('endpoints.metrics.includeNodeTypeLabel')
|
||||
? { node_type: getLabelValueForNode(event.payload.nodeType ?? 'unknown') }
|
||||
: {};
|
||||
|
||||
case EventMessageTypeNames.workflow:
|
||||
return config.getEnv('endpoints.metrics.includeWorkflowIdLabel')
|
||||
? { workflow_id: event.payload.workflowId?.toString() ?? 'unknown' }
|
||||
: {};
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
function getCounterSingletonForEvent(event: EventMessageTypes) {
|
||||
if (!prometheusCounters[event.eventName]) {
|
||||
const metricName = getMetricNameForEvent(event);
|
||||
|
||||
if (!promClient.validateMetricName(metricName)) {
|
||||
LoggerProxy.debug(`Invalid metric name: ${metricName}. Ignoring it!`);
|
||||
prometheusCounters[event.eventName] = null;
|
||||
return null;
|
||||
}
|
||||
|
||||
const counter = new promClient.Counter({
|
||||
name: metricName,
|
||||
help: `Total number of ${event.eventName} events.`,
|
||||
labelNames: Object.keys(getLabelsForEvent(event)),
|
||||
});
|
||||
|
||||
promClient.register.registerMetric(counter);
|
||||
prometheusCounters[event.eventName] = counter;
|
||||
}
|
||||
|
||||
return prometheusCounters[event.eventName];
|
||||
}
|
||||
|
||||
export async function incrementPrometheusMetric(event: EventMessageTypes): Promise<void> {
|
||||
const counter = getCounterSingletonForEvent(event);
|
||||
|
||||
if (!counter) {
|
||||
return;
|
||||
}
|
||||
|
||||
counter.inc(getLabelsForEvent(event));
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ import {
|
||||
} from 'n8n-workflow';
|
||||
import { User } from '../databases/entities/User';
|
||||
import * as ResponseHelper from '@/ResponseHelper';
|
||||
import { EventMessageNode, EventMessageNodeOptions } from './EventMessageClasses/EventMessageNode';
|
||||
|
||||
export const eventBusRouter = express.Router();
|
||||
|
||||
@@ -116,6 +117,9 @@ eventBusRouter.post(
|
||||
case EventMessageTypeNames.audit:
|
||||
msg = new EventMessageAudit(req.body as EventMessageAuditOptions);
|
||||
break;
|
||||
case EventMessageTypeNames.node:
|
||||
msg = new EventMessageNode(req.body as EventMessageNodeOptions);
|
||||
break;
|
||||
case EventMessageTypeNames.generic:
|
||||
default:
|
||||
msg = new EventMessageGeneric(req.body);
|
||||
|
||||
Reference in New Issue
Block a user