|
1 | 1 | import socket |
2 | 2 |
|
| 3 | +import pytest |
| 4 | + |
| 5 | +import sentry_sdk |
3 | 6 | from sentry_sdk import start_transaction |
4 | 7 | from sentry_sdk.integrations.socket import SocketIntegration |
5 | 8 | from tests.conftest import ApproxDict, create_mock_http_server |
6 | 9 |
|
7 | 10 | PORT = create_mock_http_server() |
8 | 11 |
|
9 | 12 |
|
10 | | -def test_getaddrinfo_trace(sentry_init, capture_events): |
11 | | - sentry_init(integrations=[SocketIntegration()], traces_sample_rate=1.0) |
12 | | - events = capture_events() |
13 | | - |
14 | | - with start_transaction(): |
15 | | - socket.getaddrinfo("localhost", PORT) |
16 | | - |
17 | | - (event,) = events |
18 | | - (span,) = event["spans"] |
19 | | - |
20 | | - assert span["op"] == "socket.dns" |
21 | | - assert span["description"] == f"localhost:{PORT}" # noqa: E231 |
22 | | - assert span["data"] == ApproxDict( |
23 | | - { |
24 | | - "host": "localhost", |
25 | | - "port": PORT, |
26 | | - } |
| 13 | +@pytest.mark.parametrize("span_streaming", [True, False]) |
| 14 | +def test_getaddrinfo_trace(sentry_init, capture_events, capture_items, span_streaming): |
| 15 | + sentry_init( |
| 16 | + integrations=[SocketIntegration()], |
| 17 | + traces_sample_rate=1.0, |
| 18 | + _experiments={"trace_lifecycle": "stream" if span_streaming else "static"}, |
27 | 19 | ) |
28 | 20 |
|
29 | | - |
30 | | -def test_create_connection_trace(sentry_init, capture_events): |
| 21 | + if span_streaming: |
| 22 | + items = capture_items("span") |
| 23 | + with sentry_sdk.traces.start_span(name="root"): |
| 24 | + socket.getaddrinfo("localhost", PORT) |
| 25 | + sentry_sdk.flush() |
| 26 | + |
| 27 | + spans = [item.payload for item in items if item.type == "span"] |
| 28 | + dns_span, _root = spans |
| 29 | + |
| 30 | + assert dns_span["attributes"]["sentry.op"] == "socket.dns" |
| 31 | + assert dns_span["attributes"]["sentry.origin"] == "auto.socket.socket" |
| 32 | + assert dns_span["name"] == f"localhost:{PORT}" # noqa: E231 |
| 33 | + assert dns_span["attributes"]["server.address"] == "localhost" |
| 34 | + assert dns_span["attributes"]["server.port"] == PORT |
| 35 | + else: |
| 36 | + events = capture_events() |
| 37 | + |
| 38 | + with start_transaction(): |
| 39 | + socket.getaddrinfo("localhost", PORT) |
| 40 | + |
| 41 | + (event,) = events |
| 42 | + (span,) = event["spans"] |
| 43 | + |
| 44 | + assert span["op"] == "socket.dns" |
| 45 | + assert span["description"] == f"localhost:{PORT}" # noqa: E231 |
| 46 | + assert span["data"] == ApproxDict( |
| 47 | + { |
| 48 | + "host": "localhost", |
| 49 | + "port": PORT, |
| 50 | + } |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.parametrize("span_streaming", [True, False]) |
| 55 | +def test_create_connection_trace( |
| 56 | + sentry_init, capture_events, capture_items, span_streaming |
| 57 | +): |
31 | 58 | timeout = 10 |
32 | 59 |
|
33 | | - sentry_init(integrations=[SocketIntegration()], traces_sample_rate=1.0) |
34 | | - events = capture_events() |
35 | | - |
36 | | - with start_transaction(): |
37 | | - socket.create_connection(("localhost", PORT), timeout, None) |
38 | | - |
39 | | - (event,) = events |
40 | | - (connect_span, dns_span) = event["spans"] |
41 | | - # as getaddrinfo gets called in create_connection it should also contain a dns span |
42 | | - |
43 | | - assert connect_span["op"] == "socket.connection" |
44 | | - assert connect_span["description"] == f"localhost:{PORT}" # noqa: E231 |
45 | | - assert connect_span["data"] == ApproxDict( |
46 | | - { |
47 | | - "address": ["localhost", PORT], |
48 | | - "timeout": timeout, |
49 | | - "source_address": None, |
50 | | - } |
51 | | - ) |
52 | | - |
53 | | - assert dns_span["op"] == "socket.dns" |
54 | | - assert dns_span["description"] == f"localhost:{PORT}" # noqa: E231 |
55 | | - assert dns_span["data"] == ApproxDict( |
56 | | - { |
57 | | - "host": "localhost", |
58 | | - "port": PORT, |
59 | | - } |
| 60 | + sentry_init( |
| 61 | + integrations=[SocketIntegration()], |
| 62 | + traces_sample_rate=1.0, |
| 63 | + _experiments={"trace_lifecycle": "stream" if span_streaming else "static"}, |
60 | 64 | ) |
61 | 65 |
|
62 | | - |
63 | | -def test_span_origin(sentry_init, capture_events): |
| 66 | + if span_streaming: |
| 67 | + items = capture_items("span") |
| 68 | + with sentry_sdk.traces.start_span(name="root"): |
| 69 | + socket.create_connection(("localhost", PORT), timeout, None) |
| 70 | + sentry_sdk.flush() |
| 71 | + |
| 72 | + spans = [item.payload for item in items if item.type == "span"] |
| 73 | + # as getaddrinfo gets called in create_connection it should also contain a dns span |
| 74 | + # spans finish in order: dns (inner) ends first, connect ends, then root |
| 75 | + dns_span, connect_span, _root = spans |
| 76 | + |
| 77 | + assert connect_span["attributes"]["sentry.op"] == "socket.connection" |
| 78 | + assert connect_span["name"] == f"localhost:{PORT}" # noqa: E231 |
| 79 | + assert connect_span["attributes"]["server.address"] == "localhost" |
| 80 | + assert connect_span["attributes"]["server.port"] == PORT |
| 81 | + |
| 82 | + assert dns_span["attributes"]["sentry.op"] == "socket.dns" |
| 83 | + assert dns_span["name"] == f"localhost:{PORT}" # noqa: E231 |
| 84 | + assert dns_span["attributes"]["server.address"] == "localhost" |
| 85 | + assert dns_span["attributes"]["server.port"] == PORT |
| 86 | + else: |
| 87 | + events = capture_events() |
| 88 | + |
| 89 | + with start_transaction(): |
| 90 | + socket.create_connection(("localhost", PORT), timeout, None) |
| 91 | + |
| 92 | + (event,) = events |
| 93 | + (connect_span, dns_span) = event["spans"] |
| 94 | + # as getaddrinfo gets called in create_connection it should also contain a dns span |
| 95 | + |
| 96 | + assert connect_span["op"] == "socket.connection" |
| 97 | + assert connect_span["description"] == f"localhost:{PORT}" # noqa: E231 |
| 98 | + assert connect_span["data"] == ApproxDict( |
| 99 | + { |
| 100 | + "address": ["localhost", PORT], |
| 101 | + "timeout": timeout, |
| 102 | + "source_address": None, |
| 103 | + } |
| 104 | + ) |
| 105 | + |
| 106 | + assert dns_span["op"] == "socket.dns" |
| 107 | + assert dns_span["description"] == f"localhost:{PORT}" # noqa: E231 |
| 108 | + assert dns_span["data"] == ApproxDict( |
| 109 | + { |
| 110 | + "host": "localhost", |
| 111 | + "port": PORT, |
| 112 | + } |
| 113 | + ) |
| 114 | + |
| 115 | + |
| 116 | +@pytest.mark.parametrize("span_streaming", [True, False]) |
| 117 | +def test_span_origin(sentry_init, capture_events, capture_items, span_streaming): |
64 | 118 | sentry_init( |
65 | 119 | integrations=[SocketIntegration()], |
66 | 120 | traces_sample_rate=1.0, |
| 121 | + _experiments={"trace_lifecycle": "stream" if span_streaming else "static"}, |
67 | 122 | ) |
68 | | - events = capture_events() |
69 | 123 |
|
70 | | - with start_transaction(name="foo"): |
71 | | - socket.create_connection(("localhost", PORT), 1, None) |
| 124 | + if span_streaming: |
| 125 | + items = capture_items("span") |
| 126 | + with sentry_sdk.traces.start_span(name="foo"): |
| 127 | + socket.create_connection(("localhost", PORT), 1, None) |
| 128 | + sentry_sdk.flush() |
| 129 | + |
| 130 | + spans = [item.payload for item in items if item.type == "span"] |
| 131 | + dns_span, connect_span, _root = spans |
| 132 | + |
| 133 | + assert connect_span["attributes"]["sentry.op"] == "socket.connection" |
| 134 | + assert connect_span["attributes"]["sentry.origin"] == "auto.socket.socket" |
| 135 | + |
| 136 | + assert dns_span["attributes"]["sentry.op"] == "socket.dns" |
| 137 | + assert dns_span["attributes"]["sentry.origin"] == "auto.socket.socket" |
| 138 | + else: |
| 139 | + events = capture_events() |
| 140 | + |
| 141 | + with start_transaction(name="foo"): |
| 142 | + socket.create_connection(("localhost", PORT), 1, None) |
72 | 143 |
|
73 | | - (event,) = events |
| 144 | + (event,) = events |
74 | 145 |
|
75 | | - assert event["contexts"]["trace"]["origin"] == "manual" |
| 146 | + assert event["contexts"]["trace"]["origin"] == "manual" |
76 | 147 |
|
77 | | - assert event["spans"][0]["op"] == "socket.connection" |
78 | | - assert event["spans"][0]["origin"] == "auto.socket.socket" |
| 148 | + assert event["spans"][0]["op"] == "socket.connection" |
| 149 | + assert event["spans"][0]["origin"] == "auto.socket.socket" |
79 | 150 |
|
80 | | - assert event["spans"][1]["op"] == "socket.dns" |
81 | | - assert event["spans"][1]["origin"] == "auto.socket.socket" |
| 151 | + assert event["spans"][1]["op"] == "socket.dns" |
| 152 | + assert event["spans"][1]["origin"] == "auto.socket.socket" |
0 commit comments