From 5b0da6df3601eedca37054ca87a39290d645212c Mon Sep 17 00:00:00 2001 From: Suzy Wang Date: Wed, 6 Nov 2024 11:28:24 -0800 Subject: [PATCH] test(opentelemetry): support variable resource attributes --- .../feat-variable-resource-attributes.yml | 4 ++ .../37-opentelemetry/02-schema_spec.lua | 12 +++++ .../37-opentelemetry/04-exporter_spec.lua | 20 ++++--- .../37-opentelemetry/07-utils_spec.lua | 53 +++++++++++++++++++ 4 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 changelog/unreleased/kong/feat-variable-resource-attributes.yml create mode 100644 spec/03-plugins/37-opentelemetry/07-utils_spec.lua diff --git a/changelog/unreleased/kong/feat-variable-resource-attributes.yml b/changelog/unreleased/kong/feat-variable-resource-attributes.yml new file mode 100644 index 000000000000..d50d9c2336a4 --- /dev/null +++ b/changelog/unreleased/kong/feat-variable-resource-attributes.yml @@ -0,0 +1,4 @@ +message: | + **Opentelemetry**: Support variable resource attributes +type: feature +scope: Plugin diff --git a/spec/03-plugins/37-opentelemetry/02-schema_spec.lua b/spec/03-plugins/37-opentelemetry/02-schema_spec.lua index 6e3a6c43133e..05cf11865538 100644 --- a/spec/03-plugins/37-opentelemetry/02-schema_spec.lua +++ b/spec/03-plugins/37-opentelemetry/02-schema_spec.lua @@ -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) diff --git a/spec/03-plugins/37-opentelemetry/04-exporter_spec.lua b/spec/03-plugins/37-opentelemetry/04-exporter_spec.lua index bdff5d7a47ec..f92dc1dfd9e0 100644 --- a/spec/03-plugins/37-opentelemetry/04-exporter_spec.lua +++ b/spec/03-plugins/37-opentelemetry/04-exporter_spec.lua @@ -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 }) @@ -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) diff --git a/spec/03-plugins/37-opentelemetry/07-utils_spec.lua b/spec/03-plugins/37-opentelemetry/07-utils_spec.lua new file mode 100644 index 000000000000..134f00ca68df --- /dev/null +++ b/spec/03-plugins/37-opentelemetry/07-utils_spec.lua @@ -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)