forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_socket.py
More file actions
152 lines (119 loc) · 5.39 KB
/
Copy pathtest_socket.py
File metadata and controls
152 lines (119 loc) · 5.39 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import socket
import pytest
import sentry_sdk
from sentry_sdk import start_transaction
from sentry_sdk.integrations.socket import SocketIntegration
from tests.conftest import ApproxDict, create_mock_http_server
PORT = create_mock_http_server()
@pytest.mark.parametrize("span_streaming", [True, False])
def test_getaddrinfo_trace(sentry_init, capture_events, capture_items, span_streaming):
sentry_init(
integrations=[SocketIntegration()],
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
if span_streaming:
items = capture_items("span")
with sentry_sdk.traces.start_span(name="root"):
socket.getaddrinfo("localhost", PORT)
sentry_sdk.flush()
spans = [item.payload for item in items if item.type == "span"]
dns_span, _root = spans
assert dns_span["attributes"]["sentry.op"] == "socket.dns"
assert dns_span["attributes"]["sentry.origin"] == "auto.socket.socket"
assert dns_span["name"] == f"localhost:{PORT}" # noqa: E231
assert dns_span["attributes"]["server.address"] == "localhost"
assert dns_span["attributes"]["server.port"] == PORT
else:
events = capture_events()
with start_transaction():
socket.getaddrinfo("localhost", PORT)
(event,) = events
(span,) = event["spans"]
assert span["op"] == "socket.dns"
assert span["description"] == f"localhost:{PORT}" # noqa: E231
assert span["data"] == ApproxDict(
{
"host": "localhost",
"port": PORT,
}
)
@pytest.mark.parametrize("span_streaming", [True, False])
def test_create_connection_trace(
sentry_init, capture_events, capture_items, span_streaming
):
timeout = 10
sentry_init(
integrations=[SocketIntegration()],
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
if span_streaming:
items = capture_items("span")
with sentry_sdk.traces.start_span(name="root"):
socket.create_connection(("localhost", PORT), timeout, None)
sentry_sdk.flush()
spans = [item.payload for item in items if item.type == "span"]
# as getaddrinfo gets called in create_connection it should also contain a dns span
# spans finish in order: dns (inner) ends first, connect ends, then root
dns_span, connect_span, _root = spans
assert connect_span["attributes"]["sentry.op"] == "socket.connection"
assert connect_span["name"] == f"localhost:{PORT}" # noqa: E231
assert connect_span["attributes"]["server.address"] == "localhost"
assert connect_span["attributes"]["server.port"] == PORT
assert dns_span["attributes"]["sentry.op"] == "socket.dns"
assert dns_span["name"] == f"localhost:{PORT}" # noqa: E231
assert dns_span["attributes"]["server.address"] == "localhost"
assert dns_span["attributes"]["server.port"] == PORT
else:
events = capture_events()
with start_transaction():
socket.create_connection(("localhost", PORT), timeout, None)
(event,) = events
(connect_span, dns_span) = event["spans"]
# as getaddrinfo gets called in create_connection it should also contain a dns span
assert connect_span["op"] == "socket.connection"
assert connect_span["description"] == f"localhost:{PORT}" # noqa: E231
assert connect_span["data"] == ApproxDict(
{
"address": ["localhost", PORT],
"timeout": timeout,
"source_address": None,
}
)
assert dns_span["op"] == "socket.dns"
assert dns_span["description"] == f"localhost:{PORT}" # noqa: E231
assert dns_span["data"] == ApproxDict(
{
"host": "localhost",
"port": PORT,
}
)
@pytest.mark.parametrize("span_streaming", [True, False])
def test_span_origin(sentry_init, capture_events, capture_items, span_streaming):
sentry_init(
integrations=[SocketIntegration()],
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
if span_streaming:
items = capture_items("span")
with sentry_sdk.traces.start_span(name="foo"):
socket.create_connection(("localhost", PORT), 1, None)
sentry_sdk.flush()
spans = [item.payload for item in items if item.type == "span"]
dns_span, connect_span, _root = spans
assert connect_span["attributes"]["sentry.op"] == "socket.connection"
assert connect_span["attributes"]["sentry.origin"] == "auto.socket.socket"
assert dns_span["attributes"]["sentry.op"] == "socket.dns"
assert dns_span["attributes"]["sentry.origin"] == "auto.socket.socket"
else:
events = capture_events()
with start_transaction(name="foo"):
socket.create_connection(("localhost", PORT), 1, None)
(event,) = events
assert event["contexts"]["trace"]["origin"] == "manual"
assert event["spans"][0]["op"] == "socket.connection"
assert event["spans"][0]["origin"] == "auto.socket.socket"
assert event["spans"][1]["op"] == "socket.dns"
assert event["spans"][1]["origin"] == "auto.socket.socket"