Skip to content

Commit 445d327

Browse files
authored
[core] Enable priority sampling by default (#774)
* Enable priority sampling by default * fix broken tests * fix linting * fix pylons test * fix logic to set tracer.priority_sampler * fix pylons test * fix priority sampling docs
1 parent a0bdd48 commit 445d327

File tree

7 files changed

+56
-27
lines changed

7 files changed

+56
-27
lines changed

ddtrace/tracer.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def configure(self, enabled=None, hostname=None, port=None, sampler=None,
9898
``Tracer.wrap()``. This is an advanced option that usually doesn't need to be changed
9999
from the default value
100100
:param priority_sampling: enable priority sampling, this is required for
101-
complete distributed tracing support.
101+
complete distributed tracing support. Enabled by default.
102102
"""
103103
if enabled is not None:
104104
self.enabled = enabled
@@ -110,8 +110,12 @@ def configure(self, enabled=None, hostname=None, port=None, sampler=None,
110110
if sampler is not None:
111111
self.sampler = sampler
112112

113-
if priority_sampling:
113+
# If priority sampling is not set or is True and no priority sampler is set yet
114+
if priority_sampling in (None, True) and not self.priority_sampler:
114115
self.priority_sampler = RateByServiceSampler()
116+
# Explicitly disable priority sampling
117+
elif priority_sampling is False:
118+
self.priority_sampler = None
115119

116120
if hostname is not None or port is not None or filters is not None or \
117121
priority_sampling is not None:

docs/advanced_usage.rst

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,10 @@ priority to the following values:
128128
- ``AUTO_REJECT``: the sampler automatically rejects the trace
129129
- ``AUTO_KEEP``: the sampler automatically keeps the trace
130130

131-
Priority sampling is disabled by default. Enabling it ensures that your sampled
132-
distributed traces will be complete. To enable priority sampling::
133-
134-
tracer.configure(priority_sampling=True)
135-
136-
Once enabled, the sampler will automatically assign a priority to your traces,
131+
Priority sampling is enabled by default.
132+
When enabled, the sampler will automatically assign a priority to your traces,
137133
depending on their service and volume.
134+
This ensures that your sampled distributed traces will be complete.
138135

139136
You can also set this priority manually to either drop an uninteresting trace or
140137
to keep an important one.
@@ -323,7 +320,7 @@ for usage.
323320
+---------------------+---------------------------------------------------------+---------------+
324321
| `sampler` | see `Sampling`_ | `AllSampler` |
325322
+---------------------+---------------------------------------------------------+---------------+
326-
| `priority_sampling` | see `Priority Sampling`_ | `False` |
323+
| `priority_sampling` | see `Priority Sampling`_ | `True` |
327324
+---------------------+---------------------------------------------------------+---------------+
328325
| `settings` | see `Advanced Usage`_ | `{}` |
329326
+---------------------+---------------------------------------------------------+---------------+
@@ -448,7 +445,7 @@ The available environment variables for ``ddtrace-run`` are:
448445
``localhost``)
449446
* ``DATADOG_TRACE_AGENT_PORT=8126``: override the port that the default tracer
450447
will submit to (default: 8126)
451-
* ``DATADOG_PRIORITY_SAMPLING`` (default: false): enables :ref:`Priority
448+
* ``DATADOG_PRIORITY_SAMPLING`` (default: true): enables :ref:`Priority
452449
Sampling`
453450

454451
``ddtrace-run`` respects a variety of common entrypoints for web applications:

tests/contrib/grpc/test_grpc.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ def test_insecure_channel(self):
4545
spans = writer.pop()
4646
eq_(len(spans), 1)
4747
span = spans[0]
48-
eq_(response.message, 'x-datadog-trace-id=%d;x-datadog-parent-id=%d' % (span.trace_id, span.span_id))
48+
eq_(
49+
response.message,
50+
(
51+
# DEV: Priority sampling is enabled by default
52+
'x-datadog-trace-id=%d;x-datadog-parent-id=%d;x-datadog-sampling-priority=1' %
53+
(span.trace_id, span.span_id)
54+
),
55+
)
4956
_check_span(span)
5057

5158
def test_secure_channel(self):
@@ -59,11 +66,18 @@ def test_secure_channel(self):
5966
eq_(len(spans), 1)
6067

6168
span = spans[0]
62-
eq_(response.message, 'x-datadog-trace-id=%d;x-datadog-parent-id=%d' % (span.trace_id, span.span_id))
69+
eq_(
70+
response.message,
71+
(
72+
# DEV: Priority sampling is enabled by default
73+
'x-datadog-trace-id=%d;x-datadog-parent-id=%d;x-datadog-sampling-priority=1' %
74+
(span.trace_id, span.span_id)
75+
),
76+
)
6377
_check_span(span)
6478

6579
def test_priority_sampling(self):
66-
self._tracer.configure(priority_sampling=True)
80+
# DEV: Priority sampling is enabled by default
6781
# Setting priority sampling reset the writer, we need to re-override it
6882
self._tracer.writer = DummyWriter()
6983

tests/contrib/pylons/test_pylons.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def test_distributed_tracing_default(self):
289289

290290
ok_(span.trace_id != 100)
291291
ok_(span.parent_id != 42)
292-
ok_(span.get_metric(SAMPLING_PRIORITY_KEY) is None)
292+
ok_(span.get_metric(SAMPLING_PRIORITY_KEY) != 2)
293293

294294
def test_distributed_tracing_enabled(self):
295295
# ensure distributed tracing propagator is working

tests/test_integration.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ def test_worker_single_trace(self):
106106
self._wait_thread_flush()
107107
eq_(self.api._put.call_count, 1)
108108
# check and retrieve the right call
109-
endpoint, payload = self._get_endpoint_payload(self.api._put.call_args_list, '/v0.3/traces')
110-
eq_(endpoint, '/v0.3/traces')
109+
endpoint, payload = self._get_endpoint_payload(self.api._put.call_args_list, '/v0.4/traces')
110+
eq_(endpoint, '/v0.4/traces')
111111
eq_(len(payload), 1)
112112
eq_(len(payload[0]), 1)
113113
eq_(payload[0][0]['name'], 'client.testing')
@@ -124,8 +124,8 @@ def test_worker_multiple_traces(self):
124124
self._wait_thread_flush()
125125
eq_(self.api._put.call_count, 1)
126126
# check and retrieve the right call
127-
endpoint, payload = self._get_endpoint_payload(self.api._put.call_args_list, '/v0.3/traces')
128-
eq_(endpoint, '/v0.3/traces')
127+
endpoint, payload = self._get_endpoint_payload(self.api._put.call_args_list, '/v0.4/traces')
128+
eq_(endpoint, '/v0.4/traces')
129129
eq_(len(payload), 2)
130130
eq_(len(payload[0]), 1)
131131
eq_(len(payload[1]), 1)
@@ -143,8 +143,8 @@ def test_worker_single_trace_multiple_spans(self):
143143
self._wait_thread_flush()
144144
eq_(self.api._put.call_count, 1)
145145
# check and retrieve the right call
146-
endpoint, payload = self._get_endpoint_payload(self.api._put.call_args_list, '/v0.3/traces')
147-
eq_(endpoint, '/v0.3/traces')
146+
endpoint, payload = self._get_endpoint_payload(self.api._put.call_args_list, '/v0.4/traces')
147+
eq_(endpoint, '/v0.4/traces')
148148
eq_(len(payload), 1)
149149
eq_(len(payload[0]), 2)
150150
eq_(payload[0][0]['name'], 'client.testing')
@@ -208,8 +208,8 @@ def test_worker_filter_request(self):
208208
# Only the second trace should have been sent
209209
eq_(self.api._put.call_count, 1)
210210
# check and retrieve the right call
211-
endpoint, payload = self._get_endpoint_payload(self.api._put.call_args_list, '/v0.3/traces')
212-
eq_(endpoint, '/v0.3/traces')
211+
endpoint, payload = self._get_endpoint_payload(self.api._put.call_args_list, '/v0.4/traces')
212+
eq_(endpoint, '/v0.4/traces')
213213
eq_(len(payload), 1)
214214
eq_(payload[0][0]['name'], 'testing.nonfilteredurl')
215215

tests/test_sampler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def test_sample_rate_deviation(self):
7474
for sample_rate in [0.1, 0.25, 0.5, 1]:
7575
tracer = get_dummy_tracer()
7676
writer = tracer.writer
77-
tracer.configure(sampler=AllSampler(), priority_sampling=True)
77+
tracer.configure(sampler=AllSampler())
7878
# We need to set the writer because tracer.configure overrides it,
7979
# indeed, as we enable priority sampling, we must ensure the writer
8080
# is priority sampling aware and pass it a reference on the
@@ -128,7 +128,7 @@ def test_set_sample_rate_by_service(self):
128128
]
129129

130130
tracer = get_dummy_tracer()
131-
tracer.configure(sampler=AllSampler(), priority_sampling=True)
131+
tracer.configure(sampler=AllSampler())
132132
priority_sampler = tracer.priority_sampler
133133
for case in cases:
134134
priority_sampler.set_sample_rate_by_service(case)

tests/utils/tracer.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
class DummyWriter(AgentWriter):
77
"""DummyWriter is a small fake writer used for tests. not thread-safe."""
88

9-
def __init__(self):
9+
def __init__(self, *args, **kwargs):
1010
# original call
11-
super(DummyWriter, self).__init__()
11+
super(DummyWriter, self).__init__(*args, **kwargs)
12+
1213
# dummy components
1314
self.spans = []
1415
self.traces = []
@@ -57,4 +58,17 @@ class DummyTracer(Tracer):
5758
"""
5859
def __init__(self, *args, **kwargs):
5960
super(DummyTracer, self).__init__(*args, **kwargs)
60-
self.writer = DummyWriter()
61+
self._update_writer()
62+
63+
def _update_writer(self):
64+
self.writer = DummyWriter(
65+
hostname=self.writer.api.hostname,
66+
port=self.writer.api.port,
67+
filters=self.writer._filters,
68+
priority_sampler=self.writer._priority_sampler,
69+
)
70+
71+
def configure(self, *args, **kwargs):
72+
super(DummyTracer, self).configure(*args, **kwargs)
73+
# `.configure()` may reset the writer
74+
self._update_writer()

0 commit comments

Comments
 (0)