mirror of
https://github.com/czlonkowski/n8n-mcp.git
synced 2026-02-06 13:33:11 +00:00
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>
This commit is contained in:
175
tests/unit/telemetry/rate-limiter.test.ts
Normal file
175
tests/unit/telemetry/rate-limiter.test.ts
Normal file
@@ -0,0 +1,175 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { TelemetryRateLimiter } from '../../../src/telemetry/rate-limiter';
|
||||
|
||||
describe('TelemetryRateLimiter', () => {
|
||||
let rateLimiter: TelemetryRateLimiter;
|
||||
|
||||
beforeEach(() => {
|
||||
rateLimiter = new TelemetryRateLimiter(1000, 5); // 5 events per second
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('allow()', () => {
|
||||
it('should allow events within the limit', () => {
|
||||
for (let i = 0; i < 5; i++) {
|
||||
expect(rateLimiter.allow()).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it('should block events exceeding the limit', () => {
|
||||
// Fill up the limit
|
||||
for (let i = 0; i < 5; i++) {
|
||||
expect(rateLimiter.allow()).toBe(true);
|
||||
}
|
||||
|
||||
// Next event should be blocked
|
||||
expect(rateLimiter.allow()).toBe(false);
|
||||
});
|
||||
|
||||
it('should allow events again after the window expires', async () => {
|
||||
// Fill up the limit
|
||||
for (let i = 0; i < 5; i++) {
|
||||
rateLimiter.allow();
|
||||
}
|
||||
|
||||
// Should be blocked
|
||||
expect(rateLimiter.allow()).toBe(false);
|
||||
|
||||
// Wait for window to expire
|
||||
await new Promise(resolve => setTimeout(resolve, 1100));
|
||||
|
||||
// Should allow events again
|
||||
expect(rateLimiter.allow()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('wouldAllow()', () => {
|
||||
it('should check without modifying state', () => {
|
||||
// Fill up 4 of 5 allowed
|
||||
for (let i = 0; i < 4; i++) {
|
||||
rateLimiter.allow();
|
||||
}
|
||||
|
||||
// Check multiple times - should always return true
|
||||
expect(rateLimiter.wouldAllow()).toBe(true);
|
||||
expect(rateLimiter.wouldAllow()).toBe(true);
|
||||
|
||||
// Actually use the last slot
|
||||
expect(rateLimiter.allow()).toBe(true);
|
||||
|
||||
// Now should return false
|
||||
expect(rateLimiter.wouldAllow()).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getStats()', () => {
|
||||
it('should return accurate statistics', () => {
|
||||
// Use 3 of 5 allowed
|
||||
for (let i = 0; i < 3; i++) {
|
||||
rateLimiter.allow();
|
||||
}
|
||||
|
||||
const stats = rateLimiter.getStats();
|
||||
expect(stats.currentEvents).toBe(3);
|
||||
expect(stats.maxEvents).toBe(5);
|
||||
expect(stats.windowMs).toBe(1000);
|
||||
expect(stats.utilizationPercent).toBe(60);
|
||||
expect(stats.remainingCapacity).toBe(2);
|
||||
});
|
||||
|
||||
it('should track dropped events', () => {
|
||||
// Fill up the limit
|
||||
for (let i = 0; i < 5; i++) {
|
||||
rateLimiter.allow();
|
||||
}
|
||||
|
||||
// Try to add more - should be dropped
|
||||
rateLimiter.allow();
|
||||
rateLimiter.allow();
|
||||
|
||||
const stats = rateLimiter.getStats();
|
||||
expect(stats.droppedEvents).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getTimeUntilCapacity()', () => {
|
||||
it('should return 0 when capacity is available', () => {
|
||||
expect(rateLimiter.getTimeUntilCapacity()).toBe(0);
|
||||
});
|
||||
|
||||
it('should return time until capacity when at limit', () => {
|
||||
// Fill up the limit
|
||||
for (let i = 0; i < 5; i++) {
|
||||
rateLimiter.allow();
|
||||
}
|
||||
|
||||
const timeUntilCapacity = rateLimiter.getTimeUntilCapacity();
|
||||
expect(timeUntilCapacity).toBeGreaterThan(0);
|
||||
expect(timeUntilCapacity).toBeLessThanOrEqual(1000);
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateLimits()', () => {
|
||||
it('should dynamically update rate limits', () => {
|
||||
// Update to allow 10 events per 2 seconds
|
||||
rateLimiter.updateLimits(2000, 10);
|
||||
|
||||
// Should allow 10 events
|
||||
for (let i = 0; i < 10; i++) {
|
||||
expect(rateLimiter.allow()).toBe(true);
|
||||
}
|
||||
|
||||
// 11th should be blocked
|
||||
expect(rateLimiter.allow()).toBe(false);
|
||||
|
||||
const stats = rateLimiter.getStats();
|
||||
expect(stats.maxEvents).toBe(10);
|
||||
expect(stats.windowMs).toBe(2000);
|
||||
});
|
||||
});
|
||||
|
||||
describe('reset()', () => {
|
||||
it('should clear all state', () => {
|
||||
// Use some events and drop some
|
||||
for (let i = 0; i < 7; i++) {
|
||||
rateLimiter.allow();
|
||||
}
|
||||
|
||||
// Reset
|
||||
rateLimiter.reset();
|
||||
|
||||
const stats = rateLimiter.getStats();
|
||||
expect(stats.currentEvents).toBe(0);
|
||||
expect(stats.droppedEvents).toBe(0);
|
||||
|
||||
// Should allow events again
|
||||
expect(rateLimiter.allow()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('sliding window behavior', () => {
|
||||
it('should correctly implement sliding window', async () => {
|
||||
const timestamps: number[] = [];
|
||||
|
||||
// Add events at different times
|
||||
for (let i = 0; i < 3; i++) {
|
||||
expect(rateLimiter.allow()).toBe(true);
|
||||
timestamps.push(Date.now());
|
||||
await new Promise(resolve => setTimeout(resolve, 300));
|
||||
}
|
||||
|
||||
// Should still have capacity
|
||||
expect(rateLimiter.allow()).toBe(true);
|
||||
expect(rateLimiter.allow()).toBe(true);
|
||||
|
||||
// Should be at limit
|
||||
expect(rateLimiter.allow()).toBe(false);
|
||||
|
||||
// Wait for first event to expire
|
||||
await new Promise(resolve => setTimeout(resolve, 200));
|
||||
|
||||
// Should have capacity again as first event is outside window
|
||||
expect(rateLimiter.allow()).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user