forked from open-telemetry/opentelemetry-python-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_botocore_instrumentation.py
403 lines (328 loc) · 13.7 KB
/
test_botocore_instrumentation.py
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from unittest.mock import Mock, patch
import botocore.session
from botocore.exceptions import ParamValidationError
from moto import ( # pylint: disable=import-error
mock_ec2,
mock_kinesis,
mock_kms,
mock_s3,
mock_sqs,
mock_sts,
mock_xray,
)
from opentelemetry import trace as trace_api
from opentelemetry.context import attach, detach, set_value
from opentelemetry.instrumentation.botocore import BotocoreInstrumentor
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY
from opentelemetry.propagate import get_global_textmap, set_global_textmap
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.test.mock_textmap import MockTextMapPropagator
from opentelemetry.test.test_base import TestBase
_REQUEST_ID_REGEX_MATCH = r"[A-Z0-9]{52}"
# pylint:disable=too-many-public-methods
class TestBotocoreInstrumentor(TestBase):
"""Botocore integration testsuite"""
def setUp(self):
super().setUp()
BotocoreInstrumentor().instrument()
self.session = botocore.session.get_session()
self.session.set_credentials(
access_key="access-key", secret_key="secret-key"
)
self.region = "us-west-2"
def tearDown(self):
super().tearDown()
BotocoreInstrumentor().uninstrument()
def _make_client(self, service: str):
return self.session.create_client(service, region_name=self.region)
def _default_span_attributes(self, service: str, operation: str):
return {
SpanAttributes.RPC_SYSTEM: "aws-api",
SpanAttributes.RPC_SERVICE: service,
SpanAttributes.RPC_METHOD: operation,
"aws.region": self.region,
"retry_attempts": 0,
SpanAttributes.HTTP_STATUS_CODE: 200,
}
def assert_only_span(self):
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(1, len(spans))
return spans[0]
def assert_span(
self,
service: str,
operation: str,
request_id=None,
attributes=None,
):
span = self.assert_only_span()
expected = self._default_span_attributes(service, operation)
if attributes:
expected.update(attributes)
span_attributes_request_id = "aws.request_id"
if request_id is _REQUEST_ID_REGEX_MATCH:
actual_request_id = span.attributes[span_attributes_request_id]
self.assertRegex(actual_request_id, _REQUEST_ID_REGEX_MATCH)
expected[span_attributes_request_id] = actual_request_id
elif request_id is not None:
expected[span_attributes_request_id] = request_id
self.assertSpanHasAttributes(span, expected)
self.assertEqual(f"{service}.{operation}", span.name)
return span
@mock_ec2
def test_traced_client(self):
ec2 = self._make_client("ec2")
ec2.describe_instances()
request_id = "fdcdcab1-ae5c-489e-9c33-4637c5dda355"
self.assert_span("EC2", "DescribeInstances", request_id=request_id)
@mock_ec2
def test_not_recording(self):
mock_tracer = Mock()
mock_span = Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
with patch("opentelemetry.trace.get_tracer") as tracer:
tracer.return_value = mock_tracer
ec2 = self._make_client("ec2")
ec2.describe_instances()
self.assertFalse(mock_span.is_recording())
self.assertTrue(mock_span.is_recording.called)
self.assertFalse(mock_span.set_attribute.called)
self.assertFalse(mock_span.set_status.called)
@mock_s3
def test_exception(self):
s3 = self._make_client("s3")
with self.assertRaises(ParamValidationError):
s3.list_objects(bucket="mybucket")
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(1, len(spans))
span = spans[0]
expected = self._default_span_attributes("S3", "ListObjects")
expected.pop(SpanAttributes.HTTP_STATUS_CODE)
expected.pop("retry_attempts")
self.assertEqual(expected, span.attributes)
self.assertIs(span.status.status_code, trace_api.StatusCode.ERROR)
self.assertEqual(1, len(span.events))
event = span.events[0]
self.assertIn(SpanAttributes.EXCEPTION_STACKTRACE, event.attributes)
self.assertIn(SpanAttributes.EXCEPTION_TYPE, event.attributes)
self.assertIn(SpanAttributes.EXCEPTION_MESSAGE, event.attributes)
@mock_s3
def test_s3_client(self):
s3 = self._make_client("s3")
s3.list_buckets()
self.assert_span("S3", "ListBuckets")
@mock_s3
def test_s3_put(self):
s3 = self._make_client("s3")
location = {"LocationConstraint": "us-west-2"}
s3.create_bucket(Bucket="mybucket", CreateBucketConfiguration=location)
self.assert_span(
"S3", "CreateBucket", request_id=_REQUEST_ID_REGEX_MATCH
)
self.memory_exporter.clear()
s3.put_object(Key="foo", Bucket="mybucket", Body=b"bar")
self.assert_span("S3", "PutObject", request_id=_REQUEST_ID_REGEX_MATCH)
self.memory_exporter.clear()
s3.get_object(Bucket="mybucket", Key="foo")
self.assert_span("S3", "GetObject", request_id=_REQUEST_ID_REGEX_MATCH)
@mock_sqs
def test_sqs_client(self):
sqs = self._make_client("sqs")
sqs.list_queues()
self.assert_span(
"SQS", "ListQueues", request_id=_REQUEST_ID_REGEX_MATCH
)
@mock_sqs
def test_sqs_send_message(self):
sqs = self._make_client("sqs")
test_queue_name = "test_queue_name"
response = sqs.create_queue(QueueName=test_queue_name)
self.assert_span(
"SQS", "CreateQueue", request_id=_REQUEST_ID_REGEX_MATCH
)
self.memory_exporter.clear()
queue_url = response["QueueUrl"]
sqs.send_message(QueueUrl=queue_url, MessageBody="Test SQS MESSAGE!")
self.assert_span(
"SQS",
"SendMessage",
request_id=_REQUEST_ID_REGEX_MATCH,
attributes={"aws.queue_url": queue_url},
)
@mock_kinesis
def test_kinesis_client(self):
kinesis = self._make_client("kinesis")
kinesis.list_streams()
self.assert_span("Kinesis", "ListStreams")
@mock_kinesis
def test_unpatch(self):
kinesis = self._make_client("kinesis")
BotocoreInstrumentor().uninstrument()
kinesis.list_streams()
self.assertEqual(0, len(self.memory_exporter.get_finished_spans()))
@mock_ec2
def test_uninstrument_does_not_inject_headers(self):
headers = {}
previous_propagator = get_global_textmap()
try:
set_global_textmap(MockTextMapPropagator())
def intercept_headers(**kwargs):
headers.update(kwargs["request"].headers)
ec2 = self._make_client("ec2")
BotocoreInstrumentor().uninstrument()
ec2.meta.events.register_first(
"before-send.ec2.DescribeInstances", intercept_headers
)
with self.tracer_provider.get_tracer("test").start_span("parent"):
ec2.describe_instances()
self.assertNotIn(MockTextMapPropagator.TRACE_ID_KEY, headers)
self.assertNotIn(MockTextMapPropagator.SPAN_ID_KEY, headers)
finally:
set_global_textmap(previous_propagator)
@mock_sqs
def test_double_patch(self):
sqs = self._make_client("sqs")
BotocoreInstrumentor().instrument()
BotocoreInstrumentor().instrument()
sqs.list_queues()
self.assert_span(
"SQS", "ListQueues", request_id=_REQUEST_ID_REGEX_MATCH
)
@mock_kms
def test_kms_client(self):
kms = self._make_client("kms")
kms.list_keys(Limit=21)
span = self.assert_only_span()
# check for exact attribute set to make sure not to leak any kms secrets
self.assertEqual(
self._default_span_attributes("KMS", "ListKeys"), span.attributes
)
@mock_sts
def test_sts_client(self):
sts = self._make_client("sts")
sts.get_caller_identity()
span = self.assert_only_span()
expected = self._default_span_attributes("STS", "GetCallerIdentity")
expected["aws.request_id"] = "c6104cbe-af31-11e0-8154-cbc7ccf896c7"
# check for exact attribute set to make sure not to leak any sts secrets
self.assertEqual(expected, span.attributes)
@mock_ec2
def test_propagator_injects_into_request(self):
headers = {}
previous_propagator = get_global_textmap()
def check_headers(**kwargs):
nonlocal headers
headers = kwargs["request"].headers
try:
set_global_textmap(MockTextMapPropagator())
ec2 = self._make_client("ec2")
ec2.meta.events.register_first(
"before-send.ec2.DescribeInstances", check_headers
)
ec2.describe_instances()
request_id = "fdcdcab1-ae5c-489e-9c33-4637c5dda355"
span = self.assert_span(
"EC2", "DescribeInstances", request_id=request_id
)
self.assertIn(MockTextMapPropagator.TRACE_ID_KEY, headers)
self.assertEqual(
str(span.get_span_context().trace_id),
headers[MockTextMapPropagator.TRACE_ID_KEY],
)
self.assertIn(MockTextMapPropagator.SPAN_ID_KEY, headers)
self.assertEqual(
str(span.get_span_context().span_id),
headers[MockTextMapPropagator.SPAN_ID_KEY],
)
finally:
set_global_textmap(previous_propagator)
@mock_xray
def test_suppress_instrumentation_xray_client(self):
xray_client = self._make_client("xray")
token = attach(set_value(_SUPPRESS_INSTRUMENTATION_KEY, True))
try:
xray_client.put_trace_segments(TraceSegmentDocuments=["str1"])
xray_client.put_trace_segments(TraceSegmentDocuments=["str2"])
finally:
detach(token)
self.assertEqual(0, len(self.get_finished_spans()))
@mock_xray
def test_suppress_http_instrumentation_xray_client(self):
xray_client = self._make_client("xray")
token = attach(set_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY, True))
try:
xray_client.put_trace_segments(TraceSegmentDocuments=["str1"])
xray_client.put_trace_segments(TraceSegmentDocuments=["str2"])
finally:
detach(token)
self.assertEqual(2, len(self.get_finished_spans()))
@mock_s3
def test_request_hook(self):
request_hook_service_attribute_name = "request_hook.service_name"
request_hook_operation_attribute_name = "request_hook.operation_name"
request_hook_api_params_attribute_name = "request_hook.api_params"
def request_hook(span, service_name, operation_name, api_params):
hook_attributes = {
request_hook_service_attribute_name: service_name,
request_hook_operation_attribute_name: operation_name,
request_hook_api_params_attribute_name: json.dumps(api_params),
}
span.set_attributes(hook_attributes)
BotocoreInstrumentor().uninstrument()
BotocoreInstrumentor().instrument(request_hook=request_hook)
s3 = self._make_client("s3")
params = {
"Bucket": "mybucket",
"CreateBucketConfiguration": {"LocationConstraint": "us-west-2"},
}
s3.create_bucket(**params)
self.assert_span(
"S3",
"CreateBucket",
attributes={
request_hook_service_attribute_name: "s3",
request_hook_operation_attribute_name: "CreateBucket",
request_hook_api_params_attribute_name: json.dumps(params),
},
)
@mock_s3
def test_response_hook(self):
response_hook_service_attribute_name = "request_hook.service_name"
response_hook_operation_attribute_name = "response_hook.operation_name"
response_hook_result_attribute_name = "response_hook.result"
def response_hook(span, service_name, operation_name, result):
hook_attributes = {
response_hook_service_attribute_name: service_name,
response_hook_operation_attribute_name: operation_name,
response_hook_result_attribute_name: len(result["Buckets"]),
}
span.set_attributes(hook_attributes)
BotocoreInstrumentor().uninstrument()
BotocoreInstrumentor().instrument(response_hook=response_hook)
s3 = self._make_client("s3")
s3.list_buckets()
self.assert_span(
"S3",
"ListBuckets",
attributes={
response_hook_service_attribute_name: "s3",
response_hook_operation_attribute_name: "ListBuckets",
response_hook_result_attribute_name: 0,
},
)