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

fix: Prevent filtered traces from biasing the sample rate #1018

Merged
merged 1 commit into from
May 6, 2019

Conversation

AaronFriel
Copy link
Contributor

The URLFilter and MethodsFilter implementations are side-effect
free, 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 the
trace 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:

  1. this.sampler.shouldTrace will return true
  2. this.urlFilter.shouldTrace will return false
  3. the && operation short-circuits and will not call
    this.methodsFilter.shouldTrace

Then if the second request received is to /foobar immediately
afterward, shouldTrace will return false!

  1. this.sampler.shouldTrace returns false because the prior call
    changed the sampling window
  2. the operation short-circuits

Fix

Call the side-effecting sampler method last.

The `URLFilter` and `MethodsFilter` implementations are side-effect
free, 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 the
trace 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:

1. `this.sampler.shouldTrace` will return true
2. `this.urlFilter.shouldTrace` will return false
3. the && operation short-circuits and will not call
   `this.methodsFilter.shouldTrace`

Then if the second request received is to `/foobar` immediately
afterward, `shouldTrace` will return false!

1. `this.sampler.shouldTrace` returns false because the prior call
   changed the sampling window
2. the operation short-circuits

Fix
===

Call the side-effecting sampler method last.
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label May 5, 2019
@codecov
Copy link

codecov bot commented May 5, 2019

Codecov Report

Merging #1018 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1018   +/-   ##
=======================================
  Coverage   94.81%   94.81%           
=======================================
  Files          97       97           
  Lines        6207     6207           
  Branches      486      486           
=======================================
  Hits         5885     5885           
  Misses        183      183           
  Partials      139      139
Impacted Files Coverage Δ
src/tracing-policy.ts 89.65% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2cbf1c2...8cede6b. Read the comment docs.

1 similar comment
@codecov
Copy link

codecov bot commented May 5, 2019

Codecov Report

Merging #1018 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1018   +/-   ##
=======================================
  Coverage   94.81%   94.81%           
=======================================
  Files          97       97           
  Lines        6207     6207           
  Branches      486      486           
=======================================
  Hits         5885     5885           
  Misses        183      183           
  Partials      139      139
Impacted Files Coverage Δ
src/tracing-policy.ts 89.65% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2cbf1c2...8cede6b. Read the comment docs.

Copy link
Contributor

@kjin kjin left a comment

Choose a reason for hiding this comment

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

Makes sense. Thanks!

@kjin kjin merged commit 1832473 into googleapis:master May 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants