forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_s3.py
More file actions
362 lines (309 loc) · 11.9 KB
/
Copy pathtest_s3.py
File metadata and controls
362 lines (309 loc) · 11.9 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
from unittest import mock
import boto3
import pytest
import sentry_sdk
from sentry_sdk.integrations.boto3 import Boto3Integration
from tests.conftest import ApproxDict
from tests.integrations.boto3 import read_fixture
from tests.integrations.boto3.aws_mock import MockResponse
session = boto3.Session(
aws_access_key_id="-",
aws_secret_access_key="-",
)
@pytest.mark.parametrize("span_streaming", [True, False])
def test_basic(
sentry_init,
capture_events,
capture_items,
span_streaming,
):
sentry_init(
traces_sample_rate=1.0,
integrations=[Boto3Integration()],
# disabled because session.resource() or s3.Bucket() result in a subprocess span for a
# shell that runs "uname -p 2> /dev/null" on Python 3.7 with boto3 version 1.12.49.
default_integrations=False,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
s3 = session.resource("s3")
bucket = s3.Bucket("bucket")
if span_streaming:
items = capture_items("span")
with sentry_sdk.traces.start_span(name="custom parent") as span, MockResponse(
s3.meta.client, 200, {}, read_fixture("s3_list.xml")
):
objects = [obj for obj in bucket.objects.all()]
assert len(objects) == 2
assert objects[0].key == "foo.txt"
assert objects[1].key == "bar.txt"
span.end()
sentry_sdk.flush()
spans = [item.payload for item in items]
assert len(spans) == 2
span = spans[0]
assert span["attributes"]["sentry.op"] == "http.client"
assert span["name"] == "aws.s3.ListObjects"
else:
events = capture_events()
with sentry_sdk.start_transaction() as transaction, MockResponse(
s3.meta.client, 200, {}, read_fixture("s3_list.xml")
):
items = [obj for obj in bucket.objects.all()]
assert len(items) == 2
assert items[0].key == "foo.txt"
assert items[1].key == "bar.txt"
transaction.finish()
(event,) = events
assert event["type"] == "transaction"
assert len(event["spans"]) == 1
(span,) = event["spans"]
assert span["op"] == "http.client"
assert span["description"] == "aws.s3.ListObjects"
@pytest.mark.parametrize("send_default_pii", [True, False])
@pytest.mark.parametrize("span_streaming", [True, False])
def test_streaming(
sentry_init,
capture_events,
capture_items,
span_streaming,
send_default_pii,
):
sentry_init(
traces_sample_rate=1.0,
integrations=[Boto3Integration()],
send_default_pii=send_default_pii,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
s3 = session.resource("s3")
obj = s3.Bucket("bucket").Object("foo.pdf")
if span_streaming:
items = capture_items("span")
with sentry_sdk.traces.start_span(name="custom parent") as span, MockResponse(
s3.meta.client, 200, {}, b"hello"
):
body = obj.get()["Body"]
assert body.read(1) == b"h"
assert body.read(2) == b"el"
assert body.read(3) == b"lo"
assert body.read(1) == b""
span.end()
sentry_sdk.flush()
spans = [item.payload for item in items]
assert len(spans) == 3
span1 = spans[0]
assert span1["attributes"]["sentry.op"] == "http.client"
assert span1["name"] == "aws.s3.GetObject"
expected_attrs = {
"http.request.method": "GET",
"rpc.method": "S3/GetObject",
"sentry.environment": "production",
"sentry.op": "http.client",
"sentry.origin": "auto.http.boto3",
"sentry.release": mock.ANY,
"sentry.sdk.name": "sentry.python",
"sentry.sdk.version": mock.ANY,
"sentry.segment.id": mock.ANY,
"sentry.segment.name": "custom parent",
"server.address": mock.ANY,
"thread.id": mock.ANY,
"thread.name": mock.ANY,
}
if send_default_pii:
expected_attrs["url.full"] = "https://bucket.s3.amazonaws.com/foo.pdf"
expected_attrs["url.fragment"] = ""
expected_attrs["url.query"] = ""
assert span1["attributes"] == ApproxDict(expected_attrs)
if not send_default_pii:
assert "url.full" not in span1["attributes"]
assert "url.fragment" not in span1["attributes"]
assert "url.query" not in span1["attributes"]
span2 = spans[1]
assert span2["attributes"]["sentry.op"] == "http.client.stream"
assert span2["name"] == "aws.s3.GetObject"
assert span2["parent_span_id"] == span1["span_id"]
else:
events = capture_events()
with sentry_sdk.start_transaction() as transaction, MockResponse(
s3.meta.client, 200, {}, b"hello"
):
body = obj.get()["Body"]
assert body.read(1) == b"h"
assert body.read(2) == b"el"
assert body.read(3) == b"lo"
assert body.read(1) == b""
transaction.finish()
(event,) = events
assert event["type"] == "transaction"
assert len(event["spans"]) == 2
span1 = event["spans"][0]
assert span1["op"] == "http.client"
assert span1["description"] == "aws.s3.GetObject"
assert span1["data"] == ApproxDict(
{
"http.method": "GET",
"aws.request.url": "https://bucket.s3.amazonaws.com/foo.pdf",
"http.fragment": "",
"http.query": "",
}
)
span2 = event["spans"][1]
assert span2["op"] == "http.client.stream"
assert span2["description"] == "aws.s3.GetObject"
assert span2["parent_span_id"] == span1["span_id"]
@pytest.mark.parametrize("span_streaming", [True, False])
def test_streaming_close(
sentry_init,
capture_events,
capture_items,
span_streaming,
):
sentry_init(
traces_sample_rate=1.0,
integrations=[Boto3Integration()],
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
s3 = session.resource("s3")
obj = s3.Bucket("bucket").Object("foo.pdf")
if span_streaming:
items = capture_items("span")
with sentry_sdk.traces.start_span(name="custom parent") as span, MockResponse(
s3.meta.client, 200, {}, b"hello"
):
body = obj.get()["Body"]
assert body.read(1) == b"h"
body.close() # close partially-read stream
span.end()
sentry_sdk.flush()
spans = [item.payload for item in items]
assert len(spans) == 3
span1 = spans[0]
assert span1["attributes"]["sentry.op"] == "http.client"
span2 = spans[1]
assert span2["attributes"]["sentry.op"] == "http.client.stream"
else:
events = capture_events()
with sentry_sdk.start_transaction() as transaction, MockResponse(
s3.meta.client, 200, {}, b"hello"
):
body = obj.get()["Body"]
assert body.read(1) == b"h"
body.close() # close partially-read stream
transaction.finish()
(event,) = events
assert event["type"] == "transaction"
assert len(event["spans"]) == 2
span1 = event["spans"][0]
assert span1["op"] == "http.client"
span2 = event["spans"][1]
assert span2["op"] == "http.client.stream"
@pytest.mark.tests_internal_exceptions
@pytest.mark.parametrize("span_streaming", [True, False])
def test_omit_url_data_if_parsing_fails(
sentry_init,
capture_events,
capture_items,
span_streaming,
):
sentry_init(
traces_sample_rate=1.0,
integrations=[Boto3Integration()],
send_default_pii=True,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
s3 = session.resource("s3")
bucket = s3.Bucket("bucket")
if span_streaming:
items = capture_items("span")
with mock.patch(
"sentry_sdk.integrations.boto3.parse_url",
side_effect=ValueError,
):
with sentry_sdk.traces.start_span(
name="custom parent"
) as span, MockResponse(
s3.meta.client, 200, {}, read_fixture("s3_list.xml")
):
objects = [obj for obj in bucket.objects.all()]
assert len(objects) == 2
assert objects[0].key == "foo.txt"
assert objects[1].key == "bar.txt"
span.end()
sentry_sdk.flush()
spans = [item.payload for item in items]
assert spans[0]["attributes"] == ApproxDict(
{
"http.request.method": "GET",
"rpc.method": "S3/ListObjects",
"sentry.environment": "production",
"sentry.op": "http.client",
"sentry.origin": "auto.http.boto3",
"sentry.release": mock.ANY,
"sentry.sdk.name": "sentry.python",
"sentry.sdk.version": mock.ANY,
"sentry.segment.id": mock.ANY,
"sentry.segment.name": "custom parent",
"server.address": mock.ANY,
"thread.id": mock.ANY,
"thread.name": mock.ANY,
}
)
assert "url.full" not in spans[0]["attributes"]
assert "url.fragment" not in spans[0]["attributes"]
assert "url.query" not in spans[0]["attributes"]
else:
events = capture_events()
with mock.patch(
"sentry_sdk.integrations.boto3.parse_url",
side_effect=ValueError,
):
with sentry_sdk.start_transaction() as transaction, MockResponse(
s3.meta.client, 200, {}, read_fixture("s3_list.xml")
):
items = [obj for obj in bucket.objects.all()]
assert len(items) == 2
assert items[0].key == "foo.txt"
assert items[1].key == "bar.txt"
transaction.finish()
(event,) = events
assert event["spans"][0]["data"] == ApproxDict(
{
"http.method": "GET",
# no url data
}
)
assert "aws.request.url" not in event["spans"][0]["data"]
assert "http.fragment" not in event["spans"][0]["data"]
assert "http.query" not in event["spans"][0]["data"]
@pytest.mark.parametrize("span_streaming", [True, False])
def test_span_origin(
sentry_init,
capture_events,
capture_items,
span_streaming,
):
sentry_init(
traces_sample_rate=1.0,
integrations=[Boto3Integration()],
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
s3 = session.resource("s3")
bucket = s3.Bucket("bucket")
if span_streaming:
items = capture_items("span")
with sentry_sdk.traces.start_span(name="custom parent"), MockResponse(
s3.meta.client, 200, {}, read_fixture("s3_list.xml")
):
_ = [obj for obj in bucket.objects.all()]
sentry_sdk.flush()
spans = [item.payload for item in items]
assert spans[1]["attributes"]["sentry.origin"] == "manual"
assert spans[0]["attributes"]["sentry.origin"] == "auto.http.boto3"
else:
events = capture_events()
with sentry_sdk.start_transaction(), MockResponse(
s3.meta.client, 200, {}, read_fixture("s3_list.xml")
):
_ = [obj for obj in bucket.objects.all()]
(event,) = events
assert event["contexts"]["trace"]["origin"] == "manual"
assert event["spans"][0]["origin"] == "auto.http.boto3"