tracing

package
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: AGPL-3.0 Imports: 15 Imported by: 2 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package tracing wires OpenTelemetry distributed tracing for the LLM proxy.

ENABLE_TRACING and TRACING_ENDPOINT have been parsed into the gateway config for some time without anything behind them; this package is what they now drive. When tracing is disabled the global provider stays OpenTelemetry's no-op implementation, so span creation on the request path costs nothing and callers never need to branch on whether it is on.

Index

Constants

View Source
const TracerName = "aistudio/proxy"

TracerName is the instrumentation scope for every span this package's callers create.

Variables

This section is empty.

Functions

func ExtractIncoming

func ExtractIncoming(ctx context.Context, header map[string][]string) context.Context

ExtractIncoming returns a context carrying the trace parent from the inbound request headers, so gateway spans become children of the caller's trace rather than starting a disconnected one.

func FilterBaggage added in v2.3.0

func FilterBaggage(h http.Header)

FilterBaggage reduces the baggage header in h to the allowlisted members. A header that does not parse is removed whole (fail closed). Keys set without canonical case (a raw map write) are folded in. It allocates only when a baggage header is present.

func FilterTraceContext added in v2.3.0

func FilterTraceContext(h http.Header)

FilterTraceContext applies the outbound rules to headers a caller supplied: baggage reduced to the allowlist, tracestate removed.

func InjectMCPMeta added in v2.3.0

func InjectMCPMeta(ctx context.Context, meta map[string]any) map[string]any

InjectMCPMeta writes the current span context into meta and returns it (a new map when meta is nil). Trace keys the client sent are replaced by the gateway span context, whose parent is the client's, so the chain stays intact; baggage is reduced to the allowlist as on HTTP. With AGENT_TRACING_PROPAGATE_MCP off the trace keys are removed instead.

func InjectOutgoing

func InjectOutgoing(ctx context.Context, header map[string][]string)

InjectOutgoing writes the current trace context into the upstream request headers so the provider (or an in-cluster model server) can continue the trace. Baggage, whether injected here or copied from the client's request, is then reduced to the allowlist, and tracestate is removed (FilterTraceContext).

func PropagateToHTTPUpstreams added in v2.3.0

func PropagateToHTTPUpstreams() bool

PropagateToHTTPUpstreams reports whether tool and other HTTP upstreams get the trace context.

func PropagateToLLMProviders added in v2.3.0

func PropagateToLLMProviders() bool

PropagateToLLMProviders reports whether LLM upstreams get the trace context.

func PropagateToMCP added in v2.3.0

func PropagateToMCP() bool

PropagateToMCP reports whether MCP servers get the trace context in _meta.

func SetBaggageAllowlist added in v2.3.0

func SetBaggageAllowlist(keys []string)

SetBaggageAllowlist sets the baggage keys forwarded upstream. An empty list forwards no baggage at all.

func SetOutboundPropagation added in v2.3.0

func SetOutboundPropagation(llmProviders, httpUpstreams, mcp bool)

SetOutboundPropagation sets which upstreams the gateway continues the trace into: LLM providers, HTTP upstreams (tools, datasources), and MCP servers (params._meta).

func StripTraceContext added in v2.3.0

func StripTraceContext(h http.Header)

StripTraceContext removes traceparent, tracestate and baggage, for an upstream that opted out of trace propagation (a per-LLM setting).

func Tracer

func Tracer() trace.Tracer

Tracer returns the tracer every proxy span is created from.

func Use

func Use(provider trace.TracerProvider, propagator propagation.TextMapPropagator)

Use makes Studio create spans from provider and carry trace context with propagator instead of the OpenTelemetry globals, which belong to an embedding host. A nil argument keeps the corresponding global.

Types

type Config

type Config struct {
	Enabled bool
	// Endpoint is the OTLP collector address. A bare "host:port" is assumed to
	// be an insecure gRPC endpoint; an http:// or https:// URL sets the
	// transport security from the scheme.
	Endpoint       string
	ServiceName    string
	ServiceVersion string
}

Config describes how to export spans. It mirrors the existing ObservabilityConfig fields rather than introducing new ones.

type MCPMetaInjector added in v2.3.0

type MCPMetaInjector interface {
	InjectMeta(ctx context.Context, meta map[string]any) map[string]any
}

MCPMetaInjector carries trace context into an outbound MCP request's params._meta (SEP-414: traceparent, tracestate and baggage as _meta keys).

Studio does not proxy MCP to MCP today: the MCP shim serves OpenAPI tools and calls their upstreams over HTTP, which carry the context as headers (InjectOutgoing). A future MCP-to-MCP proxy injects through this interface, so outbound _meta follows the same rules as the headers.

var MCPMeta MCPMetaInjector = mcpMeta{}

MCPMeta is the default MCPMetaInjector (InjectMCPMeta).

type Shutdown

type Shutdown func(context.Context) error

Shutdown flushes and stops the exporter. It is always non-nil, so callers can defer it unconditionally.

func Init

func Init(ctx context.Context, cfg Config) (Shutdown, error)

Init installs a global TracerProvider and the W3C trace context propagator.

The propagator is installed even when tracing is disabled: it costs nothing, and it means an incoming traceparent is still carried through to the upstream provider so a caller's own tracing stays connected across the gateway.

Jump to

Keyboard shortcuts

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