Skip to content

Commit 642d0b8

Browse files
Add internal setting for additional writer headers (#2689)
For testing it's desirable to be able to pass additional headers with the trace requests made to the agent. With this ability we can propagate tokens which can be used to associate traces with test cases when running snapshot tests in subprocesses (like in #2640). I debated having the flag be read at run-time instead of initialization-time but opted against it as reading from the env is not cheap (on the order of 1µs): >>> %timeit os.environ.get("") 1.23 µs ± 49.6 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent f5bc986 commit 642d0b8

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

ddtrace/internal/writer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from collections import defaultdict
33
from json import loads
44
import logging
5+
import os
56
import sys
67
from typing import List
78
from typing import Optional
@@ -22,6 +23,7 @@
2223
from ..sampler import BasePrioritySampler
2324
from ..sampler import BaseSampler
2425
from ..utils.formats import get_env
26+
from ..utils.formats import parse_tags_str
2527
from ..utils.time import StopWatch
2628
from ._encoding import BufferFull
2729
from ._encoding import BufferItemTooLarge
@@ -266,6 +268,9 @@ def __init__(
266268
max_item_size=self._max_payload_size,
267269
)
268270
self._headers.update({"Content-Type": self._encoder.content_type})
271+
additional_header_str = os.environ.get("_DD_TRACE_WRITER_ADDITIONAL_HEADERS")
272+
if additional_header_str is not None:
273+
self._headers.update(parse_tags_str(additional_header_str))
269274
self.dogstatsd = dogstatsd
270275
self._report_metrics = report_metrics
271276
self._metrics_reset()

tests/tracer/test_writer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from ddtrace.span import Span
2323
from tests.utils import AnyInt
2424
from tests.utils import BaseTestCase
25+
from tests.utils import override_env
2526

2627

2728
class DummyOutput:
@@ -538,3 +539,10 @@ def do_write(i):
538539
t.join()
539540

540541
assert len(writer._encoder) == 100
542+
543+
544+
def test_additional_headers():
545+
with override_env(dict(_DD_TRACE_WRITER_ADDITIONAL_HEADERS="additional-header:additional-value,header2:value2")):
546+
writer = AgentWriter(agent_url="http://localhost:9126")
547+
assert writer._headers["additional-header"] == "additional-value"
548+
assert writer._headers["header2"] == "value2"

0 commit comments

Comments
 (0)