Skip to content

Commit 0cfc263

Browse files
zarirhamzabrettlangdonmabdinurmajorgreys
authored
fix(structlog): appends to default_processors via list to avoid TypeError (#7679)
Fixes #7665 by changing `default_processors` to list accounting for all types of iterables before appending ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) are followed. If no release note is required, add label `changelog/no-changelog`. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. - [x] Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) - [x] If this PR touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a review from `@DataDog/security-design-and-guidance`. - [x] This PR doesn't touch any of that. --------- Co-authored-by: Brett Langdon <[email protected]> Co-authored-by: Munir Abdinur <[email protected]> Co-authored-by: Tahir H. Butt <[email protected]>
1 parent ac29fa3 commit 0cfc263

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

ddtrace/contrib/structlog/patch.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,18 @@ def _tracer_injection(_, __, event_dict):
4848

4949

5050
def _w_get_logger(func, instance, args, kwargs):
51+
"""
52+
Append the tracer injection processor to the ``default_processors`` list used by the logger
53+
The ``default_processors`` list has built in defaults which protects against a user configured ``None`` value.
54+
The argument to configure ``default_processors`` accepts an iterable type:
55+
- List: default use case which has been accounted for
56+
- Tuple: patched via list conversion
57+
- Set: ignored because structlog processors care about order notably the last value to be a Renderer
58+
- Dict: because keys are ignored, this essentially becomes a List
59+
"""
60+
5161
dd_processor = [_tracer_injection]
52-
structlog._config._CONFIG.default_processors = dd_processor + structlog._config._CONFIG.default_processors
62+
structlog._config._CONFIG.default_processors = dd_processor + list(structlog._config._CONFIG.default_processors)
5363
return func(*args, **kwargs)
5464

5565

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
structlog: Fixes ``TypeError`` raised when ddtrace log processor is configured with a tuple

tests/contrib/structlog/test_structlog_logging.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,88 @@ def test_log_DD_TAGS():
260260

261261
cf.logger.calls.clear()
262262
unpatch()
263+
264+
265+
@pytest.mark.subprocess()
266+
def test_tuple_processor_list():
267+
"""
268+
Regression test for: https://github.com/DataDog/dd-trace-py/issues/7665
269+
"""
270+
import json
271+
272+
import structlog
273+
274+
from ddtrace import config
275+
from ddtrace import tracer
276+
from ddtrace.contrib.structlog import patch
277+
from ddtrace.contrib.structlog import unpatch
278+
279+
config.service = "logging"
280+
config.env = "global.env"
281+
config.version = "global.version"
282+
283+
patch()
284+
285+
cf = structlog.testing.CapturingLoggerFactory()
286+
structlog.configure(
287+
processors=(structlog.stdlib.add_log_level, structlog.processors.JSONRenderer()),
288+
logger_factory=cf,
289+
)
290+
logger = structlog.getLogger()
291+
292+
span = tracer.trace("test.logging")
293+
logger.info("Hello!")
294+
span.finish()
295+
296+
output = cf.logger.calls
297+
298+
assert json.loads(output[0].args[0])["event"] == "Hello!"
299+
assert json.loads(output[0].args[0])["dd.trace_id"] == str(span._trace_id_64bits)
300+
assert json.loads(output[0].args[0])["dd.span_id"] == str(span.span_id)
301+
assert json.loads(output[0].args[0])["dd.env"] == "global.env"
302+
assert json.loads(output[0].args[0])["dd.service"] == "logging"
303+
assert json.loads(output[0].args[0])["dd.version"] == "global.version"
304+
305+
cf.logger.calls.clear()
306+
unpatch()
307+
308+
309+
@pytest.mark.subprocess()
310+
def test_no_configured_processor():
311+
"""
312+
Check if injected values are present when no processor is configured
313+
"""
314+
import structlog
315+
316+
from ddtrace import config
317+
from ddtrace import tracer
318+
from ddtrace.contrib.structlog import patch
319+
from ddtrace.contrib.structlog import unpatch
320+
321+
config.service = "logging"
322+
config.env = "global.env"
323+
config.version = "global.version"
324+
325+
patch()
326+
327+
cf = structlog.testing.CapturingLoggerFactory()
328+
structlog.configure(
329+
logger_factory=cf,
330+
)
331+
logger = structlog.getLogger()
332+
333+
span = tracer.trace("test.logging")
334+
logger.info("Hello!")
335+
span.finish()
336+
337+
output = cf.logger.calls
338+
339+
assert "Hello!" in output[0].args[0]
340+
assert "dd.trace_id={}".format(str(span._trace_id_64bits)) in output[0].args[0]
341+
assert "dd.span_id={}".format(str(span.span_id)) in output[0].args[0]
342+
assert "dd.env=global.env" in output[0].args[0]
343+
assert "dd.service=logging" in output[0].args[0]
344+
assert "dd.version=global.version" in output[0].args[0]
345+
346+
cf.logger.calls.clear()
347+
unpatch()

0 commit comments

Comments
 (0)