File size: 2,920 Bytes
a4d3c7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
diff --git a/tests/processors/test_processors.py b/tests/processors/test_processors.py
index e43485e..99516b4 100644
--- a/tests/processors/test_processors.py
+++ b/tests/processors/test_processors.py
@@ -36,6 +36,7 @@ from structlog.processors import (
     format_exc_info,
 )
 from structlog.stdlib import ProcessorFormatter
+from structlog.testing import CapturingLoggerFactory
 from structlog.typing import EventDict, ExcInfo
 
 from ..additional_frame import additional_frame
@@ -378,6 +379,7 @@ class TestCallsiteParameterAdder:
     async def test_async(self, wrapper_class, method_name) -> None:
         """
         Callsite information for async invocations are correct.
+        Thread information is now correctly captured before async bridge.
         """
         string_io = StringIO()
 
@@ -395,17 +397,56 @@ class TestCallsiteParameterAdder:
 
         logger = structlog.stdlib.get_logger()
 
+        # Capture thread info before async call
+        expected_thread = threading.get_ident()
+        expected_thread_name = threading.current_thread().name
+
         callsite_params = self.get_callsite_parameters()
         await getattr(logger, method_name)("baz")
         logger_params = json.loads(string_io.getvalue())
 
-        # These are different when running under async
+        assert expected_thread == logger_params["thread"]
+        assert expected_thread_name == logger_params["thread_name"]
+
+        # Remove thread info from comparison for remaining params
         for key in ["thread", "thread_name"]:
             callsite_params.pop(key)
             logger_params.pop(key)
 
         assert {"event": "baz", **callsite_params} == logger_params
 
+    @pytest.mark.asyncio
+    async def test_async_native_logger(self) -> None:
+        """
+        Callsite thread information for native async invocations is correct.
+        """
+        cf = CapturingLoggerFactory()
+        structlog.configure(
+            processors=[
+                CallsiteParameterAdder(
+                    parameters=[
+                        CallsiteParameter.THREAD,
+                        CallsiteParameter.THREAD_NAME,
+                    ]
+                ),
+            ],
+            logger_factory=cf,
+            wrapper_class=structlog._native.BoundLoggerFilteringAtInfo,
+            cache_logger_on_first_use=True,
+        )
+
+        logger = structlog.get_logger()
+
+        expected_thread = threading.get_ident()
+        expected_thread_name = threading.current_thread().name
+
+        await logger.ainfo("test native async")
+
+        captured = cf.logger.calls[0].kwargs
+
+        assert expected_thread == captured["thread"]
+        assert expected_thread_name == captured["thread_name"]
+
     def test_additional_ignores(self, monkeypatch: pytest.MonkeyPatch) -> None:
         """
         Stack frames from modules with names that start with values in