Documentation
¶
Overview ¶
Package declared extracts the context a client declares about a gateway request (spec enterprise/features/agenttracing/SPEC.md §4.2 and §4.3): W3C trace context and baggage, the X-Tyk-* tracing headers, the compatibility header families of other gateways, a few body fields, and the session ids of verified coding harnesses.
Everything here is untrusted client input. It labels and groups spans; it must never feed authentication, authorisation or App scoping.
The package is pure and inert: callers run Extract only when agent tracing is enabled (agenttrace.Enabled). StripTracingHeaders is the one part that runs in every edition, so X-Tyk-* tracing headers never reach a vendor.
Precedence (field-wise, first source wins) ¶
- Trace id, parent span id, sampled: traceparent (for MCP requests, params._meta.traceparent first) > X-Tyk-Trace-Id / X-Tyk-Parent-Span-Id > Portkey > Cloudflare AI Gateway. traceparent always wins these.
- Session, agent, path, end user: X-Tyk-* > compat families > baggage > body fields > harness extractors. §4.2's note ("Tyk/compat headers override traceparent for session/agent/path") is applied to the whole W3C context, baggage included, so an explicit Tyk or compat header beats an ambient baggage value an OTel SDK propagated.
Index ¶
Constants ¶
const ( FamilyHelicone = "helicone" FamilyPortkey = "portkey" FamilyOpenRouter = "openrouter" FamilyBifrost = "bifrost" FamilyAgentRouter = "agent_router" FamilyCloudflare = "cloudflare" FamilyAgentCore = "agentcore" FamilyMCPSession = "mcp_session" )
Compat family names (config AGENT_TRACING_COMPAT_HEADERS).
const ( HarnessClaudeCode = "claude_code" HarnessCodexCLI = "codex_cli" HarnessOpenCode = "opencode" )
Harness extractor names (config AGENT_TRACING_HARNESS_SESSION_HEADERS). Other entries in that list are header names read as a session id.
const ( BodyLLM = "llm" // an LLM request body (OpenAI, Anthropic, ...) BodyMCP = "mcp" // an MCP JSON-RPC request )
Body formats Extract understands. Any other value skips body fields.
Variables ¶
var TracingHeaders = []string{
"X-Tyk-Session-Id",
"X-Tyk-Session-Name",
"X-Tyk-Trace-Id",
"X-Tyk-Span-Id",
"X-Tyk-Parent-Span-Id",
"X-Tyk-Agent-Name",
"X-Tyk-Agent-Id",
"X-Tyk-Agent-Path",
"X-Tyk-User-Id",
"X-Tyk-Trace-Capture",
}
TracingHeaders are the X-Tyk-* tracing headers (§4.3). Only these are stripped: other X-Tyk-* headers belong to other features.
Functions ¶
func SetDefault ¶
func SetDefault(x *Extractor)
SetDefault installs the Extractor the gateway runs on every captured request. Each runtime (the hub's embedded gateway, the microgateway) builds it from its configuration at start-up; nil restores the built-in one.
func SetForwardTykHeaders ¶
func SetForwardTykHeaders(v bool)
SetForwardTykHeaders sets propagation.forward_tyk_headers (AGENT_TRACING_FORWARD_TYK_HEADERS): when true the X-Tyk-* tracing headers reach the upstream. The default, false, strips them in every edition.
func StripTracingHeaders ¶
StripTracingHeaders removes the X-Tyk-* tracing headers from an upstream request's headers unless forwarding is on. Compat headers pass through unchanged. It is cheap enough for every request: no allocation.
Types ¶
type Extractor ¶
type Extractor struct {
// contains filtered or unexported fields
}
Extractor applies one Options to requests. It is safe for concurrent use.
func Default ¶
func Default() *Extractor
Default returns the installed Extractor, or the built-in one.
func (*Extractor) ApplyBody ¶
ApplyBody adds the priority-4 body fields of an LLM request (OpenAI conversation, metadata.session_id, user) to r, and Claude Code's metadata.user_id session when its header was missing. Precedence holds as if Extract had run: a body session beats a harness one, never a header.
func (*Extractor) Extract ¶
Extract is Headers followed by ApplyBody: the whole declared context of a request. bodyKind is BodyLLM, BodyMCP or "" (no body fields); body is the already-buffered request body, never read here.
func (*Extractor) Headers ¶
Headers reads everything but the LLM body: the headers, and for an MCP request (bodyKind BodyMCP) its small JSON-RPC body, whose _meta.traceparent outbound propagation needs. It is what the request path runs. An LLM body can be large, so its fields are left to ApplyBody, which callers run once the response is on its way.
type HarnessInfo ¶
type HarnessInfo struct {
// Name is the harness (Harness* constant), set whenever the request is
// recognised, even when another source supplied the session.
Name string
Version string
// TurnID is a client-declared turn id (Codex CLI X-Codex-Turn-Metadata
// turn_id): one id for all requests of a user turn.
TurnID string
// SubAgentID is a client-declared sub-agent instance (Claude Code
// X-Claude-Code-Agent-Id); empty on the main agent's requests.
SubAgentID string
}
HarnessInfo is what a coding-harness extractor found (bead jyx.52).
type Options ¶
type Options struct {
// CompatFamilies are the enabled compat families (Family* names).
CompatFamilies []string
// Harness lists enabled harness extractors (Harness* names) and extra
// header names read as a session id.
Harness []string
// BaggageAllowlist names the baggage keys kept in DeclaredContext.Baggage
// (§4.6). The correlation keys are read whatever the allowlist says.
BaggageAllowlist []string
}
Options configure an Extractor. Build one per configuration change, not per request.
type Result ¶
type Result struct {
Declared agenttrace.DeclaredContext
Harness HarnessInfo
// MCPMethod and MCPName classify an MCP request: the Mcp-Method /
// Mcp-Name headers (MCP 2026-07-28), else the JSON-RPC body.
MCPMethod string
MCPName string
// Invalid lists the inputs that were present but rejected, as
// "<source>:<reason>", for the tracing-health counters.
Invalid []string
// contains filtered or unexported fields
}
Result is the extracted context of one request.