diff --git a/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/CHANGELOG.md b/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/CHANGELOG.md index 92e1009e486d..0e876893af6c 100644 --- a/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/CHANGELOG.md +++ b/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/CHANGELOG.md @@ -2,6 +2,8 @@ ## 1.0.0b3 (Unreleased) +- Add azure servicebus samples and docstrings to samples + ([#16580](https://github.com/Azure/azure-sdk-for-python/pull/16580)) ## 1.0.0b2 (2021-01-13) diff --git a/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/README.md b/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/README.md index da54edb06733..e41d83fa4d36 100644 --- a/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/README.md +++ b/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/README.md @@ -12,8 +12,9 @@ These code samples show common champion scenario operations with the AzureMonito * Trace: [sample_trace.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_trace.py) * Client: [sample_client.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_client.py) -* Request: [sample_request.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_request.py) * Server: [sample_server.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_server.py) +* Azure Service Bus Send: [sample_servicebus_send.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_servicebus_send.py) +* Azure Service Bus Receive: [sample_servicebus_receive.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_servicebus_receive.py) ## Installation @@ -34,7 +35,7 @@ $ # from this directory $ python sample_trace.py ``` -### Request +### Client * Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable @@ -54,15 +55,52 @@ $ python sample_request.py ```sh $ pip install opentelemetry-instrumentation-requests -$ pip install opentelemetry-instrumentation-wsgi +$ pip install opentelemetry-instrumentation-flask $ # from this directory $ python sample_server.py ``` * Open http://localhost:8080/ +### Azure Service Bus Send + +The following sample assumes that you have setup an Azure Service Bus [namespace](https://docs.microsoft.com/azure/service-bus-messaging/service-bus-quickstart-portal). + +* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable +* Update `SERVICE_BUS_CONN_STR` environment variable +* Update `SERVICE_BUS_QUEUE_NAME` environment variable + +* Run the sample + +```sh +$ # azure-servicebus library +$ pip install azure-servicebus +$ # azure sdk core tracing library for opentelemetry +$ pip install azure-core-tracing-opentelemetry +$ # from this directory +$ python sample_servicebus_send.py +``` + +### Azure Service Bus Receive + +The following sample assumes that you have setup an Azure Service Bus [namespace](https://docs.microsoft.com/azure/service-bus-messaging/service-bus-quickstart-portal). + +* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable +* Update `SERVICE_BUS_CONN_STR` environment variable +* Update `SERVICE_BUS_QUEUE_NAME` environment variable + +* Run the sample + +```sh +$ # azure-servicebus library +$ pip install azure-servicebus +$ # azure sdk core tracing library for opentelemetry +$ pip install azure-core-tracing-opentelemetry +$ # from this directory +$ python sample_servicebus_receive.py +``` ## Explore the data After running the applications, data would be available in [Azure]( -https://docs.microsoft.com/azure/azure-monitor/app/app-insights-overview#where-do-i-see-my-telemetry) \ No newline at end of file +https://docs.microsoft.com/azure/azure-monitor/app/app-insights-overview#where-do-i-see-my-telemetry) diff --git a/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_client.py b/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_client.py index 5484380f0159..b8a4a74b51b9 100644 --- a/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_client.py +++ b/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_client.py @@ -1,5 +1,10 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +""" +An example to show an application instrumented with the Opentelemetry requests instrumentations. +Calls made with the requests library will be automatically tracked and telemetry is exported to +application insights with the AzureMonitorTraceExporter. +""" import os import requests from opentelemetry import trace @@ -19,6 +24,5 @@ ) trace.get_tracer_provider().add_span_processor(span_processor) -response = requests.get(url="http://127.0.0.1:8080/") - -input("Press any key to exit...") +with tracer.start_as_current_span("parent"): + response = requests.get("https://azure.microsoft.com/", timeout=5) diff --git a/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_request.py b/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_request.py deleted file mode 100644 index c7a0f4ce3e75..000000000000 --- a/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_request.py +++ /dev/null @@ -1,29 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -# pylint: disable=import-error -# pylint: disable=no-member -# pylint: disable=no-name-in-module -import os -import requests -from opentelemetry import trace -from opentelemetry.instrumentation.requests import RequestsInstrumentor -from opentelemetry.sdk.trace import TracerProvider -from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor - -from azure.opentelemetry.exporter.azuremonitor import AzureMonitorTraceExporter - -trace.set_tracer_provider(TracerProvider()) - -RequestsInstrumentor().instrument() -span_processor = SimpleExportSpanProcessor( - AzureMonitorTraceExporter( - connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] - ) -) -trace.get_tracer_provider().add_span_processor(span_processor) -tracer = trace.get_tracer(__name__) - -with tracer.start_as_current_span("parent"): - response = requests.get("https://azure.microsoft.com/", timeout=5) - -input("Press any key to exit...") diff --git a/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_server.py b/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_server.py index 807a25f81481..c8f09f137409 100644 --- a/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_server.py +++ b/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_server.py @@ -3,6 +3,11 @@ # pylint: disable=import-error # pylint: disable=no-member # pylint: disable=no-name-in-module +""" +An example to show an application instrumented with the Opentelemetry flask and requests instrumentations. +Calls to the flask application and with the requests library will be automatically tracked and telemetry +is exported to application insights with the AzureMonitorTraceExporter. +""" import os import requests from opentelemetry import trace diff --git a/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_servicebus_receive.py b/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_servicebus_receive.py new file mode 100644 index 000000000000..875ef9ada331 --- /dev/null +++ b/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_servicebus_receive.py @@ -0,0 +1,54 @@ +""" +Examples to show usage of the azure-core-tracing-opentelemetry +with the servicebus SDK and exporting to Azure monitor backend. + +This example traces calls for receiving messages from the servicebus queue. + +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 BatchExportSpanProcessor + +trace.set_tracer_provider(TracerProvider()) +tracer = trace.get_tracer(__name__) + +# azure monitor trace exporter to send telemetry to appinsights +from azure.opentelemetry.exporter.azuremonitor import AzureMonitorTraceExporter +span_processor = BatchExportSpanProcessor( + AzureMonitorTraceExporter( + connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] + ) +) +trace.get_tracer_provider().add_span_processor(span_processor) + +# Example with Servicebus SDKs +from azure.servicebus import ServiceBusClient, ServiceBusMessage + +connstr = os.environ['SERVICE_BUS_CONN_STR'] +queue_name = os.environ['SERVICE_BUS_QUEUE_NAME'] + +with tracer.start_as_current_span(name="MyApplication2"): + with ServiceBusClient.from_connection_string(connstr) as client: + 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=queue_name, max_wait_time=5) as receiver: + # Receive all messages + for msg in receiver: + print("Received: " + str(msg)) + receiver.complete_message(msg) diff --git a/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_servicebus_send.py b/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_servicebus_send.py new file mode 100644 index 000000000000..443ce70bb8ed --- /dev/null +++ b/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_servicebus_send.py @@ -0,0 +1,52 @@ +""" +Examples to show usage of the azure-core-tracing-opentelemetry +with the servicebus SDK and exporting to Azure monitor backend. + +This example traces calls for sending messages to the servicebus queue. + +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 BatchExportSpanProcessor + +trace.set_tracer_provider(TracerProvider()) +tracer = trace.get_tracer(__name__) + +# azure monitor trace exporter to send telemetry to appinsights +from azure.opentelemetry.exporter.azuremonitor import AzureMonitorTraceExporter +span_processor = BatchExportSpanProcessor( + AzureMonitorTraceExporter( + connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] + ) +) +trace.get_tracer_provider().add_span_processor(span_processor) + +# Example with Servicebus SDKs +from azure.servicebus import ServiceBusClient, ServiceBusMessage + +connstr = os.environ['SERVICE_BUS_CONN_STR'] +queue_name = os.environ['SERVICE_BUS_QUEUE_NAME'] + +with tracer.start_as_current_span(name="MyApplication"): + with ServiceBusClient.from_connection_string(connstr) as client: + with client.get_queue_sender(queue_name) as sender: + # Sending a single message + single_message = ServiceBusMessage("Single message") + sender.send_messages(single_message) + + # Sending a list of messages + messages = [ServiceBusMessage("First message"), ServiceBusMessage("Second message")] + sender.send_messages(messages) diff --git a/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_trace.py b/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_trace.py index 95d5e19de1a2..ed9d6737b30d 100644 --- a/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_trace.py +++ b/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/samples/traces/sample_trace.py @@ -1,5 +1,9 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +""" +An example to show an application using Opentelemetry tracing api and sdk. Custom dependencies are +tracked via spans and telemetry is exported to application insights with the AzureMonitorTraceExporter. +""" import os from opentelemetry import trace from opentelemetry.sdk.trace import TracerProvider @@ -19,5 +23,3 @@ with tracer.start_as_current_span("hello"): print("Hello, World!") - -input("Press any key to exit...") diff --git a/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/setup.py b/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/setup.py index 6fc40e1108a9..19a4318d4c72 100644 --- a/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/setup.py +++ b/sdk/monitor/azure-opentelemetry-exporter-azuremonitor/setup.py @@ -79,7 +79,7 @@ install_requires=[ "azure-core<2.0.0,>=1.6.0", "msrest>=0.6.10", - "opentelemetry-api == 0.16b1", - "opentelemetry-sdk == 0.16b1" + "opentelemetry-api == 0.17b0", + "opentelemetry-sdk == 0.17b0" ], ) diff --git a/shared_requirements.txt b/shared_requirements.txt index 216f23dd64b2..5966dfc7bfc8 100644 --- a/shared_requirements.txt +++ b/shared_requirements.txt @@ -155,8 +155,8 @@ avro<2.0.0,>=1.10.0 opencensus>=0.6.0 opencensus-ext-threading opencensus-ext-azure>=0.3.1 -opentelemetry-api==0.16b1 -opentelemetry-sdk==0.16b1 +opentelemetry-api==0.17b0 +opentelemetry-sdk==0.17b0 #override azure-eventhub-checkpointstoreblob msrest>=0.6.10 #override azure-eventhub-checkpointstoreblob-aio msrest>=0.6.10 #override azure-eventhub-checkpointstoreblob-aio aiohttp<4.0,>=3.0