kong-plugin-ddtrace icon indicating copy to clipboard operation
kong-plugin-ddtrace copied to clipboard

fix: gracefully skip header_filter and log phases for unrouted requests

Open packetfodder opened this issue 7 months ago • 2 comments

When the plugin is enabled globally, requests that don't match any route (e.g. scanners, direct IP hits) skip the access phase entirely, so no spans are created. The header_filter and log phases still run for global plugins, causing error() to fire on every unrouted request.

Check kong.router.get_route() before erroring — if no route matched, log at debug level and return early. The error() is preserved for the case where a route was matched but the span is unexpectedly missing.

Resolves https://github.com/DataDog/kong-plugin-ddtrace/issues/106

packetfodder avatar Apr 13 '26 20:04 packetfodder

@packetfodder Thanks for your contribution! Could you please try to add a unit test for this? (This is currently especially important because we are finalizing the migration of the Kong plugin to a direct usage of Datadog tracer: https://github.com/DataDog/kong-plugin-ddtrace/pull/102/changes).

xlamorlette-datadog avatar Apr 15 '26 07:04 xlamorlette-datadog

@xlamorlette-datadog, I added test coverage for the unrouted-request guard in two layers:

Unit tests — spec/01-unit-tests/09_handler_spec.lua Stubs _G.kong and exercises DatadogTraceHandler:header_filter() and :log() directly (the public, pcall-wrapped entry points). Four cases, one per branch of the new guard:

Phase ctx.*_span kong.router.get_route() Asserts present Asserts absent
header_filter nil nil (no route) "skipping header_filter phase" debug log "tracing error in DatadogTraceHandler" err, "proxy span missing" err
header_filter nil route table "tracing error in DatadogTraceHandler" err, 'proxy span missing during the "header_filter" phase' err "skipping header_filter phase" debug
log nil nil (no route) "skipping log phase" debug log "tracing error in DatadogTraceHandler" err, "request span is missing" err
log nil route table "tracing error in DatadogTraceHandler" err, "request span is missing during the log phase" err "skipping log phase" debug

A small find_containing(list, needle) helper wraps the negative assertions so they don't pass vacuously on an empty captured-log list — i.e. we explicitly verify "the missing-span error was not emitted on the unrouted path", not just "no entries happened to match".

Integration tests — spec/02-integration-tests/03-unrouted_spec.lua Boots Kong with the plugin enabled globally, configures a single /mock route, runs log_level = "debug", and walks nginx_err_logs:

  1. GET /this-path-does-not-match-any-route → 404, and the error log contains none of:
    • tracing error in DatadogTraceHandler
    • proxy span missing during the "header_filter" phase
    • request span is missing during the log phase
  2. Same request → both new debug breadcrumbs are written:
    • no proxy span and no matched route, skipping header_filter phase
    • no request span and no matched route, skipping log phase
  3. GET /mock (the configured route) → 200, with neither the "skipping" debug messages nor any tracing errors — confirming the guards don't regress the normal routed flow. Negative assertions use a polling assert_log_absent helper so we wait long enough for the log phase to actually flush before declaring "no error was logged."

Together they cover:

  • The bug fix itself: unrouted requests no longer trigger kong.log.err spam from DatadogTraceHandler.
  • The non-regression: the original error() diagnostics still fire for the genuinely unexpected case where a route matched but the span vanished.
  • The happy path: routed requests are unchanged.

packetfodder avatar Apr 20 '26 14:04 packetfodder