Files
n8n-mcp/tests/integration/telemetry/mcp-telemetry.test.ts
czlonkowski e14c647b7d fix: refactor telemetry system with critical improvements (v2.14.1)
Major improvements to telemetry system addressing code review findings:

Architecture & Modularization:
- Split 636-line TelemetryManager into 7 focused modules
- Separated concerns: event tracking, batch processing, validation, rate limiting
- Lazy initialization pattern to avoid early singleton creation
- Clean separation of responsibilities

Security & Privacy:
- Added comprehensive input validation with Zod schemas
- Sanitization of sensitive data (URLs, API keys, emails)
- Expanded sensitive key detection patterns (25+ patterns)
- Row Level Security on Supabase backend
- Added data deletion contact info (romuald@n8n-mcp.com)

Performance & Reliability:
- Sliding window rate limiter (100 events/minute)
- Circuit breaker pattern for network failures
- Dead letter queue for failed events
- Exponential backoff with jitter for retries
- Performance monitoring with overhead tracking (<5%)
- Memory-safe array limits in rate limiter

Testing:
- Comprehensive test coverage (87%+ for core modules)
- Unit tests for all new modules
- Integration tests for MCP telemetry
- Fixed test isolation issues

Data Management:
- Clear user consent in welcome message
- Batch processing with deduplication
- Automatic workflow flushing

BREAKING CHANGE: TelemetryManager constructor is now private, use getInstance()

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-26 16:10:54 +02:00

660 lines
20 KiB
TypeScript

import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
import { N8nMcpServer } from '../../../src/mcp/server';
import { telemetry } from '../../../src/telemetry/telemetry-manager';
import { TelemetryConfigManager } from '../../../src/telemetry/config-manager';
import { CallToolRequest, ListToolsRequest } from '@modelcontextprotocol/sdk/types.js';
// Mock dependencies
vi.mock('../../../src/utils/logger', () => ({
logger: {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
}
}));
vi.mock('../../../src/telemetry/telemetry-manager', () => ({
telemetry: {
trackSessionStart: vi.fn(),
trackToolUsage: vi.fn(),
trackToolSequence: vi.fn(),
trackError: vi.fn(),
trackSearchQuery: vi.fn(),
trackValidationDetails: vi.fn(),
trackWorkflowCreation: vi.fn(),
trackPerformanceMetric: vi.fn(),
getMetrics: vi.fn().mockReturnValue({
status: 'enabled',
initialized: true,
tracking: { eventQueueSize: 0 },
processing: { eventsTracked: 0 },
errors: { totalErrors: 0 }
})
}
}));
vi.mock('../../../src/telemetry/config-manager');
// Mock database and other dependencies
vi.mock('../../../src/database/node-repository');
vi.mock('../../../src/services/enhanced-config-validator');
vi.mock('../../../src/services/expression-validator');
vi.mock('../../../src/services/workflow-validator');
describe('MCP Telemetry Integration', () => {
let mcpServer: N8nMcpServer;
let mockTelemetryConfig: any;
beforeEach(() => {
// Mock TelemetryConfigManager
mockTelemetryConfig = {
isEnabled: vi.fn().mockReturnValue(true),
getUserId: vi.fn().mockReturnValue('test-user-123'),
disable: vi.fn(),
enable: vi.fn(),
getStatus: vi.fn().mockReturnValue('enabled')
};
vi.mocked(TelemetryConfigManager.getInstance).mockReturnValue(mockTelemetryConfig);
// Mock database repository
const mockNodeRepository = {
searchNodes: vi.fn().mockResolvedValue({ results: [], totalResults: 0 }),
getNodeInfo: vi.fn().mockResolvedValue(null),
getAllNodes: vi.fn().mockResolvedValue([]),
close: vi.fn()
};
vi.doMock('../../../src/database/node-repository', () => ({
NodeRepository: vi.fn().mockImplementation(() => mockNodeRepository)
}));
mcpServer = new N8nMcpServer();
vi.clearAllMocks();
});
afterEach(() => {
vi.clearAllMocks();
});
describe('Session tracking', () => {
it('should track session start on MCP initialize', async () => {
const initializeRequest = {
method: 'initialize' as const,
params: {
protocolVersion: '2024-11-05',
clientInfo: {
name: 'test-client',
version: '1.0.0'
},
capabilities: {}
}
};
// Access the private server instance for testing
const server = (mcpServer as any).server;
const initializeHandler = server.requestHandlers.get('initialize');
if (initializeHandler) {
await initializeHandler(initializeRequest.params);
}
expect(telemetry.trackSessionStart).toHaveBeenCalledTimes(1);
});
});
describe('Tool usage tracking', () => {
it('should track successful tool execution', async () => {
const callToolRequest: CallToolRequest = {
method: 'tools/call',
params: {
name: 'search_nodes',
arguments: { query: 'webhook' }
}
};
// Mock the executeTool method to return a successful result
vi.spyOn(mcpServer as any, 'executeTool').mockResolvedValue({
results: [{ nodeType: 'nodes-base.webhook' }],
totalResults: 1
});
const server = (mcpServer as any).server;
const callToolHandler = server.requestHandlers.get('tools/call');
if (callToolHandler) {
await callToolHandler(callToolRequest.params);
}
expect(telemetry.trackToolUsage).toHaveBeenCalledWith(
'search_nodes',
true,
expect.any(Number)
);
});
it('should track failed tool execution', async () => {
const callToolRequest: CallToolRequest = {
method: 'tools/call',
params: {
name: 'get_node_info',
arguments: { nodeType: 'invalid-node' }
}
};
// Mock the executeTool method to throw an error
const error = new Error('Node not found');
vi.spyOn(mcpServer as any, 'executeTool').mockRejectedValue(error);
const server = (mcpServer as any).server;
const callToolHandler = server.requestHandlers.get('tools/call');
if (callToolHandler) {
try {
await callToolHandler(callToolRequest.params);
} catch (e) {
// Expected to throw
}
}
expect(telemetry.trackToolUsage).toHaveBeenCalledWith('get_node_info', false);
expect(telemetry.trackError).toHaveBeenCalledWith(
'Error',
'Node not found',
'get_node_info'
);
});
it('should track tool sequences', async () => {
// Set up previous tool state
(mcpServer as any).previousTool = 'search_nodes';
(mcpServer as any).previousToolTimestamp = Date.now() - 5000;
const callToolRequest: CallToolRequest = {
method: 'tools/call',
params: {
name: 'get_node_info',
arguments: { nodeType: 'nodes-base.webhook' }
}
};
vi.spyOn(mcpServer as any, 'executeTool').mockResolvedValue({
nodeType: 'nodes-base.webhook',
displayName: 'Webhook'
});
const server = (mcpServer as any).server;
const callToolHandler = server.requestHandlers.get('tools/call');
if (callToolHandler) {
await callToolHandler(callToolRequest.params);
}
expect(telemetry.trackToolSequence).toHaveBeenCalledWith(
'search_nodes',
'get_node_info',
expect.any(Number)
);
});
});
describe('Search query tracking', () => {
it('should track search queries with results', async () => {
const searchRequest: CallToolRequest = {
method: 'tools/call',
params: {
name: 'search_nodes',
arguments: { query: 'webhook', mode: 'OR' }
}
};
// Mock search results
vi.spyOn(mcpServer as any, 'executeTool').mockResolvedValue({
results: [
{ nodeType: 'nodes-base.webhook', score: 0.95 },
{ nodeType: 'nodes-base.httpRequest', score: 0.8 }
],
totalResults: 2
});
const server = (mcpServer as any).server;
const callToolHandler = server.requestHandlers.get('tools/call');
if (callToolHandler) {
await callToolHandler(callToolRequest.params);
}
expect(telemetry.trackSearchQuery).toHaveBeenCalledWith('webhook', 2, 'OR');
});
it('should track zero-result searches', async () => {
const searchRequest: CallToolRequest = {
method: 'tools/call',
params: {
name: 'search_nodes',
arguments: { query: 'nonexistent', mode: 'AND' }
}
};
vi.spyOn(mcpServer as any, 'executeTool').mockResolvedValue({
results: [],
totalResults: 0
});
const server = (mcpServer as any).server;
const callToolHandler = server.requestHandlers.get('tools/call');
if (callToolHandler) {
await callToolHandler(callToolRequest.params);
}
expect(telemetry.trackSearchQuery).toHaveBeenCalledWith('nonexistent', 0, 'AND');
});
it('should track fallback search queries', async () => {
const searchRequest: CallToolRequest = {
method: 'tools/call',
params: {
name: 'search_nodes',
arguments: { query: 'partial-match', mode: 'OR' }
}
};
// Mock main search with no results, triggering fallback
vi.spyOn(mcpServer as any, 'executeTool').mockResolvedValue({
results: [{ nodeType: 'nodes-base.webhook', score: 0.6 }],
totalResults: 1,
usedFallback: true
});
const server = (mcpServer as any).server;
const callToolHandler = server.requestHandlers.get('tools/call');
if (callToolHandler) {
await callToolHandler(callToolRequest.params);
}
// Should track both main query and fallback
expect(telemetry.trackSearchQuery).toHaveBeenCalledWith('partial-match', 0, 'OR');
expect(telemetry.trackSearchQuery).toHaveBeenCalledWith('partial-match', 1, 'OR_LIKE_FALLBACK');
});
});
describe('Workflow validation tracking', () => {
it('should track successful workflow creation', async () => {
const workflow = {
nodes: [
{ id: '1', type: 'webhook', name: 'Webhook' },
{ id: '2', type: 'httpRequest', name: 'HTTP Request' }
],
connections: {
'1': { main: [[{ node: '2', type: 'main', index: 0 }]] }
}
};
const validateRequest: CallToolRequest = {
method: 'tools/call',
params: {
name: 'validate_workflow',
arguments: { workflow }
}
};
vi.spyOn(mcpServer as any, 'executeTool').mockResolvedValue({
isValid: true,
errors: [],
warnings: [],
summary: { totalIssues: 0, criticalIssues: 0 }
});
const server = (mcpServer as any).server;
const callToolHandler = server.requestHandlers.get('tools/call');
if (callToolHandler) {
await callToolHandler(callToolRequest.params);
}
expect(telemetry.trackWorkflowCreation).toHaveBeenCalledWith(workflow, true);
});
it('should track validation details for failed workflows', async () => {
const workflow = {
nodes: [
{ id: '1', type: 'invalid-node', name: 'Invalid Node' }
],
connections: {}
};
const validateRequest: CallToolRequest = {
method: 'tools/call',
params: {
name: 'validate_workflow',
arguments: { workflow }
}
};
const validationResult = {
isValid: false,
errors: [
{
nodeId: '1',
nodeType: 'invalid-node',
category: 'node_validation',
severity: 'error',
message: 'Unknown node type',
details: { type: 'unknown_node_type' }
}
],
warnings: [],
summary: { totalIssues: 1, criticalIssues: 1 }
};
vi.spyOn(mcpServer as any, 'executeTool').mockResolvedValue(validationResult);
const server = (mcpServer as any).server;
const callToolHandler = server.requestHandlers.get('tools/call');
if (callToolHandler) {
await callToolHandler(callToolRequest.params);
}
expect(telemetry.trackValidationDetails).toHaveBeenCalledWith(
'invalid-node',
'unknown_node_type',
expect.objectContaining({
category: 'node_validation',
severity: 'error'
})
);
});
});
describe('Node configuration tracking', () => {
it('should track node configuration validation', async () => {
const validateNodeRequest: CallToolRequest = {
method: 'tools/call',
params: {
name: 'validate_node_operation',
arguments: {
nodeType: 'nodes-base.httpRequest',
config: { url: 'https://api.example.com', method: 'GET' }
}
}
};
vi.spyOn(mcpServer as any, 'executeTool').mockResolvedValue({
isValid: true,
errors: [],
warnings: [],
nodeConfig: { url: 'https://api.example.com', method: 'GET' }
});
const server = (mcpServer as any).server;
const callToolHandler = server.requestHandlers.get('tools/call');
if (callToolHandler) {
await callToolHandler(callToolRequest.params);
}
// Should track the validation attempt
expect(telemetry.trackToolUsage).toHaveBeenCalledWith(
'validate_node_operation',
true,
expect.any(Number)
);
});
});
describe('Performance metric tracking', () => {
it('should track slow tool executions', async () => {
const slowToolRequest: CallToolRequest = {
method: 'tools/call',
params: {
name: 'list_nodes',
arguments: { limit: 1000 }
}
};
// Mock a slow operation
vi.spyOn(mcpServer as any, 'executeTool').mockImplementation(async () => {
await new Promise(resolve => setTimeout(resolve, 2000)); // 2 second delay
return { nodes: [], totalCount: 0 };
});
const server = (mcpServer as any).server;
const callToolHandler = server.requestHandlers.get('tools/call');
if (callToolHandler) {
await callToolHandler(callToolRequest.params);
}
expect(telemetry.trackToolUsage).toHaveBeenCalledWith(
'list_nodes',
true,
expect.any(Number)
);
// Verify duration is tracked (should be around 2000ms)
const trackUsageCall = vi.mocked(telemetry.trackToolUsage).mock.calls[0];
expect(trackUsageCall[2]).toBeGreaterThan(1500); // Allow some variance
});
});
describe('Tool listing and capabilities', () => {
it('should handle tool listing without telemetry interference', async () => {
const listToolsRequest: ListToolsRequest = {
method: 'tools/list',
params: {}
};
const server = (mcpServer as any).server;
const listToolsHandler = server.requestHandlers.get('tools/list');
if (listToolsHandler) {
const result = await listToolsHandler(listToolsRequest.params);
expect(result).toHaveProperty('tools');
expect(Array.isArray(result.tools)).toBe(true);
}
// Tool listing shouldn't generate telemetry events
expect(telemetry.trackToolUsage).not.toHaveBeenCalled();
});
});
describe('Error handling and telemetry', () => {
it('should track errors without breaking MCP protocol', async () => {
const errorRequest: CallToolRequest = {
method: 'tools/call',
params: {
name: 'nonexistent_tool',
arguments: {}
}
};
const server = (mcpServer as any).server;
const callToolHandler = server.requestHandlers.get('tools/call');
if (callToolHandler) {
try {
await callToolHandler(errorRequest.params);
} catch (error) {
// Error should be handled by MCP server
expect(error).toBeDefined();
}
}
// Should track error without throwing
expect(telemetry.trackError).toHaveBeenCalled();
});
it('should handle telemetry errors gracefully', async () => {
// Mock telemetry to throw an error
vi.mocked(telemetry.trackToolUsage).mockImplementation(() => {
throw new Error('Telemetry service unavailable');
});
const callToolRequest: CallToolRequest = {
method: 'tools/call',
params: {
name: 'search_nodes',
arguments: { query: 'webhook' }
}
};
vi.spyOn(mcpServer as any, 'executeTool').mockResolvedValue({
results: [],
totalResults: 0
});
const server = (mcpServer as any).server;
const callToolHandler = server.requestHandlers.get('tools/call');
// Should not throw even if telemetry fails
if (callToolHandler) {
await expect(callToolHandler(callToolRequest.params)).resolves.toBeDefined();
}
});
});
describe('Telemetry configuration integration', () => {
it('should respect telemetry disabled state', async () => {
mockTelemetryConfig.isEnabled.mockReturnValue(false);
const callToolRequest: CallToolRequest = {
method: 'tools/call',
params: {
name: 'search_nodes',
arguments: { query: 'webhook' }
}
};
vi.spyOn(mcpServer as any, 'executeTool').mockResolvedValue({
results: [],
totalResults: 0
});
const server = (mcpServer as any).server;
const callToolHandler = server.requestHandlers.get('tools/call');
if (callToolHandler) {
await callToolHandler(callToolRequest.params);
}
// Should still track if telemetry manager handles disabled state
// The actual filtering happens in telemetry manager, not MCP server
expect(telemetry.trackToolUsage).toHaveBeenCalled();
});
});
describe('Complex workflow scenarios', () => {
it('should track comprehensive workflow validation scenario', async () => {
const complexWorkflow = {
nodes: [
{ id: '1', type: 'webhook', name: 'Webhook Trigger' },
{ id: '2', type: 'httpRequest', name: 'API Call', parameters: { url: 'https://api.example.com' } },
{ id: '3', type: 'set', name: 'Transform Data' },
{ id: '4', type: 'if', name: 'Conditional Logic' },
{ id: '5', type: 'slack', name: 'Send Notification' }
],
connections: {
'1': { main: [[{ node: '2', type: 'main', index: 0 }]] },
'2': { main: [[{ node: '3', type: 'main', index: 0 }]] },
'3': { main: [[{ node: '4', type: 'main', index: 0 }]] },
'4': { main: [[{ node: '5', type: 'main', index: 0 }]] }
}
};
const validateRequest: CallToolRequest = {
method: 'tools/call',
params: {
name: 'validate_workflow',
arguments: { workflow: complexWorkflow }
}
};
vi.spyOn(mcpServer as any, 'executeTool').mockResolvedValue({
isValid: true,
errors: [],
warnings: [
{
nodeId: '2',
nodeType: 'httpRequest',
category: 'configuration',
severity: 'warning',
message: 'Consider adding error handling'
}
],
summary: { totalIssues: 1, criticalIssues: 0 }
});
const server = (mcpServer as any).server;
const callToolHandler = server.requestHandlers.get('tools/call');
if (callToolHandler) {
await callToolHandler(callToolRequest.params);
}
expect(telemetry.trackWorkflowCreation).toHaveBeenCalledWith(complexWorkflow, true);
expect(telemetry.trackToolUsage).toHaveBeenCalledWith(
'validate_workflow',
true,
expect.any(Number)
);
});
});
describe('MCP server lifecycle and telemetry', () => {
it('should handle server initialization with telemetry', async () => {
// Verify that server creation doesn't interfere with telemetry
const newServer = new N8nMcpServer();
expect(newServer).toBeDefined();
// Telemetry should still be functional
expect(telemetry.getMetrics).toBeDefined();
expect(typeof telemetry.trackToolUsage).toBe('function');
});
it('should handle concurrent tool executions with telemetry', async () => {
const requests = [
{
method: 'tools/call' as const,
params: {
name: 'search_nodes',
arguments: { query: 'webhook' }
}
},
{
method: 'tools/call' as const,
params: {
name: 'search_nodes',
arguments: { query: 'http' }
}
},
{
method: 'tools/call' as const,
params: {
name: 'search_nodes',
arguments: { query: 'database' }
}
}
];
vi.spyOn(mcpServer as any, 'executeTool').mockResolvedValue({
results: [{ nodeType: 'test-node' }],
totalResults: 1
});
const server = (mcpServer as any).server;
const callToolHandler = server.requestHandlers.get('tools/call');
if (callToolHandler) {
await Promise.all(
requests.map(req => callToolHandler(req.params))
);
}
// All three calls should be tracked
expect(telemetry.trackToolUsage).toHaveBeenCalledTimes(3);
expect(telemetry.trackSearchQuery).toHaveBeenCalledTimes(3);
});
});
});