Skip to content

Commit 30c953d

Browse files
majorgreysocelotl
andauthored
datadog: set sampling rate (open-telemetry#740)
Set the sampling rate in the Datadog exported span if span was sampled with the ProbabilitySampler. Co-authored-by: Diego Hurtado <ocelotl@users.noreply.github.com>
1 parent 960d0a3 commit 30c953d

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

ext/opentelemetry-ext-datadog/src/opentelemetry/ext/datadog/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
AUTO_REJECT = 0
33
AUTO_KEEP = 1
44
USER_KEEP = 2
5+
SAMPLE_RATE_METRIC_KEY = "_sample_rate"
6+
SAMPLING_PRIORITY_KEY = "_sampling_priority_v1"

ext/opentelemetry-ext-datadog/src/opentelemetry/ext/datadog/exporter.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from opentelemetry.trace.status import StatusCanonicalCode
2626

2727
# pylint:disable=relative-beyond-top-level
28-
from .constants import DD_ORIGIN
28+
from .constants import DD_ORIGIN, SAMPLE_RATE_METRIC_KEY
2929

3030
logger = logging.getLogger(__name__)
3131

@@ -136,6 +136,10 @@ def _translate_to_datadog(self, spans):
136136
if origin and parent_id == 0:
137137
datadog_span.set_tag(DD_ORIGIN, origin)
138138

139+
sampling_rate = _get_sampling_rate(span)
140+
if sampling_rate is not None:
141+
datadog_span.set_metric(SAMPLE_RATE_METRIC_KEY, sampling_rate)
142+
139143
# span events and span links are not supported
140144

141145
datadog_spans.append(datadog_span)
@@ -216,3 +220,13 @@ def _get_origin(span):
216220
ctx = span.get_context()
217221
origin = ctx.trace_state.get(DD_ORIGIN)
218222
return origin
223+
224+
225+
def _get_sampling_rate(span):
226+
ctx = span.get_context()
227+
return (
228+
span.sampler.rate
229+
if ctx.trace_flags.sampled
230+
and isinstance(span.sampler, trace_api.sampling.ProbabilitySampler)
231+
else None
232+
)

ext/opentelemetry-ext-datadog/tests/test_datadog_exporter.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,35 @@ def test_origin(self):
440440
]
441441
expected = ["origin-service", None]
442442
self.assertListEqual(actual, expected)
443+
444+
def test_sampling_rate(self):
445+
context = trace_api.SpanContext(
446+
trace_id=0x000000000000000000000000DEADBEEF,
447+
span_id=0x34BF92DEEFC58C92,
448+
is_remote=False,
449+
trace_flags=trace_api.TraceFlags(trace_api.TraceFlags.SAMPLED),
450+
)
451+
sampler = trace_api.sampling.ProbabilitySampler(0.5)
452+
453+
span = trace.Span(
454+
name="sampled", context=context, parent=None, sampler=sampler
455+
)
456+
span.start()
457+
span.end()
458+
459+
# pylint: disable=protected-access
460+
exporter = datadog.DatadogSpanExporter()
461+
datadog_spans = [
462+
span.to_dict() for span in exporter._translate_to_datadog([span])
463+
]
464+
465+
self.assertEqual(len(datadog_spans), 1)
466+
467+
actual = [
468+
span["metrics"].get(datadog.constants.SAMPLE_RATE_METRIC_KEY)
469+
if "metrics" in span
470+
else None
471+
for span in datadog_spans
472+
]
473+
expected = [0.5]
474+
self.assertListEqual(actual, expected)

0 commit comments

Comments
 (0)