mcp-adr-analysis-server
Version:
MCP server for analyzing Architectural Decision Records and project architecture
249 lines • 6.25 kB
TypeScript
/**
* Monitoring and Analytics Framework
*
* Provides comprehensive monitoring and analytics for MCP server:
* - Request/response tracking
* - Performance metrics (latency, throughput)
* - Error tracking and reporting
* - Resource usage analytics
* - Cache performance metrics
* - Tool execution metrics
* - Health checks
* - Metrics aggregation and reporting
*/
/**
* Metric types for categorization
*/
export declare enum MetricType {
COUNTER = "counter",
GAUGE = "gauge",
HISTOGRAM = "histogram",
TIMER = "timer"
}
/**
* Metric category for organization
*/
export declare enum MetricCategory {
REQUEST = "request",
RESOURCE = "resource",
TOOL = "tool",
CACHE = "cache",
ERROR = "error",
PERFORMANCE = "performance",
SYSTEM = "system"
}
/**
* Individual metric data point
*/
export interface Metric {
name: string;
type: MetricType;
category: MetricCategory;
value: number;
timestamp: string;
tags?: Record<string, string>;
metadata?: Record<string, any>;
}
/**
* Aggregated metric statistics
*/
export interface MetricStats {
count: number;
sum: number;
min: number;
max: number;
avg: number;
p50: number;
p95: number;
p99: number;
}
/**
* Health check result
*/
export interface HealthCheck {
name: string;
status: 'healthy' | 'degraded' | 'unhealthy';
message?: string;
timestamp: string;
duration: number;
metadata?: Record<string, any>;
}
/**
* Request tracking data
*/
export interface RequestMetrics {
requestId: string;
type: 'tool' | 'resource' | 'prompt';
name: string;
startTime: string;
endTime?: string;
duration?: number;
status: 'pending' | 'success' | 'error';
error?: string;
metadata?: Record<string, any>;
}
/**
* Performance snapshot
*/
export interface PerformanceSnapshot {
timestamp: string;
requests: {
total: number;
success: number;
error: number;
pending: number;
};
latency: {
p50: number;
p95: number;
p99: number;
avg: number;
};
throughput: {
requestsPerSecond: number;
requestsPerMinute: number;
};
cache: {
hitRate: number;
totalHits: number;
totalMisses: number;
};
errors: {
total: number;
byType: Record<string, number>;
};
}
/**
* Monitoring configuration
*/
export interface MonitoringConfig {
enabled: boolean;
metricsRetentionMs: number;
aggregationIntervalMs: number;
maxMetricsInMemory: number;
enableHealthChecks: boolean;
healthCheckIntervalMs: number;
}
/**
* Monitoring manager class
*/
export declare class MonitoringManager {
private config;
private metrics;
private requests;
private healthChecks;
private aggregationTimer;
private healthCheckTimer;
private startTime;
constructor(config?: Partial<MonitoringConfig>);
/**
* Record a counter metric (incrementing value)
*/
recordCounter(name: string, category: MetricCategory, value?: number, tags?: Record<string, string>, metadata?: Record<string, any>): void;
/**
* Record a gauge metric (point-in-time value)
*/
recordGauge(name: string, category: MetricCategory, value: number, tags?: Record<string, string>, metadata?: Record<string, any>): void;
/**
* Record a histogram value (for distribution analysis)
*/
recordHistogram(name: string, category: MetricCategory, value: number, tags?: Record<string, string>, metadata?: Record<string, any>): void;
/**
* Start tracking a request
*/
startRequest(requestId: string, type: 'tool' | 'resource' | 'prompt', name: string, metadata?: Record<string, any>): void;
/**
* Complete a request successfully
*/
completeRequest(requestId: string, metadata?: Record<string, any>): void;
/**
* Mark a request as failed
*/
failRequest(requestId: string, error: string, metadata?: Record<string, any>): void;
/**
* Record cache hit
*/
recordCacheHit(cacheKey: string): void;
/**
* Record cache miss
*/
recordCacheMiss(cacheKey: string): void;
/**
* Register a health check
*/
registerHealthCheck(_name: string, _check: () => Promise<{
status: 'healthy' | 'degraded' | 'unhealthy';
message?: string;
}>): void;
/**
* Run a health check
*/
runHealthCheck(name: string, check: () => Promise<{
status: 'healthy' | 'degraded' | 'unhealthy';
message?: string;
}>): Promise<HealthCheck>;
/**
* Get all health checks
*/
getHealthChecks(): HealthCheck[];
/**
* Get overall health status
*/
getHealthStatus(): 'healthy' | 'degraded' | 'unhealthy';
/**
* Get performance snapshot
*/
getPerformanceSnapshot(): PerformanceSnapshot;
/**
* Get metric statistics by name and category
*/
getMetricStats(name: string, category: MetricCategory): MetricStats | null;
/**
* Get all metrics
*/
getMetrics(): Metric[];
/**
* Get metrics by category
*/
getMetricsByCategory(category: MetricCategory): Metric[];
/**
* Get recent requests
*/
getRecentRequests(limit?: number): RequestMetrics[];
/**
* Clear old metrics based on retention policy
*/
cleanup(): number;
/**
* Reset all metrics
*/
reset(): void;
/**
* Stop monitoring
*/
stop(): void;
/**
* Get uptime in milliseconds
*/
getUptime(): number;
/**
* Export metrics to JSON
*/
exportMetrics(): string;
private addMetric;
private getMetricSum;
private calculatePercentiles;
private calculateStats;
private percentile;
private startAggregation;
private startHealthChecks;
}
/**
* Singleton monitoring instance
*/
export declare const monitoring: MonitoringManager;
/**
* Helper to track async operations
*/
export declare function trackOperation<T>(operation: () => Promise<T>, name: string, category: MetricCategory, tags?: Record<string, string>): Promise<T>;
//# sourceMappingURL=monitoring.d.ts.map