PrometheusMetrics

org.llm4s.metrics.PrometheusMetrics
See thePrometheusMetrics companion object
final class PrometheusMetrics(val registry: PrometheusRegistry) extends MetricsCollector

Prometheus implementation of MetricsCollector.

Tracks request volumes, token usage, errors, and latency across different providers and models using Prometheus metrics.

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()
}

Value parameters

registry

Prometheus collector registry

Attributes

Companion
object
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

override def addTokens(provider: String, model: String, inputTokens: Long, outputTokens: Long): Unit

Record token usage.

Record token usage.

Safe: catches and logs any Prometheus errors without propagating.

Attributes

Definition Classes
override def observeRequest(provider: String, model: String, outcome: Outcome, duration: FiniteDuration): Unit

Record an LLM request with its outcome and duration.

Record an LLM request with its outcome and duration.

Safe: catches and logs any Prometheus errors without propagating.

Attributes

Definition Classes
override def recordCost(provider: String, model: String, costUsd: Double): Unit

Record estimated cost in USD.

Record estimated cost in USD.

Safe: catches and logs any Prometheus errors without propagating.

Attributes

Definition Classes

Inherited methods

def recordCircuitBreakerTransition(provider: String, newState: String): Unit

Record circuit breaker state transition.

Record circuit breaker state transition.

Value parameters

newState

New circuit breaker state ("open", "closed", "half-open")

provider

Provider name

Attributes

Inherited from:
MetricsCollector
def recordError(errorKind: ErrorKind, provider: String): Unit

Record a generic error for metrics (when full request tracking not applicable).

Record a generic error for metrics (when full request tracking not applicable).

Value parameters

errorKind

Type of error

provider

Provider name

Attributes

Inherited from:
MetricsCollector
def recordRetryAttempt(provider: String, attemptNumber: Int): Unit

Record a retry attempt for reliability tracking.

Record a retry attempt for reliability tracking.

Value parameters

attemptNumber

Which retry attempt (1 = first retry, 2 = second, etc.)

provider

Provider name

Attributes

Inherited from:
MetricsCollector