org.llm4s.metrics

Members list

Type members

Classlikes

sealed trait ErrorKind

Stable categorization of LLM errors for metrics.

Stable categorization of LLM errors for metrics.

These are stable labels safe for use in metrics dimensions. Do not use exception class names as they may change.

Attributes

Companion
object
Supertypes
class Object
trait Matchable
class Any
Known subtypes
object Network
object RateLimit
object Timeout
object Unknown
object Validation
Show all
object ErrorKind

Attributes

Companion
trait
Supertypes
trait Sum
trait Mirror
class Object
trait Matchable
class Any
Self type
ErrorKind.type

Minimal algebra for collecting metrics about LLM operations.

Minimal algebra for collecting metrics about LLM operations.

Implementations should be safe: failures must not propagate to callers. All methods should catch and log errors internally without throwing.

Example usage:

val startNanos = System.nanoTime()
client.complete(conversation) match {
 case Right(completion) =>
   val duration = FiniteDuration(System.nanoTime() - startNanos, NANOSECONDS)
   metrics.observeRequest(provider, model, Outcome.Success, duration)
   completion.usage.foreach { u =>
     metrics.addTokens(provider, model, u.promptTokens, u.completionTokens)
   }
 case Left(error) =>
   val duration = FiniteDuration(System.nanoTime() - startNanos, NANOSECONDS)
   val errorKind = ErrorKind.fromLLMError(error)
   metrics.observeRequest(provider, model, Outcome.Error(errorKind), duration)
}

Attributes

Companion
object
Supertypes
class Object
trait Matchable
class Any
Known subtypes

Attributes

Companion
trait
Supertypes
class Object
trait Matchable
class Any
Self type
sealed trait Outcome

Outcome of an LLM request.

Outcome of an LLM request.

Attributes

Companion
object
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class Error
object Success
object Outcome

Attributes

Companion
trait
Supertypes
trait Sum
trait Mirror
class Object
trait Matchable
class Any
Self type
Outcome.type
final class PrometheusEndpoint

HTTP endpoint for exposing Prometheus metrics.

HTTP endpoint for exposing Prometheus metrics.

Wraps the Prometheus HTTPServer and provides lifecycle management. Use this to expose metrics at /metrics for Prometheus scraping.

Example:

val registry = new PrometheusRegistry()
val endpointResult = PrometheusEndpoint.start(9090, registry)

endpointResult match {
 case Right(endpoint) =>
   println(s"Metrics at: ${endpoint.url}")
   // ... application runs ...
   endpoint.stop()
 case Left(error) =>
   println(s"Failed to start: ${error.message}")
}

Value parameters

port

Port the server is listening on

server

Underlying HTTP server

Attributes

Companion
object
Supertypes
class Object
trait Matchable
class Any

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
final class PrometheusMetrics(val registry: PrometheusRegistry) extends MetricsCollector

Prometheus implementation of 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
Supertypes
class Object
trait Matchable
class Any

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type