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: support TraceState in SamplingResult #3530

Merged

Conversation

raphael-theriault-swi
Copy link
Contributor

Which problem is this PR solving?

This PR makes it possible for samplers to update the tracestate by passing it to the sampling result, as introduced in version 0.7 of the spec.

Fixes #2974

Short description of the changes

A new traceState?: TraceState field has been added to SamplingResult. If present, it is used by the Tracer when creating the new span. If not present or undefined, the existing behaviour of using the parent's applies. This maintains semver compatibility and makes it easier to forward an existing tracestate. It is still possible to clear the tracestate by explicitly passing an empty one.

The builtin samplers have been updated to explicitly forward the tracestate of the passed in parent context as recommended by the spec.

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist:

  • Followed the style guidelines of this project
  • Unit tests have been added
  • Documentation has been updated

@raphael-theriault-swi raphael-theriault-swi requested a review from a team January 12, 2023 18:39
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jan 12, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

@@ -117,6 +117,10 @@ export class Tracer implements api.Tracer {
links
);

// according to the spec an empty Tracestate means it should be cleared but
Copy link
Member

Choose a reason for hiding this comment

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

I think this depends on how you define empty tracestate.
I would interpret new TraceState() as empty and this is working as specified.

not interpreting samplingResult.traceState == null sounds correct to me and not just a backward compat solution.

Copy link
Contributor Author

@raphael-theriault-swi raphael-theriault-swi Jan 13, 2023

Choose a reason for hiding this comment

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

Would it make sense to handle null given the same can be done by passing the result of createTraceState ? It's shorter but I don't remember seeing null anywhere else in the API.

Either way agreed the comment isn't great, and the behaviour should probably also be described in the new field's doc comment.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should add special handling for null. I used == null above which includes null and undefined like the ?? operator used in your change.

If a sampler want's to clear the TraceState it should return an empty instance by calling api.createTraceState().
But I would expect that a sample is usually not clearing the whole TraceState as it may contain arbitrary keys not related to sampling. Instead I assume they only set/clear/update sampler specific keys like the ot key used by consistent probability samplers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gotcha, so the behaviour should already be correct. I updated the comments.

Comment on lines 64 to 65
* the new {@link SpanContext}. Samplers SHOULD return the passed-in
* TraceState if they do not intend to change it. Leaving the value undefined

Choose a reason for hiding this comment

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

Question: The "passed-in TraceState", is it assumed that the sampler will read it off of the context that is passed in? Would it make sense to pass the tracestate as a separate argument to shouldSample?

Choose a reason for hiding this comment

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

Alternatively, perhaps this comment should mention that the TraceState from the passed-in context, just to avoid confusion?

Copy link
Member

Choose a reason for hiding this comment

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

I think it would be quite confusing to pass context and some information which is usually on context as an additional parameter. Samplers have to decide then which source they prefer and if they should use the other as fallback,

Copy link

@henrinormak henrinormak Jan 18, 2023

Choose a reason for hiding this comment

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

I agree, but reading this comment made me wonder, "passed-in where?" and immediately assumed that the function signature is out of date with this comment (that's me coming from a background of having a few custom samplers, but never having worked with tracestate). I agree that having a single source of truth is preferred, so perhaps the comment should be modified to clarify what "passed-in" refers to?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, updated the doc comment to clarify

@henrinormak
Copy link

Any ETA on when this might land? Looking forward to this ability in the samplers!

@cheempz
Copy link

cheempz commented Jan 30, 2023

Hi @Flarna and/or @open-telemetry/javascript-approvers, any chance this PR can be fully approved and merged soon? This is a critical piece in our effort to adopt OTel and we'd appreciate any help you can give :)

@codecov
Copy link

codecov bot commented Jan 30, 2023

Codecov Report

Merging #3530 (4985bac) into main (c32a845) will decrease coverage by 0.75%.
The diff coverage is 100.00%.

❗ Current head 4985bac differs from pull request most recent head c5526fa. Consider uploading reports for the commit c5526fa to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3530      +/-   ##
==========================================
- Coverage   93.98%   93.24%   -0.75%     
==========================================
  Files         260      253       -7     
  Lines        7802     7330     -472     
  Branches     1620     1534      -86     
==========================================
- Hits         7333     6835     -498     
- Misses        469      495      +26     
Impacted Files Coverage Δ
api/src/trace/SamplingResult.ts 100.00% <ø> (ø)
...ckages/opentelemetry-sdk-trace-base/src/Sampler.ts 100.00% <ø> (ø)
...ackages/opentelemetry-sdk-trace-base/src/Tracer.ts 98.59% <100.00%> (+0.02%) ⬆️
packages/opentelemetry-sdk-trace-web/src/utils.ts 65.83% <0.00%> (-29.20%) ⬇️
...emetry-core/src/platform/node/RandomIdGenerator.ts 87.50% <0.00%> (-6.25%) ⬇️
...s/opentelemetry-instrumentation-fetch/src/fetch.ts
...es/opentelemetry-context-zone-peer-dep/src/util.ts
...ry-context-zone-peer-dep/src/ZoneContextManager.ts
...mentation-xml-http-request/src/enums/EventNames.ts
...ation-xml-http-request/src/enums/AttributeNames.ts
... and 2 more

Copy link
Member

@legendecas legendecas 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 for working on this! LGTM

@cheempz
Copy link

cheempz commented Feb 8, 2023

@legendecas @Flarna could you rustle up that final reviewer/approver please?

@legendecas legendecas merged commit 65e83d4 into open-telemetry:main Feb 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support TraceStates on SamplingResults
6 participants