From 8f28e2af65868850f16fcf70b2dfeb118484451c Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Mon, 13 Sep 2021 10:37:18 -0700 Subject: [PATCH] [Monitor exporter] Add OTLP and dual exporter scenario to samples (#20634) * rpc * samples --- .../samples/traces/README.md | 55 +++++++++++++++++++ .../traces/collector/docker-compose.yml | 18 ++++++ .../collector/otel-collector-config.yaml | 21 +++++++ .../traces/collector/sample_collector.py | 35 ++++++++++++ .../samples/traces/sample_jaeger.py | 37 +++++++++++++ 5 files changed, 166 insertions(+) create mode 100644 sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/collector/docker-compose.yml create mode 100644 sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/collector/otel-collector-config.yaml create mode 100644 sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/collector/sample_collector.py create mode 100644 sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_jaeger.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 bee877a49cf8..d47b1327cf86 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/README.md @@ -14,6 +14,7 @@ These code samples show common champion scenario operations with the AzureMonito * 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) * Azure Storage Blob Create Container: [sample_storage.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_storage.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) +* 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) * Trace: [sample_trace.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_trace.py) * Server: [sample_server.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_server.py) @@ -63,6 +64,60 @@ $ python sample_server.py * Open http://localhost:8080/ +### Jaeger + +* The Jaeger project provides an all-in-one Docker container with a UI, database, and consumer. Run the following command to start Jaeger: + +```sh +$ docker run -p 16686:16686 -p 6831:6831/udp jaegertracing/all-in-one +``` + +* This command starts Jaeger locally on port 16686 and exposes the Jaeger thrift agent on port 6831. You can visit Jaeger at http://localhost:16686. + +* Install the OpenTelemetry Jaeger Exporter + +```sh +$ pip install opentelemetry-exporter-jaeger +``` + +* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable + +* Run the sample + +```sh +$ # from this directory +$ python sample_jaeger.py +``` + +* You should be able to see your traces in the Jaeger backend as well as Azure Monitor application insights backend. + +### Collector + +* Start the Collector locally to see how the Collector works in practice. + +* From the same folder as collector/otel-collector-config.yaml and collector/docker-compose.yml, start the Docker container. + +```sh +$ docker-compose up +``` + +* Install the OpenTelemetry OTLP Exporter + +```sh +$ pip install opentelemetry-exporter-otlp +``` + +* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable + +* Run the sample + +```sh +# from collector directory +$ python sample_collector.py +``` + +* You should be able to see your traces in the Zipkin backend as well as Azure Monitor application insights backend. + ### 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). diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/collector/docker-compose.yml b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/collector/docker-compose.yml new file mode 100644 index 000000000000..711b913453e8 --- /dev/null +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/collector/docker-compose.yml @@ -0,0 +1,18 @@ +version: "2" +services: + + # Zipkin + zipkin-all-in-one: + image: openzipkin/zipkin:latest + ports: + - "9411:9411" + + otel-collector: + image: otel/opentelemetry-collector:latest + command: ["--config=/etc/otel-collector-config.yaml", "${OTELCOL_ARGS}"] + volumes: + - ./otel-collector-config.yaml:/etc/otel-collector-config.yaml + ports: + - "4317:4317" # OTLP gRPC receiver + depends_on: + - zipkin-all-in-one diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/collector/otel-collector-config.yaml b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/collector/otel-collector-config.yaml new file mode 100644 index 000000000000..9613f66ada52 --- /dev/null +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/collector/otel-collector-config.yaml @@ -0,0 +1,21 @@ +receivers: + otlp: + protocols: + grpc: + http: +exporters: + logging: + loglevel: debug + + zipkin: + endpoint: "http://zipkin-all-in-one:9411/api/v2/spans" + format: proto + +processors: + batch: +service: + pipelines: + traces: + receivers: [otlp] + exporters: [logging, zipkin] + processors: [batch] diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/collector/sample_collector.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/collector/sample_collector.py new file mode 100644 index 000000000000..a042c120eb48 --- /dev/null +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/collector/sample_collector.py @@ -0,0 +1,35 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +""" +An example to show an application using Opentelemetry tracing api and sdk with the OpenTelemetry Collector +and the Azure monitor exporter. +Telemetry is exported to application insights with the AzureMonitorTraceExporter and Zipkin with the +OTLP Span exporter. +""" +import os +from opentelemetry import trace + +from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter +from opentelemetry.sdk.resources import SERVICE_NAME, Resource +from opentelemetry.sdk.trace import TracerProvider +from opentelemetry.sdk.trace.export import BatchSpanProcessor + +from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter + +trace.set_tracer_provider( + TracerProvider( + resource=Resource.create({SERVICE_NAME: "my-zipkin-service"}) + ) +) +tracer = trace.get_tracer(__name__) + +exporter = AzureMonitorTraceExporter.from_connection_string( + os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] +) +otlp_exporter = OTLPSpanExporter(endpoint="http://localhost:4317") +span_processor = BatchSpanProcessor(otlp_exporter) +trace.get_tracer_provider().add_span_processor(span_processor) + +with tracer.start_as_current_span("test"): + print("Hello world!") +input(...) \ No newline at end of file diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_jaeger.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_jaeger.py new file mode 100644 index 000000000000..ae6a4f43e764 --- /dev/null +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_jaeger.py @@ -0,0 +1,37 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +""" +An example to show an application using Opentelemetry tracing api and sdk with multiple exporters. +Telemetry is exported to application insights with the AzureMonitorTraceExporter and Jaeger backend with the JaegerExporter. +""" +import os +from opentelemetry import trace +from opentelemetry.exporter.jaeger.thrift import JaegerExporter +from opentelemetry.sdk.resources import SERVICE_NAME, Resource +from opentelemetry.sdk.trace import TracerProvider +from opentelemetry.sdk.trace.export import BatchSpanProcessor + +from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter + + +exporter = AzureMonitorTraceExporter.from_connection_string( + os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] +) + +jaeger_exporter = JaegerExporter( + agent_host_name="localhost", + agent_port=6831, +) + +# Service name needs to be populated for Jaeger to see traces +trace.set_tracer_provider( + TracerProvider( + resource=Resource.create({SERVICE_NAME: "my-jaeger-service"}) + ) +) +tracer = trace.get_tracer(__name__) +span_processor = BatchSpanProcessor(exporter) +trace.get_tracer_provider().add_span_processor(span_processor) + +with tracer.start_as_current_span("hello"): + print("Hello, World!")