# Class SpringAIObservabilityHandler

java.lang.Object
com.google.adk.models.springai.observability.SpringAIObservabilityHandler

public class SpringAIObservabilityHandler extends Object
Handles observability features for Spring AI integration using Micrometer.

This class provides:

- Metrics collection for request latency, token counts, and error rates via Micrometer
- Request/response logging with configurable content inclusion
- Performance monitoring for streaming and non-streaming requests
- Integration with any Micrometer-compatible metrics backend (Prometheus, Datadog, etc.)

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static class`
  `SpringAIObservabilityHandler.RequestContext`
  Context for tracking a single request with Micrometer timer.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `SpringAIObservabilityHandler(SpringAIProperties.Observability config)`
  Creates an observability handler with a default SimpleMeterRegistry.
  `SpringAIObservabilityHandler(SpringAIProperties.Observability config, io.micrometer.core.instrument.MeterRegistry meterRegistry)`
  Creates an observability handler with a custom MeterRegistry.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `io.micrometer.core.instrument.MeterRegistry`
  `getMeterRegistry()`
  Gets the Micrometer MeterRegistry for direct access to metrics.
  `void`
  `logRequest(String content, String modelName)`
  Logs request content if enabled.
  `void`
  `logResponse(String content, String modelName)`
  Logs response content if enabled.
  `void`
  `recordError(SpringAIObservabilityHandler.RequestContext context, Throwable error)`
  Records a failed request.
  `void`
  `recordSuccess(SpringAIObservabilityHandler.RequestContext context, int tokenCount, int inputTokens, int outputTokens)`
  Records the completion of a successful request.
  `SpringAIObservabilityHandler.RequestContext`
  `startRequest(String modelName, String requestType)`
  Records the start of a request.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### SpringAIObservabilityHandler

    public SpringAIObservabilityHandler(SpringAIProperties.Observability config)
    Creates an observability handler with a default SimpleMeterRegistry.
    Parameters:
      `config` - the observability configuration

  - ### SpringAIObservabilityHandler

    public SpringAIObservabilityHandler(SpringAIProperties.Observability config, io.micrometer.core.instrument.MeterRegistry meterRegistry)
    Creates an observability handler with a custom MeterRegistry.
    Parameters:
      `config` - the observability configuration
      `meterRegistry` - the Micrometer meter registry to use for metrics

- ## Method Details

  - ### startRequest

    public SpringAIObservabilityHandler.RequestContext startRequest(String modelName, String requestType)
    Records the start of a request.
    Parameters:
      `modelName` - the name of the model being used
      `requestType` - the type of request (e.g., "chat", "streaming")
    Returns:
      a request context for tracking the request

  - ### recordSuccess

    public void recordSuccess(SpringAIObservabilityHandler.RequestContext context, int tokenCount, int inputTokens, int outputTokens)
    Records the completion of a successful request.
    Parameters:
      `context` - the request context
      `tokenCount` - the number of tokens processed (input + output)
      `inputTokens` - the number of input tokens
      `outputTokens` - the number of output tokens

  - ### recordError

    public void recordError(SpringAIObservabilityHandler.RequestContext context, Throwable error)
    Records a failed request.
    Parameters:
      `context` - the request context
      `error` - the error that occurred

  - ### logRequest

    public void logRequest(String content, String modelName)
    Logs request content if enabled.
    Parameters:
      `content` - the request content
      `modelName` - the model name

  - ### logResponse

    public void logResponse(String content, String modelName)
    Logs response content if enabled.
    Parameters:
      `content` - the response content
      `modelName` - the model name

  - ### getMeterRegistry

    public io.micrometer.core.instrument.MeterRegistry getMeterRegistry()
    Gets the Micrometer MeterRegistry for direct access to metrics.

    This allows users to export metrics to any Micrometer-compatible backend (Prometheus, Datadog, CloudWatch, etc.) or query metrics programmatically.

    Returns:
      the MeterRegistry instance