Skip to content

Commit bd2b13c

Browse files
authored
Merge pull request #103304 from lzchen/patch-2
Add telemetry processor docs for OpenCensus Python SDK
2 parents 3443d34 + d0fd556 commit bd2b13c

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

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

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,107 @@ 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. The 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. To filter out telemetry from being exported,make sure the callback function returns `False`. 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+
426+
```python
427+
# Example for metrics exporter
428+
import time
429+
430+
from opencensus.ext.azure import metrics_exporter
431+
from opencensus.stats import aggregation as aggregation_module
432+
from opencensus.stats import measure as measure_module
433+
from opencensus.stats import stats as stats_module
434+
from opencensus.stats import view as view_module
435+
from opencensus.tags import tag_map as tag_map_module
436+
437+
stats = stats_module.stats
438+
view_manager = stats.view_manager
439+
stats_recorder = stats.stats_recorder
440+
441+
CARROTS_MEASURE = measure_module.MeasureInt("carrots",
442+
"number of carrots",
443+
"carrots")
444+
CARROTS_VIEW = view_module.View("carrots_view",
445+
"number of carrots",
446+
[],
447+
CARROTS_MEASURE,
448+
aggregation_module.CountAggregation())
449+
450+
# Callback function to only export the metric if value is greater than 0
451+
def callback_function(envelope):
452+
return envelope.data.baseData.metrics[0].value > 0
453+
454+
def main():
455+
# Enable metrics
456+
# Set the interval in seconds in which you want to send metrics
457+
exporter = metrics_exporter.new_metrics_exporter(connection_string='InstrumentationKey=<your-instrumentation-key-here>')
458+
exporter.add_telemetry_processor(callback_function)
459+
view_manager.register_exporter(exporter)
460+
461+
view_manager.register_view(CARROTS_VIEW)
462+
mmap = stats_recorder.new_measurement_map()
463+
tmap = tag_map_module.TagMap()
464+
465+
mmap.measure_int_put(CARROTS_MEASURE, 1000)
466+
mmap.record(tmap)
467+
# Default export interval is every 15.0s
468+
# Your application should run for at least this amount
469+
# of time so the exporter will meet this interval
470+
# Sleep can fulfill this
471+
time.sleep(60)
472+
473+
print("Done recording metrics")
474+
475+
if __name__ == "__main__":
476+
main()
477+
```
478+
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.
479+
379480
### Example TelemetryInitializers
380481

381482
#### Add custom property

0 commit comments

Comments
 (0)