Skip to content

Commit dba993d

Browse files
Merge pull request #215528 from lzchen/exception
Fix exception message for Python
2 parents 68411e3 + 1e05045 commit dba993d

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

articles/azure-monitor/app/opentelemetry-enable.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,8 @@ catch(error){
13221322

13231323
#### [Python](#tab/python)
13241324

1325+
The OpenTelemetry Python SDK is implemented such that exceptions thrown will automatically be captured and recorded. See below for an example of this.
1326+
13251327
```python
13261328
from opentelemetry import trace
13271329
from opentelemetry.sdk.trace import TracerProvider
@@ -1339,12 +1341,27 @@ trace.get_tracer_provider().add_span_processor(span_processor)
13391341
# Exception events
13401342
try:
13411343
with tracer.start_as_current_span("hello") as span:
1344+
# This exception will be automatically recorded
13421345
raise Exception("Custom exception message.")
13431346
except Exception:
13441347
print("Exception raised")
13451348

13461349
```
13471350

1351+
If you would like to record exceptions manually, you can disable that option when creating the span as show below.
1352+
1353+
```python
1354+
...
1355+
with tracer.start_as_current_span("hello", record_exception=False) as span:
1356+
try:
1357+
raise Exception("Custom exception message.")
1358+
except Exception as ex:
1359+
# Manually record exception
1360+
span.record_exception(ex)
1361+
...
1362+
1363+
```
1364+
13481365
---
13491366

13501367
## Enable the OTLP Exporter

0 commit comments

Comments
 (0)