Skip to content

Commit 3f24703

Browse files
committed
Update botocore documentation to include aiobotocore
1 parent aa74c12 commit 3f24703

3 files changed

Lines changed: 51 additions & 22 deletions

File tree

instrumentation/opentelemetry-instrumentation-botocore/README.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ OpenTelemetry Botocore Tracing
66
.. |pypi| image:: https://badge.fury.io/py/opentelemetry-instrumentation-botocore.svg
77
:target: https://pypi.org/project/opentelemetry-instrumentation-botocore/
88

9-
This library allows tracing requests made by the Botocore library.
9+
This library allows tracing requests made by the botocore and aiobotocore libraries.
1010

1111
Extensions
1212
----------
1313

1414
The instrumentation supports creating extensions for AWS services for enriching what is collected. We have extensions
1515
for the following AWS services:
1616

17-
- Bedrock Runtime
17+
- Bedrock Runtime (botocore only)
1818
- DynamoDB
1919
- Lambda
2020
- SNS
2121
- SQS
2222

23-
Bedrock Runtime
24-
***************
23+
Bedrock Runtime (botocore only)
24+
*******************************
2525

2626
This extension implements the GenAI semantic conventions for the following API calls:
2727

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

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
# limitations under the License.
1414

1515
"""
16-
Instrument `Botocore`_ to trace service requests.
16+
Instrument `botocore`_ and `aiobotocore`_ to trace service requests.
1717
1818
There are two options for instrumenting code. The first option is to use the
1919
``opentelemetry-instrument`` executable which will automatically
20-
instrument your Botocore client. The second is to programmatically enable
20+
instrument your botocore or aiobotocore client. The second is to programmatically enable
2121
instrumentation via the following code:
2222
23-
.. _Botocore: https://pypi.org/project/botocore/
23+
.. _botocore: https://pypi.org/project/botocore/
24+
.. _aiobotocore: https://pypi.org/project/aiobotocore/
2425
2526
Usage
2627
-----
@@ -31,17 +32,38 @@
3132
import botocore.session
3233
3334
34-
# Instrument Botocore
35+
# Instrument botocore
3536
BotocoreInstrumentor().instrument()
3637
37-
# This will create a span with Botocore-specific attributes
38+
# This will create a span with botocore-specific attributes
3839
session = botocore.session.get_session()
3940
session.set_credentials(
4041
access_key="access-key", secret_key="secret-key"
4142
)
4243
ec2 = session.create_client("ec2", region_name="us-west-2")
4344
ec2.describe_instances()
4445
46+
Async Usage
47+
-----------
48+
49+
.. code:: python
50+
51+
from opentelemetry.instrumentation.botocore import AiobotocoreInstrumentor
52+
import aiobotocore.session
53+
import asyncio
54+
55+
56+
async def main():
57+
# Instrument Aiobotocore
58+
AiobotocoreInstrumentor().instrument()
59+
60+
# This will create a span with aiobotocore-specific attributes
61+
session = aiobotocore.session.get_session()
62+
async with session.create_client("ec2") as client:
63+
await client.describe_instances()
64+
65+
asyncio.run(main())
66+
4567
Thread Context Propagation
4668
--------------------------
4769
@@ -63,7 +85,7 @@
6385
API
6486
---
6587
66-
The `instrument` method accepts the following keyword args:
88+
The `instrument` method (for both ``BotocoreInstrumentor`` and ``AiobotocoreInstrumentor``) accepts the following keyword args:
6789
6890
* tracer_provider (``TracerProvider``) - an optional tracer provider
6991
* request_hook (``Callable[[Span, str, str, dict], None]``) - a function with extra user-defined logic to be performed before performing the request
@@ -84,10 +106,10 @@ def response_hook(span, service_name, operation_name, result):
84106
# response hook logic
85107
pass
86108
87-
# Instrument Botocore with hooks
109+
# Instrument botocore with hooks
88110
BotocoreInstrumentor().instrument(request_hook=request_hook, response_hook=response_hook)
89111
90-
# This will create a span with Botocore-specific attributes, including custom attributes added from the hooks
112+
# This will create a span with botocore-specific attributes, including custom attributes added from the hooks
91113
session = botocore.session.get_session()
92114
session.set_credentials(
93115
access_key="access-key", secret_key="secret-key"
@@ -157,12 +179,14 @@ class BotocoreInstrumentor(BaseInstrumentor):
157179

158180
def __init__(self):
159181
super().__init__()
160-
self.request_hook = None
161-
self.response_hook = None
162-
self.extension_registry = ExtensionRegistry(
163-
__name__, _BOTOCORE_EXTENSIONS, None, None, None
164-
)
165-
self.propagator = AwsXRayPropagator()
182+
if not hasattr(self, "request_hook"):
183+
self.request_hook = None
184+
if not hasattr(self, "response_hook"):
185+
self.response_hook = None
186+
if not hasattr(self, "extension_registry"):
187+
self.extension_registry = None
188+
if not hasattr(self, "propagator"):
189+
self.propagator = AwsXRayPropagator()
166190

167191
def instrumentation_dependencies(self) -> Collection[str]:
168192
return _instruments_botocore
@@ -311,10 +335,14 @@ class AiobotocoreInstrumentor(BaseInstrumentor):
311335

312336
def __init__(self):
313337
super().__init__()
314-
self.request_hook = None
315-
self.response_hook = None
316-
self.extension_registry = None
317-
self.propagator = AwsXRayPropagator()
338+
if not hasattr(self, "request_hook"):
339+
self.request_hook = None
340+
if not hasattr(self, "response_hook"):
341+
self.response_hook = None
342+
if not hasattr(self, "extension_registry"):
343+
self.extension_registry = None
344+
if not hasattr(self, "propagator"):
345+
self.propagator = AwsXRayPropagator()
318346

319347
def instrumentation_dependencies(self) -> Collection[str]:
320348
return _instruments_aiobotocore

instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/extensions/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def loader():
3939
}
4040

4141
_AIOBOTOCORE_EXTENSIONS = {
42+
# TODO: Add Bedrock support for aiobotocore
4243
"dynamodb": _lazy_load(".dynamodb", "_DynamoDbExtension"),
4344
"lambda": _lazy_load(".lmbd", "_LambdaExtension"),
4445
"secretsmanager": _lazy_load(

0 commit comments

Comments
 (0)