Skip to content

Commit ae7a415

Browse files
authored
Consolidate instrumentation documentation in docstrings (#754)
1 parent 3049b4b commit ae7a415

File tree

33 files changed

+602
-363
lines changed

33 files changed

+602
-363
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.6.2-0.25b2...HEAD)
9+
910
### Fixed
1011

1112
- `opentelemetry-instrumentation-asgi` now explicitly depends on asgiref as it uses the package instead of instrumenting it.
@@ -19,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1920
([#753](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/753))
2021
- `opentelemetry-instrumentation-pika` Add `_decorate_basic_consume` to ensure post instrumentation `basic_consume` calls are also instrumented.
2122
([#759](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/759))
23+
- Consolidate instrumentation documentation in docstrings
24+
([#754](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/754))
2225

2326
### Fixed
2427

instrumentation/opentelemetry-instrumentation-aiohttp-client/README.rst

-39
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,6 @@ Installation
1616

1717
pip install opentelemetry-instrumentation-aiohttp-client
1818

19-
20-
Example
21-
-------
22-
23-
.. code:: python
24-
25-
import asyncio
26-
27-
import aiohttp
28-
29-
from opentelemetry.instrumentation.aiohttp_client import AioHttpClientInstrumentor
30-
from opentelemetry import trace
31-
from opentelemetry.exporter.jaeger.thrift import JaegerExporter
32-
from opentelemetry.sdk.trace import TracerProvider
33-
from opentelemetry.sdk.trace.export import BatchSpanProcessor
34-
35-
36-
_JAEGER_EXPORTER = JaegerExporter(
37-
service_name="example-xxx",
38-
agent_host_name="localhost",
39-
agent_port=6831,
40-
)
41-
42-
_TRACE_PROVIDER = TracerProvider()
43-
_TRACE_PROVIDER.add_span_processor(BatchSpanProcessor(_JAEGER_EXPORTER))
44-
trace.set_tracer_provider(_TRACE_PROVIDER)
45-
46-
AioHttpClientInstrumentor().instrument()
47-
48-
49-
async def span_emitter():
50-
async with aiohttp.ClientSession() as session:
51-
async with session.get("https://example.com") as resp:
52-
print(resp.status)
53-
54-
55-
asyncio.run(span_emitter())
56-
57-
5819
References
5920
----------
6021

instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py

+23
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,29 @@ def strip_query_params(url: yarl.URL) -> str:
5858
async with session.get(url) as response:
5959
await response.text()
6060
61+
Configuration
62+
-------------
63+
64+
Request/Response hooks
65+
**********************
66+
67+
Utilize request/reponse hooks to execute custom logic to be performed before/after performing a request.
68+
69+
.. code-block:: python
70+
71+
def request_hook(span: Span, params: aiohttp.TraceRequestStartParams):
72+
if span and span.is_recording():
73+
span.set_attribute("custom_user_attribute_from_request_hook", "some-value")
74+
75+
def response_hook(span: Span, params: typing.Union[
76+
aiohttp.TraceRequestEndParams,
77+
aiohttp.TraceRequestExceptionParams,
78+
]):
79+
if span and span.is_recording():
80+
span.set_attribute("custom_user_attribute_from_response_hook", "some-value")
81+
82+
AioHttpClientInstrumentor().instrument(request_hook=request_hook, response_hook=response_hook)
83+
6184
API
6285
---
6386
"""

instrumentation/opentelemetry-instrumentation-asgi/README.rst

-48
Original file line numberDiff line numberDiff line change
@@ -17,54 +17,6 @@ Installation
1717

1818
pip install opentelemetry-instrumentation-asgi
1919

20-
21-
Usage (Quart)
22-
-------------
23-
24-
.. code-block:: python
25-
26-
from quart import Quart
27-
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware
28-
29-
app = Quart(__name__)
30-
app.asgi_app = OpenTelemetryMiddleware(app.asgi_app)
31-
32-
@app.route("/")
33-
async def hello():
34-
return "Hello!"
35-
36-
if __name__ == "__main__":
37-
app.run(debug=True)
38-
39-
40-
Usage (Django 3.0)
41-
------------------
42-
43-
Modify the application's ``asgi.py`` file as shown below.
44-
45-
.. code-block:: python
46-
47-
import os
48-
from django.core.asgi import get_asgi_application
49-
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware
50-
51-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'asgi_example.settings')
52-
53-
application = get_asgi_application()
54-
application = OpenTelemetryMiddleware(application)
55-
56-
57-
Usage (Raw ASGI)
58-
----------------
59-
60-
.. code-block:: python
61-
62-
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware
63-
64-
app = ... # An ASGI application.
65-
app = OpenTelemetryMiddleware(app)
66-
67-
6820
References
6921
----------
7022

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

+76
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,82 @@
1616
The opentelemetry-instrumentation-asgi package provides an ASGI middleware that can be used
1717
on any ASGI framework (such as Django-channels / Quart) to track requests
1818
timing through OpenTelemetry.
19+
20+
Usage (Quart)
21+
-------------
22+
23+
.. code-block:: python
24+
25+
from quart import Quart
26+
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware
27+
28+
app = Quart(__name__)
29+
app.asgi_app = OpenTelemetryMiddleware(app.asgi_app)
30+
31+
@app.route("/")
32+
async def hello():
33+
return "Hello!"
34+
35+
if __name__ == "__main__":
36+
app.run(debug=True)
37+
38+
39+
Usage (Django 3.0)
40+
------------------
41+
42+
Modify the application's ``asgi.py`` file as shown below.
43+
44+
.. code-block:: python
45+
46+
import os
47+
from django.core.asgi import get_asgi_application
48+
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware
49+
50+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'asgi_example.settings')
51+
52+
application = get_asgi_application()
53+
application = OpenTelemetryMiddleware(application)
54+
55+
56+
Usage (Raw ASGI)
57+
----------------
58+
59+
.. code-block:: python
60+
61+
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware
62+
63+
app = ... # An ASGI application.
64+
app = OpenTelemetryMiddleware(app)
65+
66+
67+
Configuration
68+
-------------
69+
70+
Request/Response hooks
71+
**********************
72+
73+
Utilize request/reponse hooks to execute custom logic to be performed before/after performing a request. The server request hook takes in a server span and ASGI
74+
scope object for every incoming request. The client request hook is called with the internal span and an ASGI scope which is sent as a dictionary for when the method recieve is called.
75+
The client response hook is called with the internal span and an ASGI event which is sent as a dictionary for when the method send is called.
76+
77+
.. code-block:: python
78+
79+
def server_request_hook(span: Span, scope: dict):
80+
if span and span.is_recording():
81+
span.set_attribute("custom_user_attribute_from_request_hook", "some-value")
82+
83+
def client_request_hook(span: Span, scope: dict):
84+
if span and span.is_recording():
85+
span.set_attribute("custom_user_attribute_from_client_request_hook", "some-value")
86+
87+
def client_response_hook(span: Span, message: dict):
88+
if span and span.is_recording():
89+
span.set_attribute("custom_user_attribute_from_response_hook", "some-value")
90+
91+
OpenTelemetryMiddleware().(application, server_request_hook=server_request_hook, client_request_hook=client_request_hook, client_response_hook=client_response_hook)
92+
93+
API
94+
---
1995
"""
2096

2197
import typing

instrumentation/opentelemetry-instrumentation-celery/README.rst

-45
Original file line numberDiff line numberDiff line change
@@ -16,51 +16,6 @@ Installation
1616

1717
pip install opentelemetry-instrumentation-celery
1818

19-
Usage
20-
-----
21-
22-
* Start broker backend
23-
24-
::
25-
docker run -p 5672:5672 rabbitmq
26-
27-
28-
* Run instrumented task
29-
30-
.. code-block:: python
31-
32-
from opentelemetry import trace
33-
from opentelemetry.sdk.trace import TracerProvider
34-
from opentelemetry.sdk.trace.export import BatchSpanProcessor
35-
from opentelemetry.instrumentation.celery import CeleryInstrumentor
36-
37-
from celery import Celery
38-
from celery.signals import worker_process_init
39-
40-
@worker_process_init.connect(weak=False)
41-
def init_celery_tracing(*args, **kwargs):
42-
trace.set_tracer_provider(TracerProvider())
43-
span_processor = BatchSpanProcessor(ConsoleSpanExporter())
44-
trace.get_tracer_provider().add_span_processor(span_processor)
45-
CeleryInstrumentor().instrument()
46-
47-
app = Celery("tasks", broker="amqp://localhost")
48-
49-
@app.task
50-
def add(x, y):
51-
return x + y
52-
53-
add.delay(42, 50)
54-
55-
56-
Setting up tracing
57-
--------------------
58-
59-
When tracing a celery worker process, tracing and instrumention both must be initialized after the celery worker
60-
process is initialized. This is required for any tracing components that might use threading to work correctly
61-
such as the BatchSpanProcessor. Celery provides a signal called ``worker_process_init`` that can be used to
62-
accomplish this as shown in the example above.
63-
6419
References
6520
----------
6621
* `OpenTelemetry Celery Instrumentation <https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/celery/celery.html>`_

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

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ def add(x, y):
4747
4848
add.delay(42, 50)
4949
50+
Setting up tracing
51+
------------------
52+
53+
When tracing a celery worker process, tracing and instrumention both must be initialized after the celery worker
54+
process is initialized. This is required for any tracing components that might use threading to work correctly
55+
such as the BatchSpanProcessor. Celery provides a signal called ``worker_process_init`` that can be used to
56+
accomplish this as shown in the example above.
57+
5058
API
5159
---
5260
"""

instrumentation/opentelemetry-instrumentation-django/README.rst

-46
Original file line numberDiff line numberDiff line change
@@ -15,52 +15,6 @@ Installation
1515

1616
pip install opentelemetry-instrumentation-django
1717

18-
Configuration
19-
-------------
20-
21-
Exclude lists
22-
*************
23-
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_DJANGO_EXCLUDED_URLS`` with comma delimited regexes representing which URLs to exclude.
24-
25-
For example,
26-
27-
::
28-
29-
export OTEL_PYTHON_DJANGO_EXCLUDED_URLS="client/.*/info,healthcheck"
30-
31-
will exclude requests such as ``https://site/client/123/info`` and ``https://site/xyz/healthcheck``.
32-
33-
Request attributes
34-
********************
35-
To extract certain attributes from Django's request object and use them as span attributes, set the environment variable ``OTEL_PYTHON_DJANGO_TRACED_REQUEST_ATTRS`` to a comma
36-
delimited list of request attribute names.
37-
38-
For example,
39-
40-
::
41-
42-
export OTEL_PYTHON_DJANGO_TRACED_REQUEST_ATTRS='path_info,content_type'
43-
44-
will extract path_info and content_type attributes from every traced request and add them as span attritbues.
45-
46-
Django Request object reference: https://docs.djangoproject.com/en/3.1/ref/request-response/#attributes
47-
48-
Request and Response hooks
49-
***************************
50-
The instrumentation supports specifying request and response hooks. These are functions that get called back by the instrumentation right after a Span is created for a request
51-
and right before the span is finished while processing a response. The hooks can be configured as follows:
52-
53-
::
54-
55-
def request_hook(span, request):
56-
pass
57-
58-
def response_hook(span, request, response):
59-
pass
60-
61-
DjangoInstrumentation().instrument(request_hook=request_hook, response_hook=response_hook)
62-
63-
6418
References
6519
----------
6620

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

+6
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ def response_hook(span, request, response):
7171
pass
7272
7373
DjangoInstrumentation().instrument(request_hook=request_hook, response_hook=response_hook)
74+
75+
Django Request object: https://docs.djangoproject.com/en/3.1/ref/request-response/#httprequest-objects
76+
Django Response object: https://docs.djangoproject.com/en/3.1/ref/request-response/#httpresponse-objects
77+
78+
API
79+
---
7480
"""
7581

7682
from logging import getLogger

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@
3333
es.index(index='my-index', doc_type='my-type', id=1, body={'my': 'data', 'timestamp': datetime.now()})
3434
es.get(index='my-index', doc_type='my-type', id=1)
3535
36-
API
37-
---
38-
3936
Elasticsearch instrumentation prefixes operation names with the string "Elasticsearch". This
4037
can be changed to a different string by either setting the `OTEL_PYTHON_ELASTICSEARCH_NAME_PREFIX`
4138
environment variable or by passing the prefix as an argument to the instrumentor. For example,
@@ -79,6 +76,9 @@ def response_hook(span, response):
7976
es = elasticsearch.Elasticsearch()
8077
es.index(index='my-index', doc_type='my-type', id=1, body={'my': 'data', 'timestamp': datetime.now()})
8178
es.get(index='my-index', doc_type='my-type', id=1)
79+
80+
API
81+
---
8282
"""
8383

8484
import re

0 commit comments

Comments
 (0)