Skip to content

Commit ec76752

Browse files
authored
Create eventhub samples (Azure#24133)
1 parent 7a93573 commit ec76752

File tree

5 files changed

+176
-16
lines changed

5 files changed

+176
-16
lines changed

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class PeriodicTask(threading.Thread):
5858
"""
5959

6060
def __init__(self, interval, function, *args, **kwargs):
61-
super().__init__(name=kwargs.get('name'))
61+
super().__init__(name=kwargs.pop('name', None))
6262
self.interval = interval
6363
self.function = function
6464
self.args = args or []

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ These code samples show common champion scenario operations with the AzureMonito
1414
* Azure Service Bus Receive: [sample_servicebus_receive.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_servicebus_receive.py)
1515
* Azure Storage Blob Create Container: [sample_storage_blob.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_storage_blob.py)
1616
* Azure CosmosDb Create Db/Container: [sample_cosmos.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_cosmos.py)
17+
* Azure EventHub Send EventData: [sample_event_hub.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_event_hub.py)
18+
* Azure EventHub Blob Storage Checkpoint Store: [sample_blob_checkpoint.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_blob_checkpoint.py)
1719
* Client: [sample_client.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_client.py)
1820
* Event: [sample_span_event.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_span_event.py)
1921
* Jaeger: [sample_jaeger.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_jaeger.py)
@@ -199,14 +201,53 @@ The following sample assumes that you have setup Azure CosmosDb [account](https:
199201
* Run the sample
200202

201203
```sh
202-
$ # azure-storage-blob library
204+
$ # azure-cosmos library
203205
$ pip install azure-cosmos
204206
$ # azure sdk core tracing library for opentelemetry
205207
$ pip install azure-core-tracing-opentelemetry
206208
$ # from this directory
207209
$ python sample_cosmos.py
208210
```
209211

212+
### Azure EventHub Send EventData
213+
214+
The following sample assumes that you have setup an Azure EventHubs namespace and [EventHub](https://docs.microsoft.com/azure/event-hubs/event-hubs-create).
215+
216+
* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable
217+
* Update `EVENT_HUB_CONN_STR` environment variable
218+
* Update `EVENT_HUB_NAME` environment variable
219+
220+
* Run the sample
221+
222+
```sh
223+
$ # azure-eventhub library
224+
$ pip install azure-eventhub
225+
$ # azure sdk core tracing library for opentelemetry
226+
$ pip install azure-core-tracing-opentelemetry
227+
$ # from this directory
228+
$ python sample_event_hub.py
229+
```
230+
231+
### Azure EventHub Blob Storage Checkpoint Store
232+
233+
The following sample assumes that you have setup an Azure EventHubs namespace, [EventHub](https://docs.microsoft.com/azure/event-hubs/event-hubs-create) and Azure Blob [storage](https://docs.microsoft.com/azure/storage/blobs/storage-quickstart-blobs-portal).
234+
235+
* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable
236+
* Update `EVENT_HUB_CONN_STR` environment variable
237+
* Update `EVENT_HUB_NAME` environment variable
238+
* Update `AZURE_STORAGE_CONN_STR` environment variable
239+
240+
* Run the sample
241+
242+
```sh
243+
$ # azure-eventhub-checkpointstoreblob library
244+
$ pip install azure-eventhub-checkpointstoreblob
245+
$ # azure sdk core tracing library for opentelemetry
246+
$ pip install azure-core-tracing-opentelemetry
247+
$ # from this directory
248+
$ python sample_blob_checkpoint.py
249+
```
250+
210251
## Explore the data
211252

212253
After running the applications, data would be available in [Azure](
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
Examples to show usage of the azure-core-tracing-opentelemetry
3+
with the eventhub checkpoint storage blob SDK and exporting to
4+
Azure monitor backend. This example traces calls for sending
5+
checkpoints via the checkpoint storage blob sdk. The telemetry
6+
will be collected automatically and sent to Application Insights
7+
via the AzureMonitorTraceExporter
8+
"""
9+
10+
import os
11+
12+
# Declare OpenTelemetry as enabled tracing plugin for Azure SDKs
13+
from azure.core.settings import settings
14+
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan
15+
16+
settings.tracing_implementation = OpenTelemetrySpan
17+
18+
# Regular open telemetry usage from here, see https://github.com/open-telemetry/opentelemetry-python
19+
# for details
20+
from opentelemetry import trace
21+
from opentelemetry.sdk.trace import TracerProvider
22+
from opentelemetry.sdk.trace.export import BatchSpanProcessor
23+
24+
trace.set_tracer_provider(TracerProvider())
25+
tracer = trace.get_tracer(__name__)
26+
27+
# azure monitor trace exporter to send telemetry to appinsights
28+
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
29+
span_processor = BatchSpanProcessor(
30+
AzureMonitorTraceExporter.from_connection_string(
31+
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
32+
)
33+
)
34+
trace.get_tracer_provider().add_span_processor(span_processor)
35+
36+
# Example with EventHub SDKs
37+
from azure.eventhub import EventHubConsumerClient
38+
from azure.eventhub.extensions.checkpointstoreblob import BlobCheckpointStore
39+
40+
CONNECTION_STR = os.environ["EVENT_HUB_CONN_STR"]
41+
EVENTHUB_NAME = os.environ['EVENT_HUB_NAME']
42+
STORAGE_CONNECTION_STR = os.environ["AZURE_STORAGE_CONN_STR"]
43+
BLOB_CONTAINER_NAME = "your-blob-container-name" # Please make sure the blob container resource exists.
44+
45+
def on_event(partition_context, event):
46+
# Put your code here.
47+
# Avoid time-consuming operations.
48+
print(event)
49+
partition_context.update_checkpoint(event)
50+
51+
checkpoint_store = BlobCheckpointStore.from_connection_string(
52+
STORAGE_CONNECTION_STR,
53+
container_name=BLOB_CONTAINER_NAME,
54+
)
55+
client = EventHubConsumerClient.from_connection_string(
56+
CONNECTION_STR,
57+
consumer_group='$Default',
58+
eventhub_name=EVENTHUB_NAME,
59+
checkpoint_store=checkpoint_store
60+
)
61+
62+
with tracer.start_as_current_span(name="MyEventHub"):
63+
try:
64+
client.receive(on_event)
65+
except KeyboardInterrupt:
66+
client.close()

sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_cosmos.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
22
Examples to show usage of the azure-core-tracing-opentelemetry
3-
with the storage SDK and exporting to Azure monitor backend.
4-
This example traces calls for creating a container using storage SDK.
5-
The telemetry will be collected automatically and sent to Application
6-
Insights via the AzureMonitorTraceExporter
3+
with the CosmosDb SDK and exporting to Azure monitor backend.
4+
This example traces calls for creating a database and container using
5+
CosmosDb SDK. The telemetry will be collected automatically and sent
6+
to Application Insights via the AzureMonitorTraceExporter
77
"""
88

99
import os
@@ -40,15 +40,18 @@
4040
client = CosmosClient(url, key)
4141

4242
database_name = "testDatabase"
43-
try:
44-
database = client.create_database(id=database_name) # Call will be traced
45-
except exceptions.CosmosResourceExistsError:
46-
database = client.get_database_client(database=database_name)
43+
44+
with tracer.start_as_current_span(name="CreateDatabase"):
45+
try:
46+
database = client.create_database(id=database_name) # Call will be traced
47+
except exceptions.CosmosResourceExistsError:
48+
database = client.get_database_client(database=database_name)
4749

4850
container_name = "products"
49-
try:
50-
container = database.create_container(
51-
id=container_name, partition_key=PartitionKey(path="/productName") # Call will be traced
52-
)
53-
except exceptions.CosmosResourceExistsError:
54-
container = database.get_container_client(container_name)
51+
with tracer.start_as_current_span(name="CreateContainer"):
52+
try:
53+
container = database.create_container(
54+
id=container_name, partition_key=PartitionKey(path="/productName") # Call will be traced
55+
)
56+
except exceptions.CosmosResourceExistsError:
57+
container = database.get_container_client(container_name)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Examples to show usage of the azure-core-tracing-opentelemetry
3+
with the event hub SDK and exporting to Azure monitor backend.
4+
This example traces calls for sending event data using event hub SDK.
5+
The telemetry will be collected automatically and sent to Application
6+
Insights via the AzureMonitorTraceExporter
7+
"""
8+
9+
import os
10+
11+
# Declare OpenTelemetry as enabled tracing plugin for Azure SDKs
12+
from azure.core.settings import settings
13+
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan
14+
15+
settings.tracing_implementation = OpenTelemetrySpan
16+
17+
# Regular open telemetry usage from here, see https://github.com/open-telemetry/opentelemetry-python
18+
# for details
19+
from opentelemetry import trace
20+
from opentelemetry.sdk.trace import TracerProvider
21+
from opentelemetry.sdk.trace.export import BatchSpanProcessor
22+
23+
trace.set_tracer_provider(TracerProvider())
24+
tracer = trace.get_tracer(__name__)
25+
26+
# azure monitor trace exporter to send telemetry to appinsights
27+
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
28+
span_processor = BatchSpanProcessor(
29+
AzureMonitorTraceExporter.from_connection_string(
30+
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
31+
)
32+
)
33+
trace.get_tracer_provider().add_span_processor(span_processor)
34+
35+
# Example with EventHub SDKs
36+
from azure.eventhub import EventHubProducerClient, EventData
37+
38+
CONNECTION_STR = os.environ['EVENT_HUB_CONN_STR']
39+
EVENTHUB_NAME = os.environ['EVENT_HUB_NAME']
40+
41+
producer = EventHubProducerClient.from_connection_string(
42+
conn_str=CONNECTION_STR,
43+
eventhub_name=EVENTHUB_NAME
44+
)
45+
46+
with tracer.start_as_current_span(name="MyEventHub"):
47+
with producer:
48+
event_data_batch = producer.create_batch()
49+
event_data_batch.add(EventData('Single message'))
50+
producer.send_batch(event_data_batch)

0 commit comments

Comments
 (0)