Skip to content

Commit 2f45948

Browse files
authored
Merge branch 'main' into patch-1
2 parents 25d866f + c644f0d commit 2f45948

File tree

3 files changed

+66
-16
lines changed

3 files changed

+66
-16
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4040

4141
- `opentelemetry-instrumentation-grpc` AioClientInterceptor should propagate with a Metadata object
4242
([#2363](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2363))
43+
- `opentelemetry-instrumentation-boto3sqs` Instrument Session and resource
44+
([#2161](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2161))
4345

4446
### Added
4547

instrumentation/opentelemetry-instrumentation-boto3sqs/src/opentelemetry/instrumentation/boto3sqs/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import logging
3232
from typing import Any, Collection, Dict, Generator, List, Mapping, Optional
3333

34-
import boto3
34+
import boto3.session
3535
import botocore.client
3636
from wrapt import wrap_function_wrapper
3737

@@ -382,7 +382,7 @@ def client_wrapper(wrapped, instance, args, kwargs):
382382
self._decorate_sqs(type(retval))
383383
return retval
384384

385-
wrap_function_wrapper(boto3, "client", client_wrapper)
385+
wrap_function_wrapper(boto3.session.Session, "client", client_wrapper)
386386

387387
def _decorate_sqs(self, sqs_class: type) -> None:
388388
"""
@@ -433,7 +433,7 @@ def _instrument(self, **kwargs: Dict[str, Any]) -> None:
433433
self._decorate_sqs(client_cls)
434434

435435
def _uninstrument(self, **kwargs: Dict[str, Any]) -> None:
436-
unwrap(boto3, "client")
436+
unwrap(boto3.session.Session, "client")
437437

438438
for client_cls in botocore.client.BaseClient.__subclasses__():
439439
self._un_decorate_sqs(client_cls)

instrumentation/opentelemetry-instrumentation-boto3sqs/tests/test_boto3sqs_instrumentation.py

+61-13
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import boto3
2222
from botocore.awsrequest import AWSResponse
23-
from wrapt import BoundFunctionWrapper, FunctionWrapper
23+
from wrapt import BoundFunctionWrapper
2424

2525
from opentelemetry.instrumentation.boto3sqs import (
2626
Boto3SQSGetter,
@@ -37,8 +37,17 @@
3737
from opentelemetry.trace.span import Span, format_span_id, format_trace_id
3838

3939

40-
def _make_sqs_client():
41-
return boto3.client(
40+
def _make_sqs_client(*, session=False):
41+
return (boto3.Session() if session else boto3).client(
42+
"sqs",
43+
region_name="us-east-1",
44+
aws_access_key_id="dummy",
45+
aws_secret_access_key="dummy",
46+
)
47+
48+
49+
def _make_sqs_resource(*, session=False):
50+
return (boto3.Session() if session else boto3).resource(
4251
"sqs",
4352
region_name="us-east-1",
4453
aws_access_key_id="dummy",
@@ -48,7 +57,6 @@ def _make_sqs_client():
4857

4958
class TestBoto3SQSInstrumentor(TestCase):
5059
def _assert_instrumented(self, client):
51-
self.assertIsInstance(boto3.client, FunctionWrapper)
5260
self.assertIsInstance(client.send_message, BoundFunctionWrapper)
5361
self.assertIsInstance(client.send_message_batch, BoundFunctionWrapper)
5462
self.assertIsInstance(client.receive_message, BoundFunctionWrapper)
@@ -57,6 +65,17 @@ def _assert_instrumented(self, client):
5765
client.delete_message_batch, BoundFunctionWrapper
5866
)
5967

68+
def _assert_uninstrumented(self, client):
69+
self.assertNotIsInstance(client.send_message, BoundFunctionWrapper)
70+
self.assertNotIsInstance(
71+
client.send_message_batch, BoundFunctionWrapper
72+
)
73+
self.assertNotIsInstance(client.receive_message, BoundFunctionWrapper)
74+
self.assertNotIsInstance(client.delete_message, BoundFunctionWrapper)
75+
self.assertNotIsInstance(
76+
client.delete_message_batch, BoundFunctionWrapper
77+
)
78+
6079
@staticmethod
6180
@contextmanager
6281
def _active_instrumentor():
@@ -67,19 +86,48 @@ def _active_instrumentor():
6786
Boto3SQSInstrumentor().uninstrument()
6887

6988
def test_instrument_api_before_client_init(self) -> None:
70-
with self._active_instrumentor():
71-
client = _make_sqs_client()
72-
self._assert_instrumented(client)
89+
for session in (False, True):
90+
with self._active_instrumentor():
91+
client = _make_sqs_client(session=session)
92+
self._assert_instrumented(client)
93+
self._assert_uninstrumented(client)
7394

7495
def test_instrument_api_after_client_init(self) -> None:
75-
client = _make_sqs_client()
76-
with self._active_instrumentor():
77-
self._assert_instrumented(client)
96+
for session in (False, True):
97+
client = _make_sqs_client(session=session)
98+
with self._active_instrumentor():
99+
self._assert_instrumented(client)
100+
self._assert_uninstrumented(client)
78101

79102
def test_instrument_multiple_clients(self):
80-
with self._active_instrumentor():
81-
self._assert_instrumented(_make_sqs_client())
82-
self._assert_instrumented(_make_sqs_client())
103+
for session in (False, True):
104+
with self._active_instrumentor():
105+
self._assert_instrumented(_make_sqs_client(session=session))
106+
self._assert_instrumented(_make_sqs_client(session=session))
107+
108+
def test_instrument_api_before_resource_init(self) -> None:
109+
for session in (False, True):
110+
with self._active_instrumentor():
111+
sqs = _make_sqs_resource(session=session)
112+
self._assert_instrumented(sqs.meta.client)
113+
self._assert_uninstrumented(sqs.meta.client)
114+
115+
def test_instrument_api_after_resource_init(self) -> None:
116+
for session in (False, True):
117+
sqs = _make_sqs_resource(session=session)
118+
with self._active_instrumentor():
119+
self._assert_instrumented(sqs.meta.client)
120+
self._assert_uninstrumented(sqs.meta.client)
121+
122+
def test_instrument_multiple_resources(self):
123+
for session in (False, True):
124+
with self._active_instrumentor():
125+
self._assert_instrumented(
126+
_make_sqs_resource(session=session).meta.client
127+
)
128+
self._assert_instrumented(
129+
_make_sqs_resource(session=session).meta.client
130+
)
83131

84132

85133
class TestBoto3SQSGetter(TestCase):

0 commit comments

Comments
 (0)