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

traceIDRatioSampler: use rightmost bits #3557

Conversation

danielmmetz
Copy link
Contributor

@danielmmetz danielmmetz commented Dec 23, 2022

Before this PR:

The traceIDRatioSampler, when used with the xray.IDGenerator, does not make random sampling decisions.
In detail, the xray.IDGenerator generates trace.TraceIDs such that the first 4 bytes represent a timestamp, with the remaining 12 bytes being randomly generated.
The traceIDRatioSampler uses the first 8 bytes to make sampling decisions which means in practice any provided ratio ends up being effectively 0 or 1.0 for any given point in time.

Stated differently, for (nearly) any fixed point in time any provided ratio will be (effectivley ignored) and the sampler will consistently choose to either drop all traces or to record all traces, with no mix between.

After this PR:

The traceIDRatioSampler uses the last 8 bytes of the trace.TraceID, which lines up with the fully random portion of the ID generated by xray.IDGenerator. Also brings the Go implementation into better alignment with its Python and Java equivalents (code references linked later).

Demo

Check out https://go.dev/play/p/qpxm-boZuxj. Notably, at a sample rate of 0.25, I get sampled 0/10000 (0.000000%) but for a sample rate of 0.5, I get sampled 10000/10000 (100.000000%).

Alternatives considered

The existing traceIDRatioSampler's implementation is correct under the assumption that the first values for 8 bytes are distributed uniformly randomly. With that in mind, we could consider this to be a bug with the ID generator for breaking this requirement, or just conclude the two are incompatible. In that direction, I think we'd expect the xray package to provide a ratio sampler of its own, (and to call out clearly in documentation that it's incompatible with this package's ratio sampler).

However,

  1. anecdotally, embedding non-random data into the leftmost bits of IDs during generation doesn't seem uncommon.
  2. the Python and Java implementations of the TraceIDRatioSampler also use the rightmost bits of the trace IDs for decision making.

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Dec 23, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: danielmmetz / name: Daniel Metz (57736df)

@codecov
Copy link

codecov bot commented Dec 24, 2022

Codecov Report

Merging #3557 (fa78ac7) into main (69e44a3) will increase coverage by 0.0%.
The diff coverage is 100.0%.

Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main   #3557   +/-   ##
=====================================
  Coverage   78.0%   78.1%           
=====================================
  Files        164     164           
  Lines      11815   11815           
=====================================
+ Hits        9226    9232    +6     
+ Misses      2393    2387    -6     
  Partials     196     196           
Impacted Files Coverage Δ
sdk/trace/sampling.go 100.0% <100.0%> (ø)
sdk/metric/periodic_reader.go 91.3% <0.0%> (+1.2%) ⬆️
sdk/trace/batch_span_processor.go 81.9% <0.0%> (+1.7%) ⬆️

Copy link
Member

@hanyuancheung hanyuancheung left a comment

Choose a reason for hiding this comment

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

👍🏻

@Aneurysm9 Aneurysm9 merged commit 9d633d2 into open-telemetry:main Dec 30, 2022
@danielmmetz
Copy link
Contributor Author

I appreciate the reviews and merge, thanks! Would you also be able to share an estimate for when the next release will be cut?

@MrAlias MrAlias added this to the Release v1.12.0 milestone Jan 27, 2023
@MrAlias MrAlias mentioned this pull request Jan 28, 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.

4 participants