-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from nicolas/client-sampling
implement client sampling
- Loading branch information
Showing
5 changed files
with
189 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
|
||
module Datadog | ||
# \Sampler performs client-side trace sampling. | ||
class Sampler | ||
def sample(_span) | ||
raise NotImplementedError, 'samplers have to implement the sample() method' | ||
end | ||
end | ||
|
||
# \AllSampler samples all the traces. | ||
class AllSampler < Sampler | ||
def sample(span) | ||
span.sampled = true | ||
end | ||
end | ||
|
||
# \RateSampler is based on a sample rate. | ||
class RateSampler < Sampler | ||
KNUTH_FACTOR = 1111111111111111111 | ||
SAMPLE_RATE_METRIC_KEY = '_sample_rate'.freeze() | ||
|
||
attr_reader :sample_rate | ||
|
||
# Initialize a \RateSampler. | ||
# This sampler keeps a random subset of the traces. Its main purpose is to | ||
# reduce the instrumentation footprint. | ||
# | ||
# * +sample_rate+: the sample rate as a \Float between 0.0 and 1.0. 0.0 | ||
# means that no trace will be sampled; 1.0 means that all traces will be | ||
# sampled. | ||
def initialize(sample_rate = 1.0) | ||
unless sample_rate > 0.0 && sample_rate <= 1.0 | ||
Datadog::Tracer.log.error('sample rate is not between 0 and 1, disabling the sampler') | ||
sample_rate = 1.0 | ||
end | ||
|
||
self.sample_rate = sample_rate | ||
end | ||
|
||
def sample_rate=(sample_rate) | ||
@sample_rate = sample_rate | ||
@sampling_id_threshold = sample_rate * Span::MAX_ID | ||
end | ||
|
||
def sample(span) | ||
span.sampled = ((span.trace_id * KNUTH_FACTOR) % Datadog::Span::MAX_ID) <= @sampling_id_threshold | ||
span.set_metric(SAMPLE_RATE_METRIC_KEY, @sample_rate) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
|
||
require 'helper' | ||
require 'ddtrace/sampler' | ||
|
||
class SamplerTest < Minitest::Test | ||
def test_all_sampler | ||
spans = [Datadog::Span.new(nil, '', trace_id: 1), | ||
Datadog::Span.new(nil, '', trace_id: 2), | ||
Datadog::Span.new(nil, '', trace_id: 3)] | ||
|
||
sampler = Datadog::AllSampler.new() | ||
|
||
spans.each do |span| | ||
sampler.sample(span) | ||
assert_equal(true, span.sampled) | ||
end | ||
end | ||
|
||
def test_rate_sampler_invalid | ||
sampler = Datadog::RateSampler.new(-1.0) | ||
assert_equal(1.0, sampler.sample_rate) | ||
|
||
sampler = Datadog::RateSampler.new(0.0) | ||
assert_equal(1.0, sampler.sample_rate) | ||
|
||
sampler = Datadog::RateSampler.new(1.5) | ||
assert_equal(1.0, sampler.sample_rate) | ||
end | ||
|
||
def test_rate_sampler_1_0 | ||
spans = [Datadog::Span.new(nil, '', trace_id: 1), | ||
Datadog::Span.new(nil, '', trace_id: 2), | ||
Datadog::Span.new(nil, '', trace_id: 3)] | ||
|
||
sampler = Datadog::RateSampler.new(1.0) | ||
|
||
spans.each do |span| | ||
sampler.sample(span) | ||
assert_equal(true, span.sampled) | ||
assert_equal(1.0, span.get_metric(Datadog::RateSampler::SAMPLE_RATE_METRIC_KEY)) | ||
end | ||
end | ||
|
||
def test_rate_sampler | ||
prng = Random.new(123) | ||
|
||
[0.1, 0.25, 0.5, 0.9].each do |sample_rate| | ||
nb_spans = 1000 | ||
spans = Array.new(nb_spans) do | ||
Datadog::Span.new(nil, '', trace_id: prng.rand(Datadog::Span::MAX_ID)) | ||
end | ||
|
||
sampler = Datadog::RateSampler.new(sample_rate) | ||
|
||
spans.each do |span| | ||
sampler.sample(span) | ||
|
||
if span.sampled | ||
assert_equal(sample_rate, | ||
span.get_metric(Datadog::RateSampler::SAMPLE_RATE_METRIC_KEY)) | ||
end | ||
end | ||
|
||
sampled_spans = spans.select(&:sampled) | ||
expected = nb_spans * sample_rate | ||
assert_in_delta(sampled_spans.length, expected, 0.1 * expected) | ||
end | ||
end | ||
|
||
def test_tracer_with_rate_sampler | ||
prng = Random.new(123) | ||
|
||
tracer = get_test_tracer() | ||
tracer.configure(sampler: Datadog::RateSampler.new(0.5)) | ||
|
||
nb_spans = 1000 | ||
nb_spans.times do | ||
span = tracer.trace('test', trace_id: prng.rand(Datadog::Span::MAX_ID)) | ||
span.finish() | ||
end | ||
|
||
spans = tracer.writer.spans() | ||
expected = nb_spans * 0.5 | ||
assert_in_delta(spans.length, expected, 0.1 * expected) | ||
|
||
spans.each do |span| | ||
assert_equal(0.5, span.get_metric(Datadog::RateSampler::SAMPLE_RATE_METRIC_KEY)) | ||
end | ||
end | ||
end |