Skip to content

Commit

Permalink
apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
mayocream committed Jun 6, 2022
1 parent 5716bde commit 1a88176
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 36 deletions.
83 changes: 47 additions & 36 deletions kong/plugins/opentelemetry/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,33 @@ local default_headers = {
["Content-Type"] = "application/x-protobuf",
}

-- worker-level spans cache
local spans_cache = new_tab(5000, 0)
-- worker-level spans queue
local spans_queue = new_tab(5000, 0)
local headers_cache = setmetatable({}, { __mode = "k" })
local last_run_cache = setmetatable({}, { __mode = "k" })

local function get_cached_headers(conf_headers)
-- cache http headers
local headers = default_headers
if conf_headers then
headers = headers_cache[conf_headers]
end

if not headers then
headers = clone(default_headers)
if conf_headers and conf_headers ~= null then
for k, v in pairs(conf_headers) do
headers[k] = v and v[1]
end
end

headers_cache[conf_headers] = headers
end

return headers
end


local function http_export_request(conf, pb_data, headers)
local httpc = http.new()
httpc:set_timeouts(conf.connect_timeout, conf.send_timeout, conf.read_timeout)
Expand All @@ -63,55 +85,40 @@ local function http_export(premature, conf)
return
end

local spans = spans_cache
local spans_n = #spans
local spans_n = #spans_queue
if spans_n == 0 then
return
end

local start = ngx_now()

-- cache http headers
local headers = default_headers
if conf.headers then
headers = headers_cache[conf.headers]
end

if not headers then
headers = clone(default_headers)
if conf.headers and conf.headers ~= null then
for k, v in pairs(conf.headers) do
headers[k] = v and v[1]
end
end
headers_cache[conf.headers] = headers
end
local headers = conf.headers and get_cached_headers(conf.headers) or default_headers

-- batch send spans
local batch_spans = tablepool_fetch(POOL_BATCH_SPANS, conf.batch_span_count, 0)
local spans_buffer = tablepool_fetch(POOL_BATCH_SPANS, conf.batch_span_count, 0)

for i = 1, spans_cache.n do
local len = (batch_spans.n or 0) + 1
batch_spans[len] = spans_cache[i]
batch_spans.n = len
for i = 1, spans_n do
local len = (spans_buffer.n or 0) + 1
spans_buffer[len] = spans_queue[i]
spans_buffer.n = len

if len >= conf.batch_span_count then
local pb_data = encode_traces(batch_spans, conf.resource_attributes)
clear(batch_spans)
local pb_data = encode_traces(spans_buffer, conf.resource_attributes)
clear(spans_buffer)

http_export_request(conf, pb_data, headers)
end
end

-- remain spans
if spans_cache.n and spans_cache.n > 0 then
local pb_data = encode_traces(batch_spans, conf.resource_attributes)
if spans_queue.n and spans_queue.n > 0 then
local pb_data = encode_traces(spans_buffer, conf.resource_attributes)
http_export_request(conf, pb_data, headers)
end

clear(spans_cache)
-- clear the queue
clear(spans_queue)

tablepool_release(POOL_BATCH_SPANS, batch_spans)
tablepool_release(POOL_BATCH_SPANS, spans_buffer)

ngx_update_time()
local duration = ngx_now() - start
Expand All @@ -135,20 +142,23 @@ local function process_span(span)

local pb_span = transform_span(span)

local len = spans_cache.n or 0
local len = spans_queue.n or 0
len = len + 1

spans_cache[len] = pb_span
spans_cache.n = len
spans_queue[len] = pb_span
spans_queue.n = len
end

function OpenTelemetryHandler:rewrite()
local headers = ngx_get_headers()
local root_span = ngx.ctx.KONG_SPANS and ngx.ctx.KONG_SPANS[1]
-- tracing instrumetation not enabled

-- make propagation running with tracing instrumetation not enabled
if not root_span then
local tracer = kong.tracing.new("otel")
root_span = tracer.start_span("otel")
root_span = tracer.start_span("root")

-- the span created only for the propagation and will be bypassed to the exporter
kong.ctx.plugin.should_sample = false
end

Expand All @@ -163,6 +173,7 @@ function OpenTelemetryHandler:rewrite()
kong.ctx.plugin.trace_id = trace_id
end

-- overwrite root span's parent_id
if span_id then
root_span.parent_id = span_id
end
Expand Down
1 change: 1 addition & 0 deletions spec/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3019,6 +3019,7 @@ end
udp_server = udp_server,
kill_tcp_server = kill_tcp_server,
http_server = http_server,
kill_http_server = kill_http_server,
get_proxy_ip = get_proxy_ip,
get_proxy_port = get_proxy_port,
proxy_client = proxy_client,
Expand Down

0 comments on commit 1a88176

Please sign in to comment.