feat(ops/monitoring): add in-memory log buffer, metrics time-series and ops UI integration
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@serve.zone/dcrouter',
|
||||
version: '7.0.1',
|
||||
version: '7.1.0',
|
||||
description: 'A multifaceted routing service handling mail and SMS delivery functions.'
|
||||
}
|
||||
|
||||
@@ -1055,7 +1055,25 @@ export class DcRouter {
|
||||
|
||||
// Start the server
|
||||
await this.emailServer.start();
|
||||
|
||||
|
||||
// Wire delivery events to MetricsManager for time-series tracking
|
||||
if (this.metricsManager && this.emailServer.deliverySystem) {
|
||||
this.emailServer.deliverySystem.on('deliveryStart', (item: any) => {
|
||||
this.metricsManager.trackEmailReceived(item?.from);
|
||||
});
|
||||
this.emailServer.deliverySystem.on('deliverySuccess', (item: any) => {
|
||||
this.metricsManager.trackEmailSent(item?.to);
|
||||
});
|
||||
this.emailServer.deliverySystem.on('deliveryFailed', (item: any, error: any) => {
|
||||
this.metricsManager.trackEmailFailed(item?.to, error?.message);
|
||||
});
|
||||
}
|
||||
if (this.metricsManager && this.emailServer) {
|
||||
this.emailServer.on('bounceProcessed', () => {
|
||||
this.metricsManager.trackEmailBounced();
|
||||
});
|
||||
}
|
||||
|
||||
logger.log('info', `Email server started on ports: ${emailConfig.ports.join(', ')}`);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import * as plugins from './plugins.js';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { SmartlogDestinationBuffer } from '@push.rocks/smartlog/destination-buffer';
|
||||
|
||||
// Map NODE_ENV to valid TEnvironment
|
||||
const nodeEnv = process.env.NODE_ENV || 'production';
|
||||
@@ -10,6 +11,9 @@ const envMap: Record<string, 'local' | 'test' | 'staging' | 'production'> = {
|
||||
'production': 'production'
|
||||
};
|
||||
|
||||
// In-memory log buffer for the OpsServer UI
|
||||
export const logBuffer = new SmartlogDestinationBuffer({ maxEntries: 2000 });
|
||||
|
||||
// Default Smartlog instance
|
||||
const baseLogger = new plugins.smartlog.Smartlog({
|
||||
logContext: {
|
||||
@@ -19,6 +23,9 @@ const baseLogger = new plugins.smartlog.Smartlog({
|
||||
}
|
||||
});
|
||||
|
||||
// Wire the buffer destination so all logs are captured
|
||||
baseLogger.addLogDestination(logBuffer);
|
||||
|
||||
// Extended logger compatible with the original enhanced logger API
|
||||
class StandardLogger {
|
||||
private defaultContext: Record<string, any> = {};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import * as plugins from '../plugins.js';
|
||||
import { DcRouter } from '../classes.dcrouter.js';
|
||||
import { MetricsCache } from './classes.metricscache.js';
|
||||
import { SecurityLogger, SecurityEventType } from '../security/classes.securitylogger.js';
|
||||
|
||||
export class MetricsManager {
|
||||
private logger: plugins.smartlog.Smartlog;
|
||||
@@ -37,6 +38,10 @@ export class MetricsManager {
|
||||
responseTimes: [] as number[], // Track response times in ms
|
||||
};
|
||||
|
||||
// Per-minute time-series buckets for charts
|
||||
private emailMinuteBuckets = new Map<number, { sent: number; received: number; failed: number }>();
|
||||
private dnsMinuteBuckets = new Map<number, { queries: number }>();
|
||||
|
||||
// Track security-specific metrics
|
||||
private securityMetrics = {
|
||||
blockedIPs: 0,
|
||||
@@ -227,20 +232,45 @@ export class MetricsManager {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync security metrics from the SecurityLogger singleton (last 24h).
|
||||
* Called before returning security stats so counters reflect real events.
|
||||
*/
|
||||
private syncFromSecurityLogger(): void {
|
||||
try {
|
||||
const securityLogger = SecurityLogger.getInstance();
|
||||
const summary = securityLogger.getEventsSummary(86400000); // last 24h
|
||||
|
||||
this.securityMetrics.spamDetected = summary.byType[SecurityEventType.SPAM] || 0;
|
||||
this.securityMetrics.malwareDetected = summary.byType[SecurityEventType.MALWARE] || 0;
|
||||
this.securityMetrics.phishingDetected = summary.byType[SecurityEventType.DMARC] || 0; // phishing via DMARC
|
||||
this.securityMetrics.authFailures =
|
||||
summary.byType[SecurityEventType.AUTHENTICATION] || 0;
|
||||
this.securityMetrics.blockedIPs =
|
||||
(summary.byType[SecurityEventType.IP_REPUTATION] || 0) +
|
||||
(summary.byType[SecurityEventType.REJECTED_CONNECTION] || 0);
|
||||
} catch {
|
||||
// SecurityLogger may not be initialized yet — ignore
|
||||
}
|
||||
}
|
||||
|
||||
// Get security metrics
|
||||
public async getSecurityStats() {
|
||||
return this.metricsCache.get('securityStats', () => {
|
||||
// Sync counters from the real SecurityLogger events
|
||||
this.syncFromSecurityLogger();
|
||||
|
||||
// Get recent incidents (last 20)
|
||||
const recentIncidents = this.securityMetrics.incidents.slice(-20);
|
||||
|
||||
|
||||
return {
|
||||
blockedIPs: this.securityMetrics.blockedIPs,
|
||||
authFailures: this.securityMetrics.authFailures,
|
||||
spamDetected: this.securityMetrics.spamDetected,
|
||||
malwareDetected: this.securityMetrics.malwareDetected,
|
||||
phishingDetected: this.securityMetrics.phishingDetected,
|
||||
totalThreatsBlocked: this.securityMetrics.spamDetected +
|
||||
this.securityMetrics.malwareDetected +
|
||||
totalThreatsBlocked: this.securityMetrics.spamDetected +
|
||||
this.securityMetrics.malwareDetected +
|
||||
this.securityMetrics.phishingDetected,
|
||||
recentIncidents,
|
||||
};
|
||||
@@ -275,6 +305,7 @@ export class MetricsManager {
|
||||
// Email event tracking methods
|
||||
public trackEmailSent(recipient?: string, deliveryTimeMs?: number): void {
|
||||
this.emailMetrics.sentToday++;
|
||||
this.incrementEmailBucket('sent');
|
||||
|
||||
if (recipient) {
|
||||
const count = this.emailMetrics.recipients.get(recipient) || 0;
|
||||
@@ -311,6 +342,7 @@ export class MetricsManager {
|
||||
|
||||
public trackEmailReceived(sender?: string): void {
|
||||
this.emailMetrics.receivedToday++;
|
||||
this.incrementEmailBucket('received');
|
||||
|
||||
this.emailMetrics.recentActivity.push({
|
||||
timestamp: Date.now(),
|
||||
@@ -326,6 +358,7 @@ export class MetricsManager {
|
||||
|
||||
public trackEmailFailed(recipient?: string, reason?: string): void {
|
||||
this.emailMetrics.failedToday++;
|
||||
this.incrementEmailBucket('failed');
|
||||
|
||||
this.emailMetrics.recentActivity.push({
|
||||
timestamp: Date.now(),
|
||||
@@ -361,6 +394,7 @@ export class MetricsManager {
|
||||
// DNS event tracking methods
|
||||
public trackDnsQuery(queryType: string, domain: string, cacheHit: boolean, responseTimeMs?: number): void {
|
||||
this.dnsMetrics.totalQueries++;
|
||||
this.incrementDnsBucket();
|
||||
|
||||
if (cacheHit) {
|
||||
this.dnsMetrics.cacheHits++;
|
||||
@@ -547,4 +581,90 @@ export class MetricsManager {
|
||||
};
|
||||
}, 200); // Use 200ms cache for more frequent updates
|
||||
}
|
||||
|
||||
// --- Time-series helpers ---
|
||||
|
||||
private static minuteKey(ts: number = Date.now()): number {
|
||||
return Math.floor(ts / 60000) * 60000;
|
||||
}
|
||||
|
||||
private incrementEmailBucket(field: 'sent' | 'received' | 'failed'): void {
|
||||
const key = MetricsManager.minuteKey();
|
||||
let bucket = this.emailMinuteBuckets.get(key);
|
||||
if (!bucket) {
|
||||
bucket = { sent: 0, received: 0, failed: 0 };
|
||||
this.emailMinuteBuckets.set(key, bucket);
|
||||
}
|
||||
bucket[field]++;
|
||||
}
|
||||
|
||||
private incrementDnsBucket(): void {
|
||||
const key = MetricsManager.minuteKey();
|
||||
let bucket = this.dnsMinuteBuckets.get(key);
|
||||
if (!bucket) {
|
||||
bucket = { queries: 0 };
|
||||
this.dnsMinuteBuckets.set(key, bucket);
|
||||
}
|
||||
bucket.queries++;
|
||||
}
|
||||
|
||||
private pruneOldBuckets(): void {
|
||||
const cutoff = Date.now() - 86400000; // 24h
|
||||
for (const key of this.emailMinuteBuckets.keys()) {
|
||||
if (key < cutoff) this.emailMinuteBuckets.delete(key);
|
||||
}
|
||||
for (const key of this.dnsMinuteBuckets.keys()) {
|
||||
if (key < cutoff) this.dnsMinuteBuckets.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get email time-series data for the last N hours, aggregated per minute.
|
||||
*/
|
||||
public getEmailTimeSeries(hours: number = 24): {
|
||||
sent: Array<{ timestamp: number; value: number }>;
|
||||
received: Array<{ timestamp: number; value: number }>;
|
||||
failed: Array<{ timestamp: number; value: number }>;
|
||||
} {
|
||||
this.pruneOldBuckets();
|
||||
const cutoff = Date.now() - hours * 3600000;
|
||||
const sent: Array<{ timestamp: number; value: number }> = [];
|
||||
const received: Array<{ timestamp: number; value: number }> = [];
|
||||
const failed: Array<{ timestamp: number; value: number }> = [];
|
||||
|
||||
const sortedKeys = Array.from(this.emailMinuteBuckets.keys())
|
||||
.filter((k) => k >= cutoff)
|
||||
.sort((a, b) => a - b);
|
||||
|
||||
for (const key of sortedKeys) {
|
||||
const bucket = this.emailMinuteBuckets.get(key)!;
|
||||
sent.push({ timestamp: key, value: bucket.sent });
|
||||
received.push({ timestamp: key, value: bucket.received });
|
||||
failed.push({ timestamp: key, value: bucket.failed });
|
||||
}
|
||||
|
||||
return { sent, received, failed };
|
||||
}
|
||||
|
||||
/**
|
||||
* Get DNS time-series data for the last N hours, aggregated per minute.
|
||||
*/
|
||||
public getDnsTimeSeries(hours: number = 24): {
|
||||
queries: Array<{ timestamp: number; value: number }>;
|
||||
} {
|
||||
this.pruneOldBuckets();
|
||||
const cutoff = Date.now() - hours * 3600000;
|
||||
const queries: Array<{ timestamp: number; value: number }> = [];
|
||||
|
||||
const sortedKeys = Array.from(this.dnsMinuteBuckets.keys())
|
||||
.filter((k) => k >= cutoff)
|
||||
.sort((a, b) => a - b);
|
||||
|
||||
for (const key of sortedKeys) {
|
||||
const bucket = this.dnsMinuteBuckets.get(key)!;
|
||||
queries.push({ timestamp: key, value: bucket.queries });
|
||||
}
|
||||
|
||||
return { queries };
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import * as plugins from '../../plugins.js';
|
||||
import type { OpsServer } from '../classes.opsserver.js';
|
||||
import * as interfaces from '../../../ts_interfaces/index.js';
|
||||
import { logBuffer } from '../../logger.js';
|
||||
|
||||
export class LogsHandler {
|
||||
public typedrouter = new plugins.typedrequest.TypedRouter();
|
||||
@@ -64,6 +65,32 @@ export class LogsHandler {
|
||||
);
|
||||
}
|
||||
|
||||
private static mapLogLevel(smartlogLevel: string): 'debug' | 'info' | 'warn' | 'error' {
|
||||
switch (smartlogLevel) {
|
||||
case 'silly':
|
||||
case 'debug':
|
||||
return 'debug';
|
||||
case 'warn':
|
||||
return 'warn';
|
||||
case 'error':
|
||||
return 'error';
|
||||
default:
|
||||
return 'info';
|
||||
}
|
||||
}
|
||||
|
||||
private static deriveCategory(
|
||||
zone?: string,
|
||||
message?: string
|
||||
): 'smtp' | 'dns' | 'security' | 'system' | 'email' {
|
||||
const msg = (message || '').toLowerCase();
|
||||
if (msg.includes('[security:') || msg.includes('security')) return 'security';
|
||||
if (zone === 'email' || msg.includes('email') || msg.includes('smtp') || msg.includes('mta')) return 'email';
|
||||
if (zone === 'dns' || msg.includes('dns')) return 'dns';
|
||||
if (msg.includes('smtp')) return 'smtp';
|
||||
return 'system';
|
||||
}
|
||||
|
||||
private async getRecentLogs(
|
||||
level?: 'error' | 'warn' | 'info' | 'debug',
|
||||
category?: 'smtp' | 'dns' | 'security' | 'system' | 'email',
|
||||
@@ -78,42 +105,64 @@ export class LogsHandler {
|
||||
message: string;
|
||||
metadata?: any;
|
||||
}>> {
|
||||
// TODO: Implement actual log retrieval from storage or logger
|
||||
// For now, return mock data
|
||||
const mockLogs: Array<{
|
||||
// Compute a timestamp cutoff from timeRange
|
||||
let since: number | undefined;
|
||||
if (timeRange) {
|
||||
const rangeMs: Record<string, number> = {
|
||||
'1h': 3600000,
|
||||
'6h': 21600000,
|
||||
'24h': 86400000,
|
||||
'7d': 604800000,
|
||||
'30d': 2592000000,
|
||||
};
|
||||
since = Date.now() - (rangeMs[timeRange] || 86400000);
|
||||
}
|
||||
|
||||
// Map the UI level to smartlog levels for filtering
|
||||
const smartlogLevels: string[] | undefined = level
|
||||
? level === 'debug'
|
||||
? ['debug', 'silly']
|
||||
: level === 'info'
|
||||
? ['info', 'ok', 'success', 'note', 'lifecycle']
|
||||
: [level]
|
||||
: undefined;
|
||||
|
||||
// Fetch a larger batch from buffer, then apply category filter client-side
|
||||
const rawEntries = logBuffer.getEntries({
|
||||
level: smartlogLevels as any,
|
||||
search,
|
||||
since,
|
||||
limit: limit * 3, // over-fetch to compensate for category filtering
|
||||
offset: 0,
|
||||
});
|
||||
|
||||
// Map ILogPackage → UI log format and apply category filter
|
||||
const mapped: Array<{
|
||||
timestamp: number;
|
||||
level: 'debug' | 'info' | 'warn' | 'error';
|
||||
category: 'smtp' | 'dns' | 'security' | 'system' | 'email';
|
||||
message: string;
|
||||
metadata?: any;
|
||||
}> = [];
|
||||
|
||||
const categories: Array<'smtp' | 'dns' | 'security' | 'system' | 'email'> = ['smtp', 'dns', 'security', 'system', 'email'];
|
||||
const levels: Array<'debug' | 'info' | 'warn' | 'error'> = ['info', 'warn', 'error', 'debug'];
|
||||
const now = Date.now();
|
||||
|
||||
// Generate some mock log entries
|
||||
for (let i = 0; i < 50; i++) {
|
||||
const mockCategory = categories[Math.floor(Math.random() * categories.length)];
|
||||
const mockLevel = levels[Math.floor(Math.random() * levels.length)];
|
||||
|
||||
// Filter by requested criteria
|
||||
if (level && mockLevel !== level) continue;
|
||||
if (category && mockCategory !== category) continue;
|
||||
|
||||
mockLogs.push({
|
||||
timestamp: now - (i * 60000), // 1 minute apart
|
||||
level: mockLevel,
|
||||
category: mockCategory,
|
||||
message: `Sample log message ${i} from ${mockCategory}`,
|
||||
metadata: {
|
||||
requestId: plugins.uuid.v4(),
|
||||
},
|
||||
|
||||
for (const pkg of rawEntries) {
|
||||
const uiLevel = LogsHandler.mapLogLevel(pkg.level);
|
||||
const uiCategory = LogsHandler.deriveCategory(pkg.context?.zone, pkg.message);
|
||||
|
||||
if (category && uiCategory !== category) continue;
|
||||
|
||||
mapped.push({
|
||||
timestamp: pkg.timestamp,
|
||||
level: uiLevel,
|
||||
category: uiCategory,
|
||||
message: pkg.message,
|
||||
metadata: pkg.data,
|
||||
});
|
||||
|
||||
if (mapped.length >= limit) break;
|
||||
}
|
||||
|
||||
// Apply pagination
|
||||
return mockLogs.slice(offset, offset + limit);
|
||||
|
||||
return mapped;
|
||||
}
|
||||
|
||||
private setupLogStream(
|
||||
|
||||
@@ -2,6 +2,7 @@ import * as plugins from '../../plugins.js';
|
||||
import type { OpsServer } from '../classes.opsserver.js';
|
||||
import * as interfaces from '../../../ts_interfaces/index.js';
|
||||
import { MetricsManager } from '../../monitoring/index.js';
|
||||
import { SecurityLogger } from '../../security/classes.securitylogger.js';
|
||||
|
||||
export class StatsHandler {
|
||||
public typedrouter = new plugins.typedrequest.TypedRouter();
|
||||
@@ -203,6 +204,11 @@ export class StatsHandler {
|
||||
if (sections.email) {
|
||||
promises.push(
|
||||
this.collectEmailStats().then(stats => {
|
||||
// Get time-series data from MetricsManager
|
||||
const timeSeries = this.opsServerRef.dcRouterRef.metricsManager
|
||||
? this.opsServerRef.dcRouterRef.metricsManager.getEmailTimeSeries(24)
|
||||
: undefined;
|
||||
|
||||
metrics.email = {
|
||||
sent: stats.sentToday,
|
||||
received: stats.receivedToday,
|
||||
@@ -212,6 +218,7 @@ export class StatsHandler {
|
||||
averageDeliveryTime: 0,
|
||||
deliveryRate: stats.deliveryRate,
|
||||
bounceRate: stats.bounceRate,
|
||||
timeSeries,
|
||||
};
|
||||
})
|
||||
);
|
||||
@@ -220,6 +227,11 @@ export class StatsHandler {
|
||||
if (sections.dns) {
|
||||
promises.push(
|
||||
this.collectDnsStats().then(stats => {
|
||||
// Get time-series data from MetricsManager
|
||||
const timeSeries = this.opsServerRef.dcRouterRef.metricsManager
|
||||
? this.opsServerRef.dcRouterRef.metricsManager.getDnsTimeSeries(24)
|
||||
: undefined;
|
||||
|
||||
metrics.dns = {
|
||||
totalQueries: stats.totalQueries,
|
||||
cacheHits: stats.cacheHits,
|
||||
@@ -228,6 +240,7 @@ export class StatsHandler {
|
||||
activeDomains: stats.topDomains.length,
|
||||
averageResponseTime: 0,
|
||||
queryTypes: stats.queryTypes,
|
||||
timeSeries,
|
||||
};
|
||||
})
|
||||
);
|
||||
@@ -236,6 +249,19 @@ export class StatsHandler {
|
||||
if (sections.security && this.opsServerRef.dcRouterRef.metricsManager) {
|
||||
promises.push(
|
||||
this.opsServerRef.dcRouterRef.metricsManager.getSecurityStats().then(stats => {
|
||||
// Get recent events from the SecurityLogger singleton
|
||||
const securityLogger = SecurityLogger.getInstance();
|
||||
const recentEvents = securityLogger.getRecentEvents(50).map((evt) => ({
|
||||
timestamp: evt.timestamp,
|
||||
level: evt.level,
|
||||
type: evt.type,
|
||||
message: evt.message,
|
||||
details: evt.details,
|
||||
ipAddress: evt.ipAddress,
|
||||
domain: evt.domain,
|
||||
success: evt.success,
|
||||
}));
|
||||
|
||||
metrics.security = {
|
||||
blockedIPs: stats.blockedIPs,
|
||||
reputationScores: {},
|
||||
@@ -244,6 +270,7 @@ export class StatsHandler {
|
||||
phishingDetected: stats.phishingDetected,
|
||||
authenticationFailures: stats.authFailures,
|
||||
suspiciousActivities: stats.totalThreatsBlocked,
|
||||
recentEvents,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user