Skip to content

Commit

Permalink
Add and fix samples for OT (Azure#17876)
Browse files Browse the repository at this point in the history
* Add  and fix samples for OT

* Update sample_receive_sb.py

* Update README.md

* Update README.md
  • Loading branch information
rakshith91 authored and benbp committed Apr 19, 2021
1 parent aeee0f5 commit 2e2638d
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 4 deletions.
9 changes: 8 additions & 1 deletion sdk/core/azure-core-tracing-opentelemetry/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ These code samples show using the tracing opentelemetry integration with few ser
* Trace Storage Calls to create a container: [sample_storage.py][python-sample-storage]
* Trace Eventgrid calls to publish an event : [sample_eventgrid.py][python-sample-eventgrid]
* Trace Servicebus calls to send messages to queue : [sample_servicebus.py][python-sample-servicebus]
* Trace Servicebus calls to receive messages from a queue : [sample_receive_sb.py][python-sample-servicebus-receive]
* Trace calls to send data to Eventhub : [sample_eventhubs.py][python-sample-eventhub-send]
* Trace calls to receive data from EventHub : [sample_receive_eh.py][python-sample-eventhub-receive]

It is assumed that the relevant SDKs are installed along with this extension. Below are the relevant packages which can be found in PyPI.

- [azure-storage-blob](https://pypi.org/project/azure-storage-blob/) v12 or greater.
- [azure-servicebus](https://pypi.org/project/azure-servicebus) v7 or greater.
- [azure-eventgrid](https://pypi.org/project/azure-eventgrid) v2.0.0b1 or greater.
- [azure-eventgrid](https://pypi.org/project/azure-eventgrid) v4 or greater.
- [azure-eventhub](https://pypi.org/project/azure-eventhub/) v5 or greater
- [opentelemtry-sdk](https://pypi.org/project/opentelemetry-sdk/)

[python-sample-storage]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core-tracing-opentelemetry/samples/sample_storage.py
[python-sample-eventgrid]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core-tracing-opentelemetry/samples/sample_eventgrid.py
[python-sample-servicebus]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core-tracing-opentelemetry/samples/sample_servicebus.py
[python-sample-servicebus-receive]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core-tracing-opentelemetry/samples/sample_receive_sb.py
[python-sample-eventhub-send]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core-tracing-opentelemetry/samples/sample_eventhubs.py
[python-sample-eventhub-receive]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core-tracing-opentelemetry/samples/sample_receive_eh.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@

# Example with Eventgrid SDKs
import os
from azure.eventgrid import EventGridPublisherClient, CloudEvent
from azure.core.messaging import CloudEvent
from azure.eventgrid import EventGridPublisherClient
from azure.core.credentials import AzureKeyCredential

hostname = os.environ['CLOUD_TOPIC_HOSTNAME']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
Examples to show usage of the azure-core-tracing-opentelemetry
with the Eventhub SDK.
This example traces calls for senda batch to eventhub.
An alternative path to export using AzureMonitor is also mentioned in the sample. Please take
a look at the commented code.
"""

# 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

# In the below example, we use a simple console exporter, uncomment these lines to use
# the Azure Monitor Exporter. It can be installed from https://pypi.org/project/opentelemetry-azure-monitor/
# Example of Azure Monitor exporter, but you can use anything OpenTelemetry supports
# from azure_monitor import AzureMonitorSpanExporter
# exporter = AzureMonitorSpanExporter(
# instrumentation_key="uuid of the instrumentation key (see your Azure Monitor account)"
# )

# 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 ConsoleSpanExporter
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

# Simple console exporter
exporter = ConsoleSpanExporter()

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
trace.get_tracer_provider().add_span_processor(
SimpleSpanProcessor(exporter)
)

from azure.eventhub import EventHubProducerClient, EventData
import os

FULLY_QUALIFIED_NAMESPACE = os.environ['EVENT_HUB_HOSTNAME']
EVENTHUB_NAME = os.environ['EVENT_HUB_NAME']

credential = os.environ['EVENTHUB_CONN_STR']

def on_event(context, event):
print(context.partition_id, ":", event)

with tracer.start_as_current_span(name="MyApplication"):
producer_client = EventHubProducerClient.from_connection_string(
conn_str=credential,
fully_qualified_namespace=FULLY_QUALIFIED_NAMESPACE,
eventhub_name=EVENTHUB_NAME,
logging_enable=True
)
with producer_client:
event_data_batch = producer_client.create_batch()
event_data_batch.add(EventData('Single message'))
producer_client.send_batch(event_data_batch)
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""
Examples to show usage of the azure-core-tracing-opentelemetry
with the Eventhub SDK.
This example traces calls for senda batch to eventhub.
An alternative path to export using AzureMonitor is also mentioned in the sample. Please take
a look at the commented code.
"""

# 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

# In the below example, we use a simple console exporter, uncomment these lines to use
# the Azure Monitor Exporter. It can be installed from https://pypi.org/project/opentelemetry-azure-monitor/
# Example of Azure Monitor exporter, but you can use anything OpenTelemetry supports
# from azure_monitor import AzureMonitorSpanExporter
# exporter = AzureMonitorSpanExporter(
# instrumentation_key="uuid of the instrumentation key (see your Azure Monitor account)"
# )

# 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 ConsoleSpanExporter
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

# Simple console exporter
exporter = ConsoleSpanExporter()

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
trace.get_tracer_provider().add_span_processor(
SimpleSpanProcessor(exporter)
)

from azure.eventhub import EventHubProducerClient, EventData, EventHubConsumerClient
import os

FULLY_QUALIFIED_NAMESPACE = os.environ['EVENT_HUB_HOSTNAME']
EVENTHUB_NAME = os.environ['EVENT_HUB_NAME']

credential = os.environ['EVENTHUB_CONN_STR']

def on_event(partition_context, event):
# Put your code here.
# If the operation is i/o intensive, multi-thread will have better performance.
print("Received event from partition: {}.".format(partition_context.partition_id))

def on_partition_initialize(partition_context):
# Put your code here.
print("Partition: {} has been initialized.".format(partition_context.partition_id))


def on_partition_close(partition_context, reason):
# Put your code here.
print("Partition: {} has been closed, reason for closing: {}.".format(
partition_context.partition_id,
reason
))


def on_error(partition_context, error):
# Put your code here. partition_context can be None in the on_error callback.
if partition_context:
print("An exception: {} occurred during receiving from Partition: {}.".format(
partition_context.partition_id,
error
))
else:
print("An exception: {} occurred during the load balance process.".format(error))

with tracer.start_as_current_span(name="MyApplication"):
consumer_client = EventHubConsumerClient.from_connection_string(
conn_str=credential,
consumer_group='$Default',
eventhub_name=EVENTHUB_NAME,
)

try:
with consumer_client:
consumer_client.receive(
on_event=on_event,
on_partition_initialize=on_partition_initialize,
on_partition_close=on_partition_close,
on_error=on_error,
starting_position="-1", # "-1" is from the beginning of the partition.
)
except KeyboardInterrupt:
print('Stopped receiving.')

Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@

with tracer.start_as_current_span(name="MyApplication2"):
with ServiceBusClient.from_connection_string(connstr) as client:
with client.get_queue_sender("new_queue") as sender:
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="new_queue", max_wait_time=5) as receiver:
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))
Expand Down

0 comments on commit 2e2638d

Please sign in to comment.