|
| 1 | +OpenTelemetry Azure Monitor Exporters |
| 2 | +===================================== |
| 3 | + |
| 4 | +|pypi| |
| 5 | + |
| 6 | +.. |pypi| image:: https://badge.fury.io/py/opentelemetry-azure-monitor-exporter.svg |
| 7 | + :target: https://pypi.org/project/opentelemetry-azure-monitor-exporter/ |
| 8 | + |
| 9 | +Installation |
| 10 | +------------ |
| 11 | + |
| 12 | +:: |
| 13 | + |
| 14 | + pip install opentelemetry-azure-monitor-exporter |
| 15 | + |
| 16 | +Usage |
| 17 | +----- |
| 18 | + |
| 19 | +Trace |
| 20 | +~~~~~ |
| 21 | + |
| 22 | +The **Azure Monitor Trace Exporter** allows you to export `OpenTelemetry`_ traces to `Azure Monitor`_. |
| 23 | + |
| 24 | +This example shows how to send a span "hello" to Azure Monitor. |
| 25 | + |
| 26 | +* Create an Azure Monitor resource and get the instrumentation key, more information can be found `here <https://docs.microsoft.com/azure/azure-monitor/app/create-new-resource>`_. |
| 27 | +* Place your instrumentation key in a `connection string` and directly into your code. |
| 28 | +* Alternatively, you can specify your `connection string` in an environment variable ``APPLICATIONINSIGHTS_CONNECTION_STRING``. |
| 29 | + |
| 30 | + .. code:: python |
| 31 | +
|
| 32 | + from azure_monitor import AzureMonitorSpanExporter |
| 33 | + from opentelemetry import trace |
| 34 | + from opentelemetry.sdk.trace import TracerProvider |
| 35 | + from opentelemetry.sdk.trace.export import BatchExportSpanProcessor |
| 36 | +
|
| 37 | + # The preferred tracer implementation must be set, as the opentelemetry-api |
| 38 | + # defines the interface with a no-op implementation. |
| 39 | + trace.set_preferred_tracer_provider_implementation(lambda T: TracerProvider()) |
| 40 | +
|
| 41 | + # We tell OpenTelemetry who it is that is creating spans. In this case, we have |
| 42 | + # no real name (no setup.py), so we make one up. If we had a version, we would |
| 43 | + # also specify it here. |
| 44 | + tracer = trace.get_tracer(__name__) |
| 45 | +
|
| 46 | + exporter = AzureMonitorSpanExporter( |
| 47 | + connection_string='InstrumentationKey=99c42f65-1656-4c41-afde-bd86b709a4a7', |
| 48 | + ) |
| 49 | +
|
| 50 | + # SpanExporter receives the spans and send them to the target location. |
| 51 | + span_processor = BatchExportSpanProcessor(exporter) |
| 52 | + trace.tracer_provider().add_span_processor(span_processor) |
| 53 | +
|
| 54 | + with tracer.start_as_current_span('hello'): |
| 55 | + print('Hello World!') |
| 56 | +
|
| 57 | +Integrations |
| 58 | +############ |
| 59 | + |
| 60 | +OpenTelemetry also supports several `integrations <https://github.com/open-telemetry/opentelemetry-python/tree/master/ext>`_ which allows to integrate with third party libraries. |
| 61 | + |
| 62 | +This example shows how to integrate with the `requests <https://2.python-requests.org/en/master/>`_ library. |
| 63 | + |
| 64 | +* Create an Azure Monitor resource and get the instrumentation key, more information can be found `here <https://docs.microsoft.com/azure/azure-monitor/app/create-new-resource>`_. |
| 65 | +* Install the `requests integration package using ``pip install opentelemetry-ext-http-requests``. |
| 66 | +* Place your instrumentation key in a `connection string` and directly into your code. |
| 67 | +* Alternatively, you can specify your `connection string` in an environment variable ``APPLICATIONINSIGHTS_CONNECTION_STRING``. |
| 68 | + |
| 69 | +.. code:: python |
| 70 | +
|
| 71 | + import requests |
| 72 | +
|
| 73 | + from azure_monitor import AzureMonitorSpanExporter |
| 74 | + from opentelemetry import trace |
| 75 | + from opentelemetry.ext import http_requests |
| 76 | + from opentelemetry.sdk.trace import TracerProvider |
| 77 | + from opentelemetry.sdk.trace.export import ( |
| 78 | + BatchExportSpanProcessor, |
| 79 | + ConsoleSpanExporter, |
| 80 | + ) |
| 81 | +
|
| 82 | + # The preferred tracer implementation must be set, as the opentelemetry-api |
| 83 | + # defines the interface with a no-op implementation. |
| 84 | + trace.set_preferred_tracer_provider_implementation(lambda T: TracerProvider()) |
| 85 | + tracer_provider = trace.tracer_provider() |
| 86 | +
|
| 87 | + exporter = AzureMonitorSpanExporter( |
| 88 | + connection_string='InstrumentationKey=99c42f65-1656-4c41-afde-bd86b709a4a7', |
| 89 | + ) |
| 90 | + span_processor = BatchExportSpanProcessor(exporter) |
| 91 | + tracer_provider.add_span_processor(span_processor) |
| 92 | +
|
| 93 | + http_requests.enable(tracer_provider) |
| 94 | + response = requests.get(url="https://azure.microsoft.com/") |
| 95 | +
|
| 96 | +Modifying Traces |
| 97 | +################ |
| 98 | + |
| 99 | +* You can pass a callback function to the exporter to process telemetry before it is exported. |
| 100 | +* Your callback function can return `False` if you do not want this envelope exported. |
| 101 | +* Your callback function must accept an [envelope](https://github.com/microsoft/opentelemetry-exporters-python/blob/master/azure_monitor/src/azure_monitor/protocol.py#L80) data type as its parameter. |
| 102 | +* You can see the schema for Azure Monitor data types in the envelopes [here](https://github.com/microsoft/opentelemetry-exporters-python/blob/master/azure_monitor/src/azure_monitor/protocol.py). |
| 103 | +* The `AzureMonitorSpanExporter` handles `Data` data types. |
| 104 | + |
| 105 | +.. code:: python |
| 106 | +
|
| 107 | + from azure_monitor import AzureMonitorSpanExporter |
| 108 | + from opentelemetry import trace |
| 109 | + from opentelemetry.sdk.trace import TracerProvider |
| 110 | + from opentelemetry.sdk.trace.export import BatchExportSpanProcessor |
| 111 | +
|
| 112 | + # Callback function to add os_type: linux to span properties |
| 113 | + def callback_function(envelope): |
| 114 | + envelope.data.baseData.properties['os_type'] = 'linux' |
| 115 | + return True |
| 116 | +
|
| 117 | + exporter = AzureMonitorSpanExporter( |
| 118 | + connection_string='InstrumentationKey=99c42f65-1656-4c41-afde-bd86b709a4a7' |
| 119 | + ) |
| 120 | + exporter.add_telemetry_processor(callback_function) |
| 121 | +
|
| 122 | + trace.set_preferred_tracer_provider_implementation(lambda T: TracerProvider()) |
| 123 | + tracer = trace.get_tracer(__name__) |
| 124 | + span_processor = BatchExportSpanProcessor(exporter) |
| 125 | + trace.tracer_provider().add_span_processor(span_processor) |
| 126 | +
|
| 127 | + with tracer.start_as_current_span('hello'): |
| 128 | + print('Hello World!') |
| 129 | +
|
| 130 | +References |
| 131 | +---------- |
| 132 | + |
| 133 | +* `Azure Monitor <https://docs.microsoft.com/azure/azure-monitor/>`_ |
| 134 | +* `OpenTelemetry Project <https://opentelemetry.io/>`_ |
| 135 | +* `OpenTelemetry Python Client <https://github.com/open-telemetry/opentelemetry-python>`_ |
| 136 | + |
0 commit comments