Tracks request volumes, token usage, errors, and latency across different providers and models using Prometheus metrics.
Registered metric names:
llm4s_requests_total
llm4s_tokens_total
llm4s_cost_usd_total
llm4s_errors_total
llm4s_request_duration_seconds
llm4s_image_generations_total
llm4s_images_generated_total
llm4s_image_generation_duration_seconds
llm4s_image_generation_cost_usd_total
llm4s_image_generation_errors_total
All operations are wrapped in try-catch to ensure metric failures never propagate to callers. This implementation is thread-safe.
Example usage:
val registry = new PrometheusRegistry()
val metrics = new PrometheusMetrics(registry)
// Use with endpoint
PrometheusEndpoint.start(9090, registry).foreach { endpoint =>
// ... use metrics ...
endpoint.stop()
}
Updates llm4s_image_generations_total and llm4s_image_generation_duration_seconds. Successful operations with images also update llm4s_images_generated_total; error outcomes update llm4s_errors_total and llm4s_image_generation_errors_total.
Safe: catches and logs any Prometheus errors without propagating.