Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Probability sampler is broken for low sample rates #378

@akoshochrein

Description

@akoshochrein

Hello everyone,

I've found an issue with the probability sampler not working for low sample rates. There are 2 piecens of code related to this:
https://github.com/census-instrumentation/opencensus-python/blob/master/opencensus/trace/samplers/probability.py#L56-L66
As the code says, it fetches the last 8 bytes of the trace id, that will later be used to compare to a constant value calculated using the sample rate. This algorithm is, in fact, flawless and propagates traces perfectly.
The issue comes from a bit deeper:
https://github.com/census-instrumentation/opencensus-python/blob/master/opencensus/trace/span_context.py#L165-L173
As a matter of fact, uuid4 is not an entirely random number according to the uuid specs.
Here's some empyrical data as proof:
https://gist.github.com/akoskaaa/a53c8b0201a8bffc1ae4df91e5965933

Source:
https://tools.ietf.org/html/rfc4122
https://en.wikipedia.org/wiki/Universally_unique_identifier

Solution could be to change the tracing id generation to a more random one. I've actually patched it in my systems with this function:

def patched_generate_trace_id():
    hexdigits = '0123456789abcdef'
    return ''.join([hexdigits[random.randint(0, 0xF)] for _ in range(32)])

Do you have any better ideas?

Also, I'd just like to say thanks to the owners and everyone who put time into making this happen. Super useful!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions