Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit abc798d

Browse files
committed
Adding usage instructions
1 parent 6f17655 commit abc798d

File tree

4 files changed

+167
-47
lines changed

4 files changed

+167
-47
lines changed

README.md CONTRIBUTING.md

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
2-
# Contributing
3-
4-
This project welcomes contributions and suggestions. Most contributions require you to agree to a
5-
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
6-
the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
7-
8-
When you submit a pull request, a CLA bot will automatically determine whether you need to provide
9-
a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions
10-
provided by the bot. You will only need to do this once across all repos using our CLA.
11-
12-
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
13-
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
14-
contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
1+
2+
# Contributing
3+
4+
This project welcomes contributions and suggestions. Most contributions require you to agree to a
5+
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
6+
the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
7+
8+
When you submit a pull request, a CLA bot will automatically determine whether you need to provide
9+
a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions
10+
provided by the bot. You will only need to do this once across all repos using our CLA.
11+
12+
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
13+
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
14+
contact [[email protected]](mailto:[email protected]) with any additional questions or comments.

README.rst

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+

azure_monitor/README.rst

-19
This file was deleted.
+17-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
# Copyright (c) Microsoft Corporation. All rights reserved.
2-
# Licensed under the MIT License.
3-
# pylint: disable=import-error
4-
# pylint: disable=no-member
1+
from azure_monitor import AzureMonitorSpanExporter
52
from opentelemetry import trace
6-
from opentelemetry.sdk.trace import TracerSource
7-
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor
3+
from opentelemetry.sdk.trace import TracerProvider
4+
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor
85

9-
from azure_monitor import AzureMonitorSpanExporter
6+
# Callback function to add os_type: linux to span properties
7+
def callback_function(envelope):
8+
envelope.data.baseData.properties['os_type'] = 'linux'
9+
return True
1010

11-
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())
12-
span_processor = SimpleExportSpanProcessor(
13-
AzureMonitorSpanExporter(instrumentation_key="<INSTRUMENTATION KEY HERE>")
11+
exporter = AzureMonitorSpanExporter(
12+
connection_string='InstrumentationKey=99c42f65-1656-4c41-afde-bd86b709a4a7'
1413
)
15-
trace.tracer_source().add_span_processor(span_processor)
16-
tracer = trace.tracer_source().get_tracer(__name__)
14+
exporter.add_telemetry_processor(callback_function)
15+
16+
trace.set_preferred_tracer_provider_implementation(lambda T: TracerProvider())
17+
tracer = trace.get_tracer(__name__)
18+
span_processor = BatchExportSpanProcessor(exporter)
19+
trace.tracer_provider().add_span_processor(span_processor)
1720

18-
with tracer.start_as_current_span("hello") as span:
19-
print("Hello, World!")
21+
with tracer.start_as_current_span('hello'):
22+
print('Hello, World!')

0 commit comments

Comments
 (0)