Skip to content

Commit

Permalink
test(opentelemetry): support variable resource attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
SuzyWangIBMer authored and samugi committed Dec 23, 2024
1 parent 5ab8c15 commit 5b0da6d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
message: |
**Opentelemetry**: Support variable resource attributes
type: feature
scope: Plugin
12 changes: 12 additions & 0 deletions spec/03-plugins/37-opentelemetry/02-schema_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,16 @@ describe("Plugin: OpenTelemetry (schema)", function()
}
}, err)
end)

it("accepts variable values", function()
local ok, err = validate_plugin_config_schema({
endpoint = "http://example.dev",
resource_attributes = {
foo = "$(headers.host)",
},
}, schema_def)

assert.truthy(ok)
assert.is_nil(err)
end)
end)
20 changes: 13 additions & 7 deletions spec/03-plugins/37-opentelemetry/04-exporter_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,8 @@ for _, strategy in helpers.each_strategy() do
resource_attributes = {
["service.name"] = "kong_oss",
["os.version"] = "debian",
["host.name"] = "$(headers.host)",
["validstr"] = "$($@#)",
}
})
mock = helpers.http_mock(HTTP_SERVER_PORT_TRACES, { timeout = HTTP_MOCK_TIMEOUT })
Expand Down Expand Up @@ -608,13 +610,17 @@ for _, strategy in helpers.each_strategy() do
local res_attr = decoded.resource_spans[1].resource.attributes
sort_by_key(res_attr)
-- resource attributes
assert.same("os.version", res_attr[1].key)
assert.same({string_value = "debian", value = "string_value"}, res_attr[1].value)
assert.same("service.instance.id", res_attr[2].key)
assert.same("service.name", res_attr[3].key)
assert.same({string_value = "kong_oss", value = "string_value"}, res_attr[3].value)
assert.same("service.version", res_attr[4].key)
assert.same({string_value = kong.version, value = "string_value"}, res_attr[4].value)
assert.same("host.name", res_attr[1].key)
assert.same({string_value = "0.0.0.0:" .. PROXY_PORT, value = "string_value"}, res_attr[1].value)
assert.same("os.version", res_attr[2].key)
assert.same({string_value = "debian", value = "string_value"}, res_attr[2].value)
assert.same("service.instance.id", res_attr[3].key)
assert.same("service.name", res_attr[4].key)
assert.same({string_value = "kong_oss", value = "string_value"}, res_attr[4].value)
assert.same("service.version", res_attr[5].key)
assert.same({string_value = kong.version, value = "string_value"}, res_attr[5].value)
assert.same("validstr", res_attr[6].key)
assert.same({string_value = "$($@#)", value = "string_value"}, res_attr[6].value)

local scope_spans = decoded.resource_spans[1].scope_spans
assert.is_true(#scope_spans > 0, scope_spans)
Expand Down
53 changes: 53 additions & 0 deletions spec/03-plugins/37-opentelemetry/07-utils_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require("spec.helpers")
local LOG_PHASE = require("kong.pdk.private.phases").phases.log

describe("compile_resource_attributes()", function()
local mock_request
local old_ngx
local compile_resource_attributes

setup(function()
old_ngx = _G.ngx
_G.ngx = {
ctx = {
KONG_PHASE = LOG_PHASE
},
req = {
get_headers = function() return mock_request.headers end,
},
get_phase = function() return "log" end,
}
package.loaded["kong.pdk.request"] = nil
package.loaded["kong.plugins.opentelemetry.utils"] = nil

local pdk_request = require "kong.pdk.request"
kong.request = pdk_request.new(kong)
compile_resource_attributes = require "kong.plugins.opentelemetry.utils".compile_resource_attributes
end)

lazy_teardown(function()
_G.ngx = old_ngx
end)


it("accepts valid template and valid string", function()
mock_request = {
headers = {
host = "kong-test",
},
}
local resource_attributes = {
["valid_variable"] = "$(headers.host)",
["nonexist_variable"] = "$($@#)",
["valid_string"] = "valid",
}
local rendered_resource_attributes, err = compile_resource_attributes(resource_attributes)

assert.is_nil(err)
assert.same(rendered_resource_attributes["valid_variable"], "kong-test")

-- take as a normal string if variable does not exist
assert.same(rendered_resource_attributes["nonexist_variable"], "$($@#)")
assert.same(rendered_resource_attributes["valid_string"], "valid")
end)
end)

1 comment on commit 5b0da6d

@github-actions
Copy link
Contributor

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:5b0da6df3601eedca37054ca87a39290d645212c
Artifacts available https://github.com/Kong/kong/actions/runs/12468830217

Please sign in to comment.