From 81409f70a5d78c5792481483ccfd91cb02c6fd58 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Fri, 29 Apr 2022 11:36:28 -0700 Subject: [PATCH 1/3] chat --- .../samples/traces/README.md | 21 ++++++- .../samples/traces/sample_comm_chat.py | 56 +++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_chat.py diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/README.md index b61aa395ce50..bcf5816b702d 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/README.md @@ -17,6 +17,7 @@ These code samples show common champion scenario operations with the AzureMonito * 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) * 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) * 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) +* 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) * 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) * 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) * 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,7 +260,7 @@ The following sample assumes that you have setup an Azure Event Grid [Topic](htt * Run the sample ```sh -$ # azure-eventhub-checkpointstoreblob library +$ # azure-azure-eventgrid library $ pip install azure-eventgrid $ # azure sdk core tracing library for opentelemetry $ pip install azure-core-tracing-opentelemetry @@ -267,6 +268,24 @@ $ # from this directory $ python sample_event_grid.py ``` +### Azure Communication Chat Create Client/Thread + +The following sample assumes that you have setup an Azure Communication Services [resource](https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource). +* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable + +* Run the sample + +```sh +$ # azure-communication-chat library +$ pip install azure-communication-chat +$ # azure-communication-identity library for authentication +$ pip install azure-communication-identity +$ # azure sdk core tracing library for opentelemetry +$ pip install azure-core-tracing-opentelemetry +$ # from this directory +$ python sample_comm_chat.py +``` + ## Explore the data After running the applications, data would be available in [Azure]( diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_chat.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_chat.py new file mode 100644 index 000000000000..e8179fc3305a --- /dev/null +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_chat.py @@ -0,0 +1,56 @@ +""" +Examples to show usage of the azure-core-tracing-opentelemetry +with the Communication Chat SDK and exporting to Azure monitor backend. +This example traces calls for creating a database and container using +CosmosDb SDK. 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 BatchSpanProcessor + +trace.set_tracer_provider(TracerProvider()) +tracer = trace.get_tracer(__name__) + +# azure monitor trace exporter to send telemetry to appinsights +from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter +span_processor = BatchSpanProcessor( + AzureMonitorTraceExporter.from_connection_string( + os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] + ) +) +trace.get_tracer_provider().add_span_processor(span_processor) + +# Example with Communication Chat SDKs +# Authenticate with Communication Identity SDK +from azure.communication.identity import CommunicationIdentityClient +comm_connection_string = "" +identity_client = CommunicationIdentityClient.from_connection_string(comm_connection_string) + +# Telemetry will be sent for creating the user and getting the token as well +user = identity_client.create_user() +tokenresponse = identity_client.get_token(user, scopes=["chat"]) +token = tokenresponse.token + +# Create a Chat Client +from azure.communication.chat import ChatClient, CommunicationTokenCredential + +# Your unique Azure Communication service endpoint +endpoint = "https://.communcationservices.azure.com" +with tracer.start_as_current_span(name="CreateChatClient"): + chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) + # Create a Chat Thread + with tracer.start_as_current_span(name="CreateChatThread"): + create_chat_thread_result = chat_client.create_chat_thread("test topic") + chat_thread_client = chat_client.get_chat_thread_client(create_chat_thread_result.chat_thread.id) From 4d60b7a607a29d43729b1e55b1172b9cb7e62f25 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Mon, 2 May 2022 10:56:57 -0700 Subject: [PATCH 2/3] phone --- .../samples/traces/README.md | 17 +++++++ .../samples/traces/sample_comm_chat.py | 6 +-- .../samples/traces/sample_comm_phone.py | 45 +++++++++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_phone.py diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/README.md index bcf5816b702d..128e70902c27 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/README.md @@ -18,6 +18,7 @@ These code samples show common champion scenario operations with the AzureMonito * 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) * 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) * 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) +* 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) * 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) * 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) * 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) @@ -286,6 +287,22 @@ $ # from this directory $ python sample_comm_chat.py ``` +### Azure Communication Phone Numbers List Purchased Numbers + +The following sample assumes that you have setup an Azure Communication Services [resource](https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource). +* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable + +* Run the sample + +```sh +$ # azure-communication-phonenumbers library +$ pip install azure-communication-phonenumbers +$ # azure sdk core tracing library for opentelemetry +$ pip install azure-core-tracing-opentelemetry +$ # from this directory +$ python sample_comm_phone.py +``` + ## Explore the data After running the applications, data would be available in [Azure]( diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_chat.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_chat.py index e8179fc3305a..0d9e9e565751 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_chat.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_chat.py @@ -1,9 +1,9 @@ """ Examples to show usage of the azure-core-tracing-opentelemetry with the Communication Chat SDK and exporting to Azure monitor backend. -This example traces calls for creating a database and container using -CosmosDb SDK. The telemetry will be collected automatically and sent -to Application Insights via the AzureMonitorTraceExporter +This example traces calls for creating a chat client and thread using +Communication Chat SDK. The telemetry will be collected automatically +and sent to Application Insights via the AzureMonitorTraceExporter """ import os diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_phone.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_phone.py new file mode 100644 index 000000000000..d5b98f2f3c7f --- /dev/null +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_phone.py @@ -0,0 +1,45 @@ +""" +Examples to show usage of the azure-core-tracing-opentelemetry +with the Communication Phone SDK and exporting to Azure monitor backend. +This example traces calls for creating a phone client getting phone numbers + using Communication Phone SDK. 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 BatchSpanProcessor + +trace.set_tracer_provider(TracerProvider()) +tracer = trace.get_tracer(__name__) + +# azure monitor trace exporter to send telemetry to appinsights +from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter +span_processor = BatchSpanProcessor( + AzureMonitorTraceExporter.from_connection_string( + os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] + ) +) +trace.get_tracer_provider().add_span_processor(span_processor) + +# Example with Communication Phone SDKs +from azure.communication.phonenumbers import PhoneNumbersClient + +# Create a Phone Client +connection_str = "endpoint=ENDPOINT;accessKey=KEY" +phone_numbers_client = PhoneNumbersClient.from_connection_string(connection_str) + +with tracer.start_as_current_span(name="PurchasedPhoneNumbers"): + purchased_phone_numbers = phone_numbers_client.list_purchased_phone_numbers() + for acquired_phone_number in purchased_phone_numbers: + print(acquired_phone_number.phone_number) From e06291d89a0042dc39f98090f4d1583faaab5df9 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Mon, 2 May 2022 13:00:52 -0700 Subject: [PATCH 3/3] sms --- .../samples/traces/README.md | 17 +++++++ .../samples/traces/sample_comm_phone.py | 2 +- .../samples/traces/sample_comm_sms.py | 48 +++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_sms.py diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/README.md index 128e70902c27..a5ca5f7b4f8b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/README.md @@ -19,6 +19,7 @@ These code samples show common champion scenario operations with the AzureMonito * 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) * 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) * 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) +* 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) * 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) * 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) * 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) @@ -303,6 +304,22 @@ $ # from this directory $ python sample_comm_phone.py ``` +### Azure Communication SMS Send Message + +The following sample assumes that you have setup an Azure Communication Services [resource](https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource). +* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable + +* Run the sample + +```sh +$ # azure-communication-sms library +$ pip install azure-communication-sms +$ # azure sdk core tracing library for opentelemetry +$ pip install azure-core-tracing-opentelemetry +$ # from this directory +$ python sample_comm_sms.py +``` + ## Explore the data After running the applications, data would be available in [Azure]( diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_phone.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_phone.py index d5b98f2f3c7f..0cb5d31fdf10 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_phone.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_phone.py @@ -2,7 +2,7 @@ Examples to show usage of the azure-core-tracing-opentelemetry with the Communication Phone SDK and exporting to Azure monitor backend. This example traces calls for creating a phone client getting phone numbers - using Communication Phone SDK. The telemetry will be collected automatically +using Communication Phone SDK. The telemetry will be collected automatically and sent to Application Insights via the AzureMonitorTraceExporter """ diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_sms.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_sms.py new file mode 100644 index 000000000000..755298c9ee0c --- /dev/null +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_sms.py @@ -0,0 +1,48 @@ +""" +Examples to show usage of the azure-core-tracing-opentelemetry +with the Communication SMS SDK and exporting to Azure monitor backend. +This example traces calls for sending an SMS message using Communication +SMS SDK. 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 BatchSpanProcessor + +trace.set_tracer_provider(TracerProvider()) +tracer = trace.get_tracer(__name__) + +# azure monitor trace exporter to send telemetry to appinsights +from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter +span_processor = BatchSpanProcessor( + AzureMonitorTraceExporter.from_connection_string( + os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] + ) +) +trace.get_tracer_provider().add_span_processor(span_processor) + +# Example with Communication SMS SDKs +from azure.communication.sms import SmsClient + +# Create a SMS Client +connection_str = "endpoint=ENDPOINT;accessKey=KEY" +sms_client = SmsClient.from_connection_string(connection_str) + +with tracer.start_as_current_span(name="SendSMS"): + sms_responses = sms_client.send( + from_="", + to="", + message="Hello World via SMS", + enable_delivery_report=True, # optional property + tag="custom-tag") # optional property