Skip to content

Commit

Permalink
perf(tracing): improving performance of trace ID size lookup
Browse files Browse the repository at this point in the history
Replace table_contains with a more efficient lookup table for trace ID sizes, improving performance of trace ID size lookup.

Signed-off-by: tzssangglass <tzssangglass@gmail.com>
  • Loading branch information
tzssangglass committed Feb 21, 2025
1 parent bd936d5 commit 557608b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/perf-trace-ID-size-lookup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "improving performance of trace ID size lookup."
type: performance
scope: Core
11 changes: 8 additions & 3 deletions kong/observability/tracing/propagation/injectors/_base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ local propagation_utils = require "kong.observability.tracing.propagation.utils"

local to_id_size = propagation_utils.to_id_size
local set_header = kong.service.request.set_header
local contains = require("kong.tools.table").table_contains
local type = type
local ipairs = ipairs

Expand Down Expand Up @@ -72,6 +71,11 @@ function _INJECTOR:new(e)
inst.span_id_size_bytes > 0,
err .. "invalid span_id_size_bytes variable")

local allowed_lookup = {}
for _, size in ipairs(inst.trace_id_allowed_sizes) do
allowed_lookup[size] = true
end
inst.trace_id_allowed_sizes_lookup = allowed_lookup
return inst
end

Expand Down Expand Up @@ -148,8 +152,9 @@ function _INJECTOR:inject(inj_tracing_ctx)
-- Extractors automatically set `trace_id_original_size` during extraction.
local orig_size = inj_tracing_ctx.trace_id_original_size
local allowed = self.trace_id_allowed_sizes
local new_trace_id_size = contains(allowed, orig_size) and orig_size
or allowed[1]
local lookup = self.trace_id_allowed_sizes_lookup

local new_trace_id_size = (orig_size ~= nil and lookup[orig_size]) and orig_size or allowed[1]

inj_tracing_ctx.trace_id = to_id_size(inj_tracing_ctx.trace_id, new_trace_id_size)
inj_tracing_ctx.span_id = to_id_size(inj_tracing_ctx.span_id, self.span_id_size_bytes)
Expand Down

0 comments on commit 557608b

Please sign in to comment.