feat(core): Add metrics option to cache (#6846)

* add metrics to cache

* use events for metrics

* pr comments / broken test

* lint fix

* update the test

* improve tests

* Update packages/cli/src/config/schema.ts

* disable flaky test

* lint fix

---------

Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <netroy@users.noreply.github.com>
This commit is contained in:
Michael Auerswald
2023-08-04 20:51:07 +02:00
committed by GitHub
parent fdfc6c5a92
commit adcf5a96e8
12 changed files with 315 additions and 159 deletions

View File

@@ -1,54 +1,23 @@
import type { EventDestinations } from '@db/entities/EventDestinations';
import { promClient } from '@/metrics';
import {
EventMessageTypeNames,
LoggerProxy,
MessageEventBusDestinationTypeNames,
} from 'n8n-workflow';
import { EventMessageTypeNames } 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';
import { MessageEventBusDestinationWebhook } from './MessageEventBusDestinationWebhook.ee';
import type { MessageEventBus } from '../MessageEventBus/MessageEventBus';
export function messageEventBusDestinationFromDb(
eventBusInstance: MessageEventBus,
dbData: EventDestinations,
): MessageEventBusDestination | null {
const destinationData = dbData.destination;
if ('__type' in destinationData) {
switch (destinationData.__type) {
case MessageEventBusDestinationTypeNames.sentry:
return MessageEventBusDestinationSentry.deserialize(eventBusInstance, destinationData);
case MessageEventBusDestinationTypeNames.syslog:
return MessageEventBusDestinationSyslog.deserialize(eventBusInstance, destinationData);
case MessageEventBusDestinationTypeNames.webhook:
return MessageEventBusDestinationWebhook.deserialize(eventBusInstance, destinationData);
default:
LoggerProxy.debug('MessageEventBusDestination __type unknown');
}
}
return null;
}
export const METRICS_EVENT_NAME = 'metrics.messageEventBus.Event';
const prometheusCounters: Record<string, promClient.Counter<string> | null> = {};
function getMetricNameForEvent(event: EventMessageTypes): string {
export 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 {
export function getLabelValueForNode(nodeType: string): string {
return nodeType.replace('n8n-nodes-', '').replace(/\./g, '_');
}
function getLabelValueForCredential(credentialType: string): string {
export function getLabelValueForCredential(credentialType: string): string {
return credentialType.replace(/\./g, '_');
}
function getLabelsForEvent(event: EventMessageTypes): Record<string, string> {
export function getLabelsForEvent(event: EventMessageTypes): Record<string, string> {
switch (event.__type) {
case EventMessageTypeNames.audit:
if (event.eventName.startsWith('n8n.audit.user.credentials')) {
@@ -81,36 +50,3 @@ function getLabelsForEvent(event: EventMessageTypes): Record<string, string> {
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));
}