fix(core)!: Change last activity to use unix time (#15951)

This commit is contained in:
Juuso Tapaninen
2025-06-03 16:55:55 +03:00
committed by GitHub
parent 2c9c3dab33
commit 4e2229752b
4 changed files with 39 additions and 15 deletions

View File

@@ -1,6 +1,7 @@
import { GlobalConfig } from '@n8n/config';
import { WorkflowRepository } from '@n8n/db';
import { Container } from '@n8n/di';
import { DateTime } from 'luxon';
import { parse as semverParse } from 'semver';
import request, { type Response } from 'supertest';
@@ -53,6 +54,11 @@ describe('PrometheusMetricsService', () => {
prometheusService.disableAllLabels();
});
afterEach(() => {
// Make sure fake timers aren't in effect after a test
jest.useRealTimers();
});
it('should return n8n version', async () => {
/**
* Arrange
@@ -211,9 +217,11 @@ describe('PrometheusMetricsService', () => {
/**
* Arrange
*/
const startTime = DateTime.now().toUnixInteger();
jest.useFakeTimers().setSystemTime(startTime * 1000);
prometheusService.enableMetric('routes');
await prometheusService.init(server.app);
await agent.get('/api/v1/workflows');
/**
* Act
@@ -230,26 +238,30 @@ describe('PrometheusMetricsService', () => {
expect(lines).toContainEqual(expect.stringContaining('n8n_test_last_activity'));
const lastActivityLine = lines.find((line) =>
line.startsWith('n8n_test_last_activity{timestamp='),
);
const lastActivityLine = lines.find((line) => line.startsWith('n8n_test_last_activity'));
expect(lastActivityLine).toBeDefined();
expect(lastActivityLine?.endsWith('1')).toBe(true);
const value = lastActivityLine!.split(' ')[1];
expect(parseInt(value, 10)).toBe(startTime);
// Update last activity
jest.advanceTimersByTime(1000);
await agent.get('/api/v1/workflows');
response = await agent.get('/metrics');
const updatedLines = toLines(response);
const newLastActivityLine = updatedLines.find((line) =>
line.startsWith('n8n_test_last_activity{timestamp='),
line.startsWith('n8n_test_last_activity'),
);
expect(newLastActivityLine).toBeDefined();
// Timestamp label should be different
expect(newLastActivityLine).not.toBe(lastActivityLine);
const newValue = newLastActivityLine!.split(' ')[1];
expect(parseInt(newValue, 10)).toBe(startTime + 1);
});
it('should return labels in route metrics if enabled', async () => {