Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

SuzyWangIBMer
Copy link

@SuzyWangIBMer SuzyWangIBMer commented Nov 6, 2024

Summary

This change allows render resource attributes as lua code.
For example,

  config:
    traces_endpoint: "https://otlp-red-saas.instana.io:4318/v1/traces"
    resource_attributes:
      host.id: "$(headers.host)"

host.id is 0.0.0.0:9000 or depends on headers.host value.

Checklist

  • The Pull Request has tests
  • A changelog file has been created under changelog/unreleased/kong or skip-changelog label added on PR if changelog is unnecessary. README.md
  • There is a user-facing docs PR against https://github.com/Kong/docs.konghq.com - PUT DOCS PR HERE

Issue reference

Fix #12538

@github-actions github-actions bot added plugins/opentelemetry schema-change-noteworthy cherry-pick kong-ee schedule this PR for cherry-picking to kong/kong-ee labels Nov 6, 2024
@team-eng-enablement team-eng-enablement added the author/community PRs from the open-source community (not Kong Inc) label Nov 6, 2024
@samugi samugi self-assigned this Nov 15, 2024
@samugi
Copy link
Member

samugi commented Nov 15, 2024

failing test is expected, let's rebase after #13877 is merged

@samugi samugi force-pushed the feat/otel_variable_resource_attributes branch from 6dc12b9 to eaf8f25 Compare November 15, 2024 13:31
@samugi samugi self-requested a review November 15, 2024 17:07
Copy link
Member

@samugi samugi left a 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 Show resolved Hide resolved
kong/plugins/opentelemetry/utils.lua Outdated Show resolved Hide resolved
kong/plugins/opentelemetry/traces.lua Outdated Show resolved Hide resolved
kong/plugins/opentelemetry/logs.lua Outdated Show resolved Hide resolved
kong/plugins/opentelemetry/handler.lua Outdated Show resolved Hide resolved
kong/plugins/opentelemetry/utils.lua Outdated Show resolved Hide resolved
kong/plugins/opentelemetry/utils.lua Outdated Show resolved Hide resolved
@SuzyWangIBMer SuzyWangIBMer force-pushed the feat/otel_variable_resource_attributes branch from eaf8f25 to 8a185af Compare November 18, 2024 22:21
@SuzyWangIBMer SuzyWangIBMer requested a review from samugi November 20, 2024 13:34
kong/plugins/opentelemetry/handler.lua Outdated Show resolved Hide resolved
kong/plugins/opentelemetry/utils.lua Outdated Show resolved Hide resolved
@SuzyWangIBMer SuzyWangIBMer force-pushed the feat/otel_variable_resource_attributes branch 2 times, most recently from 0581567 to 4cfc804 Compare November 21, 2024 19:49
@SuzyWangIBMer SuzyWangIBMer requested a review from samugi November 21, 2024 19:50
Copy link
Member

@samugi samugi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the quick action! I left a couple more suggestions, then I think it's good to go.

kong/plugins/opentelemetry/handler.lua Outdated Show resolved Hide resolved
kong/plugins/opentelemetry/logs.lua Show resolved Hide resolved
kong/plugins/opentelemetry/traces.lua Show resolved Hide resolved
kong/plugins/opentelemetry/utils.lua Outdated Show resolved Hide resolved
kong/plugins/opentelemetry/handler.lua Show resolved Hide resolved
@SuzyWangIBMer SuzyWangIBMer force-pushed the feat/otel_variable_resource_attributes branch from 4cfc804 to aba7105 Compare November 22, 2024 15:31
@SuzyWangIBMer SuzyWangIBMer requested a review from samugi November 22, 2024 16:12
@SuzyWangIBMer SuzyWangIBMer force-pushed the feat/otel_variable_resource_attributes branch from aba7105 to 48c4be0 Compare November 25, 2024 18:36
Copy link
Member

@samugi samugi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thank you!!

Could you just update the documentation line for this configuration field in the plugin's schema? This is used to auto-generate docs.

@SuzyWangIBMer SuzyWangIBMer force-pushed the feat/otel_variable_resource_attributes branch 2 times, most recently from f9e3afb to 4e2c7ab Compare November 28, 2024 17:11
Copy link
Member

@samugi samugi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great job and thanks again!

Edit: sorry about the failing CI, it's because the new schema description exceeded the length limit, if you undo that change I'll take care of updating the plugin's docs as required.

@SuzyWangIBMer SuzyWangIBMer force-pushed the feat/otel_variable_resource_attributes branch from 4e2c7ab to d5a0c23 Compare November 28, 2024 18:02
@SuzyWangIBMer SuzyWangIBMer requested a review from samugi December 2, 2024 13:34
@SuzyWangIBMer
Copy link
Author

Do we need a second reviewer to get this in?

@samugi
Copy link
Member

samugi commented Dec 5, 2024

yes @SuzyWangIBMer I'm trying to get the second approval, thank you for your patience

kong/plugins/opentelemetry/utils.lua Outdated Show resolved Hide resolved
return {
http_export_request = http_export_request,
get_headers = get_headers,
_log_prefix = _log_prefix,
read_resource_attributes = read_resource_attributes,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we have a unit test for this function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We now have a test for possible resource attributes sent and received. I can also work on one if you think it does not cover everything.

setup_instrumentations("all", {
resource_attributes = {
["service.name"] = "kong_oss",
["os.version"] = "debian",
["host.name"] = "$(headers.host)",
["validstr"] = "$($@#)",
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SuzyWangIBMer I think @fffonion is referring to the fact we could have a dedicated unit test for this function, in addition to the integration test you've mentioned, for example in a spec/03-plugins/37-opentelemetry/06-utils_spec.lua file.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I will work on one.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR updated, let me know what do you guys think.

kong/plugins/opentelemetry/utils.lua Outdated Show resolved Hide resolved
kong/plugins/opentelemetry/handler.lua Show resolved Hide resolved
@SuzyWangIBMer SuzyWangIBMer force-pushed the feat/otel_variable_resource_attributes branch from d5a0c23 to 982f2a3 Compare December 5, 2024 22:11
@SuzyWangIBMer SuzyWangIBMer force-pushed the feat/otel_variable_resource_attributes branch from 982f2a3 to 7239a60 Compare December 13, 2024 03:21
@SuzyWangIBMer SuzyWangIBMer force-pushed the feat/otel_variable_resource_attributes branch from 7239a60 to f56f481 Compare December 17, 2024 17:45
@samugi samugi requested a review from nowNick December 18, 2024 08:22
Copy link
Contributor

@nowNick nowNick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks great! Thank you @SuzyWangIBMer for your contribution! 🚀

Copy link
Member

@samugi samugi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SuzyWangIBMer please address these, then CI should become green and we should be ready.

@@ -1,9 +1,17 @@
local http = require "resty.http"
local clone = require "table.clone"

local sandbox = require "kong.tools.sandbox"
local deep_copy = require("kong.tools.table").deep_copy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
local deep_copy = require("kong.tools.table").deep_copy
local cycle_aware_deep_copy = require("kong.tools.table").cycle_aware_deep_copy

sorry, I noticed this last-minute

local template_env = {}
if lua_enabled and sandbox_enabled then
-- load the sandbox environment to be used to render the template
template_env = deep_copy(sandbox.configuration.environment)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
template_env = deep_copy(sandbox.configuration.environment)
template_env = cycle_aware_deep_copy(sandbox.configuration.environment)

@@ -0,0 +1,52 @@
local LOG_PHASE = require("kong.pdk.private.phases").phases.log

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
require "spec.helpers"

we still need to require helpers because it does some needed setup on the environment, but don't assign it to a local variable, or else the linter will complain because it's unused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author/community PRs from the open-source community (not Kong Inc) cherry-pick kong-ee schedule this PR for cherry-picking to kong/kong-ee plugins/opentelemetry schema-change-noteworthy size/L
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OTEL plugin: No way to set variable values to resource attributes
6 participants