Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[boto3sqs] Instrument Session and resource #2161

Merged
merged 11 commits into from
Apr 22, 2024
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- `opentelemetry-instrumentation-boto3sqs` Instrument Session and resource
([#2161](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2161))
- Drop uspport for 3.7
([#2151](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2151))
- `opentelemetry-resource-detector-azure` Added 10s timeout to VM Resource Detector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,9 @@ def client_wrapper(wrapped, instance, args, kwargs):
return retval

wrap_function_wrapper(boto3, "client", client_wrapper)
wrap_function_wrapper(boto3, "resource", client_wrapper)
wrap_function_wrapper(boto3.Session, "client", client_wrapper)
wrap_function_wrapper(boto3.Session, "resource", client_wrapper)

def _decorate_sqs(self, sqs_class: type) -> None:
"""
Expand Down Expand Up @@ -434,6 +437,9 @@ def _instrument(self, **kwargs: Dict[str, Any]) -> None:

def _uninstrument(self, **kwargs: Dict[str, Any]) -> None:
unwrap(boto3, "client")
unwrap(boto3, "resource")
unwrap(boto3.Session, "client")
unwrap(boto3.Session, "resource")

for client_cls in botocore.client.BaseClient.__subclasses__():
self._un_decorate_sqs(client_cls)
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,17 @@
from opentelemetry.trace.span import Span, format_span_id, format_trace_id


def _make_sqs_client():
return boto3.client(
def _make_sqs_client(*, session=False):
return (boto3.Session() if session else boto3).client(
"sqs",
region_name="us-east-1",
aws_access_key_id="dummy",
aws_secret_access_key="dummy",
)


def _make_sqs_resource(*, session=False):
return (boto3.Session() if session else boto3).resource(
"sqs",
region_name="us-east-1",
aws_access_key_id="dummy",
Expand Down Expand Up @@ -67,19 +76,44 @@ def _active_instrumentor():
Boto3SQSInstrumentor().uninstrument()

def test_instrument_api_before_client_init(self) -> None:
with self._active_instrumentor():
client = _make_sqs_client()
self._assert_instrumented(client)
for session in (False, True):
with self._active_instrumentor():
client = _make_sqs_client(session=session)
self._assert_instrumented(client)

def test_instrument_api_after_client_init(self) -> None:
client = _make_sqs_client()
with self._active_instrumentor():
self._assert_instrumented(client)
for session in (False, True):
client = _make_sqs_client(session=session)
with self._active_instrumentor():
self._assert_instrumented(client)

def test_instrument_multiple_clients(self):
with self._active_instrumentor():
self._assert_instrumented(_make_sqs_client())
self._assert_instrumented(_make_sqs_client())
for session in (False, True):
with self._active_instrumentor():
self._assert_instrumented(_make_sqs_client(session=session))
self._assert_instrumented(_make_sqs_client(session=session))

def test_instrument_api_before_resource_init(self) -> None:
for session in (False, True):
with self._active_instrumentor():
sqs = _make_sqs_resource(session=session)
self._assert_instrumented(sqs.meta.client)

def test_instrument_api_after_resource_init(self) -> None:
for session in (False, True):
sqs = _make_sqs_resource(session=session)
with self._active_instrumentor():
self._assert_instrumented(sqs.meta.client)

def test_instrument_multiple_resources(self):
for session in (False, True):
with self._active_instrumentor():
self._assert_instrumented(
_make_sqs_resource(session=session).meta.client
)
self._assert_instrumented(
_make_sqs_resource(session=session).meta.client
)


class TestBoto3SQSGetter(TestCase):
Expand Down
Loading