Skip to content

Commit

Permalink
feat(tracing): Add instana header support in propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
SuzyWangIBMer committed Nov 23, 2024
1 parent fa9bed9 commit e08a270
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 2 deletions.
4 changes: 4 additions & 0 deletions changelog/unreleased/kong/instana-header-support.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
message: |
**OpenTelemetry**: include instana header support in propagation
type: feature
scope: Plugin
2 changes: 2 additions & 0 deletions kong-3.9.0-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ build = {
["kong.observability.tracing.propagation.extractors.gcp"] = "kong/observability/tracing/propagation/extractors/gcp.lua",
["kong.observability.tracing.propagation.extractors.aws"] = "kong/observability/tracing/propagation/extractors/aws.lua",
["kong.observability.tracing.propagation.extractors.datadog"] = "kong/observability/tracing/propagation/extractors/datadog.lua",
["kong.observability.tracing.propagation.extractors.instana"] = "kong/observability/tracing/propagation/extractors/instana.lua",
["kong.observability.tracing.propagation.injectors._base"] = "kong/observability/tracing/propagation/injectors/_base.lua",
["kong.observability.tracing.propagation.injectors.w3c"] = "kong/observability/tracing/propagation/injectors/w3c.lua",
["kong.observability.tracing.propagation.injectors.b3"] = "kong/observability/tracing/propagation/injectors/b3.lua",
Expand All @@ -691,6 +692,7 @@ build = {
["kong.observability.tracing.propagation.injectors.gcp"] = "kong/observability/tracing/propagation/injectors/gcp.lua",
["kong.observability.tracing.propagation.injectors.aws"] = "kong/observability/tracing/propagation/injectors/aws.lua",
["kong.observability.tracing.propagation.injectors.datadog"] = "kong/observability/tracing/propagation/injectors/datadog.lua",
["kong.observability.tracing.propagation.injectors.instana"] = "kong/observability/tracing/propagation/injectors/instana.lua",
["kong.observability.tracing.request_id"] = "kong/observability/tracing/request_id.lua",
["kong.observability.tracing.tracing_context"] = "kong/observability/tracing/tracing_context.lua",

Expand Down
57 changes: 57 additions & 0 deletions kong/observability/tracing/propagation/extractors/instana.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
local _EXTRACTOR = require "kong.observability.tracing.propagation.extractors._base"
local propagation_utils = require "kong.observability.tracing.propagation.utils"
local from_hex = propagation_utils.from_hex

local INSTANA_EXTRACTOR = _EXTRACTOR:new({
headers_validate = {
any = {
"x-instana-t",
"x-instana-s",
"x-instana-l",
}
}
})

function INSTANA_EXTRACTOR:get_context(headers)

local trace_id = headers["x-instana-t"]
local span_id = headers["x-instana-s"]
local level_id = headers["x-instana-l"]

if trace_id then
trace_id = trace_id:match("^(%x+)")
if not trace_id then
kong.log.warn("x-instana-t header invalid; ignoring.")
end
end

if span_id then
span_id = span_id:match("^(%x+)")
if not span_id then
kong.log.warn("x-instana-s header invalid; ignoring.")
end
end

if level_id then
-- the flag can come in as "0" or "1"
-- or something like the following format
-- "1,correlationType=web;correlationId=1234567890abcdef"
-- here we only care about the first value
level_id = level_id:match("^([0-1])$")
or level_id:match("^([0-1]),correlationType=(.-);correlationId=(.*)")
end
local should_sample = level_id or "1"



trace_id = from_hex(trace_id) or nil
span_id = from_hex(span_id) or nil

return {
trace_id = trace_id,
span_id = span_id,
should_sample = should_sample,
}
end

return INSTANA_EXTRACTOR
3 changes: 2 additions & 1 deletion kong/observability/tracing/propagation/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ local function get_plugin_params(config)
formats.DATADOG,
formats.AWS,
formats.GCP,

formats.INSTANA
}
propagation_config.inject = { "preserve" }

Expand All @@ -63,6 +63,7 @@ local function get_plugin_params(config)
formats.DATADOG,
formats.AWS,
formats.GCP,
formats.INSTANA,
}
propagation_config.inject = {
-- the old logic used to propagate the "found" incoming format
Expand Down
31 changes: 31 additions & 0 deletions kong/observability/tracing/propagation/injectors/instana.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
local _INJECTOR = require "kong.observability.tracing.propagation.injectors._base"
local to_hex = require "resty.string".to_hex

local INSTANA_INJECTOR = _INJECTOR:new({
name = "instana",
context_validate = {}, -- all fields are optional
trace_id_allowed_sizes = { 16 },
span_id_size_bytes = 16,
})


function INSTANA_INJECTOR:create_headers(out_tracing_ctx)
local headers = {
["x-instana-t"] = to_hex(out_tracing_ctx.trace_id) or nil,
["x-instana-s"] = to_hex(out_tracing_ctx.span_id) or nil,
}

if out_tracing_ctx.should_sample ~= nil then
headers["x-instana-l"] = out_tracing_ctx.should_sample and "1" or "0"
end

return headers
end


function INSTANA_INJECTOR:get_formatted_trace_id(trace_id)
return { instana = to_hex(trace_id) }
end


return INSTANA_INJECTOR
1 change: 1 addition & 0 deletions kong/observability/tracing/propagation/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ local FORMATS = {
DATADOG = "datadog",
AWS = "aws",
GCP = "gcp",
INSTANA = "instana",
}

local function hex_to_char(c)
Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/opentelemetry/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ return {
old_default = "preserve" },
required = false,
default = "preserve",
one_of = { "preserve", "ignore", "b3", "b3-single", "w3c", "jaeger", "ot", "aws", "gcp", "datadog" } } },
one_of = { "preserve", "ignore", "b3", "b3-single", "w3c", "jaeger", "ot", "aws", "gcp", "datadog", "instana" } } },
{ sampling_rate = {
description = "Tracing sampling rate for configuring the probability-based sampler. When set, this value supersedes the global `tracing_sampling_rate` setting from kong.conf.",
type = "number",
Expand Down

0 comments on commit e08a270

Please sign in to comment.