Skip to content

Commit bc9bb07

Browse files
authored
Create communication samples (Azure#24268)
1 parent 5ebba6e commit bc9bb07

File tree

4 files changed

+203
-1
lines changed

4 files changed

+203
-1
lines changed

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ These code samples show common champion scenario operations with the AzureMonito
1717
* 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)
1818
* 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)
1919
* Azure EventGrid Send Event: [sample_event_grid.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_event_grid.py)
20+
* Azure Communication Chat Create Client/Thread: [sample_comm_chat.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_chat.py)
21+
* Azure Communication Phone Numbers List Purchased Numbers: [sample_comm_phone.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_phone.py)
22+
* Azure Communication SMS Send Message: [sample_comm_sms.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_sms.py)
2023
* 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)
2124
* 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)
2225
* 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)
@@ -259,14 +262,64 @@ The following sample assumes that you have setup an Azure Event Grid [Topic](htt
259262
* Run the sample
260263

261264
```sh
262-
$ # azure-eventhub-checkpointstoreblob library
265+
$ # azure-azure-eventgrid library
263266
$ pip install azure-eventgrid
264267
$ # azure sdk core tracing library for opentelemetry
265268
$ pip install azure-core-tracing-opentelemetry
266269
$ # from this directory
267270
$ python sample_event_grid.py
268271
```
269272

273+
### Azure Communication Chat Create Client/Thread
274+
275+
The following sample assumes that you have setup an Azure Communication Services [resource](https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource).
276+
* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable
277+
278+
* Run the sample
279+
280+
```sh
281+
$ # azure-communication-chat library
282+
$ pip install azure-communication-chat
283+
$ # azure-communication-identity library for authentication
284+
$ pip install azure-communication-identity
285+
$ # azure sdk core tracing library for opentelemetry
286+
$ pip install azure-core-tracing-opentelemetry
287+
$ # from this directory
288+
$ python sample_comm_chat.py
289+
```
290+
291+
### Azure Communication Phone Numbers List Purchased Numbers
292+
293+
The following sample assumes that you have setup an Azure Communication Services [resource](https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource).
294+
* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable
295+
296+
* Run the sample
297+
298+
```sh
299+
$ # azure-communication-phonenumbers library
300+
$ pip install azure-communication-phonenumbers
301+
$ # azure sdk core tracing library for opentelemetry
302+
$ pip install azure-core-tracing-opentelemetry
303+
$ # from this directory
304+
$ python sample_comm_phone.py
305+
```
306+
307+
### Azure Communication SMS Send Message
308+
309+
The following sample assumes that you have setup an Azure Communication Services [resource](https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource).
310+
* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable
311+
312+
* Run the sample
313+
314+
```sh
315+
$ # azure-communication-sms library
316+
$ pip install azure-communication-sms
317+
$ # azure sdk core tracing library for opentelemetry
318+
$ pip install azure-core-tracing-opentelemetry
319+
$ # from this directory
320+
$ python sample_comm_sms.py
321+
```
322+
270323
## Explore the data
271324

272325
After running the applications, data would be available in [Azure](
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
Examples to show usage of the azure-core-tracing-opentelemetry
3+
with the Communication Chat SDK and exporting to Azure monitor backend.
4+
This example traces calls for creating a chat client and thread using
5+
Communication Chat SDK. The telemetry will be collected automatically
6+
and sent to Application 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 Communication Chat SDKs
36+
# Authenticate with Communication Identity SDK
37+
from azure.communication.identity import CommunicationIdentityClient
38+
comm_connection_string = "<connection string of your Communication service>"
39+
identity_client = CommunicationIdentityClient.from_connection_string(comm_connection_string)
40+
41+
# Telemetry will be sent for creating the user and getting the token as well
42+
user = identity_client.create_user()
43+
tokenresponse = identity_client.get_token(user, scopes=["chat"])
44+
token = tokenresponse.token
45+
46+
# Create a Chat Client
47+
from azure.communication.chat import ChatClient, CommunicationTokenCredential
48+
49+
# Your unique Azure Communication service endpoint
50+
endpoint = "https://<RESOURCE_NAME>.communcationservices.azure.com"
51+
with tracer.start_as_current_span(name="CreateChatClient"):
52+
chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))
53+
# Create a Chat Thread
54+
with tracer.start_as_current_span(name="CreateChatThread"):
55+
create_chat_thread_result = chat_client.create_chat_thread("test topic")
56+
chat_thread_client = chat_client.get_chat_thread_client(create_chat_thread_result.chat_thread.id)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Examples to show usage of the azure-core-tracing-opentelemetry
3+
with the Communication Phone SDK and exporting to Azure monitor backend.
4+
This example traces calls for creating a phone client getting phone numbers
5+
using Communication Phone SDK. The telemetry will be collected automatically
6+
and sent to Application 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 Communication Phone SDKs
36+
from azure.communication.phonenumbers import PhoneNumbersClient
37+
38+
# Create a Phone Client
39+
connection_str = "endpoint=ENDPOINT;accessKey=KEY"
40+
phone_numbers_client = PhoneNumbersClient.from_connection_string(connection_str)
41+
42+
with tracer.start_as_current_span(name="PurchasedPhoneNumbers"):
43+
purchased_phone_numbers = phone_numbers_client.list_purchased_phone_numbers()
44+
for acquired_phone_number in purchased_phone_numbers:
45+
print(acquired_phone_number.phone_number)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
Examples to show usage of the azure-core-tracing-opentelemetry
3+
with the Communication SMS SDK and exporting to Azure monitor backend.
4+
This example traces calls for sending an SMS message using Communication
5+
SMS SDK. The telemetry will be collected automatically and sent to
6+
Application 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 Communication SMS SDKs
36+
from azure.communication.sms import SmsClient
37+
38+
# Create a SMS Client
39+
connection_str = "endpoint=ENDPOINT;accessKey=KEY"
40+
sms_client = SmsClient.from_connection_string(connection_str)
41+
42+
with tracer.start_as_current_span(name="SendSMS"):
43+
sms_responses = sms_client.send(
44+
from_="<from-phone-number>",
45+
to="<to-phone-number-1>",
46+
message="Hello World via SMS",
47+
enable_delivery_report=True, # optional property
48+
tag="custom-tag") # optional property

0 commit comments

Comments
 (0)