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.
Package
OpenTelemetry
Package Version
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
TraceIdRatioBasedSamplermakes 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 ifGetLowerLongreturnslong.MinValue. It's impossible to get an absolute value oflong.MinValuebecause the corresponding positive value is bigger thenlong.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.Minequivalent like this:Steps to Reproduce
long.MinValueTraceIdRatioBasedSamplerExpected Result
TraceIdRatioBasedSamplershould never throw regardless of which trace id is being sampledActual Result
Additional Context
No response
Tip
React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding
+1orme too, to help us triage it. Learn more here.