类:SpringAIObservabilityHandler

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

public class SpringAIObservabilityHandler extends Object
使用 Micrometer 处理 Spring AI 集成中的可观测性(observability)相关功能。

该类提供了以下功能:

  • 通过 Micrometer 收集请求延迟、令牌数量和错误率的指标数据。
  • 支持请求/响应日志记录功能,并可配置是否包含相关内容(即是否在日志中显示这些内容)。
  • 针对流式请求和非流式请求的性能监控
  • 可以与任何支持 Micrometer 标准的指标后端(如 Prometheus、Datadog 等)进行集成。
  • 构造函数详细信息

    • SpringAIObservabilityHandler

      public SpringAIObservabilityHandler(SpringAIProperties.Observability config)
      创建一个可观测性处理程序(observability handler),该处理程序使用默认的 SimpleMeterRegistry。
      Parameters:
      config - the observability configuration
    • SpringAIObservabilityHandler

      public SpringAIObservabilityHandler(SpringAIProperties.Observability config, io.micrometer.core.instrument.MeterRegistry meterRegistry)
      创建一个可观测性处理程序(observability handler),该处理程序使用自定义的 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)
      记录请求的开始时间。
      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)
      用于记录成功请求的完成情况。
      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)
      记录一个失败的请求。
      Parameters:
      context - the request context
      error - the error that occurred
    • logRequest

      public void logRequest(String content, String modelName)
      如果启用了该功能,系统会记录请求的内容。
      Parameters:
      content - the request content
      modelName - the model name
    • logResponse

      public void logResponse(String content, String modelName)
      如果启用了该功能,系统会记录响应内容(即服务器返回的数据)。
      Parameters:
      content - the response content
      modelName - the model name
    • getMeterRegistry

      public io.micrometer.core.instrument.MeterRegistry getMeterRegistry()
      用于获取 Micrometer MeterRegistry,以便直接访问相关指标数据。

      这允许用户将指标数据导出到任何与 Micrometer 兼容的后端系统(如 Prometheus、Datadog、CloudWatch 等),或通过编程方式查询这些指标数据。

      Returns:
      the MeterRegistry instance