Spaces:
Build error
Build error
File size: 2,561 Bytes
f6b5e8d 24f36b8 f6b5e8d 3541440 f6b5e8d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# metrics.py
import os
import time
from prometheus_client import (
Counter,
Histogram,
Gauge,
Summary,
start_http_server,
)
SERVICE_NAME = os.getenv("SERVICE_NAME", "chord-bot-api")
#tot requests by status
REQUEST_COUNT = Counter(
"chordbot_requests_total",
"Total number of Chord-Bot requests",
["service", "endpoint", "status"], # status: success | invalid_input | error
)
#latency
REQUEST_LATENCY = Histogram(
"chordbot_request_latency_seconds",
"Latency of Chord-Bot requests in seconds",
["service", "endpoint"],
)
# Number of note tokens per request
NOTES_PER_REQUEST = Histogram(
"chordbot_notes_per_request",
"Number of note tokens received per request",
["service", "endpoint"],
buckets=(1, 2, 3, 4, 5, 6, 8, 12, 16),
)
INVALID_REQUESTS = Counter(
"chordbot_invalid_requests_total",
"Number of Chord-Bot requests with invalid input",
["service", "endpoint"],
)
#counts of predicted chords by label
CHORD_PREDICTIONS = Counter(
"chordbot_chord_predictions_total",
"Number of times each chord label is predicted",
["service", "endpoint", "chord_label"],
)
#how many requests
ACTIVE_REQUESTS = Gauge(
"chordbot_active_requests",
"Number of active Chord-Bot requests being processed",
["service", "endpoint"],
)
def init_metrics(port: int = 8000, service_name: str | None = None) -> None:
global SERVICE_NAME
if service_name is not None:
SERVICE_NAME = service_name
#expose on http://0.0.0.0:port/metrics
start_http_server(port)
print(f"[metrics] Prometheus metrics server started on :{port} for service={SERVICE_NAME}")
def instrument_chord_request(endpoint: str, note_tokens, chord_label: str | None, status: str, elapsed_seconds: float) -> None:
REQUEST_COUNT.labels(
service=SERVICE_NAME,
endpoint=endpoint,
status=status,
).inc()
REQUEST_LATENCY.labels(
service=SERVICE_NAME,
endpoint=endpoint,
).observe(elapsed_seconds)
if note_tokens is not None:
NOTES_PER_REQUEST.labels(
service=SERVICE_NAME,
endpoint=endpoint,
).observe(len(note_tokens))
if status == "invalid_input":
INVALID_REQUESTS.labels(
service=SERVICE_NAME,
endpoint=endpoint,
).inc()
if status == "success" and chord_label is not None:
CHORD_PREDICTIONS.labels(
service=SERVICE_NAME,
endpoint=endpoint,
chord_label=chord_label,
).inc()
|