Skip to content

Commit 549f61f

Browse files
authored
Add telemetry processor docs for OpenCensus Python SDK
1 parent 7142ade commit 549f61f

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

articles/azure-monitor/app/api-filtering-sampling.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,54 @@ For a summary of the non-custom properties available on the telemetryItem, see [
376376

377377
You can add as many initializers as you like, and they are called in the order they are added.
378378

379+
### OpenCensus Python telemetry processors
380+
381+
Telemetry processors in OpenCensus Python are simply callback functions called to process telemetry before they are exported. Your callback function must accept an [envelope](https://github.com/census-instrumentation/opencensus-python/blob/master/contrib/opencensus-ext-azure/opencensus/ext/azure/common/protocol.py#L86) data type as its parameter. Your callback function can return `False` if you do not want a envelopes to be exported. You can see the schema for Azure Monitor data types in the envelopes [here](https://github.com/census-instrumentation/opencensus-python/blob/master/contrib/opencensus-ext-azure/opencensus/ext/azure/common/protocol.py).
382+
383+
```python
384+
# Example for log exporter
385+
import logging
386+
387+
from opencensus.ext.azure.log_exporter import AzureLogHandler
388+
389+
logger = logging.getLogger(__name__)
390+
391+
# Callback function to append '_hello' to each log message telemetry
392+
def callback_function(envelope):
393+
envelope.data.baseData.message += '_hello'
394+
return True
395+
396+
handler = AzureLogHandler(connection_string='InstrumentationKey=<your-instrumentation_key-here>')
397+
handler.add_telemetry_processor(callback_function)
398+
logger.addHandler(handler)
399+
logger.warning('Hello, World!')
400+
```
401+
```python
402+
# Example for trace exporter
403+
import requests
404+
405+
from opencensus.ext.azure.trace_exporter import AzureExporter
406+
from opencensus.trace import config_integration
407+
from opencensus.trace.samplers import ProbabilitySampler
408+
from opencensus.trace.tracer import Tracer
409+
410+
config_integration.trace_integrations(['requests'])
411+
412+
# Callback function to add os_type: linux to span properties
413+
def callback_function(envelope):
414+
envelope.data.baseData.properties['os_type'] = 'linux'
415+
return True
416+
417+
exporter = AzureExporter(
418+
connection_string='InstrumentationKey=<your-instrumentation-key-here>'
419+
)
420+
exporter.add_telemetry_processor(callback_function)
421+
tracer = Tracer(exporter=exporter, sampler=ProbabilitySampler(1.0))
422+
with tracer.span(name='parent'):
423+
response = requests.get(url='https://www.wikipedia.org/wiki/Rabbit')
424+
```
425+
You can add as many processors as you like, and they are called in the order they are added. If one processor should throw an exception, it does not impact the following processors.
426+
379427
### Example TelemetryInitializers
380428

381429
#### Add custom property

0 commit comments

Comments
 (0)