3
3
4
4
import pytest
5
5
6
+ import sentry_sdk
6
7
from sentry_sdk .tracing import trace
7
- from sentry_sdk .tracing_utils import start_child_span_decorator
8
8
from sentry_sdk .utils import logger
9
- from tests .conftest import patch_start_tracing_child
10
9
11
10
12
11
def my_example_function ():
@@ -17,68 +16,80 @@ async def my_async_example_function():
17
16
return "return_of_async_function"
18
17
19
18
20
- @pytest .mark .forked
21
- def test_trace_decorator ():
22
- with patch_start_tracing_child () as fake_start_child :
19
+ def test_trace_decorator (sentry_init , capture_events ):
20
+ sentry_init (traces_sample_rate = 1.0 )
21
+ events = capture_events ()
22
+
23
+ with sentry_sdk .start_span (name = "test" ):
23
24
result = my_example_function ()
24
- fake_start_child .assert_not_called ()
25
25
assert result == "return_of_sync_function"
26
26
27
- result2 = start_child_span_decorator (my_example_function )()
28
- fake_start_child .assert_called_once_with (
29
- op = "function" , name = "test_decorator.my_example_function"
30
- )
27
+ result2 = trace (my_example_function )()
31
28
assert result2 == "return_of_sync_function"
32
29
30
+ (event ,) = events
31
+ (span ,) = event ["spans" ]
32
+ assert span ["op" ] == "function"
33
+ assert span ["description" ] == "test_decorator.my_example_function"
34
+
35
+
36
+ def test_trace_decorator_no_trx (sentry_init , capture_events ):
37
+ sentry_init (traces_sample_rate = 1.0 )
38
+ events = capture_events ()
39
+
40
+ with mock .patch .object (logger , "debug" , mock .Mock ()) as fake_debug :
41
+ result = my_example_function ()
42
+ assert result == "return_of_sync_function"
43
+ fake_debug .assert_not_called ()
33
44
34
- @ pytest . mark . forked
35
- def test_trace_decorator_no_trx ():
36
- with patch_start_tracing_child ( fake_transaction_is_none = True ):
37
- with mock . patch . object ( logger , "debug" , mock . Mock ()) as fake_debug :
38
- result = my_example_function ()
39
- fake_debug . assert_not_called ()
40
- assert result == "return_of_sync_function"
45
+ result2 = trace ( my_example_function )()
46
+ assert result2 == "return_of_sync_function"
47
+ fake_debug . assert_called_once_with (
48
+ "Cannot create a child span for %s. "
49
+ "Please start a Sentry transaction before calling this function." ,
50
+ "test_decorator.my_example_function" ,
51
+ )
41
52
42
- result2 = start_child_span_decorator (my_example_function )()
43
- fake_debug .assert_called_once_with (
44
- "Cannot create a child span for %s. "
45
- "Please start a Sentry transaction before calling this function." ,
46
- "test_decorator.my_example_function" ,
47
- )
48
- assert result2 == "return_of_sync_function"
53
+ assert len (events ) == 0
49
54
50
55
51
- @pytest .mark .forked
52
56
@pytest .mark .asyncio
53
- async def test_trace_decorator_async ():
54
- with patch_start_tracing_child () as fake_start_child :
57
+ async def test_trace_decorator_async (sentry_init , capture_events ):
58
+ sentry_init (traces_sample_rate = 1.0 )
59
+ events = capture_events ()
60
+
61
+ with sentry_sdk .start_span (name = "test" ):
55
62
result = await my_async_example_function ()
56
- fake_start_child .assert_not_called ()
57
63
assert result == "return_of_async_function"
58
64
59
- result2 = await start_child_span_decorator (my_async_example_function )()
60
- fake_start_child .assert_called_once_with (
61
- op = "function" ,
62
- name = "test_decorator.my_async_example_function" ,
63
- )
65
+ result2 = await trace (my_async_example_function )()
64
66
assert result2 == "return_of_async_function"
65
67
68
+ (event ,) = events
69
+ (span ,) = event ["spans" ]
70
+ assert span ["op" ] == "function"
71
+ assert span ["description" ] == "test_decorator.my_async_example_function"
72
+
66
73
67
74
@pytest .mark .asyncio
68
- async def test_trace_decorator_async_no_trx ():
69
- with patch_start_tracing_child (fake_transaction_is_none = True ):
70
- with mock .patch .object (logger , "debug" , mock .Mock ()) as fake_debug :
71
- result = await my_async_example_function ()
72
- fake_debug .assert_not_called ()
73
- assert result == "return_of_async_function"
74
-
75
- result2 = await start_child_span_decorator (my_async_example_function )()
76
- fake_debug .assert_called_once_with (
77
- "Cannot create a child span for %s. "
78
- "Please start a Sentry transaction before calling this function." ,
79
- "test_decorator.my_async_example_function" ,
80
- )
81
- assert result2 == "return_of_async_function"
75
+ async def test_trace_decorator_async_no_trx (sentry_init , capture_events ):
76
+ sentry_init (traces_sample_rate = 1.0 )
77
+ events = capture_events ()
78
+
79
+ with mock .patch .object (logger , "debug" , mock .Mock ()) as fake_debug :
80
+ result = await my_async_example_function ()
81
+ fake_debug .assert_not_called ()
82
+ assert result == "return_of_async_function"
83
+
84
+ result2 = await trace (my_async_example_function )()
85
+ fake_debug .assert_called_once_with (
86
+ "Cannot create a child span for %s. "
87
+ "Please start a Sentry transaction before calling this function." ,
88
+ "test_decorator.my_async_example_function" ,
89
+ )
90
+ assert result2 == "return_of_async_function"
91
+
92
+ assert len (events ) == 0
82
93
83
94
84
95
def test_functions_to_trace_signature_unchanged_sync (sentry_init ):
0 commit comments