Skip to content

Commit d0995b4

Browse files
author
Emanuele Palazzetti
authored
Merge pull request #426 from palazzem/priority-sampling-env
[ddtrace-run] enable Distributed Sampling via environment variable
2 parents ad342e8 + 09dec03 commit d0995b4

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

ddtrace/bootstrap/sitecustomize.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import os
77
import logging
88

9+
from ddtrace.util import asbool
10+
11+
912
debug = os.environ.get("DATADOG_TRACE_DEBUG")
1013
if debug and debug.lower() == "true":
1114
logging.basicConfig(level=logging.DEBUG)
@@ -46,6 +49,7 @@ def update_patched_modules():
4649
enabled = os.environ.get("DATADOG_TRACE_ENABLED")
4750
hostname = os.environ.get("DATADOG_TRACE_AGENT_HOSTNAME")
4851
port = os.environ.get("DATADOG_TRACE_AGENT_PORT")
52+
priority_sampling = os.environ.get("DATADOG_PRIORITY_SAMPLING")
4953

5054
opts = {}
5155

@@ -56,6 +60,8 @@ def update_patched_modules():
5660
opts["hostname"] = hostname
5761
if port:
5862
opts["port"] = int(port)
63+
if priority_sampling:
64+
opts["priority_sampling"] = asbool(priority_sampling)
5965

6066
if opts:
6167
tracer.configure(**opts)

docs/index.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ The available environment variables for `ddtrace-run` are:
4141

4242
* ``DATADOG_TRACE_ENABLED=true|false`` (default: true): Enable web framework and library instrumentation. When false, your application code
4343
will not generate any traces.
44-
* ``DATADOG_ENV`` (no default): Set an application's environment e.g. ``prod``, ``pre-prod``, ``stage``
44+
* ``DATADOG_ENV`` (no default): Set an application's environment e.g. ``prod``, ``pre-prod``, ``stage``
4545
* ``DATADOG_TRACE_DEBUG=true|false`` (default: false): Enable debug logging in the tracer
4646
* ``DATADOG_SERVICE_NAME`` (no default): override the service name to be used for this program. This value is passed through when setting up middleware for web framework integrations (e.g. pylons, flask, django). For tracing without a web integration, prefer setting the service name in code.
47-
* ``DATADOG_PATCH_MODULES=module:patch,module:patch...`` e.g. ``boto:true,redis:false`` : override the modules patched for this execution of the program (default: none)
48-
* ``DATADOG_TRACE_AGENT_HOSTNAME=localhost`` : override the address of the trace agent host that the default tracer will attempt to submit to (default: ``localhost``)
49-
* ``DATADOG_TRACE_AGENT_PORT=8126`` : override the port that the default tracer will submit to (default: 8126)
47+
* ``DATADOG_PATCH_MODULES=module:patch,module:patch...`` e.g. ``boto:true,redis:false``: override the modules patched for this execution of the program (default: none)
48+
* ``DATADOG_TRACE_AGENT_HOSTNAME=localhost``: override the address of the trace agent host that the default tracer will attempt to submit to (default: ``localhost``)
49+
* ``DATADOG_TRACE_AGENT_PORT=8126``: override the port that the default tracer will submit to (default: 8126)
50+
* ``DATADOG_PRIORITY_SAMPLING`` (default: false): enables `Priority sampling`_
5051

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from __future__ import print_function
2+
3+
from ddtrace import tracer
4+
5+
from nose.tools import ok_
6+
7+
if __name__ == '__main__':
8+
ok_(tracer.priority_sampler is not None)
9+
print("Test success")

tests/commands/test_runner.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ def test_host_port_from_env(self):
9595
)
9696
assert out.startswith(b"Test success")
9797

98+
def test_priority_sampling_from_env(self):
99+
"""
100+
DATADOG_PRIORITY_SAMPLING enables Distributed Sampling
101+
"""
102+
os.environ["DATADOG_PRIORITY_SAMPLING"] = "True"
103+
out = subprocess.check_output(
104+
['ddtrace-run', 'python', 'tests/commands/ddtrace_run_priority_sampling.py']
105+
)
106+
assert out.startswith(b"Test success")
107+
98108
def test_patch_modules_from_env(self):
99109
"""
100110
DATADOG_PATCH_MODULES overrides the defaults for patch_all()

0 commit comments

Comments
 (0)