Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 1.0.0b3 (Unreleased)

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

## 1.0.0b2 (2021-01-13)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ These code samples show common champion scenario operations with the AzureMonito

* 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)
* 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)
* 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)
* 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)
* 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)
* 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)

## Installation

Expand All @@ -34,7 +35,7 @@ $ # from this directory
$ python sample_trace.py
```

### Request
### Client

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

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

```sh
$ pip install opentelemetry-instrumentation-requests
$ pip install opentelemetry-instrumentation-wsgi
$ pip install opentelemetry-instrumentation-flask
$ # from this directory
$ python sample_server.py
```

* Open http://localhost:8080/

### Azure Service Bus Send

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).

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable
* Update `SERVICE_BUS_CONN_STR` environment variable
* Update `SERVICE_BUS_QUEUE_NAME` environment variable

* Run the sample

```sh
$ # azure-servicebus library
$ pip install azure-servicebus
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_servicebus_send.py
```

### Azure Service Bus Receive

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).

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable
* Update `SERVICE_BUS_CONN_STR` environment variable
* Update `SERVICE_BUS_QUEUE_NAME` environment variable

* Run the sample

```sh
$ # azure-servicebus library
$ pip install azure-servicebus
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_servicebus_receive.py
```

## Explore the data

After running the applications, data would be available in [Azure](
https://docs.microsoft.com/azure/azure-monitor/app/app-insights-overview#where-do-i-see-my-telemetry)
https://docs.microsoft.com/azure/azure-monitor/app/app-insights-overview#where-do-i-see-my-telemetry)
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""
An example to show an application instrumented with the Opentelemetry requests instrumentations.
Calls made with the requests library will be automatically tracked and telemetry is exported to
application insights with the AzureMonitorTraceExporter.
"""
import os
import requests
from opentelemetry import trace
Expand All @@ -19,6 +24,5 @@
)
trace.get_tracer_provider().add_span_processor(span_processor)

response = requests.get(url="http://127.0.0.1:8080/")

input("Press any key to exit...")
with tracer.start_as_current_span("parent"):
response = requests.get("https://azure.microsoft.com/", timeout=5)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
# pylint: disable=import-error
# pylint: disable=no-member
# pylint: disable=no-name-in-module
"""
An example to show an application instrumented with the Opentelemetry flask and requests instrumentations.
Calls to the flask application and with the requests library will be automatically tracked and telemetry
is exported to application insights with the AzureMonitorTraceExporter.
"""
import os
import requests
from opentelemetry import trace
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
Examples to show usage of the azure-core-tracing-opentelemetry
with the servicebus SDK and exporting to Azure monitor backend.

This example traces calls for receiving messages from the servicebus queue.

The telemetry will be collected automatically and sent to Application
Insights via the AzureMonitorTraceExporter
"""

import os

# Declare OpenTelemetry as enabled tracing plugin for Azure SDKs
from azure.core.settings import settings
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan

settings.tracing_implementation = OpenTelemetrySpan

# Regular open telemetry usage from here, see https://github.com/open-telemetry/opentelemetry-python
# for details
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)

# azure monitor trace exporter to send telemetry to appinsights
from azure.opentelemetry.exporter.azuremonitor import AzureMonitorTraceExporter
span_processor = BatchExportSpanProcessor(
AzureMonitorTraceExporter(
connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
)
)
trace.get_tracer_provider().add_span_processor(span_processor)

# Example with Servicebus SDKs
from azure.servicebus import ServiceBusClient, ServiceBusMessage

connstr = os.environ['SERVICE_BUS_CONN_STR']
queue_name = os.environ['SERVICE_BUS_QUEUE_NAME']

with tracer.start_as_current_span(name="MyApplication2"):
with ServiceBusClient.from_connection_string(connstr) as client:
with client.get_queue_sender(queue_name) as sender:
# Sending a single message
single_message = ServiceBusMessage("Single message")
sender.send_messages(single_message)
# continually receives new messages until it doesn't receive any new messages for 5 (max_wait_time) seconds.
with client.get_queue_receiver(queue_name=queue_name, max_wait_time=5) as receiver:
# Receive all messages
for msg in receiver:
print("Received: " + str(msg))
receiver.complete_message(msg)
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
Examples to show usage of the azure-core-tracing-opentelemetry
with the servicebus SDK and exporting to Azure monitor backend.

This example traces calls for sending messages to the servicebus queue.

The telemetry will be collected automatically and sent to Application
Insights via the AzureMonitorTraceExporter
"""

import os

# Declare OpenTelemetry as enabled tracing plugin for Azure SDKs
from azure.core.settings import settings
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan

settings.tracing_implementation = OpenTelemetrySpan

# Regular open telemetry usage from here, see https://github.com/open-telemetry/opentelemetry-python
# for details
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)

# azure monitor trace exporter to send telemetry to appinsights
from azure.opentelemetry.exporter.azuremonitor import AzureMonitorTraceExporter
span_processor = BatchExportSpanProcessor(
AzureMonitorTraceExporter(
connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
)
)
trace.get_tracer_provider().add_span_processor(span_processor)

# Example with Servicebus SDKs
from azure.servicebus import ServiceBusClient, ServiceBusMessage

connstr = os.environ['SERVICE_BUS_CONN_STR']
queue_name = os.environ['SERVICE_BUS_QUEUE_NAME']

with tracer.start_as_current_span(name="MyApplication"):
with ServiceBusClient.from_connection_string(connstr) as client:
with client.get_queue_sender(queue_name) as sender:
# Sending a single message
single_message = ServiceBusMessage("Single message")
sender.send_messages(single_message)

# Sending a list of messages
messages = [ServiceBusMessage("First message"), ServiceBusMessage("Second message")]
sender.send_messages(messages)
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""
An example to show an application using Opentelemetry tracing api and sdk. Custom dependencies are
tracked via spans and telemetry is exported to application insights with the AzureMonitorTraceExporter.
"""
import os
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
Expand All @@ -19,5 +23,3 @@

with tracer.start_as_current_span("hello"):
print("Hello, World!")

input("Press any key to exit...")
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
install_requires=[
"azure-core<2.0.0,>=1.6.0",
"msrest>=0.6.10",
"opentelemetry-api == 0.16b1",
"opentelemetry-sdk == 0.16b1"
"opentelemetry-api == 0.17b0",
"opentelemetry-sdk == 0.17b0"
],
)
4 changes: 2 additions & 2 deletions shared_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ avro<2.0.0,>=1.10.0
opencensus>=0.6.0
opencensus-ext-threading
opencensus-ext-azure>=0.3.1
opentelemetry-api==0.16b1
opentelemetry-sdk==0.16b1
opentelemetry-api==0.17b0
opentelemetry-sdk==0.17b0
#override azure-eventhub-checkpointstoreblob msrest>=0.6.10
#override azure-eventhub-checkpointstoreblob-aio msrest>=0.6.10
#override azure-eventhub-checkpointstoreblob-aio aiohttp<4.0,>=3.0
Expand Down