From 8a185afcff3da67f4df2d0ef8e26db5a3d54ec99 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 --- .../kong/feat-variable-resource-attributes.yml | 4 ++++ .../37-opentelemetry/02-schema_spec.lua | 12 ++++++++++++ .../37-opentelemetry/04-exporter_spec.lua | 17 ++++++++++------- 3 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 changelog/unreleased/kong/feat-variable-resource-attributes.yml 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..47bf6bd910e8 100644 --- a/spec/03-plugins/37-opentelemetry/04-exporter_spec.lua +++ b/spec/03-plugins/37-opentelemetry/04-exporter_spec.lua @@ -567,6 +567,7 @@ for _, strategy in helpers.each_strategy() do resource_attributes = { ["service.name"] = "kong_oss", ["os.version"] = "debian", + ["host.name"] = "$(headers.host)", } }) mock = helpers.http_mock(HTTP_SERVER_PORT_TRACES, { timeout = HTTP_MOCK_TIMEOUT }) @@ -608,13 +609,15 @@ 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:9000", 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) local scope_spans = decoded.resource_spans[1].scope_spans assert.is_true(#scope_spans > 0, scope_spans)