-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(opentelemetry): support variable resource attributes #13839
base: master
Are you sure you want to change the base?
feat(opentelemetry): support variable resource attributes #13839
Conversation
failing test is expected, let's rebase after #13877 is merged |
6dc12b9
to
eaf8f25
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much for your contribution, this is great! I left a few comments for things that I feel could be improved a bit. There's some code duplication with request-transformer
that we may want to take care of, but that refactor can be done later.
kong/plugins/opentelemetry/utils.lua
Outdated
@@ -13,6 +21,8 @@ local DEFAULT_HEADERS = { | |||
|
|||
local _log_prefix = "[otel] " | |||
|
|||
local compiled_resource_attributes = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keeping this variable at the module level's scope can be problematic because it will persist across different requests, which may introduce cross-request pollution. I'd recommend we just return the compiled resource attributes from the function that computes them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@samugi Thanks for quick response. I have updated the pr as suggested.
kong/plugins/opentelemetry/utils.lua
Outdated
end | ||
|
||
if res then | ||
current_name = current_name:lower() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this needed?
@@ -122,7 +122,7 @@ end | |||
|
|||
local function http_export_traces(conf, spans) | |||
local headers = get_headers(conf.headers) | |||
local payload = encode_traces(spans, conf.resource_attributes) | |||
local payload = encode_traces(spans, otel_utils.compiled_resource_attributes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(see other comments)
kong/plugins/opentelemetry/logs.lua
Outdated
@@ -21,7 +21,7 @@ local prepare_logs = otlp.prepare_logs | |||
|
|||
local function http_export_logs(conf, logs_batch) | |||
local headers = get_headers(conf.headers) | |||
local payload = encode_logs(logs_batch, conf.resource_attributes) | |||
local payload = encode_logs(logs_batch, otel_utils.compiled_resource_attributes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(see my comments in the traces.lua
file)
if conf.resource_attributes then | ||
otel_utils.read_resource_attributes(conf.resource_attributes) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of reading resource attributes in the access()
phase, we can do the rendering of the attributes in log()
and pass the result as part of the conf
parameter, when we enqueue the entry to be processed by http_export_traces
. This way we don't need a global variable anymore (see my other comment in utils.lua
), and is also more performant because the rendering will happen out of the hot path, after the response is already returned to the client.
kong/plugins/opentelemetry/utils.lua
Outdated
local compiled_templates = template_cache[resource_attributes] | ||
if not compiled_templates then | ||
compiled_templates = {} | ||
-- store it by `config_array` which is part of the plugin `conf` table |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
caching looks good here. Please update the comment which still refers to the original code (mentioning config_array).
kong/plugins/opentelemetry/utils.lua
Outdated
for current_name, current_value in pairs(resource_attributes) do | ||
local res, err = param_value(current_value, resource_attributes, template_env) | ||
if err then | ||
return error(" failed to render the template " .. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
error
is a bit harsh here, it can make sense in request-transformer where a failure in modifying the request can have security implications, but here I would just log an error and then leave the original (raw) value for the attribute, wdyt?
@@ -33,4 +33,16 @@ describe("Plugin: OpenTelemetry (schema)", function() | |||
} | |||
}, err) | |||
end) | |||
|
|||
it("accepts variable values", function() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you add a test for a failing template as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't touch original resource attributes schema much, except than adding a compiled template. It is still a map with boolean/string/number type. In the future, we might add validator limiting variable to be existing env vars.
eaf8f25
to
8a185af
Compare
Summary
This change allows render resource attributes as lua code.
For example,
host.id
is0.0.0.0:9000
or depends onheaders.host
value.Checklist
changelog/unreleased/kong
orskip-changelog
label added on PR if changelog is unnecessary. README.mdIssue reference
Fix #12538