Skip to content

Commit

Permalink
feat: remove toggle for active workflow count metric
Browse files Browse the repository at this point in the history
Include metric if metrics are enabled. Leave cache TTL as
configurable, since a good time to cache might depend on how
often metrics is pinged
  • Loading branch information
juusotn8n committed Feb 25, 2025
1 parent b0573ff commit 64bc2a9
Show file tree
Hide file tree
Showing 7 changed files with 5 additions and 26 deletions.
4 changes: 0 additions & 4 deletions packages/@n8n/config/src/configs/endpoints.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ class PrometheusMetricsConfig {
@Env('N8N_METRICS_QUEUE_METRICS_INTERVAL')
queueMetricsInterval: number = 20;

/** Whether to include metric for active workflow count */
@Env('N8N_METRICS_INCLUDE_ACTIVE_WORKFLOW_COUNT_METRIC')
includeActiveWorkflowCountMetric: boolean = false;

/** How often (in seconds) to update active workflow metric */
@Env('N8N_METRICS_ACTIVE_WORKFLOW_METRIC_INTERVAL')
activeWorkflowCountInterval: number = 60;
Expand Down
1 change: 0 additions & 1 deletion packages/@n8n/config/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ describe('GlobalConfig', () => {
includeApiStatusCodeLabel: false,
includeQueueMetrics: false,
queueMetricsInterval: 20,
includeActiveWorkflowCountMetric: false,
activeWorkflowCountInterval: 60,
},
additionalNonUIRoutes: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ describe('PrometheusMetricsService', () => {

await prometheusMetricsService.init(app);

expect(promClient.Gauge).toHaveBeenCalledTimes(1); // version metric
expect(promClient.Gauge).toHaveBeenCalledTimes(2); // version metric + active workflow count metric
expect(promClient.Counter).toHaveBeenCalledTimes(0); // cache metrics
expect(eventService.on).not.toHaveBeenCalled();
});
Expand All @@ -234,14 +234,12 @@ describe('PrometheusMetricsService', () => {

await prometheusMetricsService.init(app);

expect(promClient.Gauge).toHaveBeenCalledTimes(1); // version metric
expect(promClient.Gauge).toHaveBeenCalledTimes(2); // version metric + active workflow count metric
expect(promClient.Counter).toHaveBeenCalledTimes(0); // cache metrics
expect(eventService.on).not.toHaveBeenCalled();
});

it('should setup active workflow count metric if enabled', async () => {
prometheusMetricsService.enableMetric('activeWorkflowCount');

it('should setup active workflow count metric', async () => {
await prometheusMetricsService.init(app);

// First call is n8n version metric
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ describe('Active workflow count', () => {
endpoints: {
metrics: {
prefix: '',
includeActiveWorkflowCountMetric: true,
activeWorkflowCountInterval: 30,
},
},
Expand All @@ -140,7 +139,6 @@ describe('Active workflow count', () => {
});

it('should prioritize cached value', async () => {
prometheusMetricsService.enableMetric('activeWorkflowCount');
await prometheusMetricsService.init(app);

cacheService.get.mockReturnValueOnce(Promise.resolve('1'));
Expand All @@ -160,7 +158,6 @@ active_workflow_count 1"
});

it('should query value from database if cache misses', async () => {
prometheusMetricsService.enableMetric('activeWorkflowCount');
await prometheusMetricsService.init(app);

cacheService.get.mockReturnValueOnce(Promise.resolve(undefined));
Expand Down
3 changes: 0 additions & 3 deletions packages/cli/src/metrics/prometheus-metrics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export class PrometheusMetricsService {
cache: this.globalConfig.endpoints.metrics.includeCacheMetrics,
logs: this.globalConfig.endpoints.metrics.includeMessageEventBusMetrics,
queue: this.globalConfig.endpoints.metrics.includeQueueMetrics,
activeWorkflowCount: this.globalConfig.endpoints.metrics.includeActiveWorkflowCountMetric,
},
labels: {
credentialsType: this.globalConfig.endpoints.metrics.includeCredentialTypeLabel,
Expand Down Expand Up @@ -299,8 +298,6 @@ export class PrometheusMetricsService {
* configurable.
*/
private initActiveWorkflowCountMetric() {
if (!this.includes.metrics.activeWorkflowCount) return;

const workflowRepository = this.workflowRepository;
const cacheService = this.cacheService;
const cacheKey = 'metrics:active-workflow-count';
Expand Down
8 changes: 1 addition & 7 deletions packages/cli/src/metrics/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
export type MetricCategory =
| 'default'
| 'routes'
| 'cache'
| 'logs'
| 'queue'
| 'activeWorkflowCount';
export type MetricCategory = 'default' | 'routes' | 'cache' | 'logs' | 'queue';

export type MetricLabel =
| 'credentialsType'
Expand Down
4 changes: 1 addition & 3 deletions packages/cli/test/integration/prometheus-metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ globalConfig.endpoints.metrics = {
includeApiStatusCodeLabel: true,
includeQueueMetrics: true,
queueMetricsInterval: 20,
includeActiveWorkflowCountMetric: true,
activeWorkflowCountInterval: 60,
};

Expand Down Expand Up @@ -336,8 +335,7 @@ describe('PrometheusMetricsService', () => {
expect(lines).toContain('n8n_test_scaling_mode_queue_jobs_failed 0');
});

it('should return active workflow count if enabled', async () => {
prometheusService.enableMetric('activeWorkflowCount');
it('should return active workflow count', async () => {
await prometheusService.init(server.app);

let response = await agent.get('/metrics');
Expand Down

0 comments on commit 64bc2a9

Please sign in to comment.