MicrometerMetrics

org.llm4s.metrics.MicrometerMetrics
See theMicrometerMetrics companion object
final class MicrometerMetrics(registry: MeterRegistry) extends MetricsCollector

A MetricsCollector that records into a Micrometer io.micrometer.core.instrument.MeterRegistry.

Use it when the application already has a registry (Spring Boot's Actuator registry, a Quarkus registry, a PrometheusMeterRegistry of your own) and the LLM metrics should appear next to the rest:

val metrics = new MicrometerMetrics(registry)
val client  = LLMConnect.getClient(config, LlmClientOptions(metrics = metrics))

The meter names follow the series that llm4s-observability-prometheus exposes, so a dashboard built on llm4s_requests_total or llm4s_request_duration_seconds keeps working when the registry is exported as Prometheus (Micrometer's Prometheus naming turns llm4s.requests into llm4s_requests_total). See MicrometerMetrics.Names for every meter and its tags; the full table is in the monitoring guide.

Properties:

  • No global state: the registry is the only state, and two collectors on two registries are independent.
  • Safe: like every MetricsCollector, no method throws. A failure inside Micrometer (for example a MeterFilter that rejects a meter) is logged at WARN and dropped.
  • Bounded tag cardinality: tags are provider, model, a fixed outcome or error label and a fixed token type, and a blank provider or model is recorded as unknown. A generic Metric's labels are bounded as MetricsCollector.increment describes. Prompts, user ids, request ids and other unbounded values are never tags. provider and model come from the caller, so they are capped too: after 64 distinct providers or 256 distinct models on one collector, further values are recorded as other (logged once at WARN), so arbitrary model strings cannot grow the registry without limit.
  • Thread-safe: Micrometer meters are, and the only mutable state this class holds is the two sets of provider and model tag values admitted under the caps above, which are concurrent sets guarded for the check-then-add.

Difference from PrometheusMetrics: it ignores negative or non-finite amounts instead of passing them to the registry. ReliableClient's retries, circuit-breaker transitions and errors are generic metrics (org.llm4s.reliability.ReliabilityMetrics), recorded through increment here as in every backend. Its errors go to their own meter, MicrometerMetrics.Names.ReliabilityErrors, never to llm4s.errors: ReliableClient reports each failed attempt through the provider client's observeRequest and then the final failure as that metric, so sharing one counter would count the same failure twice when both report to this collector. llm4s.errors therefore means exactly what llm4s_errors_total means in the Prometheus module: failed requests, and what a module adds through MetricsCollector.Errors, as llm4s-image does for a failed image generation. The image metrics themselves are generic too (ImageMetrics in llm4s-image), under the names they always had: llm4s.image.generations, llm4s.images.generated, llm4s.image.generation.duration, llm4s.image.generation.cost.usd and llm4s.image.generation.errors.

This module is Beta: the meter names are intended to be stable, but they are not covered by the 1.x compatibility promise.

Value parameters

registry

the registry to record into; must not be null

Attributes

Companion
object
Source
MicrometerMetrics.scala
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.

Implementations typically record input and output tokens separately so dashboards can show prompt and completion usage independently.

Value parameters

inputTokens

Number of input/prompt tokens

model

Model name

outputTokens

Number of output/completion tokens

provider

Provider name

Attributes

Definition Classes
Source
MicrometerMetrics.scala
override def increment(metric: Counter, labelValues: Seq[String], amount: Double): Unit

Adds amount to counter metric - a Micrometer counter of the metric's name, which Prometheus naming exposes as <name>_total - dropping a negative or non-finite amount.

Adds amount to counter metric - a Micrometer counter of the metric's name, which Prometheus naming exposes as <name>_total - dropping a negative or non-finite amount.

Attributes

Definition Classes
Source
MicrometerMetrics.scala
override def observe(metric: Timer, labelValues: Seq[String], duration: FiniteDuration): Unit

Records duration in timer metric: a Micrometer timer publishing the metric's buckets as its SLOs.

Records duration in timer metric: a Micrometer timer publishing the metric's buckets as its SLOs.

Attributes

Definition Classes
Source
MicrometerMetrics.scala
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.

The outcome dimension separates successful requests from failures. Error outcomes carry a stable ErrorKind such as ErrorKind.RateLimit, ErrorKind.Timeout, or ErrorKind.ServiceError so metrics backends can aggregate failures without depending on exception class names.

Value parameters

duration

Request duration

model

Model name (e.g., "gpt-4o", "claude-3-5-sonnet-latest")

outcome

Success or Error with stable error kind

provider

Provider name (e.g., "openai", "anthropic", "ollama")

Attributes

Definition Classes
Source
MicrometerMetrics.scala
override def recordCost(provider: String, model: String, costUsd: Double): Unit

Record estimated cost in USD.

Record estimated cost in USD.

Use this after pricing metadata is available for a request or image generation operation. Implementations should treat the value as an additive counter.

Value parameters

costUsd

Estimated cost in USD

model

Model name

provider

Provider name

Attributes

Definition Classes
Source
MicrometerMetrics.scala
override def set(metric: Gauge, labelValues: Seq[String], value: Double): Unit

Sets gauge metric to value, dropping a non-finite one.

Sets gauge metric to value, dropping a non-finite one.

Attributes

Definition Classes
Source
MicrometerMetrics.scala