fix: Prevent filtered traces from biasing the sample rate #1018
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
URLFilter
andMethodsFilter
implementations are side-effectfree, and are safe to run in any order. However,
sampler.shouldTrace
is not, a result of
true
from it has the side effect of altering thetrace window.
Expected behavior
RPCs to a filtered URL or method should not affect sample rates of
unfiltered URLs or methods.
Actual behavior
A high rate of RPCs relative to the sample rate that are filtered causes
the actual sampling to be biased and below the expected rate.
Suppose that external RPCs occur at the same frequency as interval as
health checks: the resulting sample rate will be approximately 1/2 the
specified rate, because it will be (about) a coin flip whether the first
request in an interval is a healthcheck or a valid RPC.
If the first request received within a sampling window has a filtered
url,
shouldTrace
will return false, because:this.sampler.shouldTrace
will return truethis.urlFilter.shouldTrace
will return falsethis.methodsFilter.shouldTrace
Then if the second request received is to
/foobar
immediatelyafterward,
shouldTrace
will return false!this.sampler.shouldTrace
returns false because the prior callchanged the sampling window
Fix
Call the side-effecting sampler method last.