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..a5ca5f7b4f8b 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,9 @@ 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) +* 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) @@ -259,7 +262,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 +270,56 @@ $ # 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 +``` + +### 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 +``` + +### 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_chat.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_chat.py new file mode 100644 index 000000000000..0d9e9e565751 --- /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 chat client and thread using +Communication Chat 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) 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..0cb5d31fdf10 --- /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) 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