"""Prometheus метрики и rate limiter для TTS API.""" import time from prometheus_client import Counter, Histogram, generate_latest, CONTENT_TYPE_LATEST from slowapi import Limiter from slowapi.util import get_remote_address # ─── Prometheus метрики ───────────────────────────────────────── _request_counter = Counter( "tts_requests_total", "Общее число запросов к TTS API", ["endpoint", "status"], ) _synthesis_latency = Histogram( "tts_synthesis_seconds", "Время синтеза речи", ["voice"], ) _audio_size_bytes = Histogram( "tts_audio_size_bytes", "Размер сгенерированного аудио в байтах", ["voice"], ) _errors_total = Counter( "tts_errors_total", "Количество ошибок синтеза", ["error_type"], ) # ─── Rate Limiter ───────────────────────────────────────────────── limiter = Limiter(key_func=get_remote_address, default_limits=["100 per minute"])