Skip to content

Commit b9e3619

Browse files
authored
Add servicebus samples to azure monitor (#16580)
1 parent 2cbb044 commit b9e3619

10 files changed

Lines changed: 170 additions & 42 deletions

File tree

sdk/monitor/azure-opentelemetry-exporter-azuremonitor/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## 1.0.0b3 (Unreleased)
44

5+
- Add azure servicebus samples and docstrings to samples
6+
([#16580](https://github.com/Azure/azure-sdk-for-python/pull/16580))
57

68
## 1.0.0b2 (2021-01-13)
79

sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/README.md

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ These code samples show common champion scenario operations with the AzureMonito
1212

1313
* Trace: [sample_trace.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_trace.py)
1414
* Client: [sample_client.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_client.py)
15-
* Request: [sample_request.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_request.py)
1615
* Server: [sample_server.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_server.py)
16+
* Azure Service Bus Send: [sample_servicebus_send.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_servicebus_send.py)
17+
* Azure Service Bus Receive: [sample_servicebus_receive.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_servicebus_receive.py)
1718

1819
## Installation
1920

@@ -34,7 +35,7 @@ $ # from this directory
3435
$ python sample_trace.py
3536
```
3637

37-
### Request
38+
### Client
3839

3940
* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable
4041

@@ -54,15 +55,52 @@ $ python sample_request.py
5455

5556
```sh
5657
$ pip install opentelemetry-instrumentation-requests
57-
$ pip install opentelemetry-instrumentation-wsgi
58+
$ pip install opentelemetry-instrumentation-flask
5859
$ # from this directory
5960
$ python sample_server.py
6061
```
6162

6263
* Open http://localhost:8080/
6364

65+
### Azure Service Bus Send
66+
67+
The following sample assumes that you have setup an Azure Service Bus [namespace](https://docs.microsoft.com/azure/service-bus-messaging/service-bus-quickstart-portal).
68+
69+
* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable
70+
* Update `SERVICE_BUS_CONN_STR` environment variable
71+
* Update `SERVICE_BUS_QUEUE_NAME` environment variable
72+
73+
* Run the sample
74+
75+
```sh
76+
$ # azure-servicebus library
77+
$ pip install azure-servicebus
78+
$ # azure sdk core tracing library for opentelemetry
79+
$ pip install azure-core-tracing-opentelemetry
80+
$ # from this directory
81+
$ python sample_servicebus_send.py
82+
```
83+
84+
### Azure Service Bus Receive
85+
86+
The following sample assumes that you have setup an Azure Service Bus [namespace](https://docs.microsoft.com/azure/service-bus-messaging/service-bus-quickstart-portal).
87+
88+
* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable
89+
* Update `SERVICE_BUS_CONN_STR` environment variable
90+
* Update `SERVICE_BUS_QUEUE_NAME` environment variable
91+
92+
* Run the sample
93+
94+
```sh
95+
$ # azure-servicebus library
96+
$ pip install azure-servicebus
97+
$ # azure sdk core tracing library for opentelemetry
98+
$ pip install azure-core-tracing-opentelemetry
99+
$ # from this directory
100+
$ python sample_servicebus_receive.py
101+
```
64102

65103
## Explore the data
66104

67105
After running the applications, data would be available in [Azure](
68-
https://docs.microsoft.com/azure/azure-monitor/app/app-insights-overview#where-do-i-see-my-telemetry)
106+
https://docs.microsoft.com/azure/azure-monitor/app/app-insights-overview#where-do-i-see-my-telemetry)

sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_client.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
3+
"""
4+
An example to show an application instrumented with the Opentelemetry requests instrumentations.
5+
Calls made with the requests library will be automatically tracked and telemetry is exported to
6+
application insights with the AzureMonitorTraceExporter.
7+
"""
38
import os
49
import requests
510
from opentelemetry import trace
@@ -19,6 +24,5 @@
1924
)
2025
trace.get_tracer_provider().add_span_processor(span_processor)
2126

22-
response = requests.get(url="http://127.0.0.1:8080/")
23-
24-
input("Press any key to exit...")
27+
with tracer.start_as_current_span("parent"):
28+
response = requests.get("https://azure.microsoft.com/", timeout=5)

sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_request.py

Lines changed: 0 additions & 29 deletions
This file was deleted.

sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_server.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
# pylint: disable=import-error
44
# pylint: disable=no-member
55
# pylint: disable=no-name-in-module
6+
"""
7+
An example to show an application instrumented with the Opentelemetry flask and requests instrumentations.
8+
Calls to the flask application and with the requests library will be automatically tracked and telemetry
9+
is exported to application insights with the AzureMonitorTraceExporter.
10+
"""
611
import os
712
import requests
813
from opentelemetry import trace
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
Examples to show usage of the azure-core-tracing-opentelemetry
3+
with the servicebus SDK and exporting to Azure monitor backend.
4+
5+
This example traces calls for receiving messages from the servicebus queue.
6+
7+
The telemetry will be collected automatically and sent to Application
8+
Insights via the AzureMonitorTraceExporter
9+
"""
10+
11+
import os
12+
13+
# Declare OpenTelemetry as enabled tracing plugin for Azure SDKs
14+
from azure.core.settings import settings
15+
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan
16+
17+
settings.tracing_implementation = OpenTelemetrySpan
18+
19+
# Regular open telemetry usage from here, see https://github.com/open-telemetry/opentelemetry-python
20+
# for details
21+
from opentelemetry import trace
22+
from opentelemetry.sdk.trace import TracerProvider
23+
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor
24+
25+
trace.set_tracer_provider(TracerProvider())
26+
tracer = trace.get_tracer(__name__)
27+
28+
# azure monitor trace exporter to send telemetry to appinsights
29+
from azure.opentelemetry.exporter.azuremonitor import AzureMonitorTraceExporter
30+
span_processor = BatchExportSpanProcessor(
31+
AzureMonitorTraceExporter(
32+
connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
33+
)
34+
)
35+
trace.get_tracer_provider().add_span_processor(span_processor)
36+
37+
# Example with Servicebus SDKs
38+
from azure.servicebus import ServiceBusClient, ServiceBusMessage
39+
40+
connstr = os.environ['SERVICE_BUS_CONN_STR']
41+
queue_name = os.environ['SERVICE_BUS_QUEUE_NAME']
42+
43+
with tracer.start_as_current_span(name="MyApplication2"):
44+
with ServiceBusClient.from_connection_string(connstr) as client:
45+
with client.get_queue_sender(queue_name) as sender:
46+
# Sending a single message
47+
single_message = ServiceBusMessage("Single message")
48+
sender.send_messages(single_message)
49+
# continually receives new messages until it doesn't receive any new messages for 5 (max_wait_time) seconds.
50+
with client.get_queue_receiver(queue_name=queue_name, max_wait_time=5) as receiver:
51+
# Receive all messages
52+
for msg in receiver:
53+
print("Received: " + str(msg))
54+
receiver.complete_message(msg)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Examples to show usage of the azure-core-tracing-opentelemetry
3+
with the servicebus SDK and exporting to Azure monitor backend.
4+
5+
This example traces calls for sending messages to the servicebus queue.
6+
7+
The telemetry will be collected automatically and sent to Application
8+
Insights via the AzureMonitorTraceExporter
9+
"""
10+
11+
import os
12+
13+
# Declare OpenTelemetry as enabled tracing plugin for Azure SDKs
14+
from azure.core.settings import settings
15+
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan
16+
17+
settings.tracing_implementation = OpenTelemetrySpan
18+
19+
# Regular open telemetry usage from here, see https://github.com/open-telemetry/opentelemetry-python
20+
# for details
21+
from opentelemetry import trace
22+
from opentelemetry.sdk.trace import TracerProvider
23+
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor
24+
25+
trace.set_tracer_provider(TracerProvider())
26+
tracer = trace.get_tracer(__name__)
27+
28+
# azure monitor trace exporter to send telemetry to appinsights
29+
from azure.opentelemetry.exporter.azuremonitor import AzureMonitorTraceExporter
30+
span_processor = BatchExportSpanProcessor(
31+
AzureMonitorTraceExporter(
32+
connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
33+
)
34+
)
35+
trace.get_tracer_provider().add_span_processor(span_processor)
36+
37+
# Example with Servicebus SDKs
38+
from azure.servicebus import ServiceBusClient, ServiceBusMessage
39+
40+
connstr = os.environ['SERVICE_BUS_CONN_STR']
41+
queue_name = os.environ['SERVICE_BUS_QUEUE_NAME']
42+
43+
with tracer.start_as_current_span(name="MyApplication"):
44+
with ServiceBusClient.from_connection_string(connstr) as client:
45+
with client.get_queue_sender(queue_name) as sender:
46+
# Sending a single message
47+
single_message = ServiceBusMessage("Single message")
48+
sender.send_messages(single_message)
49+
50+
# Sending a list of messages
51+
messages = [ServiceBusMessage("First message"), ServiceBusMessage("Second message")]
52+
sender.send_messages(messages)

sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_trace.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
3+
"""
4+
An example to show an application using Opentelemetry tracing api and sdk. Custom dependencies are
5+
tracked via spans and telemetry is exported to application insights with the AzureMonitorTraceExporter.
6+
"""
37
import os
48
from opentelemetry import trace
59
from opentelemetry.sdk.trace import TracerProvider
@@ -19,5 +23,3 @@
1923

2024
with tracer.start_as_current_span("hello"):
2125
print("Hello, World!")
22-
23-
input("Press any key to exit...")

sdk/monitor/azure-opentelemetry-exporter-azuremonitor/setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
install_requires=[
8080
"azure-core<2.0.0,>=1.6.0",
8181
"msrest>=0.6.10",
82-
"opentelemetry-api == 0.16b1",
83-
"opentelemetry-sdk == 0.16b1"
82+
"opentelemetry-api == 0.17b0",
83+
"opentelemetry-sdk == 0.17b0"
8484
],
8585
)

shared_requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ avro<2.0.0,>=1.10.0
155155
opencensus>=0.6.0
156156
opencensus-ext-threading
157157
opencensus-ext-azure>=0.3.1
158-
opentelemetry-api==0.16b1
159-
opentelemetry-sdk==0.16b1
158+
opentelemetry-api==0.17b0
159+
opentelemetry-sdk==0.17b0
160160
#override azure-eventhub-checkpointstoreblob msrest>=0.6.10
161161
#override azure-eventhub-checkpointstoreblob-aio msrest>=0.6.10
162162
#override azure-eventhub-checkpointstoreblob-aio aiohttp<4.0,>=3.0

0 commit comments

Comments
 (0)