Files
copykar/core/metrics.py
T

80 lines
1.9 KiB
Python

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"]
)
SOURCE_ACTIVITY_TOTAL = Counter(
"copykar_source_activity_total",
"Total posts ingested per source channel",
["channel_id", "title"]
)
TARGET_ACTIVITY_TOTAL = Counter(
"copykar_target_activity_total",
"Total posts published per target channel",
["channel_id", "title"]
)
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"]
)
ADMIN_ACTIONS_TOTAL = Counter(
"copykar_admin_actions_total",
"Total review decisions by admins",
["action"]
)
POSTS_PUBLISHED_TOTAL = Counter(
"copykar_posts_published_total",
"Total posts successfully published to target channels",
["target_channel_id"]
)
ERRORS_TOTAL = Counter(
"copykar_errors_total",
"Total exceptions and errors caught across services",
["service", "error_type"]
)
# 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"]
)
REDIS_QUEUE_SIZE_GAUGE = Gauge(
"copykar_redis_queue_size",
"Current number of posts waiting in Redis incoming queue"
)
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}")