Design an Observability System

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-12-15

Problem Statement

Designing an observability system to monitor the health, performance, and reliability of distributed applications. Focus is on metrics collection, logging, tracing, alerting, and modular design.


✅ Functional Requirements


1.Metrics Collection
•Collect metrics from multiple services (CPU, memory, response time, request counts).
•Support custom application metrics (business-specific KPIs).
•Store metrics with timestamps for historical analysis.
2.Logging
•Capture logs from applications and services.
•Support structured logging (JSON, key-value pairs).
•Filter and query logs by service, severity, or timestamp.
3.Tracing
•Track requests across multiple services.
•Correlate logs and metrics using trace IDs.
•Measure latency and identify bottlenecks in request flows.
4.Alerting & Notification
•Define rules for alerts (thresholds, anomalies).
•Notify engineers via email, Slack, or other channels.
•Support alert silencing and deduplication.
5.Visualization
•Display metrics, logs, and traces in dashboards.
•Support graphs, heatmaps, and timelines.
•Allow drill-down from high-level metrics to raw logs/traces.
6.Data Retention & Storage
•Store metrics, logs, and traces efficiently.
•Support configurable retention policies.
•Provide quick retrieval for recent data, archival for old data.


🔐 Constraints & Assumptions


•Focus on LLD, class design, and modular architecture, not on low-level storage optimizations.
•Assume agents or SDKs are used to collect metrics and logs from applications.
•High throughput: system should handle thousands of events per second.


🧱 Key Components & Classes


1.Metric Class
1 2class Metric { 3private: 4 string metricName; 5 double value; 6 string serviceName; 7 DateTime timestamp; 8public: 9 double getValue(); 10 string getServiceName(); 11};
1.LogEntry Class
1 2class LogEntry { 3private: 4 string message; 5 string severity; // INFO, WARN, ERROR 6 string serviceName; 7 DateTime timestamp; 8 string traceId; // optional 9public: 10 string getMessage(); 11};
1.Trace Class
1 2class Trace { 3private: 4 string traceId; 5 vector<Span*> spans; 6public: 7 void addSpan(Span* span); 8}; 9class Span { 10private: 11 string spanId; 12 string operationName; 13 DateTime startTime; 14 DateTime endTime; 15public: 16 double getDuration(); 17};
1.Collector Class
•Receives metrics, logs, and traces from agents.
1 2class Collector { 3public: 4 void receiveMetric(Metric* metric); 5 void receiveLog(LogEntry* log); 6 void receiveTrace(Trace* trace); 7};
1.StorageManager Class
•Handles storage and retrieval of metrics, logs, and traces.
1 2class StorageManager { 3public: 4 void storeMetric(Metric* metric); 5 void storeLog(LogEntry* log); 6 void storeTrace(Trace* trace); 7 vector<Metric*> queryMetrics(string serviceName, DateTime start, DateTime end); 8 vector<LogEntry*> queryLogs(string serviceName, DateTime start, DateTime end); 9};
1.AlertManager Class
•Evaluates thresholds and triggers notifications.
1 2class AlertManager { 3public: 4 void addAlertRule(AlertRule* rule); 5 void evaluateAlerts(); 6};
1.Dashboard Class
•Provides visualization of metrics, logs, and traces.
1 2class Dashboard { 3public: 4 void displayMetrics(vector<Metric*> metrics); 5 void displayLogs(vector<LogEntry*> logs); 6 void displayTraces(vector<Trace*> traces); 7};


🔄 Example Workflow


1.Application generates metrics, logs, and traces → sent to Collector.
2.Collector stores them using StorageManager.
3.AlertManager continuously evaluates rules on metrics → triggers notifications if thresholds exceeded.
4.Admin views dashboards → Dashboard fetches metrics, logs, and traces for visualization.


🧪 Expected Deliverables


•Class diagram showing Metric, LogEntry, Trace, Collector, StorageManager, AlertManager, and Dashboard.
•Implementation of data collection, storage, alert evaluation, and dashboard retrieval.
•Test cases:
•Collect and store metrics/logs/traces
•Evaluate alert rules
•Query metrics and logs for a given time range
•Visualize metrics on dashboards


💡 Discussion Points


•Scalability: high ingestion rate, partitioning, and sharding.
•Concurrency: handling multiple service events in parallel.
•Extensibility: adding new metric types or alerting mechanisms.
•Retention & archival: efficient storage and retrieval.


Solution

Comments