Class MicrometerMetricsChatModelListener
java.lang.Object
dev.langchain4j.micrometer.metrics.listeners.MicrometerMetricsChatModelListener
- All Implemented Interfaces:
ChatModelListener
@Experimental
public class MicrometerMetricsChatModelListener
extends Object
implements ChatModelListener
A
ChatModelListener that uses a Micrometer MeterRegistry to collect metrics
about chat model interactions following OpenTelemetry Semantic Conventions for Generative AI.
This listener records token usage metrics (input and output tokens) when a chat model response is received,
using a DistributionSummary consistent with the
OpenTelemetry Semantic Conventions for gen_ai.client.token.usage.
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 MicrometerMetricsChatModelListener
must be instantiated separately (e.g., via Spring Boot auto-configuration or manual instantiation).
-
Constructor Summary
ConstructorsConstructorDescriptionMicrometerMetricsChatModelListener(io.micrometer.core.instrument.MeterRegistry meterRegistry) Creates a newMicrometerMetricsChatModelListener. -
Method Summary
Modifier and TypeMethodDescriptionvoidonError(ChatModelErrorContext errorContext) This method is called when an error occurs during interaction with the model.voidonRequest(ChatModelRequestContext requestContext) This method is called before the request is sent to the model.voidonResponse(ChatModelResponseContext responseContext) This method is called after the response is received from the model.
-
Constructor Details
-
MicrometerMetricsChatModelListener
public MicrometerMetricsChatModelListener(io.micrometer.core.instrument.MeterRegistry meterRegistry) Creates a newMicrometerMetricsChatModelListener.- Parameters:
meterRegistry- theMeterRegistryto register metrics with
-
-
Method Details
-
onRequest
Description copied from interface:ChatModelListenerThis method is called before the request is sent to the model.- Specified by:
onRequestin interfaceChatModelListener- Parameters:
requestContext- The request context. It contains theChatRequestand attributes. The attributes can be used to pass data between methods of this listener or between multiple listeners.
-
onResponse
Description copied from interface:ChatModelListenerThis method is called after the response is received from the model.- Specified by:
onResponsein interfaceChatModelListener- Parameters:
responseContext- The response context. It containsChatResponse, correspondingChatRequestand attributes. The attributes can be used to pass data between methods of this listener or between multiple listeners.
-
onError
Description copied from interface:ChatModelListenerThis method is called when an error occurs during interaction with the model.- Specified by:
onErrorin interfaceChatModelListener- Parameters:
errorContext- The error context. It contains the error, correspondingChatRequest, partialChatResponse(if available) and attributes. The attributes can be used to pass data between methods of this listener or between multiple listeners.
-