Skip to content

Commit f832976

Browse files
lmolkovapvaneck
andauthored
Update tracing docs and add note to the @distributed_trace decorator that it's not intended for end users (#37478)
* Update tracing docs and add note to the @distributed_trace decorator * Apply suggestions from code review Co-authored-by: Paul Van Eck <[email protected]> * suppress readme checks --------- Co-authored-by: Paul Van Eck <[email protected]>
1 parent 5e6da8c commit f832976

File tree

4 files changed

+82
-47
lines changed

4 files changed

+82
-47
lines changed

eng/.docsettings.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ known_content_issues:
119119
- ['sdk/core/azure-common/README.rst', 'common']
120120
- ['sdk/core/azure-core/README.md', 'common']
121121
- ['sdk/core/azure-core/samples/README.md', 'common']
122+
- ['sdk/core/azure-core-tracing-opentelemetry/README.md', 'common']
122123
- ['sdk/core/corehttp/README.md', 'common']
123124
- ['sdk/core/corehttp/samples/README.md', 'common']
124125
- ['sdk/search/azure-search-documents/README.md', 'common']

sdk/core/azure-core-tracing-opentelemetry/README.md

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,80 @@
44

55
## Getting started
66

7-
### Install the package
7+
You can enable distributed tracing in Azure client libraries by configuring the OpenTelemetry SDK.
8+
OpenTelemetry is a popular open-source observability framework for generating, capturing, and collecting telemetry data for cloud-native software.
89

9-
Install the Azure Core OpenTelemetry Tracing plugin for Python with [pip](https://pypi.org/project/pip/):
10+
There are two key concepts related to tracing: span and trace. A span represents a single operation in a trace. A span can represent an HTTP request,
11+
a remote procedure call (RPC), a database query, or even the path that your code takes. A trace is a tree of spans showing the path of work through
12+
a system. You can distinguish a trace on its own by a unique 16-byte sequence called a TraceID. For more information on these concepts and how they
13+
relate to OpenTelemetry, see the [OpenTelemetry documentation](https://opentelemetry.io/docs/).
1014

11-
```bash
12-
pip install azure-core-tracing-opentelemetry
15+
## Tracing with Azure Monitor OpenTelemetry Distro
16+
17+
[Azure Monitor OpenTelemetry Distro](https://learn.microsoft.com/azure/azure-monitor/app/opentelemetry-enable?tabs=python) supports tracing for Azure
18+
SDKs by default. Just install and configure the distro and use Azure clients as usual.
19+
20+
```python
21+
22+
# Enable Azure Monitor OpenTelemetry Distro
23+
# It confiures Azure SDKs to use OpenTelemetry as well
24+
from azure.monitor.opentelemetry import configure_azure_monitor
25+
from opentelemetry import trace
26+
27+
configure_azure_monitor(
28+
connection_string="<your-connection-string>"
29+
)
30+
31+
# Use Azure SDKs as usual, here as an example with Storage SDKs
32+
# you may also report your own spans for it.
33+
from azure.storage.blob import BlobServiceClient
34+
35+
tracer = trace.get_tracer(__name__)
36+
with tracer.start_as_current_span(name="MyApplication"):
37+
client = BlobServiceClient.from_connection_string('connectionstring')
38+
client.create_container('my_container') # Call will be traced
1339
```
1440

15-
Now you can use OpenTelemetry for Python as usual with any SDKs that are compatible
16-
with azure-core tracing. This includes (not exhaustive list), azure-storage-blob, azure-keyvault-secrets, azure-eventhub, etc.
41+
The Azure Monitor OpenTelemetry Distro can be found in the [`azure-monitor-opentelemetry`](https://pypi.org/project/azure-monitor-opentelemetry) package.
1742

18-
## Key concepts
43+
## Tracing with generic OpenTelemetry
1944

20-
* You don't need to pass any context, SDK will get it for you
21-
* There are two ways to enable the tracing plugin in code:
45+
Check out your observability provider documentation on how to enable distributed tracing with OpenTelemetry
46+
or follow [OpenTelemetry Python documentation](https://opentelemetry.io/docs/languages/python/) on generic configuration.
2247

23-
```python
24-
from azure.core.settings import settings
25-
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan
26-
settings.tracing_implementation = OpenTelemetrySpan
27-
```
48+
In addition to common OpenTelemetry configuration, follow this steps to configure Azure SDKs:
2849

29-
or
50+
1. Install the Azure Core OpenTelemetry Tracing plugin for Python with [pip](https://pypi.org/project/pip/):
3051

31-
```python
32-
from azure.core.settings import settings
33-
settings.tracing_implementation = "opentelemetry"
34-
```
52+
```bash
53+
pip install azure-core-tracing-opentelemetry
54+
```
3555

36-
* Alternatively, if you have the latest version of `azure-core` installed, you can also set the following environment variable to enable tracing with OpenTelemetry:
56+
Now you can use Azure Core OpenTelemetry Tracing plugin for Python as usual with any SDKs that are compatible
57+
with azure-core tracing. This includes (not exhaustive list), `azure-storage-blob`, `azure-keyvault-secrets`, `azure-eventhub`, etc.
3758

38-
```bash
39-
AZURE_SDK_TRACING_IMPLEMENTATION=opentelemetry
40-
```
59+
2. Specify which tracing implementation Azure SDK should use in one of the following ways:
60+
- By setting `AZURE_SDK_TRACING_IMPLEMENTATION` environment variable to `opentelemetry`
61+
(just make sure you use a fresh version of `azure-core` and `azure-core-tracing-opentelemetry`)
4162

42-
## Examples
63+
```bash
64+
AZURE_SDK_TRACING_IMPLEMENTATION=opentelemetry
65+
```
4366

44-
There is no explicit context to pass, you just create your usual opentelemetry tracer and
45-
call any SDK code that is compatible with azure-core tracing. This is an example
46-
using Azure Monitor exporter, but you can use any exporter (Zipkin, etc.).
67+
- Alternatively, you can set it up in the code:
68+
69+
```python
70+
from azure.core.settings import settings
71+
settings.tracing_implementation = "opentelemetry"
72+
```
73+
74+
This configuration instructs Azure SDK clients to emit spans using global OpenTelemetry instance and
75+
corresponding tracer provider.
76+
77+
There is no need to write any additional code to trace Azure SDK calls or pass trace context explicitly -
78+
Azure SDKs and OpenTelemetry will do it for you.
79+
80+
Here's a full example:
4781
4882
```python
4983
@@ -52,16 +86,10 @@ from azure.core.settings import settings
5286
5387
settings.tracing_implementation = "opentelemetry"
5488
55-
# In the below example, we use a simple console exporter, uncomment these lines to use
56-
# the OpenTelemetry exporter for Azure Monitor.
57-
# Example of a trace exporter for Azure Monitor, but you can use anything OpenTelemetry supports
58-
# from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
59-
# exporter = AzureMonitorTraceExporter(
60-
# connection_string="the connection string used for your Application Insights resource"
61-
# )
89+
# In the below example, we use a simple console exporter.
90+
91+
# See https://opentelemetry.io/docs/languages/python/ for more details on OpenTelemetry configuration
6292
63-
# Regular open telemetry usage from here, see https://github.com/open-telemetry/opentelemetry-python
64-
# for details
6593
from opentelemetry import trace
6694
from opentelemetry.sdk.trace import TracerProvider
6795
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
@@ -71,7 +99,6 @@ from opentelemetry.sdk.trace.export import SimpleSpanProcessor
7199
exporter = ConsoleSpanExporter()
72100
73101
trace.set_tracer_provider(TracerProvider())
74-
tracer = trace.get_tracer(__name__)
75102
trace.get_tracer_provider().add_span_processor(
76103
SimpleSpanProcessor(exporter)
77104
)
@@ -80,29 +107,20 @@ trace.get_tracer_provider().add_span_processor(
80107
81108
from azure.storage.blob import BlobServiceClient
82109
110+
tracer = trace.get_tracer(__name__)
83111
with tracer.start_as_current_span(name="MyApplication"):
84112
client = BlobServiceClient.from_connection_string('connectionstring')
85113
client.create_container('my_container') # Call will be traced
86114
```
87115
88-
The Azure Monitor OpenTelemetry Exporter can be found in the [`azure-monitor-opentelemetry-exporter`](https://pypi.org/project/azure-monitor-opentelemetry-exporter/) package.
89-
90-
91116
## HTTP instrumentation
92117
93118
With the Azure Core OpenTelemetry Tracing plugin enabled, HTTP requests made by Azure SDK clients are typically instrumented via the [`DistributedTracingPolicy`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/core/azure-core/azure/core/pipeline/policies/_distributed_tracing.py) automatically. Since Azure Core handles HTTP instrumentation for Azure service calls, automatic HTTP instrumentation from other libraries such as `opentelemetry-requests-instrumentation` are suppressed to avoid duplicate spans from being created.
94119
95-
96120
## Troubleshooting
97121
98122
This client raises exceptions defined in [Azure Core](https://learn.microsoft.com/python/api/azure-core/azure.core.exceptions?view=azure-python).
99123
100-
101-
## Next steps
102-
103-
More documentation on OpenTelemetry configuration can be found on the [OpenTelemetry website](https://opentelemetry.io)
104-
105-
106124
## Contributing
107125
108126
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

sdk/core/azure-core/azure/core/tracing/decorator.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ def distributed_trace(
6868
6969
Span will use the func name or "name_of_span".
7070
71+
Note:
72+
73+
This decorator SHOULD NOT be used by application developers. It's
74+
intended to be called by Azure client libraries only.
75+
76+
Application developers should use OpenTelemetry or other tracing libraries to
77+
instrument their applications.
78+
7179
:param callable __func: A function to decorate
7280
:keyword name_of_span: The span name to replace func name if necessary
7381
:paramtype name_of_span: str

sdk/core/azure-core/azure/core/tracing/decorator_async.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ def distributed_trace_async( # pylint: disable=unused-argument
7777
7878
Span will use the func name or "name_of_span".
7979
80+
Note:
81+
82+
This decorator SHOULD NOT be used by application developers. It's
83+
intended to be called by Azure client libraries only.
84+
85+
Application developers should use OpenTelemetry or other tracing libraries to
86+
instrument their applications.
87+
8088
:param callable __func: A function to decorate
8189
:keyword name_of_span: The span name to replace func name if necessary
8290
:paramtype name_of_span: str

0 commit comments

Comments
 (0)