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
- func ExtractIncoming(ctx context.Context, header map[string][]string) context.Context
- func FilterBaggage(h http.Header)
- func FilterTraceContext(h http.Header)
- func InjectMCPMeta(ctx context.Context, meta map[string]any) map[string]any
- func InjectOutgoing(ctx context.Context, header map[string][]string)
- func PropagateToHTTPUpstreams() bool
- func PropagateToLLMProviders() bool
- func PropagateToMCP() bool
- func SetBaggageAllowlist(keys []string)
- func SetOutboundPropagation(llmProviders, httpUpstreams, mcp bool)
- func StripTraceContext(h http.Header)
- func Tracer() trace.Tracer
- func Use(provider trace.TracerProvider, propagator propagation.TextMapPropagator)
- type Config
- type MCPMetaInjector
- type Shutdown
Constants ¶
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 ¶
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
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
FilterTraceContext applies the outbound rules to headers a caller supplied: baggage reduced to the allowlist, tracestate removed.
func InjectMCPMeta ¶ added in v2.3.0
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 ¶
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
StripTraceContext removes traceparent, tracestate and baggage, for an upstream that opted out of trace propagation (a per-LLM setting).
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 ¶
Shutdown flushes and stops the exporter. It is always non-nil, so callers can defer it unconditionally.
func Init ¶
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.