Class MicrometerMetricsEmbeddingModelListener

java.lang.Object
dev.langchain4j.micrometer.metrics.listeners.MicrometerMetricsEmbeddingModelListener
All Implemented Interfaces:
EmbeddingModelListener

@Experimental public class MicrometerMetricsEmbeddingModelListener extends Object implements EmbeddingModelListener
An EmbeddingModelListener that uses a Micrometer MeterRegistry to collect metrics about embedding model interactions following OpenTelemetry Semantic Conventions for Generative AI.

This listener records token usage metrics when an embedding model response is received, using a DistributionSummary consistent with the OpenTelemetry Semantic Conventions for gen_ai.client.token.usage. Embedding calls have no output tokens, so gen_ai.token.type is always input.

It also records the duration of every embedding operation as gen_ai.client.operation.duration using a Timer, consistent with the OpenTelemetry Semantic Conventions for gen_ai.client.operation.duration. The duration is recorded for both successful and failed calls, with an outcome tag set to SUCCESS or ERROR, and an error.type tag set to none or to the class name of the exception. A failed call has no response, so its gen_ai.response.model is unknown. Every duration carries the same set of tags, as some registries (e.g., Prometheus) require that.

Histogram publishing and bucket boundaries are not configured by this listener. Users can enable histograms and set bucket boundaries through their MeterRegistry configuration (e.g., via Spring Boot properties or MeterFilter).

Note: The MicrometerMetricsEmbeddingModelListener must be instantiated separately (e.g., via Spring Boot auto-configuration or manual instantiation).

  • Constructor Details

    • MicrometerMetricsEmbeddingModelListener

      public MicrometerMetricsEmbeddingModelListener(io.micrometer.core.instrument.MeterRegistry meterRegistry)
      Parameters:
      meterRegistry - the MeterRegistry to register metrics with
  • Method Details

    • onRequest

      public void onRequest(EmbeddingModelRequestContext requestContext)
      Description copied from interface: EmbeddingModelListener
      This method is called before the request is executed against the embedding model.
      Specified by:
      onRequest in interface EmbeddingModelListener
      Parameters:
      requestContext - The request context. It contains the input and attributes. The attributes can be used to pass data between methods of this listener or between multiple listeners.
    • onResponse

      public void onResponse(EmbeddingModelResponseContext responseContext)
      Description copied from interface: EmbeddingModelListener
      This method is called after a successful embedding operation completes.
      Specified by:
      onResponse in interface EmbeddingModelListener
      Parameters:
      responseContext - The response context. It contains the response, corresponding request and attributes. The attributes can be used to pass data between methods of this listener or between multiple listeners.
    • onError

      public void onError(EmbeddingModelErrorContext errorContext)
      Description copied from interface: EmbeddingModelListener
      This method is called when an error occurs during interaction with the embedding model.
      Specified by:
      onError in interface EmbeddingModelListener
      Parameters:
      errorContext - The error context. It contains the error, corresponding request and attributes. The attributes can be used to pass data between methods of this listener or between multiple listeners.