Skip to content

Commit

Permalink
tests(tracing): add propagation integration test (#10623)
Browse files Browse the repository at this point in the history
Add a test to confirm the span hierarchy is propagated correctly.
The propagated span id must be that of the balancer span.
  • Loading branch information
samugi authored Apr 6, 2023
1 parent d2cd14a commit adb72d5
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
79 changes: 79 additions & 0 deletions spec/02-integration/14-tracing/02-propagation_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
local helpers = require "spec.helpers"
local cjson = require "cjson"

local TCP_PORT = 35001
for _, strategy in helpers.each_strategy() do
local proxy_client

describe("tracing propagation spec #" .. strategy, function()
describe("spans hierarchy", function ()

lazy_setup(function()
local bp, _ = assert(helpers.get_db_utils(strategy, {
"routes",
"plugins",
}, { "tcp-trace-exporter", "trace-propagator" }))

bp.routes:insert({
hosts = { "propagate.test" },
})

bp.plugins:insert({
name = "tcp-trace-exporter",
config = {
host = "127.0.0.1",
port = TCP_PORT,
custom_spans = false,
}
})

bp.plugins:insert({
name = "trace-propagator"
})

assert(helpers.start_kong {
database = strategy,
nginx_conf = "spec/fixtures/custom_nginx.template",
plugins = "tcp-trace-exporter,trace-propagator",
tracing_instrumentations = "balancer",
})

proxy_client = helpers.proxy_client()
end)

lazy_teardown(function()
helpers.stop_kong()
end)

it("propagates the balancer span", function ()
local thread = helpers.tcp_server(TCP_PORT)
local r = assert(proxy_client:send {
method = "GET",
path = "/request",
headers = {
["Host"] = "propagate.test",
}
})
assert.res_status(200, r)
local body = r:read_body()
body = assert(body and cjson.decode(body))

local ok, res = thread:join()
assert.True(ok)
assert.is_string(res)

-- expected spans are returned
local spans = cjson.decode(res)
assert.is_same(2, #spans, res)
local balancer_span = spans[2]
assert.is_same("balancer try #1", balancer_span.name)

local traceparent = assert(body.headers.traceparent)
local trace_id = balancer_span.trace_id
local span_id = balancer_span.span_id
-- traceparent contains correct trace id and the balancer span's id
assert.equals("00-" .. trace_id .. "-" .. span_id .. "-01", traceparent)
end)
end)
end)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
local propagation = require "kong.tracing.propagation"

local ngx = ngx
local kong = kong
local propagation_parse = propagation.parse
local propagation_set = propagation.set

local _M = {
PRIORITY = 1001,
VERSION = "1.0",
}


function _M:access(conf)
local headers = ngx.req.get_headers()
local tracer = kong.tracing.new("trace-propagator")
local root_span = tracer.start_span("root")

local header_type, trace_id, span_id, parent_id = propagation_parse(headers)

if trace_id then
root_span.trace_id = trace_id
end

if span_id then
root_span.parent_id = span_id

elseif parent_id then
root_span.parent_id = parent_id
end

local new_span = ngx.ctx.last_try_balancer_span
if new_span == nil then
new_span = tracer.create_span(nil, {
span_kind = 3,
parent = root_span,
})
ngx.ctx.last_try_balancer_span = new_span
end

local type = header_type and "preserve" or "w3c"
propagation_set(type, header_type, new_span, "trace-propagator")
end

return _M
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
return {
name = "trace-propagator",
fields = {
{
config = {
type = "record",
fields = { }
}
}
}
}

1 comment on commit adb72d5

@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:adb72d54651487e1e8f479cb42aa999d352a47b5
Artifacts available https://github.com/Kong/kong/actions/runs/4626710610

Please sign in to comment.