Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tracing): set attribute http.status_code when no route entry matched #11711

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/11711.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "set attribute `http.status_code` to 404 in tracing span when no route is matched"
type: bugfix
scope: Core
5 changes: 5 additions & 0 deletions kong/runloop/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,11 @@ return {
req_dyn_hook.run_hooks("timing", "after:router")
if not match_t then
-- tracing
local root_span = ctx.KONG_SPANS and ctx.KONG_SPANS[1]
if root_span then
root_span:set_attribute("http.status_code", 404)
end

if span then
span:set_status(2)
span:finish()
Expand Down
40 changes: 30 additions & 10 deletions spec/02-integration/14-tracing/01-instrumentations_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ for _, strategy in helpers.each_strategy() do

bp.routes:insert({ service = http_srv,
protocols = { "http" },
paths = { "/" }})
paths = { "/proxy" }})

bp.routes:insert({ service = http_srv,
protocols = { "http" },
Expand Down Expand Up @@ -114,7 +114,7 @@ for _, strategy in helpers.each_strategy() do
local thread = helpers.tcp_server(TCP_PORT)
local r = assert(proxy_client:send {
method = "GET",
path = "/",
path = "/proxy",
})
assert.res_status(200, r)

Expand Down Expand Up @@ -142,7 +142,7 @@ for _, strategy in helpers.each_strategy() do
local thread = helpers.tcp_server(TCP_PORT)
local r = assert(proxy_client:send {
method = "GET",
path = "/",
path = "/proxy",
})
assert.res_status(200, r)

Expand Down Expand Up @@ -176,7 +176,7 @@ for _, strategy in helpers.each_strategy() do
local thread = helpers.tcp_server(TCP_PORT)
local r = assert(proxy_client:send {
method = "GET",
path = "/",
path = "/proxy",
})
assert.res_status(200, r)

Expand All @@ -195,6 +195,26 @@ for _, strategy in helpers.each_strategy() do
assert_has_no_span("kong.rewrite.plugin." .. tcp_trace_plugin_name, spans)
assert_has_no_span("kong.header_filter.plugin." .. tcp_trace_plugin_name, spans)
end)

it("http.status_code 404 when no route matched", function()
local thread = helpers.tcp_server(TCP_PORT)
local r = assert(proxy_client:send {
method = "GET",
path = "/404",
})
assert.res_status(404, r)

-- Getting back the TCP server input
local ok, res = thread:join()
assert.True(ok)
assert.is_string(res)

local spans = cjson.decode(res)
local span = assert_has_span("kong", spans)
assert_has_attributes(span, {
["http.status_code"] = "404",}
)
end)
end)

describe("http_client", function ()
Expand All @@ -210,7 +230,7 @@ for _, strategy in helpers.each_strategy() do
local thread = helpers.tcp_server(TCP_PORT)
local r = assert(proxy_client:send {
method = "GET",
path = "/",
path = "/proxy",
})
assert.res_status(200, r)

Expand Down Expand Up @@ -244,7 +264,7 @@ for _, strategy in helpers.each_strategy() do
local thread = helpers.tcp_server(TCP_PORT)
local r = assert(proxy_client:send {
method = "GET",
path = "/",
path = "/proxy",
})
assert.res_status(200, r)

Expand Down Expand Up @@ -278,7 +298,7 @@ for _, strategy in helpers.each_strategy() do
local thread = helpers.tcp_server(TCP_PORT)
local r = assert(proxy_client:send {
method = "GET",
path = "/",
path = "/proxy",
})
assert.res_status(200, r)

Expand Down Expand Up @@ -312,7 +332,7 @@ for _, strategy in helpers.each_strategy() do
local thread = helpers.tcp_server(TCP_PORT)
local r = assert(proxy_client:send {
method = "GET",
path = "/",
path = "/proxy",
})
assert.res_status(200, r)

Expand Down Expand Up @@ -380,7 +400,7 @@ for _, strategy in helpers.each_strategy() do
local thread = helpers.tcp_server(TCP_PORT)
local r = assert(proxy_client:send {
method = "GET",
path = "/",
path = "/proxy",
})
assert.res_status(200, r)

Expand Down Expand Up @@ -489,7 +509,7 @@ for _, strategy in helpers.each_strategy() do
local thread = helpers.tcp_server(TCP_PORT)
local r = assert(proxy_client:send {
method = "GET",
path = "/",
path = "/proxy",
})
assert.res_status(200, r)

Expand Down