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

fix(tracing): ensure sampling decisions apply to correct trace ID #13275

Merged
merged 2 commits into from
Jun 26, 2024

Conversation

samugi
Copy link
Member

@samugi samugi commented Jun 21, 2024

Summary

Kong generates a temporary trace ID when spans are created, this trace ID can be overwritten if tracing headers are passed in the request. Before this change, the global sampling rate from kong.conf was applied to the temporary trace ID, which was only valid for traces created by Kong, and not in distributed tracing scenarios.

This commit ensures the sampling decision always happens during the OTel plugin's access phase, so that it is done after headers have been read, when the trace id is final. This ensures Kong's sampling decision matches that of other tracing systems, to prevent broken traces.

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
  • (no) There is a user-facing docs PR against https://github.com/Kong/docs.konghq.com - PUT DOCS PR HERE

Issue reference

KAG-4785

@nowNick nowNick requested a review from a team June 24, 2024 13:29
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.

Codewise - looks good 👍
... but I'm missing a lot of context

Kong generates a temporary trace ID when spans are created, this trace
ID can be overwritten if tracing headers are passed in the request.
Before this change, the global sampling rate from kong.conf was applied
to the temporary trace ID, which was only valid for traces created by
Kong, and not in distributed tracing scenarios.

This commit ensures the sampling decision is only taken once, after
headers have been read, when the trace id is final. This ensures Kong's
sampling decision matches that of other tracing systems and therefore
guarantees that traces are not broken when sampling rates are set to
the same values.
@samugi
Copy link
Member Author

samugi commented Jun 26, 2024

I removed a small part of the diff (one less change to the tracer). Initially I also wanted to ensure the sampler is only ran once, and removed it on span creation so that the sampling decision is only taken by the OTel plugin. On second thought I realized that could be breaking for any users who rely on the PDK's logic and are not using the OTel plugin, so I reduced the scope of this PR. All we want to fix this is that, during the access phase, both the local (plugin) and the global (kong.conf) sampling rates are taken into account (with precedence on the plugin) to take the sampling decision.

Copy link
Contributor

@StarlightIbuki StarlightIbuki left a comment

Choose a reason for hiding this comment

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

LGTM after offline discussion

@samugi samugi merged commit ddfe7d3 into master Jun 26, 2024
25 checks passed
@samugi samugi deleted the fix/otel-sampler branch June 26, 2024 11:53
@team-gateway-bot
Copy link
Collaborator

Successfully created cherry-pick PR for master:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cherry-pick kong-ee schedule this PR for cherry-picking to kong/kong-ee core/pdk core/tracing size/M
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants