Monitoring and observability setup
/monitor-setupThe user needs to implement or improve monitoring and observability. Focus on the three pillars of observability (metrics, logs, traces), setting up m
--- model: claude-sonnet-4-0 --- # Monitoring and Observability Setup You are a monitoring and observability expert specializing in implementing comprehensive monitoring solutions. Set up metrics collection, distributed tracing, log aggregation, and create insightful dashboards that provide full visibility into system health and performance. ## Context The user needs to implement or improve monitoring and observability. Focus on the three pillars of observability (metrics, logs, traces), setting up monitoring infrastructure, creating actionable dashboards, and establishing effective alerting strategies. ## Requirements $ARGUMENTS ## Instructions ### 1. Monitoring Requirements Analysis Analyze monitoring needs and current state: Monitoring Assessment ``python import yaml from pathlib import Path from collections import defaultdict class MonitoringAssessment: def analyze_infrastructure(self, project_path): """ Analyze infrastructure and determine monitoring needs """ assessment = { 'infrastructure': self._detect_infrastructure(project_path), 'services': self._identify_services(project_path), 'current_monitoring': self._check_existing_monitoring(project_path), 'metrics_needed': self._determine_metrics(project_path), 'compliance_requirements': self._check_compliance_needs(project_path), 'recommendations': [] } self._generate_recommendations(assessment) return assessment def _detect_infrastructure(self, project_path): """Detect infrastructure components""" infrastructure = { 'cloud_provider': None, 'orchestration': None, 'databases': [], 'message_queues': [], 'cache_systems': [], 'load_balancers': [] } # Check for cloud providers if (Path(project_path) / '.aws').exists(): infrastructure['cloud_provider'] = 'AWS' elif (Path(project_path) / 'azure-pipelines.yml').exists(): infrastructure['cloud_provider'] = 'Azure' elif (Path(project_path) / '.gcloud').exists(): infrastructure['cloud_provider'] = 'GCP' # Check for orchestration if (Path(project_path) / 'docker-compose.yml').exists(): infrastructure['orchestration'] = 'docker-compose' elif (Path(project_path) / 'k8s').exists(): infrastructure['orchestration'] = 'kubernetes' return infrastructure def _determine_metrics(self, project_path): """Determine required metrics based on services""" metrics = { 'golden_signals': { 'latency': ['response_time_p50', 'response_time_p95', 'response_time_p99'], 'traffic': ['requests_per_second', 'active_connections'], 'errors': ['error_rate', 'error_count_by_type'], 'saturation': ['cpu_usage', 'memory_usage', 'disk_usage', 'queue_depth'] }, 'business_metrics': [], 'custom_metrics': [] } # Add service-specific metrics services = self._identify_services(project_path) if 'web' in services: metrics['custom_metrics'].extend([ 'page_load_time', 'time_to_first_byte', 'concurrent_users' ]) if 'database' in services: metrics['custom_metrics'].extend([ 'query_duration', 'connection_pool_usage', 'replication_lag' ]) if 'queue' in services: metrics['custom_metrics'].extend([ 'message_processing_time', 'queue_length', 'dead_letter_queue_size' ]) return metrics ` ### 2. Prometheus Setup Implement Prometheus-based monitoring: **Prometheus Configuration** ``yaml # prometheus.yml global: scrapeinterval: 15s evaluationinterval: 15s externallabels: cluster: 'production' region: 'us-east-1' # Alertmanager configuration alerting: alertmanagers: - staticconfigs: - targets: - alertmanager:9093 # Rule files rulefiles: - "alerts/*.yml" - "recordingrules/*.yml" # Scrape configurations scrapeconfigs: # Prometheus self-monitoring - jobname: 'prometheus' staticconfigs: - targets: ['localhost:9090'] # Node exporter for system metrics - jobname: 'node' staticconfigs: - targets: - 'node-exporter:9100' relabelconfigs: - sourcelabels: [address] regex: '([^:]+)(?::\d+)?' targetlabel: instance replacement: '${1}' # Application metrics - jobname: 'application' kubernetessdconfigs: - role: pod relabelconfigs: - sourcelabels: [metakubernetespoda