Skip to content

Commit

Permalink
fix(tracing): record http.status_code when request is not proxied (#1…
Browse files Browse the repository at this point in the history
…1406)

* fix(tracing): record http.status_code even when request is not proxied

* docs(CHANGELOG) description updated

* tests(tracing): kong span includes status code

---------

Co-authored-by: Jonah Back <jback@legalzoom.com>
  • Loading branch information
locao and backjo authored Aug 22, 2023
1 parent 84d7661 commit b00c73e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@
[#11066](https://github.com/Kong/kong/pull/11066)
- Fix a bug that caused sampling rate to be applied to individual spans producing split traces.
[#11135](https://github.com/Kong/kong/pull/11135)
- Fix a bug that caused spans to not be instrumented with http.status_code when the request was not proxied to an upstream.
Thanks [@backjo](https://github.com/backjo) for contributing this change.
[#11152](https://github.com/Kong/kong/pull/11152),
[#11406](https://github.com/Kong/kong/pull/11406)
- Fix a bug that caused the router to fail in `traditional_compatible` mode when a route with multiple paths and no service was created.
[#11158](https://github.com/Kong/kong/pull/11158)
- Fix an issue where the router of flavor `expressions` can not work correctly
Expand Down
1 change: 1 addition & 0 deletions kong/runloop/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,7 @@ return {
header_filter = {
before = function(ctx)
if not ctx.KONG_PROXIED then
instrumentation.runloop_before_header_filter(ngx.status)
return
end

Expand Down
50 changes: 49 additions & 1 deletion spec/02-integration/14-tracing/01-instrumentations_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ for _, strategy in helpers.each_strategy() do
hosts = { "status" },
strip_path = false })

local np_route = bp.routes:insert({
service = http_srv,
protocols = { "http" },
paths = { "/noproxy" },
strip_path = false
})

bp.plugins:insert({
name = tcp_trace_plugin_name,
config = {
Expand All @@ -74,10 +81,19 @@ for _, strategy in helpers.each_strategy() do
}
})

bp.plugins:insert({
name = "request-termination",
route = np_route,
config = {
status_code = 418,
message = "No coffee for you. I'm a teapot.",
}
})

assert(helpers.start_kong {
database = strategy,
nginx_conf = "spec/fixtures/custom_nginx.template",
plugins = "tcp-trace-exporter",
plugins = "bundled, tcp-trace-exporter",
tracing_instrumentations = types,
tracing_sampling_rate = 1,
})
Expand Down Expand Up @@ -316,6 +332,38 @@ for _, strategy in helpers.each_strategy() do
assert_has_no_span("kong.dns", spans)
assert_has_no_span("kong.rewrite.plugin." .. tcp_trace_plugin_name, spans)
end)

it("contains the expected kong span with status code when request is not proxied", function ()
local thread = helpers.tcp_server(TCP_PORT)
local r = assert(proxy_client:send {
method = "GET",
path = "/noproxy",
})
assert.res_status(418, r)

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

-- Making sure it's alright
local spans = cjson.decode(res)
local kong_span = assert_has_span("kong", spans)

assert_has_attributes(kong_span, {
["http.method"] = "GET",
["http.flavor"] = "1.1",
["http.status_code"] = "418",
["http.route"] = "/noproxy",
["http.url"] = "http://0.0.0.0/noproxy",
})
assert_has_span("kong.header_filter.plugin." .. tcp_trace_plugin_name, spans)
assert_has_no_span("kong.balancer", spans)
assert_has_no_span("kong.database.query", spans)
assert_has_no_span("kong.router", spans)
assert_has_no_span("kong.dns", spans)
assert_has_no_span("kong.rewrite.plugin." .. tcp_trace_plugin_name, spans)
end)
end)


Expand Down

1 comment on commit b00c73e

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:b00c73e4f8e9c5bc89432227e50cd7602c507228
Artifacts available https://github.com/Kong/kong/actions/runs/5943829268

Please sign in to comment.