Class: Google::Cloud::Trace::TimeSampler
- Inherits:
-
Object
- Object
- Google::Cloud::Trace::TimeSampler
- Defined in:
- lib/google/cloud/trace/time_sampler.rb
Overview
A sampler determines whether a given request's latency trace should
actually be reported. It is usually not necessary to trace every
request, especially for an application serving heavy traffic. You may
use a sampler to decide, for a given request, whether to report its
trace. A sampler is simply a Proc that takes the Rack environment as an
argument and returns a boolean indicating whether or not to sample the
current request. Alternately, it could be an object that duck-types the
Proc interface by implementing the call
method.
TimeSampler is the default sampler. It bases its sampling decision on two considerations:
- It allows you to blacklist certain URI paths that should never be
traced. For example, the Google App Engine health check request
path
/_ah/health
is blacklisted by default. Kubernetes default health check/healthz
is also ignored. - It spaces samples out by delaying a minimum time between each sample. This enforces a maximum QPS for this Ruby process.
Constant Summary collapse
- DEFAULT_PATH_BLACKLIST =
Default list of paths for which to disable traces. Currently includes App Engine Flex health checks.
["/_ah/health", "/healthz"].freeze
Class Method Summary collapse
-
.default ⇒ TimeSampler
Get the default global TimeSampler.
Instance Method Summary collapse
-
#call(env) ⇒ Boolean
Implements the sampler contract.
-
#initialize(qps: 0.1, path_blacklist: DEFAULT_PATH_BLACKLIST) ⇒ TimeSampler
constructor
Create a TimeSampler for the given QPS.
Constructor Details
#initialize(qps: 0.1, path_blacklist: DEFAULT_PATH_BLACKLIST) ⇒ TimeSampler
Create a TimeSampler for the given QPS.
53 54 55 56 57 |
# File 'lib/google/cloud/trace/time_sampler.rb', line 53 def initialize qps: 0.1, path_blacklist: DEFAULT_PATH_BLACKLIST @delay_secs = 1.0 / qps @last_time = ::Time.now.to_f - @delay_secs @path_blacklist = path_blacklist end |
Class Method Details
.default ⇒ TimeSampler
Get the default global TimeSampler.
66 67 68 |
# File 'lib/google/cloud/trace/time_sampler.rb', line 66 def self.default @default end |
Instance Method Details
#call(env) ⇒ Boolean
Implements the sampler contract. Checks to see whether a sample should be taken at this time.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/google/cloud/trace/time_sampler.rb', line 77 def call env return false if path_blacklisted? env time = ::Time.now.to_f delays = (time - @last_time) / @delay_secs if delays >= 2.0 @last_time = time - @delay_secs true elsif delays >= 1.0 @last_time += @delay_secs true else false end end |