Skip to content

Commit 690b189

Browse files
ufootEmanuele Palazzetti
authored andcommitted
[dual sampling] support -1 as a priority sampling value (#290)
* [dual sampling] updated tests to make sure -1 and 2 are handled * [dual sampling] updated doc to mention rules about -1 and 2 sampling priority values * [dual sampling] adding constants for sampling priority values * [dual sampling] rephrased documentation
1 parent 5df711a commit 690b189

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

docs/GettingStarted.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -722,9 +722,10 @@ overhead.
722722

723723
Priority sampling consists in deciding if a trace will be kept by using a priority attribute that will be propagated for distributed traces. Its value gives indication to the Agent and to the backend on how important the trace is.
724724

725-
* 0: Don’t keep the trace.
726-
* 1: The sampler automatically decided to keep the trace.
727-
* 2: The user asked the keep the trace.
725+
The sampler can set the priority to the following values:
726+
727+
* `Datadog::Ext::Priority::AUTO_REJECT`: the sampler automatically decided to reject the trace.
728+
* `Datadog::Ext::Priority::AUTO_KEEP`: the sampler automatically decided to keep the trace.
728729

729730
For now, priority sampling is disabled by default. Enabling it ensures that your sampled distributed traces will be complete. To enable the priority sampling:
730731

@@ -734,14 +735,26 @@ Datadog.tracer.configure(priority_sampling: true)
734735

735736
Once enabled, the sampler will automatically assign a priority of 0 or 1 to traces, depending on their service and volume.
736737

737-
You can also set this priority manually to either drop a non-interesting trace or to keep an important one. For that, set the `context#sampling_priority` to 0 or 2. It has to be done before any context propagation (fork, RPC calls) to be effective:
738+
You can also set this priority manually to either drop a non-interesting trace or to keep an important one. For that, set the `context#sampling_priority` to:
739+
740+
* `Datadog::Ext::Priority::USER_REJECT`: the user asked to reject the trace.
741+
* `Datadog::Ext::Priority::USER_KEEP`: the user asked to keep the trace.
742+
743+
When not using [distributed tracing](#Distributed_Tracing), you may change the priority at any time,
744+
as long as the trace is not finished yet.
745+
But it has to be done before any context propagation (fork, RPC calls) to be effective in a distributed context.
746+
Changing the priority after context has been propagated causes different parts of a distributed trace
747+
to use different priorities. Some parts might be kept, some parts might be rejected,
748+
and this can cause the trace to be partially stored and remain incomplete.
749+
750+
If you change the priority, we recommend you do it as soon as possible, when the root span has just been created.
738751

739752
```rb
740-
# Indicate to not keep the trace
741-
span.context.sampling_priority = 0
753+
# Indicate to reject the trace
754+
span.context.sampling_priority = Datadog::Ext::Priority::USER_REJECT
742755

743756
# Indicate to keep the trace
744-
span.context.sampling_priority = 2
757+
span.context.sampling_priority = Datadog::Ext::Priority::USER_KEEP
745758
```
746759

747760
### Distributed Tracing

lib/ddtrace/ext/priority.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Datadog
2+
module Ext
3+
# Priority is a hint given to the backend so that it knows which traces to reject or kept.
4+
# In a distributed context, it should be set before any context propagation (fork, RPC calls) to be effective.
5+
module Priority
6+
# Use this to explicitely inform the backend that a trace should be rejected and not stored.
7+
USER_REJECT = -1
8+
# Used by the builtin sampler to inform the backend that a trace should be rejected and not stored.
9+
AUTO_REJECT = 0
10+
# Used by the builtin sampler to inform the backend that a trace should be kept and stored.
11+
AUTO_KEEP = 1
12+
# Use this to explicitely inform the backend that a trace should be kept and stored.
13+
USER_KEEP = 2
14+
end
15+
end
16+
end

lib/ddtrace/sampler.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
require 'forwardable'
22

3+
require 'ddtrace/ext/priority'
4+
35
module Datadog
46
# \Sampler performs client-side trace sampling.
57
class Sampler
@@ -96,10 +98,10 @@ def initialize(opts = {})
9698
end
9799

98100
def sample(span)
99-
span.context.sampling_priority = 0 if span.context
101+
span.context.sampling_priority = Datadog::Ext::Priority::AUTO_REJECT if span.context
100102
return unless @base_sampler.sample(span)
101103
return unless @post_sampler.sample(span)
102-
span.context.sampling_priority = 1 if span.context
104+
span.context.sampling_priority = Datadog::Ext::Priority::AUTO_KEEP if span.context
103105

104106
true
105107
end

test/context_test.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ def test_sampling_priority
5959

6060
assert_nil(ctx.sampling_priority)
6161

62-
[0, 1, 2, nil, 999].each do |sampling_priority|
62+
[Datadog::Ext::Priority::USER_REJECT,
63+
Datadog::Ext::Priority::AUTO_REJECT,
64+
Datadog::Ext::Priority::AUTO_KEEP,
65+
Datadog::Ext::Priority::USER_KEEP,
66+
nil, 999].each do |sampling_priority|
6367
ctx.sampling_priority = sampling_priority
6468
if sampling_priority
6569
assert_equal(sampling_priority, ctx.sampling_priority)

0 commit comments

Comments
 (0)