Skip to content

[bug] Rare but deadly computational bug in TraceIdRatioBasedSampler #6928

Description

@ArtemSerostanov

Package

OpenTelemetry

Package Version

Package Name Version
OpenTelemetry 1.15.0

Runtime Version

net10.0

Description

Unfortunately, I was unable to compile the source code locally, so I'm filing an issue instead of a full-blown pull request.

The problem

TraceIdRatioBasedSampler makes a decision on whether to sample a trace or not by converting a trace id to a long value and then comparing this value to lower and upper boundaries. However, the process of converting a trace id to long is bugged.
More specifically, the call Math.Abs(GetLowerLong(traceIdBytes) throws if GetLowerLong returns long.MinValue. It's impossible to get an absolute value of long.MinValue because the corresponding positive value is bigger then long.MaxValue.

While I understand that this problem is very rare (one has to generate a trace id that converts exactly to long.MinValue), the consequences are fatal. Throwing an exception from a sampler essentially discard the entire http request.

The solution

Implement a "safe" Math.Min equivalent like this:

    private static long SafeAbs(long value)
    {
        if (value == long.MinValue)
        {
            return long.MaxValue;
        }
        else
        {
            return Math.Abs(value);
        }
    }

Steps to Reproduce

  • Create a mock trace id generator that generates trace ids that convert to long.MinValue
  • Feed these trace ids to TraceIdRatioBasedSampler

Expected Result

TraceIdRatioBasedSampler should never throw regardless of which trace id is being sampled

Actual Result

System.OverflowException: Negating the minimum value of a twos complement number is invalid.

Additional Context

No response

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedGood for taking. Extra help will be provided by maintainerspkg:OpenTelemetryIssues related to OpenTelemetry NuGet package

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions