From 019a65553c3bf2a5879d7a1d524299e6af8c74e8 Mon Sep 17 00:00:00 2001 From: Helen <56097766+heyams@users.noreply.github.com> Date: Fri, 24 Feb 2023 17:11:25 -0800 Subject: [PATCH] Fix GlobalOpenTelemetry usage (#33678) * Fix sample * Add copyright * Fix banned dependencies * Fix banned dependencies * Upgrade to 1.23 across all libraries * Fix nullpointerexception * Wait longer * Remove debug code for enabling fiddler * Remove since it will get overwritten by the script * Address comments --- eng/versioning/external_dependencies.txt | 14 +-- .../azure-security-attestation/pom.xml | 12 +- .../azure-core-metrics-opentelemetry/pom.xml | 20 +-- .../pom.xml | 4 +- .../azure-core-tracing-opentelemetry/pom.xml | 12 +- .../azure-messaging-eventhubs/pom.xml | 4 +- .../pom.xml | 14 +-- .../exporter/AzureMonitorExporterBuilder.java | 1 + .../opentelemetry/exporter/NoopTracer.java | 46 +++++++ .../AzureMonitorMetricExporterSample.java | 119 ++++++++++++++++++ .../heartbeat/HeartbeatTests.java | 2 +- .../azure-messaging-servicebus/pom.xml | 4 +- 12 files changed, 209 insertions(+), 43 deletions(-) create mode 100644 sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/NoopTracer.java create mode 100644 sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorMetricExporterSample.java diff --git a/eng/versioning/external_dependencies.txt b/eng/versioning/external_dependencies.txt index fa80235031a98..bedc54ed0b3f3 100644 --- a/eng/versioning/external_dependencies.txt +++ b/eng/versioning/external_dependencies.txt @@ -194,13 +194,13 @@ com.microsoft.azure:azure-storage;8.0.0 com.microsoft.azure:msal4j;1.13.4 com.microsoft.azure:msal4j-persistence-extension;1.1.0 com.sun.activation:jakarta.activation;1.2.2 -io.opentelemetry:opentelemetry-api;1.20.0 -io.opentelemetry:opentelemetry-sdk;1.20.0 -io.opentelemetry:opentelemetry-exporter-logging;1.20.0 -io.opentelemetry:opentelemetry-exporter-jaeger;1.20.0 -io.opentelemetry:opentelemetry-exporter-otlp;1.20.0 -io.opentelemetry:opentelemetry-sdk-testing;1.20.0 -io.opentelemetry:opentelemetry-sdk-logs;1.20.0-alpha +io.opentelemetry:opentelemetry-api;1.23.0 +io.opentelemetry:opentelemetry-sdk;1.23.0 +io.opentelemetry:opentelemetry-exporter-logging;1.23.0 +io.opentelemetry:opentelemetry-exporter-jaeger;1.23.0 +io.opentelemetry:opentelemetry-exporter-otlp;1.23.0 +io.opentelemetry:opentelemetry-sdk-testing;1.23.0 +io.opentelemetry:opentelemetry-sdk-logs;1.23.0-alpha io.projectreactor:reactor-test;3.4.26 junit:junit;4.13.2 commons-cli:commons-cli;1.3 diff --git a/sdk/attestation/azure-security-attestation/pom.xml b/sdk/attestation/azure-security-attestation/pom.xml index 3d9df2bab7707..4b5c9b2c16ff9 100644 --- a/sdk/attestation/azure-security-attestation/pom.xml +++ b/sdk/attestation/azure-security-attestation/pom.xml @@ -77,19 +77,19 @@ io.opentelemetry opentelemetry-api - 1.20.0 + 1.23.0 test io.opentelemetry opentelemetry-exporter-logging - 1.20.0 + 1.23.0 test io.opentelemetry opentelemetry-sdk - 1.20.0 + 1.23.0 test @@ -149,9 +149,9 @@ com.nimbusds:nimbus-jose-jwt:[9.22] - io.opentelemetry:opentelemetry-api:[1.20.0] - io.opentelemetry:opentelemetry-sdk:[1.20.0] - io.opentelemetry:opentelemetry-exporter-logging:[1.20.0] + io.opentelemetry:opentelemetry-api:[1.23.0] + io.opentelemetry:opentelemetry-sdk:[1.23.0] + io.opentelemetry:opentelemetry-exporter-logging:[1.23.0] diff --git a/sdk/core/azure-core-metrics-opentelemetry/pom.xml b/sdk/core/azure-core-metrics-opentelemetry/pom.xml index 8df7cfd7ae0fd..d2d26b2b48d73 100644 --- a/sdk/core/azure-core-metrics-opentelemetry/pom.xml +++ b/sdk/core/azure-core-metrics-opentelemetry/pom.xml @@ -41,7 +41,7 @@ io.opentelemetry opentelemetry-api - 1.20.0 + 1.23.0 com.azure @@ -59,14 +59,14 @@ io.opentelemetry opentelemetry-sdk - 1.20.0 + 1.23.0 test io.opentelemetry opentelemetry-sdk-testing - 1.20.0 + 1.23.0 test @@ -121,7 +121,7 @@ io.opentelemetry opentelemetry-exporter-otlp - 1.20.0 + 1.23.0 test @@ -136,12 +136,12 @@ - io.opentelemetry:opentelemetry-api:[1.20.0] - io.opentelemetry:opentelemetry-sdk:[1.20.0] - io.opentelemetry:opentelemetry-sdk-testing:[1.20.0] - io.opentelemetry:opentelemetry-exporter-logging:[1.20.0] - io.opentelemetry:opentelemetry-exporter-otlp:[1.20.0] - io.opentelemetry:opentelemetry-exporter-jaeger:[1.20.0] + io.opentelemetry:opentelemetry-api:[1.23.0] + io.opentelemetry:opentelemetry-sdk:[1.23.0] + io.opentelemetry:opentelemetry-sdk-testing:[1.23.0] + io.opentelemetry:opentelemetry-exporter-logging:[1.23.0] + io.opentelemetry:opentelemetry-exporter-otlp:[1.23.0] + io.opentelemetry:opentelemetry-exporter-jaeger:[1.23.0] diff --git a/sdk/core/azure-core-tracing-opentelemetry-samples/pom.xml b/sdk/core/azure-core-tracing-opentelemetry-samples/pom.xml index 32239b6c116a6..bd3da5f71b4bd 100644 --- a/sdk/core/azure-core-tracing-opentelemetry-samples/pom.xml +++ b/sdk/core/azure-core-tracing-opentelemetry-samples/pom.xml @@ -54,13 +54,13 @@ io.opentelemetry opentelemetry-exporter-logging - 1.20.0 + 1.23.0 test io.opentelemetry opentelemetry-exporter-jaeger - 1.20.0 + 1.23.0 test diff --git a/sdk/core/azure-core-tracing-opentelemetry/pom.xml b/sdk/core/azure-core-tracing-opentelemetry/pom.xml index c301d27c0815e..5138728b19310 100644 --- a/sdk/core/azure-core-tracing-opentelemetry/pom.xml +++ b/sdk/core/azure-core-tracing-opentelemetry/pom.xml @@ -44,7 +44,7 @@ io.opentelemetry opentelemetry-api - 1.20.0 + 1.23.0 com.azure @@ -62,7 +62,7 @@ io.opentelemetry opentelemetry-sdk - 1.20.0 + 1.23.0 test @@ -99,7 +99,7 @@ io.opentelemetry opentelemetry-sdk-testing test - 1.20.0 + 1.23.0 org.openjdk.jmh @@ -125,9 +125,9 @@ - io.opentelemetry:opentelemetry-api:[1.20.0] - io.opentelemetry:opentelemetry-sdk:[1.20.0] - io.opentelemetry:opentelemetry-sdk-testing:[1.20.0] + io.opentelemetry:opentelemetry-api:[1.23.0] + io.opentelemetry:opentelemetry-sdk:[1.23.0] + io.opentelemetry:opentelemetry-sdk-testing:[1.23.0] diff --git a/sdk/eventhubs/azure-messaging-eventhubs/pom.xml b/sdk/eventhubs/azure-messaging-eventhubs/pom.xml index c55416e126439..8850dba79eb6f 100644 --- a/sdk/eventhubs/azure-messaging-eventhubs/pom.xml +++ b/sdk/eventhubs/azure-messaging-eventhubs/pom.xml @@ -101,14 +101,14 @@ io.opentelemetry opentelemetry-api - 1.20.0 + 1.23.0 test io.opentelemetry opentelemetry-sdk - 1.20.0 + 1.23.0 test diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/pom.xml b/sdk/monitor/azure-monitor-opentelemetry-exporter/pom.xml index f2dbb42d9be6e..007ae5dfd40ef 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/pom.xml +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/pom.xml @@ -54,17 +54,17 @@ io.opentelemetry opentelemetry-api - 1.20.0 + 1.23.0 io.opentelemetry opentelemetry-sdk - 1.20.0 + 1.23.0 io.opentelemetry opentelemetry-sdk-logs - 1.20.0-alpha + 1.23.0-alpha com.github.spotbugs @@ -112,7 +112,7 @@ io.opentelemetry opentelemetry-sdk-testing - 1.20.0 + 1.23.0 test @@ -164,9 +164,9 @@ - io.opentelemetry:opentelemetry-api:[1.20.0] - io.opentelemetry:opentelemetry-sdk:[1.20.0] - io.opentelemetry:opentelemetry-sdk-logs:[1.20.0-alpha] + io.opentelemetry:opentelemetry-api:[1.23.0] + io.opentelemetry:opentelemetry-sdk:[1.23.0] + io.opentelemetry:opentelemetry-sdk-logs:[1.23.0-alpha] com.github.spotbugs:spotbugs-annotations:[4.2.2] diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilder.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilder.java index 93e081eb4e76b..761b8dbdc88fe 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilder.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilder.java @@ -329,6 +329,7 @@ private HttpPipeline createHttpPipeline() { return new HttpPipelineBuilder() .policies(policies.toArray(new HttpPipelinePolicy[0])) .httpClient(httpClient) + .tracer(new NoopTracer()) .build(); } diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/NoopTracer.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/NoopTracer.java new file mode 100644 index 0000000000000..cc00bd944d080 --- /dev/null +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/NoopTracer.java @@ -0,0 +1,46 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.monitor.opentelemetry.exporter; + +import com.azure.core.util.Context; +import com.azure.core.util.tracing.Tracer; + +import java.util.Objects; + +class NoopTracer implements Tracer { + + static final AutoCloseable NOOP_CLOSEABLE = () -> { + }; + + static final Tracer INSTANCE = new NoopTracer(); + + NoopTracer() { + } + + @Override + public Context start(String spanName, Context context) { + Objects.requireNonNull(spanName, "'spanName' cannot be null"); + return context; + } + + @Override + public void end(String statusMessage, Throwable error, Context context) { + } + + @Override + public void setAttribute(String key, String value, Context context) { + Objects.requireNonNull(key, "'key' cannot be null"); + Objects.requireNonNull(value, "'value' cannot be null"); + } + + @Override + public boolean isEnabled() { + return false; + } + + @Override + public AutoCloseable makeSpanCurrent(Context context) { + return NOOP_CLOSEABLE; + } +} diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorMetricExporterSample.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorMetricExporterSample.java new file mode 100644 index 0000000000000..ac0f3c3ca2bd2 --- /dev/null +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorMetricExporterSample.java @@ -0,0 +1,119 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.monitor.opentelemetry.exporter; + +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.metrics.DoubleHistogram; +import io.opentelemetry.api.metrics.LongCounter; +import io.opentelemetry.api.metrics.Meter; +import io.opentelemetry.sdk.OpenTelemetrySdk; +import io.opentelemetry.sdk.metrics.SdkMeterProvider; +import io.opentelemetry.sdk.metrics.export.MetricExporter; +import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader; + +import static java.util.concurrent.TimeUnit.MINUTES; + +public class AzureMonitorMetricExporterSample { + + private static final String APPINSIGHTS_CONNECTION_STRING = ""; + + public static void main(String[] args) { + sendDoubleHistogram(); + } + + private static void sendDoubleHistogram() { + try { + MetricExporter exporter = new AzureMonitorExporterBuilder() + .connectionString(APPINSIGHTS_CONNECTION_STRING) + .buildMetricExporter(); + PeriodicMetricReader periodicMetricReader = PeriodicMetricReader + .builder(exporter) + .build(); + SdkMeterProvider sdkMeterProvider = SdkMeterProvider.builder() + .registerMetricReader(periodicMetricReader) + .build(); + OpenTelemetry openTelemetry = OpenTelemetrySdk.builder() + .setMeterProvider(sdkMeterProvider) + .buildAndRegisterGlobal(); + Meter meter = openTelemetry.meterBuilder("OTEL.AzureMonitor.Demo") + .build(); + DoubleHistogram histogram = meter.histogramBuilder("histogram").build(); + histogram.record(1.0); + histogram.record(100.0); + histogram.record(30.0); + + // metrics are exported every 60 seconds by default + MINUTES.sleep(5); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + private static void sendLongCounter() { + try { + MetricExporter exporter = new AzureMonitorExporterBuilder() + .connectionString(APPINSIGHTS_CONNECTION_STRING) + .buildMetricExporter(); + PeriodicMetricReader periodicMetricReader = PeriodicMetricReader + .builder(exporter) + .build(); + SdkMeterProvider sdkMeterProvider = SdkMeterProvider.builder() + .registerMetricReader(periodicMetricReader) + .build(); + OpenTelemetry openTelemetry = OpenTelemetrySdk.builder() + .setMeterProvider(sdkMeterProvider) + .buildAndRegisterGlobal(); + Meter meter = openTelemetry.meterBuilder("OTEL.AzureMonitor.Demo") + .build(); + + LongCounter myFruitCounter = meter + .counterBuilder("MyFruitCounter") + .build(); + + myFruitCounter.add(1, Attributes.of(AttributeKey.stringKey("name"), "apple", AttributeKey.stringKey("color"), "red")); + myFruitCounter.add(2, Attributes.of(AttributeKey.stringKey("name"), "lemon", AttributeKey.stringKey("color"), "yellow")); + myFruitCounter.add(1, Attributes.of(AttributeKey.stringKey("name"), "lemon", AttributeKey.stringKey("color"), "yellow")); + myFruitCounter.add(2, Attributes.of(AttributeKey.stringKey("name"), "apple", AttributeKey.stringKey("color"), "green")); + myFruitCounter.add(5, Attributes.of(AttributeKey.stringKey("name"), "apple", AttributeKey.stringKey("color"), "red")); + myFruitCounter.add(4, Attributes.of(AttributeKey.stringKey("name"), "lemon", AttributeKey.stringKey("color"), "yellow")); + + // metrics are exported every 60 seconds by default + MINUTES.sleep(5); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + private static void sendGaugeMetric() { + try { + MetricExporter exporter = new AzureMonitorExporterBuilder() + .connectionString(APPINSIGHTS_CONNECTION_STRING) + .buildMetricExporter(); + PeriodicMetricReader periodicMetricReader = PeriodicMetricReader + .builder(exporter) + .build(); + SdkMeterProvider sdkMeterProvider = SdkMeterProvider.builder() + .registerMetricReader(periodicMetricReader) + .build(); + OpenTelemetry openTelemetry = OpenTelemetrySdk.builder() + .setMeterProvider(sdkMeterProvider) + .buildAndRegisterGlobal(); + Meter meter = openTelemetry.getMeter("OTEL.AzureMonitor.Demo"); + + meter.gaugeBuilder("gauge") + .buildWithCallback( + observableMeasurement -> { + double randomNumber = Math.floor(Math.random() * 100); + observableMeasurement.record(randomNumber, Attributes.of(AttributeKey.stringKey("testKey"), "testValue")); + }); + + // metrics are exported every 60 seconds by default + MINUTES.sleep(5); + } catch (Exception ex) { + ex.printStackTrace(); + } + } +} diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/heartbeat/HeartbeatTests.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/heartbeat/HeartbeatTests.java index eff9e4eaa564a..c2a6effd07a12 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/heartbeat/HeartbeatTests.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/heartbeat/HeartbeatTests.java @@ -31,7 +31,7 @@ void heartBeatPayloadContainsDataByDefault() throws InterruptedException { }, telemetryItemsConsumer); // some of the initialization above happens in a separate thread - Thread.sleep(500); + Thread.sleep(2000); // then MetricsData data = (MetricsData) provider.gatherData().getData().getBaseData(); diff --git a/sdk/servicebus/azure-messaging-servicebus/pom.xml b/sdk/servicebus/azure-messaging-servicebus/pom.xml index 268b47cdd8623..297fac6bbf151 100644 --- a/sdk/servicebus/azure-messaging-servicebus/pom.xml +++ b/sdk/servicebus/azure-messaging-servicebus/pom.xml @@ -131,14 +131,14 @@ io.opentelemetry opentelemetry-api - 1.20.0 + 1.23.0 test io.opentelemetry opentelemetry-sdk - 1.20.0 + 1.23.0 test