feat: add Prometheus metrics + fix CI/CD health check port
- Add prometheus-client dependency - Create middleware/metrics.py with PrometheusMiddleware - Expose /metrics endpoint in Prometheus text format - Track http_requests_total, translation_total, translation_duration_seconds, file_size_bytes - Instrument translate routes with record_translation() and record_file_size() - Fix deploy.yml health check: localhost:8000 -> localhost:8001 (Portainer conflict) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -60,6 +60,7 @@ from schemas.translation import (
|
||||
)
|
||||
from schemas.errors import ErrorResponse
|
||||
from utils.file_handler import FileHandler
|
||||
from middleware.metrics import record_translation, record_file_size
|
||||
from services.progress_tracker import ProgressTracker
|
||||
from services.storage_tracker import storage_tracker
|
||||
from core.redis import set_job_status_async, get_job_status_async
|
||||
@@ -696,6 +697,10 @@ async def translate_document_v1(
|
||||
|
||||
_cleanup_old_jobs()
|
||||
|
||||
# Record file size metric
|
||||
if file_extension and file_size:
|
||||
record_file_size(file_extension, file_size)
|
||||
|
||||
_translation_jobs[job_id] = {
|
||||
"id": job_id,
|
||||
"status": "queued",
|
||||
@@ -1124,10 +1129,15 @@ async def _run_translation_job(
|
||||
logger.warning(f"Job {job_id}: watermark failed: {wm_err}")
|
||||
|
||||
tracker.set_completed(str(output_path))
|
||||
# Record translation metric
|
||||
duration = time.time() - time.mktime(datetime.fromisoformat(job["created_at"].replace("Z", "+00:00")).timetuple())
|
||||
record_translation(provider=provider, file_type=file_extension or "unknown", duration=duration, status="success")
|
||||
logger.info(f"Job {job_id}: Completed successfully")
|
||||
|
||||
except Exception as e:
|
||||
tracker.set_error(str(e))
|
||||
# Record translation failure metric
|
||||
record_translation(provider=provider, file_type=file_extension or "unknown", duration=0, status="error")
|
||||
logger.error(f"Job {job_id}: Failed - {e}")
|
||||
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user