api

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2025 License: Apache-2.0 Imports: 5 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.

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 NoopTracer 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 NoopChatCompletionTracer

type NoopChatCompletionTracer struct{}

NoopChatCompletionTracer is a ChatCompletionTracer that doesn't do anything.

func (NoopChatCompletionTracer) StartSpanAndInjectHeaders

StartSpanAndInjectHeaders implements ChatCompletionTracer.StartSpanAndInjectHeaders.

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) Shutdown

func (NoopTracing) Shutdown(context.Context) error

Shutdown implements Tracing.Shutdown.

type Tracing

type Tracing interface {
	ChatCompletionTracer() ChatCompletionTracer
	Shutdown(context.Context) error
}

Tracing gives access to tracer types needed for endpoints such as OpenAI chat completions.

type TracingConfig

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

TracingConfig is used when Tracing is not NoopTracing.

Jump to

Keyboard shortcuts

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