api

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package api provides types for OpenTelemetry tracing support, notably to reduce chance of cyclic imports. No implementations besides no-op are here.

Package api provides types for OpenTelemetry tracing support, notably to reduce chance of cyclic imports. No implementations besides no-op are here.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatCompletionRecorder

type ChatCompletionRecorder interface {
	// StartParams returns the name and options to start the span with.
	//
	// Parameters:
	//   - req: contains the completion request
	//   - body: contains the complete request body.
	//
	// Note: Do not do any expensive data conversions as the span might not be
	// sampled.
	StartParams(req *openai.ChatCompletionRequest, body []byte) (spanName string, opts []trace.SpanStartOption)

	// RecordRequest records request attributes to the span.
	//
	// Parameters:
	//   - req: contains the completion request
	//   - body: contains the complete request body.
	RecordRequest(span trace.Span, req *openai.ChatCompletionRequest, body []byte)

	// RecordResponseChunks records response chunk attributes to the span for streaming response.
	RecordResponseChunks(span trace.Span, chunks []*openai.ChatCompletionResponseChunk)

	// RecordResponse records response attributes to the span for non-streaming response.
	RecordResponse(span trace.Span, resp *openai.ChatCompletionResponse)

	// RecordResponseOnError ends recording the span with an error status.
	RecordResponseOnError(span trace.Span, statusCode int, body []byte)
}

ChatCompletionRecorder records attributes to a span according to a semantic convention.

type ChatCompletionSpan

type ChatCompletionSpan interface {
	// RecordResponseChunk records the response chunk attributes to the span for streaming response.
	RecordResponseChunk(resp *openai.ChatCompletionResponseChunk)

	// RecordResponse records the response attributes to the span for non-streaming response.
	RecordResponse(resp *openai.ChatCompletionResponse)

	// EndSpanOnError finalizes and ends the span with an error status.
	EndSpanOnError(statusCode int, body []byte)

	// EndSpan finalizes and ends the span.
	EndSpan()
}

ChatCompletionSpan represents an OpenAI chat completion.

type ChatCompletionTracer

type ChatCompletionTracer interface {
	// StartSpanAndInjectHeaders starts a span and injects trace context into
	// the header mutation.
	//
	// Parameters:
	//   - ctx: might include a parent span context.
	//   - headers: Incoming HTTP headers used to extract parent trace context.
	//   - headerMutation: The new LLM Span will have its context written to
	//     these headers unless NoopTracing is used.
	//   - req: The OpenAI chat completion request. Used to detect streaming
	//     and record request attributes.
	//
	// Returns nil unless the span is sampled.
	StartSpanAndInjectHeaders(ctx context.Context, headers map[string]string, headerMutation *extprocv3.HeaderMutation, req *openai.ChatCompletionRequest, body []byte) ChatCompletionSpan
}

ChatCompletionTracer creates spans for OpenAI chat completion requests.

type CompletionRecorder added in v0.4.0

type CompletionRecorder interface {
	// StartParams returns the name and options to start the span with.
	//
	// Parameters:
	//   - req: contains the completion request
	//   - body: contains the complete request body.
	//
	// Note: Do not do any expensive data conversions as the span might not be
	// sampled.
	StartParams(req *openai.CompletionRequest, body []byte) (spanName string, opts []trace.SpanStartOption)

	// RecordRequest records request attributes to the span.
	//
	// Parameters:
	//   - req: contains the completion request
	//   - body: contains the complete request body.
	RecordRequest(span trace.Span, req *openai.CompletionRequest, body []byte)

	// RecordResponseChunks records response chunk attributes to the span for streaming response.
	// Note: Completion chunks are full CompletionResponse objects, not deltas like chat.
	RecordResponseChunks(span trace.Span, chunks []*openai.CompletionResponse)

	// RecordResponse records response attributes to the span for non-streaming response.
	RecordResponse(span trace.Span, resp *openai.CompletionResponse)

	// RecordResponseOnError ends recording the span with an error status.
	RecordResponseOnError(span trace.Span, statusCode int, body []byte)
}

CompletionRecorder records attributes to a span according to a semantic convention.

type CompletionSpan added in v0.4.0

type CompletionSpan interface {
	// RecordResponseChunk records the response chunk attributes to the span for streaming response.
	// Note: Unlike chat completions, completion streaming uses full CompletionResponse objects, not deltas.
	RecordResponseChunk(resp *openai.CompletionResponse)

	// RecordResponse records the response attributes to the span for non-streaming response.
	RecordResponse(resp *openai.CompletionResponse)

	// EndSpanOnError finalizes and ends the span with an error status.
	EndSpanOnError(statusCode int, body []byte)

	// EndSpan finalizes and ends the span.
	EndSpan()
}

CompletionSpan represents an OpenAI completion request.

type CompletionTracer added in v0.4.0

type CompletionTracer interface {
	// StartSpanAndInjectHeaders starts a span and injects trace context into
	// the header mutation.
	//
	// Parameters:
	//   - ctx: might include a parent span context.
	//   - headers: Incoming HTTP headers used to extract parent trace context.
	//   - headerMutation: The new LLM Span will have its context written to
	//     these headers unless NoopTracing is used.
	//   - req: The OpenAI completion request. Used to detect streaming
	//     and record request attributes.
	//   - body: contains the original raw request body as a byte slice.
	//
	// Returns nil unless the span is sampled.
	StartSpanAndInjectHeaders(ctx context.Context, headers map[string]string, headerMutation *extprocv3.HeaderMutation, req *openai.CompletionRequest, body []byte) CompletionSpan
}

CompletionTracer creates spans for OpenAI completion requests.

type EmbeddingsRecorder added in v0.4.0

type EmbeddingsRecorder interface {
	// StartParams returns the name and options to start the span with.
	//
	// Parameters:
	//   - req: contains the embeddings request
	//   - body: contains the complete request body.
	//
	// Note: Do not do any expensive data conversions as the span might not be
	// sampled.
	StartParams(req *openai.EmbeddingRequest, body []byte) (spanName string, opts []trace.SpanStartOption)

	// RecordRequest records request attributes to the span.
	//
	// Parameters:
	//   - req: contains the embeddings request
	//   - body: contains the complete request body.
	RecordRequest(span trace.Span, req *openai.EmbeddingRequest, body []byte)

	// RecordResponse records response attributes to the span.
	RecordResponse(span trace.Span, resp *openai.EmbeddingResponse)

	// RecordResponseOnError ends recording the span with an error status.
	RecordResponseOnError(span trace.Span, statusCode int, body []byte)
}

EmbeddingsRecorder records attributes to a span according to a semantic convention.

type EmbeddingsSpan added in v0.4.0

type EmbeddingsSpan interface {
	// RecordResponse records the response attributes to the span.
	RecordResponse(resp *openai.EmbeddingResponse)

	// EndSpanOnError finalizes and ends the span with an error status.
	EndSpanOnError(statusCode int, body []byte)

	// EndSpan finalizes and ends the span.
	EndSpan()
}

EmbeddingsSpan represents an OpenAI embeddings request.

type EmbeddingsTracer added in v0.4.0

type EmbeddingsTracer interface {
	// StartSpanAndInjectHeaders starts a span and injects trace context into
	// the header mutation.
	//
	// Parameters:
	//   - ctx: might include a parent span context.
	//   - headers: Incoming HTTP headers used to extract parent trace context.
	//   - headerMutation: The new Embeddings Span will have its context
	//     written to these headers unless NoopTracing is used.
	//   - req: The OpenAI embeddings request. Used to record request attributes.
	//   - body: contains the original raw request body as a byte slice.
	//
	// Returns nil unless the span is sampled.
	StartSpanAndInjectHeaders(ctx context.Context, headers map[string]string, headerMutation *extprocv3.HeaderMutation, req *openai.EmbeddingRequest, body []byte) EmbeddingsSpan
}

EmbeddingsTracer creates spans for OpenAI embeddings requests.

type ImageGenerationRecorder added in v0.4.0

type ImageGenerationRecorder interface {
	// StartParams returns the name and options to start the span with.
	//
	// Parameters:
	//   - req: contains the image generation request
	//   - body: contains the complete request body.
	//
	// Note: Do not do any expensive data conversions as the span might not be
	// sampled.
	StartParams(req *openaisdk.ImageGenerateParams, body []byte) (spanName string, opts []trace.SpanStartOption)

	// RecordRequest records request attributes to the span.
	//
	// Parameters:
	//   - req: contains the image generation request
	//   - body: contains the complete request body.
	RecordRequest(span trace.Span, req *openaisdk.ImageGenerateParams, body []byte)

	// RecordResponse records response attributes to the span.
	RecordResponse(span trace.Span, resp *openaisdk.ImagesResponse)

	// RecordResponseOnError ends recording the span with an error status.
	RecordResponseOnError(span trace.Span, statusCode int, body []byte)
}

ImageGenerationRecorder records attributes to a span according to a semantic convention.

type ImageGenerationSpan added in v0.4.0

type ImageGenerationSpan interface {
	// RecordResponse records the response attributes to the span.
	RecordResponse(resp *openaisdk.ImagesResponse)

	// EndSpanOnError finalizes and ends the span with an error status.
	EndSpanOnError(statusCode int, body []byte)

	// EndSpan finalizes and ends the span.
	EndSpan()
}

ImageGenerationSpan represents an OpenAI image generation.

type ImageGenerationTracer added in v0.4.0

type ImageGenerationTracer interface {
	// StartSpanAndInjectHeaders starts a span and injects trace context into
	// the header mutation.
	//
	// Parameters:
	//   - ctx: might include a parent span context.
	//   - headers: Incoming HTTP headers used to extract parent trace context.
	//   - headerMutation: The new LLM Span will have its context written to
	//     these headers unless NoopTracer is used.
	//   - req: The OpenAI image generation request. Used to record request attributes.
	//
	// Returns nil unless the span is sampled.
	StartSpanAndInjectHeaders(ctx context.Context, headers map[string]string, headerMutation *extprocv3.HeaderMutation, req *openaisdk.ImageGenerateParams, body []byte) ImageGenerationSpan
}

ImageGenerationTracer creates spans for OpenAI image generation requests.

type MCPSpan added in v0.4.0

type MCPSpan interface {
	// RecordRouteToBackend records the backend that was routed to.
	RecordRouteToBackend(backend string, session string, isNew bool)
	// EndSpan finalizes and ends the span.
	EndSpan()
	// EndSpanOnError finalizes and ends the span with an error status.
	EndSpanOnError(errType string, err error)
}

MCPSpan represents an MCP span.

type MCPTracer added in v0.4.0

type MCPTracer interface {
	// StartSpanAndInjectMeta starts a span and injects trace context into
	// the _meta mutation.
	//
	// Parameters:
	//   - ctx: might include a parent span context.
	//   - req: Incoming MCP request message.
	//   - param: Incoming MCP parameter used to extract parent trace context.
	//   - headers: Request HTTP request headers.
	//
	// Returns nil unless the span is sampled.
	StartSpanAndInjectMeta(ctx context.Context, req *jsonrpc.Request, param mcp.Params, headers http.Header) MCPSpan
}

MCPTracer creates spans for MCP requests.

type NoopChatCompletionTracer

type NoopChatCompletionTracer struct{}

NoopChatCompletionTracer is a ChatCompletionTracer that doesn't do anything.

func (NoopChatCompletionTracer) StartSpanAndInjectHeaders

StartSpanAndInjectHeaders implements ChatCompletionTracer.StartSpanAndInjectHeaders.

type NoopCompletionTracer added in v0.4.0

type NoopCompletionTracer struct{}

NoopCompletionTracer is a CompletionTracer that doesn't do anything.

func (NoopCompletionTracer) StartSpanAndInjectHeaders added in v0.4.0

StartSpanAndInjectHeaders implements CompletionTracer.StartSpanAndInjectHeaders.

type NoopEmbeddingsTracer added in v0.4.0

type NoopEmbeddingsTracer struct{}

NoopEmbeddingsTracer is an EmbeddingsTracer that doesn't do anything.

func (NoopEmbeddingsTracer) StartSpanAndInjectHeaders added in v0.4.0

StartSpanAndInjectHeaders implements EmbeddingsTracer.StartSpanAndInjectHeaders.

type NoopImageGenerationTracer added in v0.4.0

type NoopImageGenerationTracer struct{}

NoopImageGenerationTracer is a ImageGenerationTracer that doesn't do anything.

func (NoopImageGenerationTracer) StartSpanAndInjectHeaders added in v0.4.0

StartSpanAndInjectHeaders implements ImageGenerationTracer.StartSpanAndInjectHeaders.

type NoopMCPTracer added in v0.4.0

type NoopMCPTracer struct{}

NoopMCPTracer is a no-op implementation of MCPTracer.

func (NoopMCPTracer) StartSpanAndInjectMeta added in v0.4.0

func (NoopMCPTracer) StartSpanAndInjectMeta(context.Context, *jsonrpc.Request, mcp.Params, http.Header) MCPSpan

StartSpanAndInjectMeta implements [MCPTracer.StartSpanAndInjectMeta].

type NoopTracing

type NoopTracing struct{}

NoopTracing is a Tracing that doesn't do anything.

func (NoopTracing) ChatCompletionTracer

func (NoopTracing) ChatCompletionTracer() ChatCompletionTracer

ChatCompletionTracer implements Tracing.ChatCompletionTracer.

func (NoopTracing) CompletionTracer added in v0.4.0

func (NoopTracing) CompletionTracer() CompletionTracer

CompletionTracer implements Tracing.CompletionTracer.

func (NoopTracing) EmbeddingsTracer added in v0.4.0

func (NoopTracing) EmbeddingsTracer() EmbeddingsTracer

EmbeddingsTracer implements Tracing.EmbeddingsTracer.

func (NoopTracing) ImageGenerationTracer added in v0.4.0

func (NoopTracing) ImageGenerationTracer() ImageGenerationTracer

ImageGenerationTracer implements Tracing.ImageGenerationTracer.

func (NoopTracing) MCPTracer added in v0.4.0

func (t NoopTracing) MCPTracer() MCPTracer

func (NoopTracing) Shutdown

func (NoopTracing) Shutdown(context.Context) error

Shutdown implements Tracing.Shutdown.

type Tracing

type Tracing interface {
	// ChatCompletionTracer creates spans for OpenAI chat completion requests on /chat/completions endpoint.
	ChatCompletionTracer() ChatCompletionTracer
	// ImageGenerationTracer creates spans for OpenAI image generation requests.
	ImageGenerationTracer() ImageGenerationTracer
	// CompletionTracer creates spans for OpenAI completion requests on /completions endpoint.
	CompletionTracer() CompletionTracer
	// EmbeddingsTracer creates spans for OpenAI embeddings requests on /embeddings endpoint.
	EmbeddingsTracer() EmbeddingsTracer
	// MCPTracer creates spans for MCP requests.
	MCPTracer() MCPTracer
	// Shutdown shuts down the tracer, flushing any buffered spans.
	Shutdown(context.Context) error
}

Tracing gives access to tracer types needed for endpoints such as OpenAI chat completions, image generation, embeddings, and MCP requests.

type TracingConfig

type TracingConfig struct {
	Tracer                  trace.Tracer
	Propagator              propagation.TextMapPropagator
	ChatCompletionRecorder  ChatCompletionRecorder
	CompletionRecorder      CompletionRecorder
	ImageGenerationRecorder ImageGenerationRecorder
	EmbeddingsRecorder      EmbeddingsRecorder
}

TracingConfig is used when Tracing is not NoopTracing.

Implementations of the Tracing interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL