LLMTracingAdapter

abstract class LLMTracingAdapter(genAISystem: String)(source)

Base adapter for tracing LLM provider API interactions using OpenTelemetry.

This abstract class provides the foundation for implementing provider-specific tracing adapters. It handles HTTP request/response interception, attribute extraction, error handling, and streaming support. Subclasses implement provider-specific parsing logic for different API formats (OpenAI, Anthropic, Gemini, etc.).

Usage

Extend this class to create a provider-specific adapter:

class AnthropicLLMTracingAdapter : LLMTracingAdapter(GenAiSystemIncubatingValues.ANTHROPIC) {
override fun getRequestBodyAttributes(span: Span, request: Request) {
// Parse Anthropic-specific request format
}
override fun getResponseBodyAttributes(span: Span, response: Response) {
// Parse Anthropic-specific response format
}
override fun getSpanName(request: Request) = "Anthropic-generation"
override fun isStreamingRequest(request: Request) = false
override fun handleStreaming(span: Span, url: Url, events: String) = Unit
}

Use with the instrument() function:

val client = instrument(HttpClient(), AnthropicLLMTracingAdapter())

Parameters

genAISystem

The name of the GenAI system (e.g., "openai", "anthropic", "gemini")

Constructors

Link copied to clipboard
constructor(genAISystem: String)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
abstract fun getSpanName(request: TracyHttpRequest): String
Link copied to clipboard
abstract fun handleStreaming(span: Span, url: TracyHttpUrl, events: String)
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard