from prometheus_client import Counter, Histogram, Gauge, start_http_server import logging logger = logging.getLogger(__name__) # Counters COLLECTED_POSTS_TOTAL = Counter( "copykar_posts_collected_total", "Total posts collected by the Telethon Userbot", ["source_channel_id"] ) AI_REQUESTS_TOTAL = Counter( "copykar_ai_requests_total", "Total AI API calls made", ["action", "status"] ) DUPLICATES_DETECTED_TOTAL = Counter( "copykar_duplicates_detected_total", "Total duplicate posts detected", ["method"] # "hash" or "ai_semantic" ) ADMIN_ACTIONS_TOTAL = Counter( "copykar_admin_actions_total", "Total review decisions by admins", ["action"] # "approved", "rejected", "routed" ) POSTS_PUBLISHED_TOTAL = Counter( "copykar_posts_published_total", "Total posts successfully published to target channels", ["target_channel_id"] ) # Histograms AI_LATENCY_SECONDS = Histogram( "copykar_ai_latency_seconds", "Time taken for AI API operations", ["action"] ) # Gauges QUEUE_POSTS_GAUGE = Gauge( "copykar_posts_queue_gauge", "Number of posts currently in various queue states", ["status"] ) def start_metrics_server(port: int = 8000): try: start_http_server(port) logger.info(f"Prometheus metrics server running on port {port}") except Exception as e: logger.error(f"Failed to start Prometheus metrics server: {e}")