Skip to content

Commit bc024e7

Browse files
brettlangdonmergify[bot]Kyle-Verhoog
authored
Only call _store_request_headers/_store_response_headers if header tracing is enabled (#2550)
* Only call _store_request_headers/_store_response_headers if header tracing is enabled. This was found during the investigation of #2549, we only need to convert the headers to a `dict` if there are traced headers configured * properties are nice * Add additional tests Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Kyle Verhoog <[email protected]>
1 parent 630056d commit bc024e7

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

ddtrace/contrib/trace_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,10 @@ def set_http_meta(
250250
if query is not None and integration_config.trace_query_string:
251251
span._set_str_tag(http.QUERY_STRING, query)
252252

253-
if request_headers is not None:
253+
if request_headers is not None and integration_config.is_header_tracing_configured:
254254
_store_request_headers(dict(request_headers), span, integration_config)
255255

256-
if response_headers is not None:
256+
if response_headers is not None and integration_config.is_header_tracing_configured:
257257
_store_response_headers(dict(response_headers), span, integration_config)
258258

259259
if retries_remain is not None:

ddtrace/settings/integration.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ def trace_query_string(self):
100100
return self.http.trace_query_string
101101
return self.global_config.http.trace_query_string
102102

103+
@property
104+
def is_header_tracing_configured(self):
105+
# type: (...) -> bool
106+
"""Returns whether header tracing is enabled for this integration.
107+
108+
Will return true if traced headers are configured for this integration
109+
or if they are configured globally.
110+
"""
111+
return self.http.is_header_tracing_configured or self.global_config.http.is_header_tracing_configured
112+
103113
def header_is_traced(self, header_name):
104114
"""
105115
Returns whether or not the current header should be traced.

tests/tracer/test_settings.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from ddtrace.settings import Config
24
from ddtrace.settings import HttpConfig
35
from ddtrace.settings import IntegrationConfig
@@ -319,3 +321,31 @@ def test_service_name_env_var(self):
319321
def test_service_name_env_var_legacy(self):
320322
ic = IntegrationConfig(self.config, "foo")
321323
assert ic.service == "foo-svc"
324+
325+
326+
@pytest.mark.parametrize(
327+
"global_headers,int_headers,expected",
328+
(
329+
(None, None, (False, False, False)),
330+
([], None, (False, False, False)),
331+
(["Header"], None, (True, False, True)),
332+
(None, ["Header"], (False, True, True)),
333+
(None, [], (False, False, False)),
334+
(["Header"], ["Header"], (True, True, True)),
335+
([], [], (False, False, False)),
336+
),
337+
)
338+
def test_config_is_header_tracing_configured(global_headers, int_headers, expected):
339+
config = Config()
340+
integration_config = config.myint
341+
342+
if global_headers is not None:
343+
config.trace_headers(global_headers)
344+
if int_headers is not None:
345+
integration_config.http.trace_headers(int_headers)
346+
347+
assert (
348+
config.http.is_header_tracing_configured,
349+
integration_config.http.is_header_tracing_configured,
350+
integration_config.is_header_tracing_configured,
351+
) == expected

tests/tracer/test_trace_utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,21 @@ def test_set_http_meta_custom_errors(mock_log, span, int_config, error_codes, st
376376
mock_log.exception.assert_not_called()
377377

378378

379+
@mock.patch("ddtrace.contrib.trace_utils._store_headers")
380+
def test_set_http_meta_no_headers(mock_store_headers, span, int_config):
381+
assert int_config.myint.is_header_tracing_configured is False
382+
trace_utils.set_http_meta(
383+
span,
384+
int_config.myint,
385+
request_headers={"HTTP_REQUEST_HEADER", "value"},
386+
response_headers={"HTTP_RESPONSE_HEADER": "value"},
387+
)
388+
assert list(span.meta.keys()) == [
389+
"runtime-id",
390+
]
391+
mock_store_headers.assert_not_called()
392+
393+
379394
@mock.patch("ddtrace.contrib.trace_utils.log")
380395
@pytest.mark.parametrize(
381396
"val, bad",

0 commit comments

Comments
 (0)