Skip to content

Commit 55a8f04

Browse files
authored
Merge pull request #103164 from lzchen/logs
Update OpenCensus Python SDK docs for custom dimensions using Log Exporter
2 parents 87d1ae2 + 44c2609 commit 55a8f04

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

articles/azure-monitor/app/opencensus-python.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,9 @@ Here are the exporters that OpenCensus provides mapped to the types of telemetry
333333
main()
334334
```
335335

336-
6. You can also add custom dimensions to your logs. These will appear as key-value pairs in `customDimensions` in Azure Monitor.
336+
6. You can also add custom properties to your log messages in the *extra* keyword argument using the custom_dimensions field. These will appear as key-value pairs in `customDimensions` in Azure Monitor.
337337
> [!NOTE]
338-
> For this feature to work, you need to pass a dictionary as an argument to your logs, any other data structure will be ignored. To maintain string formatting, store them in a dictionary and pass them as arguments.
338+
> For this feature to work, you need to pass a dictionary to the custom_dimensions field. If you pass arguments of any other type, the logger will ignore them.
339339

340340
```python
341341
import logging
@@ -347,7 +347,17 @@ Here are the exporters that OpenCensus provides mapped to the types of telemetry
347347
logger.addHandler(AzureLogHandler(
348348
connection_string='InstrumentationKey=00000000-0000-0000-0000-000000000000')
349349
)
350-
logger.warning('action', {'key-1': 'value-1', 'key-2': 'value2'})
350+
351+
properties = {'custom_dimensions': {'key_1': 'value_1', 'key_2': 'value_2'}}
352+
353+
# Use properties in logging statements
354+
logger.warning('action', extra=properties)
355+
356+
# Use properties in exception logs
357+
try:
358+
result = 1 / 0 # generate a ZeroDivisionError
359+
except Exception:
360+
logger.exception('Captured an exception.', extra=properties)
351361
```
352362

353363
7. For details on how to enrich your logs with trace context data, see OpenCensus Python [logs integration](https://docs.microsoft.com/azure/azure-monitor/app/correlation#log-correlation).

0 commit comments

Comments
 (0)