AI Gateway has a series of built-in tracing instrumentations which are configured by the tracing_instrumentations parameter. AI Gateway creates a top-level span for each request by default when tracing_instrumentations is enabled.
The top level span has the following attributes:
-
http.method: HTTP method
-
http.url: HTTP URL
-
http.host: HTTP host
-
http.scheme: HTTP scheme (http or https)
-
http.flavor: HTTP version
-
net.peer.ip: Client IP address
For more information, see the Tracing reference.
Note: When the OpenTelemetry Policy is used together with the Proxy Cache Advanced Policy, cache-HIT responses are not traced.
This is expected behavior. When a request results in a cache-HIT, the response is served before the request lifecycle reaches the phase where the OpenTelemetry Policy executes. As a result, no spans are generated for cache-HIT requests. Cache-MISS requests continue through the full request lifecycle and are traced normally.
AI-specific span attributes are emitted following the OpenTelemetry Gen AI semantic conventions. These attributes capture model parameters, token usage, and tool-call metadata.
The OpenTelemetry AI Policy supports propagation of the following header formats:
This AI Policy offers extensive options for configuring tracing header propagation, providing a high degree of flexibility.
You can customize which headers are used to extract and inject tracing context. Additionally, you can configure headers to be cleared after the tracing context extraction process, enabling a high level of customization.
flowchart LR
id1(Original Request) --> Extract
id1(Original Request) -->|"headers (original)"| Extract
id1(Original Request) --> Extract
subgraph ide1 [Headers Propagation]
Extract --> Clear
Extract -->|"headers (original)"| Clear
Extract --> Clear
Clear -->|"headers (filtered)"| Inject
end
Extract -.->|extracted ctx| id2((tracing logic))
id2((tracing logic)) -.->|updated ctx| Inject
Inject -->|"headers (updated ctx)"| id3(Updated request)
See the Policy’s configuration reference for a complete overview of the available options and values.
Note: If any of the config.propagation.* configuration options (extract, clear, or inject) are configured, the config.propagation configuration takes precedence over the deprecated header_type parameter.
If none of the config.propagation.* configuration options are set, the header_type parameter is still used to determine the propagation behavior.
The OpenTelemetry AI Policy implements the OTLP/HTTP exporter, which uses Protobuf payloads encoded in binary format and is sent via an HTTP/1.1.
config.connect_timeout, config.read_timeout, and config.send_timeout are used to set the timeouts for the HTTP request.
config.batch_span_count and config.batch_flush_delay are used to set the maximum number of spans and the delay between two consecutive batches.
The OpenTelemetry AI Policy is built on top of the AI Gateway tracing PDK. You can customize the spans and add your own spans through the universal tracing PDK.
-
Create a file named custom-span.lua with the following content:
-- Modify the root span
local root_span = kong.tracing.get_root_span()
root_span:set_attribute("custom.attribute", "custom value")
-- Modify the active span
local active_span = kong.tracing.active_span()
active_span:set_attribute("custom.attribute", "custom value")
-- Create a custom span
local span = kong.tracing.start_span("custom-span")
-- Append attributes
span:set_attribute("custom.attribute", "custom value")
-- Close the span
span:finish()
-
Load the file into an environment variable:
export CUSTOM_SPAN_LUA=$(cat custom-span.lua)
-
Apply the Lua code with the Post-function Policy:
curl -X POST https://{region}.api.konghq.com/v1/ai-gateways/{AIGatewayId}/policies \
--header "accept: application/json" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $KONNECT_TOKEN" \
--data '
{
"display_name": "Post Function - Custom Span",
"name": "post-function",
"type": "post-function",
"config": {
"access": [
"'$CUSTOM_SPAN_LUA'"
]
}
}
'
ai_gateway_policies:
- ref: post-function
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
display_name: Post Function - Custom Span
name: post-function
type: post-function
config:
access:
- !env CUSTOM_SPAN_LUA
Make sure to replace the following placeholders with your own values:
-
AI_GATEWAY_ID: The id of your AI Gateway.