[Bug]: Error logs when plugin is enabled globally and requests don't match any route
Kong Version
3.9
Plugin Version
0.2.4-1
On which environment your Kong instance is running?
Kubernetes
Plugin Configuration
apiVersion: configuration.konghq.com/v1
kind: KongClusterPlugin
metadata:
annotations:
kubernetes.io/ingress.class: kong
labels:
global: "true"
name: ddtrace
plugin: ddtrace
config:
agent_host: datadog-agent.datadog.svc.cluster.local
trace_agent_port: 8126
What happened?
When the ddtrace plugin is enabled globally via KongClusterPlugin with global: "true", requests that don't match any Kong route produce error-level log entries:
[error] 1407#0: *2658 [kong] handler.lua:410 [ddtrace] tracing error in DatadogTraceHandler:log: handler.lua:340: request span is missing during the log phase while logging request
This happens because Kong skips the access phase for unrouted requests (no matching route = no proxy), but still invokes header_filter and log for globally-enabled plugins. Since the access phase is where request_span and proxy_span are created, both header_filter and log find nil spans and call error().
In practice this is triggered by internet scanners, bots, and direct IP hits — none of which match a configured route. On an internet-facing gateway this generates thousands of error log entries per day that are not actionable.
The relevant code in handler.lua:
-- header_filter()
if ctx.proxy_span == nil then
error('proxy span missing during the "header_filter" phase')
end
-- log()
if ctx.request_span == nil then
error("request span is missing during the log phase")
end
Expected Behavior
The plugin should gracefully skip the header_filter and log phases when no spans exist and no route is configured rather than raising an error. A kong.log.debug and early return would be appropriate since this is an expected condition for unrouted requests, not a bug in the tracing pipeline.
https://github.com/DataDog/kong-plugin-ddtrace/pull/105