From ebc96bee3743cefa396516bc599961a7da5e2f21 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Tue, 3 Sep 2024 15:56:05 +0200 Subject: [PATCH 01/53] Rework OTel exporter API and documentation --- .../README.md | 173 ++++++++++++++---- .../monitor/opentelemetry/AzureMonitor.java | 92 ++++++++++ .../exporter/AzureMonitorExporterBuilder.java | 146 +++++---------- .../AzureMonitorExporterServiceVersion.java | 36 ---- ...nfigurationAzureMonitorExporterSample.java | 6 +- .../AzureMonitorMetricExporterSample.java | 16 +- .../EventHubsAzureMonitorExporterSample.java | 6 +- .../opentelemetry/exporter/ReadmeSamples.java | 166 ++++++++++++++++- .../AzureMonitorExporterBuilderTest.java | 3 +- .../implementation/utils/TestUtils.java | 22 +-- 10 files changed, 467 insertions(+), 199 deletions(-) create mode 100644 sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java delete mode 100644 sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterServiceVersion.java diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index 4decac6f78679..a899555e28db5 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -29,50 +29,160 @@ In order to export telemetry data to Azure Monitor, you will need the instrument search for your resource. On the overview page of your resource, you will find the instrumentation key in the top right corner. -### Creating exporter builder for Azure Monitor -```java readme-sample-createExporterBuilder -AzureMonitorExporterBuilder azureMonitorExporterBuilder = new AzureMonitorExporterBuilder() - .connectionString("{connection-string}"); -``` -#### Exporting span data +### Setup OpenTelemetry SDK to work with Azure Monitor exporter -The following example shows how to export a trace data to Azure Monitor through the - `AzureMonitorTraceExporter` +#### Use the Azure Monitor OpenTelemetry Exporter with the OpenTelemetry SDK autoconfiguration -##### Setup OpenTelemetry SDK to work with Azure Monitor exporter -```java readme-sample-setupExporter -// Create Azure Monitor exporter and initialize OpenTelemetry SDK -// This should be done just once when application starts up +The following example shows how to configure the OpenTelemetry SDK auto-configuration with the Azure Monitor exporter: +```java readme-sample-autoconfigure AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); +new AzureMonitor("{connection-string}") + .configure(sdkBuilder); +OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); +``` + +#### Use the Azure Monitor OpenTelemetry Exporter with the OpenTelemetry SDK builder + +The following snippet shows how to configure the OpenTelemetry SDK with the Azure Monitor exporter: + +```java readme-sample-sdk-builder +public void exporterAndOpenTelemetrySdkBuilder() { + SdkTracerProvider tracerProvider = buildTracerProvider(); + SdkLoggerProvider loggerProvider = buildLoggerProvider(); + SdkMeterProvider meterProvider = buildMeterProvider(); + OpenTelemetry openTelemetry = OpenTelemetrySdk.builder() + .setTracerProvider(tracerProvider) + .setLoggerProvider(loggerProvider) + .setMeterProvider(meterProvider) + .build(); +} + +private SdkTracerProvider buildTracerProvider() { + SdkTracerProviderBuilder tracerProviderBuilder = SdkTracerProvider.builder(); + SpanExporter azureSpanExporter = new AzureMonitorExporterBuilder().connectionString("{connection-string}").buildTraceExporter(); + return tracerProviderBuilder.addSpanProcessor(BatchSpanProcessor.builder(azureSpanExporter).build()).build(); +} + +private SdkLoggerProvider buildLoggerProvider() { + SdkLoggerProviderBuilder loggerProviderBuilder = SdkLoggerProvider.builder(); + LogRecordExporter azureLogRecordExporter = new AzureMonitorExporterBuilder() + .connectionString("{connection-string}") + .buildLogRecordExporter(); + return loggerProviderBuilder.addLogRecordProcessor(BatchLogRecordProcessor.builder(azureLogRecordExporter).build()).build(); +} + +private SdkMeterProvider buildMeterProvider() { + MetricExporter azureMetricExporter = new AzureMonitorExporterBuilder().connectionString("{connection-string}").buildMetricExporter(); + SdkMeterProviderBuilder meterProviderBuilder = SdkMeterProvider.builder(); + PeriodicMetricReader periodicMetricReader = createPeriodicMetricReader(azureMetricExporter); + return meterProviderBuilder.registerMetricReader(periodicMetricReader).build(); +} + +private PeriodicMetricReader createPeriodicMetricReader(MetricExporter metricExporter) { + PeriodicMetricReaderBuilder metricReaderBuilder = + PeriodicMetricReader.builder(metricExporter); + Duration oneMinute = Duration.ofMinutes(1); + metricReaderBuilder.setInterval(oneMinute); + return metricReaderBuilder.build(); +} +``` + +### Direct Azure exporters usage + +We have previously see how to configure Opentelemetry SDK to work with Azure Monitor exporter. The OpenTelemetry SDK will then export the telemetry data (OpenTelemetry spans, log records and metrics) to Azure. -new AzureMonitorExporterBuilder() +You can also use the Azure Monitor `SpanExporter`, `LogRecordExporter` and `MetricExporter` exporters directly to export telemetry data to Azure Monitor. + +The following code snippet shows how to directly export spans to Azure. +```java readme-sample-direct-span-export +SpanExporter azureSpanExporter = new AzureMonitorExporterBuilder() .connectionString("{connection-string}") - .install(sdkBuilder); + .buildTraceExporter(); -OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); +Collection spanData = getSpanDataCollection(); +azureSpanExporter.export(spanData); +``` -Tracer tracer = openTelemetry.getTracer("Sample"); +To directly export log records to Azure. +```java readme-sample-direct-log-record-export +LogRecordExporter azureLogRecordExporter = new AzureMonitorExporterBuilder() + .connectionString("{connection-string}") + .buildLogRecordExporter(); + +Collection logRecords = getLogRecordCollection(); +azureLogRecordExporter.export(logRecords); +``` + +To directly export metrics to Azure. +```java readme-sample-direct-metric-export +MetricExporter azureMetricExporter = new AzureMonitorExporterBuilder() + .connectionString("{connection-string}") + .buildMetricExporter(); + +azureMetricExporter.export(getMetricDataCollection()); ``` -##### Create spans +### Use the Azure Monitor OpenTelemetry Exporter with OpenTelemetry APIs + +The following example shows how create a span: + +```java +AutoConfiguredOpenTelemetrySdkBuilder otelSdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); -```java readme-sample-createSpans -// Make service calls by adding new parent spans -ConfigurationClient client = new ConfigurationClientBuilder() - .connectionString("{app-config-connection-string}") - .buildClient(); +new AzureMonitor("{connection-string}") + .configure(otelSdkBuilder); -Span span = tracer.spanBuilder("user-parent-span").startSpan(); -final Scope scope = span.makeCurrent(); -try { - // Thread bound (sync) calls will automatically pick up the parent span and you don't need to pass it explicitly. - client.setConfigurationSetting("hello", "text", "World"); +OpenTelemetry openTelemetry = otelSdkBuilder.build().getOpenTelemetrySdk(); +Tracer tracer = openTelemetry.getTracer("Sample"); + +Span span = tracer.spanBuilder("spanName").startSpan(); + +// Make the span the current span +try (Scope scope = span.makeCurrent()) { + // Your application logic here +} catch (Throwable t) { + span.recordException(t); + throw t; } finally { span.end(); - scope.close(); } ``` +The following example demonstrates how to add a span processor to the OpenTelemetry SDK autoconfiguration. + +```java readme-sample-span-processor +AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); + +SpanProcessor spanProcessor = new SpanProcessor() { + @Override + public void onStart(Context context, ReadWriteSpan span) { + span.setAttribute("random", RandomStringUtils.random(10)); + } + + @Override + public boolean isStartRequired() { + return true; + } + + @Override + public void onEnd(ReadableSpan readableSpan) { + } + + @Override + public boolean isEndRequired() { + return false; + } +}; + +sdkBuilder.addTracerProviderCustomizer( + (sdkTracerProviderBuilder, configProperties) -> sdkTracerProviderBuilder + .addSpanProcessor(spanProcessor)); +``` +More advanced examples with OpenTelemetry APIs: +* [Advanced examples - 1][advanced_examples_1] +* [Advanced examples - 2][advanced_examples_2] +* [Event Hubs example][event_hubs_example] + ## Key concepts @@ -94,9 +204,6 @@ Some key concepts for the Azure Monitor exporter include: For more information on the OpenTelemetry project, please review the [OpenTelemetry Specifications][opentelemetry_specification]. -## Examples - -More examples can be found in [samples][samples_code]. ## Troubleshooting @@ -150,7 +257,9 @@ This project has adopted the [Microsoft Open Source Code of Conduct][coc]. For m [span_processor]: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/sdk.md#span-processor [sampler_ref]: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/sdk.md#sampling [trace_concept]: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/overview.md#trace -[samples_code]: https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples +[advanced_examples_1]: https://github.com/Azure-Samples/ApplicationInsights-Java-Samples/tree/main/opentelemetry-api/exporter/ +[advanced_examples_2]: https://github.com/open-telemetry/opentelemetry-java-examples/tree/main/sdk-usage/src/main/java/io/opentelemetry/sdk/example +[event_hubs_example]: https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/EventHubsExample.java [cla]: https://cla.microsoft.com [coc]: https://opensource.microsoft.com/codeofconduct/ [coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/ diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java new file mode 100644 index 0000000000000..a262c7fdac088 --- /dev/null +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java @@ -0,0 +1,92 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.monitor.opentelemetry; + +import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterBuilder; +import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorExporterProviderKeys; +import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorLogRecordExporterProvider; +import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorMetricExporterProvider; +import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorSpanExporterProvider; +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder; +import io.opentelemetry.sdk.metrics.Aggregation; +import io.opentelemetry.sdk.metrics.InstrumentSelector; +import io.opentelemetry.sdk.metrics.View; + +import java.util.HashMap; +import java.util.Map; + +/** + * Class to configure OpenTelemetry for Azure Monitor. + */ +public final class AzureMonitor { + + private final AzureMonitorExporterBuilder azureMonitorExporterBuilder; + + /** + * Construct an instance of {@link AzureMonitor} + */ + public AzureMonitor() { + this.azureMonitorExporterBuilder = new AzureMonitorExporterBuilder(); + } + + /** + * Construct an instance of {@link AzureMonitor} + * @param connectionString the Azure connection string to use. + */ + public AzureMonitor(String connectionString) { + this.azureMonitorExporterBuilder = new AzureMonitorExporterBuilder().connectionString(connectionString); + } + + /** + * Construct an instance of {@link AzureMonitor} + * @param azureMonitorExporterBuilder to have access to advanced configuration options in addition to the connection string. + */ + public AzureMonitor(AzureMonitorExporterBuilder azureMonitorExporterBuilder) { + this.azureMonitorExporterBuilder = azureMonitorExporterBuilder; + } + + /** + * Configures an {@link AutoConfiguredOpenTelemetrySdkBuilder} for Azure Monitor based on the options set. + * @param autoConfiguredOpenTelemetrySdkBuilder the {@link AutoConfiguredOpenTelemetrySdkBuilder} object. + */ + public void configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfiguredOpenTelemetrySdkBuilder) { + autoConfiguredOpenTelemetrySdkBuilder.addPropertiesSupplier(() -> { + Map props = new HashMap<>(); + props.put("otel.traces.exporter", AzureMonitorExporterProviderKeys.EXPORTER_NAME); + props.put("otel.metrics.exporter", AzureMonitorExporterProviderKeys.EXPORTER_NAME); + props.put("otel.logs.exporter", AzureMonitorExporterProviderKeys.EXPORTER_NAME); + props.put(AzureMonitorExporterProviderKeys.INTERNAL_USING_AZURE_MONITOR_EXPORTER_BUILDER, "true"); + return props; + }); + autoConfiguredOpenTelemetrySdkBuilder.addSpanExporterCustomizer((spanExporter, configProperties) -> { + if (spanExporter instanceof AzureMonitorSpanExporterProvider.MarkerSpanExporter) { + spanExporter = azureMonitorExporterBuilder.buildTraceExporter(configProperties); + } + return spanExporter; + }); + autoConfiguredOpenTelemetrySdkBuilder.addMetricExporterCustomizer((metricExporter, configProperties) -> { + if (metricExporter instanceof AzureMonitorMetricExporterProvider.MarkerMetricExporter) { + metricExporter = azureMonitorExporterBuilder.buildMetricExporter(configProperties); + } + return metricExporter; + }); + autoConfiguredOpenTelemetrySdkBuilder.addLogRecordExporterCustomizer((logRecordExporter, configProperties) -> { + if (logRecordExporter instanceof AzureMonitorLogRecordExporterProvider.MarkerLogRecordExporter) { + logRecordExporter = azureMonitorExporterBuilder.buildLogRecordExporter(configProperties); + } + return logRecordExporter; + }); + // TODO (trask) + // sdkBuilder.addTracerProviderCustomizer((sdkTracerProviderBuilder, configProperties) -> { + // QuickPulse quickPulse = QuickPulse.create(getHttpPipeline()); + // return sdkTracerProviderBuilder.addSpanProcessor( + // ne + autoConfiguredOpenTelemetrySdkBuilder + .addMeterProviderCustomizer((sdkMeterProviderBuilder, config) -> sdkMeterProviderBuilder + .registerView(InstrumentSelector.builder().setMeterName("io.opentelemetry.sdk.trace").build(), + View.builder().setAggregation(Aggregation.drop()).build()) + .registerView(InstrumentSelector.builder().setMeterName("io.opentelemetry.sdk.logs").build(), + View.builder().setAggregation(Aggregation.drop()).build())); + } +} 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 b9f5f7192e55d..b5357367b61da 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 @@ -3,6 +3,7 @@ package com.azure.monitor.opentelemetry.exporter; +import com.azure.core.annotation.Fluent; import com.azure.core.credential.TokenCredential; import com.azure.core.http.HttpClient; import com.azure.core.http.HttpPipeline; @@ -17,10 +18,6 @@ import com.azure.core.util.Configuration; import com.azure.core.util.CoreUtils; import com.azure.core.util.logging.ClientLogger; -import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorExporterProviderKeys; -import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorLogRecordExporterProvider; -import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorMetricExporterProvider; -import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorSpanExporterProvider; import com.azure.monitor.opentelemetry.exporter.implementation.LogDataMapper; import com.azure.monitor.opentelemetry.exporter.implementation.MetricDataMapper; import com.azure.monitor.opentelemetry.exporter.implementation.NoopTracer; @@ -39,13 +36,9 @@ import com.azure.monitor.opentelemetry.exporter.implementation.utils.TempDirs; import com.azure.monitor.opentelemetry.exporter.implementation.utils.VersionGenerator; import com.azure.monitor.opentelemetry.exporter.implementation.utils.ResourceParser; -import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder; import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; import io.opentelemetry.sdk.logs.export.LogRecordExporter; -import io.opentelemetry.sdk.metrics.Aggregation; -import io.opentelemetry.sdk.metrics.InstrumentSelector; -import io.opentelemetry.sdk.metrics.View; import io.opentelemetry.sdk.metrics.export.MetricExporter; import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.trace.export.SpanExporter; @@ -53,7 +46,6 @@ import java.io.File; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; @@ -66,6 +58,7 @@ /** * This class provides a fluent builder API to configure the OpenTelemetry SDK with Azure Monitor Exporters. */ +@Fluent public final class AzureMonitorExporterBuilder { private static final ClientLogger LOGGER = new ClientLogger(AzureMonitorExporterBuilder.class); @@ -84,10 +77,6 @@ public final class AzureMonitorExporterBuilder { private ConnectionString connectionString; private TokenCredential credential; - // suppress warnings is needed in ApplicationInsights-Java repo, can be removed when upstreaming - @SuppressWarnings({ "UnusedVariable", "FieldCanBeLocal" }) - private AzureMonitorExporterServiceVersion serviceVersion; - private HttpPipeline httpPipeline; private HttpClient httpClient; private HttpLogOptions httpLogOptions; @@ -206,21 +195,6 @@ public AzureMonitorExporterBuilder connectionString(String connectionString) { return this; } - /** - * Sets the Azure Monitor service version. - * - * @param serviceVersion The Azure Monitor service version. - * @return The update {@link AzureMonitorExporterBuilder} object. - */ - public AzureMonitorExporterBuilder serviceVersion(AzureMonitorExporterServiceVersion serviceVersion) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "serviceVersion cannot be changed after any of the build methods have been called")); - } - this.serviceVersion = serviceVersion; - return this; - } - /** * Sets the token credential required for authentication with the ingestion endpoint service. * @@ -246,27 +220,22 @@ public AzureMonitorExporterBuilder credential(TokenCredential credential) { */ public SpanExporter buildTraceExporter() { ConfigProperties defaultConfig = DefaultConfigProperties.create(Collections.emptyMap()); - internalBuildAndFreeze(defaultConfig); - // TODO (trask) how to pass along configuration properties? return buildTraceExporter(defaultConfig); } /** - * Creates an {@link AzureMonitorMetricExporter} based on the options set in the builder. This - * exporter is an implementation of OpenTelemetry {@link MetricExporter}. - * - *

When a new {@link MetricExporter} is created, it will automatically start {@link - * HeartbeatExporter}. + * Creates an {@link AzureMonitorTraceExporter} based on the options set in the builder. This + * exporter is an implementation of OpenTelemetry {@link SpanExporter}. * - * @return An instance of {@link AzureMonitorMetricExporter}. + * @param configProperties The OpenTelemetry configuration properties. + * @return An instance of {@link AzureMonitorTraceExporter}. * @throws NullPointerException if the connection string is not set on this builder or if the * environment variable "APPLICATIONINSIGHTS_CONNECTION_STRING" is not set. */ - public MetricExporter buildMetricExporter() { - ConfigProperties defaultConfig = DefaultConfigProperties.create(Collections.emptyMap()); - internalBuildAndFreeze(defaultConfig); - // TODO (trask) how to pass along configuration properties? - return buildMetricExporter(defaultConfig); + public SpanExporter buildTraceExporter(ConfigProperties configProperties) { + internalBuildAndFreeze(configProperties); + return new AzureMonitorTraceExporter(createSpanDataMapper(configProperties), builtTelemetryItemExporter, + statsbeatModule); } /** @@ -279,64 +248,29 @@ public MetricExporter buildMetricExporter() { */ public LogRecordExporter buildLogRecordExporter() { ConfigProperties defaultConfig = DefaultConfigProperties.create(Collections.emptyMap()); - internalBuildAndFreeze(defaultConfig); - // TODO (trask) how to pass along configuration properties? return buildLogRecordExporter(defaultConfig); } /** - * Configures an {@link AutoConfiguredOpenTelemetrySdkBuilder} based on the options set in the builder. + * Creates an {@link AzureMonitorLogRecordExporter} based on the options set in the builder. This + * exporter is an implementation of OpenTelemetry {@link LogRecordExporter}. * - * @param sdkBuilder the {@link AutoConfiguredOpenTelemetrySdkBuilder} in which to install the azure monitor exporters. + * @param configProperties The OpenTelemetry configuration properties. + * @return An instance of {@link AzureMonitorLogRecordExporter}. + * @throws NullPointerException if the connection string is not set on this builder or if the + * environment variable "APPLICATIONINSIGHTS_CONNECTION_STRING" is not set. */ - public void install(AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder) { - sdkBuilder.addPropertiesSupplier(() -> { - Map props = new HashMap<>(); - props.put("otel.traces.exporter", AzureMonitorExporterProviderKeys.EXPORTER_NAME); - props.put("otel.metrics.exporter", AzureMonitorExporterProviderKeys.EXPORTER_NAME); - props.put("otel.logs.exporter", AzureMonitorExporterProviderKeys.EXPORTER_NAME); - props.put(AzureMonitorExporterProviderKeys.INTERNAL_USING_AZURE_MONITOR_EXPORTER_BUILDER, "true"); - return props; - }); - sdkBuilder.addSpanExporterCustomizer((spanExporter, configProperties) -> { - if (spanExporter instanceof AzureMonitorSpanExporterProvider.MarkerSpanExporter) { - internalBuildAndFreeze(configProperties); - spanExporter = buildTraceExporter(configProperties); - } - return spanExporter; - }); - sdkBuilder.addMetricExporterCustomizer((metricExporter, configProperties) -> { - if (metricExporter instanceof AzureMonitorMetricExporterProvider.MarkerMetricExporter) { - internalBuildAndFreeze(configProperties); - metricExporter = buildMetricExporter(configProperties); - } - return metricExporter; - }); - sdkBuilder.addLogRecordExporterCustomizer((logRecordExporter, configProperties) -> { - if (logRecordExporter instanceof AzureMonitorLogRecordExporterProvider.MarkerLogRecordExporter) { - internalBuildAndFreeze(configProperties); - logRecordExporter = buildLogRecordExporter(configProperties); - } - return logRecordExporter; - }); - // TODO (trask) - // sdkBuilder.addTracerProviderCustomizer((sdkTracerProviderBuilder, configProperties) -> { - // QuickPulse quickPulse = QuickPulse.create(getHttpPipeline()); - // return sdkTracerProviderBuilder.addSpanProcessor( - // new LiveMetricsSpanProcessor(quickPulse, createSpanDataMapper())); - // }); - sdkBuilder.addMeterProviderCustomizer((sdkMeterProviderBuilder, config) -> sdkMeterProviderBuilder - .registerView(InstrumentSelector.builder().setMeterName("io.opentelemetry.sdk.trace").build(), - View.builder().setAggregation(Aggregation.drop()).build()) - .registerView(InstrumentSelector.builder().setMeterName("io.opentelemetry.sdk.logs").build(), - View.builder().setAggregation(Aggregation.drop()).build())); + public LogRecordExporter buildLogRecordExporter(ConfigProperties configProperties) { + internalBuildAndFreeze(configProperties); + return new AzureMonitorLogRecordExporter( + new LogDataMapper(true, false, createDefaultsPopulator(configProperties)), builtTelemetryItemExporter); } // One caveat: ConfigProperties will get used only once when initializing/starting StatsbeatModule. // When a customer call build(AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder) multiple times with a diff ConfigProperties each time, // the new ConfigProperties will not get applied to StatsbeatModule because of "frozen" guard. Luckily, we're using the config properties // in StatsbeatModule for testing only. We might need to revisit this approach later. - private void internalBuildAndFreeze(ConfigProperties configProperties) { + void internalBuildAndFreeze(ConfigProperties configProperties) { if (!frozen) { HttpPipeline httpPipeline = createHttpPipeline(); statsbeatModule = initStatsbeatModule(configProperties); @@ -350,12 +284,37 @@ private void internalBuildAndFreeze(ConfigProperties configProperties) { } } - private SpanExporter buildTraceExporter(ConfigProperties configProperties) { - return new AzureMonitorTraceExporter(createSpanDataMapper(configProperties), builtTelemetryItemExporter, - statsbeatModule); + /** + * Creates an {@link AzureMonitorMetricExporter} based on the options set in the builder. This + * exporter is an implementation of OpenTelemetry {@link MetricExporter}. + * + *

When a new {@link MetricExporter} is created, it will automatically start {@link + * HeartbeatExporter}. + * + * @return An instance of {@link AzureMonitorMetricExporter}. + * @throws NullPointerException if the connection string is not set on this builder or if the + * environment variable "APPLICATIONINSIGHTS_CONNECTION_STRING" is not set. + */ + public MetricExporter buildMetricExporter() { + ConfigProperties defaultConfig = DefaultConfigProperties.create(Collections.emptyMap()); + return buildMetricExporter(defaultConfig); } - private MetricExporter buildMetricExporter(ConfigProperties configProperties) { + /** + * Creates an {@link AzureMonitorMetricExporter} based on the options set in the builder. This + * exporter is an implementation of OpenTelemetry {@link MetricExporter}. + * + *

When a new {@link MetricExporter} is created, it will automatically start {@link + * HeartbeatExporter}. + * + * + * @param configProperties The OpenTelemetry configuration properties. + * @return An instance of {@link AzureMonitorMetricExporter}. + * @throws NullPointerException if the connection string is not set on this builder or if the + * environment variable "APPLICATIONINSIGHTS_CONNECTION_STRING" is not set. + */ + public MetricExporter buildMetricExporter(ConfigProperties configProperties) { + internalBuildAndFreeze(configProperties); HeartbeatExporter.start(MINUTES.toSeconds(15), createDefaultsPopulator(configProperties), builtTelemetryItemExporter::send); return new AzureMonitorMetricExporter(new MetricDataMapper(createDefaultsPopulator(configProperties), true), @@ -373,11 +332,6 @@ private StatsbeatConnectionString getStatsbeatConnectionString() { return StatsbeatConnectionString.create(connectionString, null, null); } - private LogRecordExporter buildLogRecordExporter(ConfigProperties configProperties) { - return new AzureMonitorLogRecordExporter( - new LogDataMapper(true, false, createDefaultsPopulator(configProperties)), builtTelemetryItemExporter); - } - private SpanDataMapper createSpanDataMapper(ConfigProperties configProperties) { return new SpanDataMapper(true, createDefaultsPopulator(configProperties), (event, instrumentationName) -> false, (span, event) -> false); diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterServiceVersion.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterServiceVersion.java deleted file mode 100644 index cf5abe5058324..0000000000000 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterServiceVersion.java +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -package com.azure.monitor.opentelemetry.exporter; - -import com.azure.core.util.ServiceVersion; - -/** - * The versions of Azure Monitor service supported by this client library. - */ -public enum AzureMonitorExporterServiceVersion implements ServiceVersion { - /** - * Service version {@code 2020-09-15_Preview}. - */ - V2020_09_15_PREVIEW("2020-09-15_Preview"); - - private final String version; - - AzureMonitorExporterServiceVersion(String version) { - this.version = version; - } - - @Override - public String getVersion() { - return version; - } - - /** - * Gets the latest service version supported by this client library. - * - * @return the latest service version. - */ - public static AzureMonitorExporterServiceVersion getLatest() { - return V2020_09_15_PREVIEW; - } -} diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java index 223ac89c581f0..08aa0ebb9b4bf 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java @@ -5,6 +5,7 @@ import com.azure.data.appconfiguration.ConfigurationClient; import com.azure.data.appconfiguration.ConfigurationClientBuilder; +import com.azure.monitor.opentelemetry.AzureMonitor; import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.Tracer; @@ -39,9 +40,8 @@ public static void main(String[] args) { private static Tracer configureAzureMonitorExporter() { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - new AzureMonitorExporterBuilder() - .connectionString("{connection-string}") - .install(sdkBuilder); + new AzureMonitor("{connection-string}") + .configure(sdkBuilder); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); 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 index ed3d7f487bc20..11f68f3d821be 100644 --- 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 @@ -3,6 +3,7 @@ package com.azure.monitor.opentelemetry.exporter; +import com.azure.monitor.opentelemetry.AzureMonitor; import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; @@ -26,9 +27,8 @@ private static void sendDoubleHistogram() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - new AzureMonitorExporterBuilder() - .connectionString(APPINSIGHTS_CONNECTION_STRING) - .install(sdkBuilder); + new AzureMonitor(APPINSIGHTS_CONNECTION_STRING) + .configure(sdkBuilder); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); Meter meter = openTelemetry.meterBuilder("OTEL.AzureMonitor.Demo").build(); @@ -49,9 +49,8 @@ private static void sendLongCounter() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - new AzureMonitorExporterBuilder() - .connectionString(APPINSIGHTS_CONNECTION_STRING) - .install(sdkBuilder); + new AzureMonitor(APPINSIGHTS_CONNECTION_STRING) + .configure(sdkBuilder); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); Meter meter = openTelemetry.meterBuilder("OTEL.AzureMonitor.Demo").build(); @@ -75,9 +74,8 @@ private static void sendGaugeMetric() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - new AzureMonitorExporterBuilder() - .connectionString(APPINSIGHTS_CONNECTION_STRING) - .install(sdkBuilder); + new AzureMonitor(APPINSIGHTS_CONNECTION_STRING) + .configure(sdkBuilder); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); Meter meter = openTelemetry.getMeter("OTEL.AzureMonitor.Demo"); diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java index 28f1086dc14f7..c27e98bbd0c8a 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java @@ -8,6 +8,7 @@ import com.azure.messaging.eventhubs.EventHubClientBuilder; import com.azure.messaging.eventhubs.EventHubProducerAsyncClient; import com.azure.messaging.eventhubs.models.CreateBatchOptions; +import com.azure.monitor.opentelemetry.AzureMonitor; import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.Tracer; @@ -50,9 +51,8 @@ public static void main(String[] args) { private static Tracer configureAzureMonitorExporter() { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - new AzureMonitorExporterBuilder() - .connectionString("{connection-string}") - .install(sdkBuilder); + new AzureMonitor("{connection-string}") + .configure(sdkBuilder); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java index 8cb2b69fc5870..58841e217bafc 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java @@ -6,14 +6,33 @@ import com.azure.data.appconfiguration.ConfigurationClient; import com.azure.data.appconfiguration.ConfigurationClientBuilder; +import com.azure.monitor.opentelemetry.AzureMonitor; import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; +import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder; +import io.opentelemetry.sdk.logs.SdkLoggerProvider; +import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder; +import io.opentelemetry.sdk.logs.data.LogRecordData; +import io.opentelemetry.sdk.logs.export.BatchLogRecordProcessor; +import io.opentelemetry.sdk.logs.export.LogRecordExporter; +import io.opentelemetry.sdk.metrics.SdkMeterProvider; +import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder; +import io.opentelemetry.sdk.metrics.data.MetricData; +import io.opentelemetry.sdk.metrics.export.MetricExporter; +import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader; +import io.opentelemetry.sdk.metrics.export.PeriodicMetricReaderBuilder; +import io.opentelemetry.sdk.trace.*; import io.opentelemetry.sdk.trace.data.SpanData; +import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; +import io.opentelemetry.sdk.trace.export.SpanExporter; +import org.apache.commons.lang3.RandomStringUtils; +import java.time.Duration; import java.util.Collection; import java.util.Collections; @@ -41,13 +60,12 @@ public void createExporterBuilder() { */ public void setupExporter() { // BEGIN: readme-sample-setupExporter - // Create Azure Monitor exporter and initialize OpenTelemetry SDK + // Configure OpenTelemetry to export data to Azure Monitor // This should be done just once when application starts up AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - new AzureMonitorExporterBuilder() - .connectionString("{connection-string}") - .install(sdkBuilder); + new AzureMonitor("{connection-string}") + .configure(sdkBuilder); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); @@ -81,4 +99,144 @@ private Collection getSpanDataCollection() { return Collections.emptyList(); } + /** + * Sample to use the Azure Monitor OpenTelemetry Exporter with the OpenTelemetry SDK auto-configuration + */ + public void exporterAndOpenTelemetryAutoconfiguration() { + // BEGIN: readme-sample-autoconfigure + AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); + new AzureMonitor("{connection-string}") + .configure(sdkBuilder); + OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); + // END: readme-sample-autoconfigure + } + + /** + * Sample to add a span processor to the OpenTelemetry SDK auto-configuration + */ + public void spanProcessor() { + // BEGIN: readme-sample-span-processor + AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); + + SpanProcessor spanProcessor = new SpanProcessor() { + @Override + public void onStart(Context context, ReadWriteSpan span) { + span.setAttribute("random", RandomStringUtils.random(10)); + } + + @Override + public boolean isStartRequired() { + return true; + } + + @Override + public void onEnd(ReadableSpan readableSpan) { + } + + @Override + public boolean isEndRequired() { + return false; + } + }; + + sdkBuilder.addTracerProviderCustomizer( + (sdkTracerProviderBuilder, configProperties) -> sdkTracerProviderBuilder + .addSpanProcessor(spanProcessor)); + // END: readme-sample-span-processor + } + + /** + * Sample to directly export spans to Azure. + */ + public void directSpanExport() { + // BEGIN: readme-sample-direct-span-export + SpanExporter azureSpanExporter = new AzureMonitorExporterBuilder() + .connectionString("{connection-string}") + .buildTraceExporter(); + + Collection spanData = getSpanDataCollection(); + azureSpanExporter.export(spanData); + // END: readme-sample-direct-span-export + } + + /** + * Sample to directly export spans to Azure. + */ + public void directLogExport() { + // BEGIN: readme-sample-direct-log-record-export + LogRecordExporter azureLogRecordExporter = new AzureMonitorExporterBuilder() + .connectionString("{connection-string}") + .buildLogRecordExporter(); + + Collection logRecords = getLogRecordCollection(); + azureLogRecordExporter.export(logRecords); + // END: readme-sample-direct-log-record-export + } + + private Collection getLogRecordCollection() { + return Collections.emptyList(); + } + + /** + * Sample to directly export metrics to Azure. + */ + public void directMetricExport() { + // BEGIN: readme-sample-direct-metric-export + MetricExporter azureMetricExporter = new AzureMonitorExporterBuilder() + .connectionString("{connection-string}") + .buildMetricExporter(); + + azureMetricExporter.export(getMetricDataCollection()); + // END: readme-sample-direct-metric-export + } + + private Collection getMetricDataCollection() { + return Collections.emptyList(); + } + + + /** + * Sample to the Azure Monitor OpenTelemetry Exporter with the OpenTelemetry SDK builder + */ + // BEGIN: readme-sample-sdk-builder + public void exporterAndOpenTelemetrySdkBuilder() { + SdkTracerProvider tracerProvider = buildTracerProvider(); + SdkLoggerProvider loggerProvider = buildLoggerProvider(); + SdkMeterProvider meterProvider = buildMeterProvider(); + OpenTelemetry openTelemetry = OpenTelemetrySdk.builder() + .setTracerProvider(tracerProvider) + .setLoggerProvider(loggerProvider) + .setMeterProvider(meterProvider) + .build(); + } + + private SdkTracerProvider buildTracerProvider() { + SdkTracerProviderBuilder tracerProviderBuilder = SdkTracerProvider.builder(); + SpanExporter azureSpanExporter = new AzureMonitorExporterBuilder().connectionString("{connection-string}").buildTraceExporter(); + return tracerProviderBuilder.addSpanProcessor(BatchSpanProcessor.builder(azureSpanExporter).build()).build(); + } + + private SdkLoggerProvider buildLoggerProvider() { + SdkLoggerProviderBuilder loggerProviderBuilder = SdkLoggerProvider.builder(); + LogRecordExporter azureLogRecordExporter = new AzureMonitorExporterBuilder() + .connectionString("{connection-string}") + .buildLogRecordExporter(); + return loggerProviderBuilder.addLogRecordProcessor(BatchLogRecordProcessor.builder(azureLogRecordExporter).build()).build(); + } + + private SdkMeterProvider buildMeterProvider() { + MetricExporter azureMetricExporter = new AzureMonitorExporterBuilder().connectionString("{connection-string}").buildMetricExporter(); + SdkMeterProviderBuilder meterProviderBuilder = SdkMeterProvider.builder(); + PeriodicMetricReader periodicMetricReader = createPeriodicMetricReader(azureMetricExporter); + return meterProviderBuilder.registerMetricReader(periodicMetricReader).build(); + } + + private PeriodicMetricReader createPeriodicMetricReader(MetricExporter metricExporter) { + PeriodicMetricReaderBuilder metricReaderBuilder = + PeriodicMetricReader.builder(metricExporter); + Duration oneMinute = Duration.ofMinutes(1); + metricReaderBuilder.setInterval(oneMinute); + return metricReaderBuilder.build(); + } + // END: readme-sample-sdk-builder } diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java index 574aa58acea88..82ef1330c5dbf 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java @@ -3,6 +3,7 @@ package com.azure.monitor.opentelemetry.exporter; +import com.azure.monitor.opentelemetry.AzureMonitor; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder; import org.junit.jupiter.api.Assertions; @@ -22,7 +23,7 @@ public void testInvalidConnectionStrings(String con Class exceptionExpected) { Assertions.assertThrows(exceptionExpected, () -> { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - new AzureMonitorExporterBuilder().connectionString(connectionString).install(sdkBuilder); + new AzureMonitor(connectionString).configure(sdkBuilder); sdkBuilder.build(); }); } diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java index 123bb4f0f1b84..f0c80516a6a0f 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java @@ -7,25 +7,16 @@ import com.azure.json.JsonProviders; import com.azure.json.JsonReader; import com.azure.json.JsonToken; +import com.azure.monitor.opentelemetry.AzureMonitor; import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterBuilder; -import com.azure.monitor.opentelemetry.exporter.implementation.models.MessageData; -import com.azure.monitor.opentelemetry.exporter.implementation.models.MetricDataPoint; -import com.azure.monitor.opentelemetry.exporter.implementation.models.MetricsData; -import com.azure.monitor.opentelemetry.exporter.implementation.models.MonitorBase; -import com.azure.monitor.opentelemetry.exporter.implementation.models.MonitorDomain; -import com.azure.monitor.opentelemetry.exporter.implementation.models.RemoteDependencyData; -import com.azure.monitor.opentelemetry.exporter.implementation.models.TelemetryItem; +import com.azure.monitor.opentelemetry.exporter.implementation.models.*; import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder; import io.opentelemetry.sdk.resources.Resource; import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; public final class TestUtils { @@ -88,9 +79,10 @@ public static OpenTelemetrySdk createOpenTelemetrySdk(HttpPipeline httpPipeline, String connectionString) { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - new AzureMonitorExporterBuilder().connectionString(connectionString) - .httpPipeline(httpPipeline) - .install(sdkBuilder); + AzureMonitorExporterBuilder azureMonitorExporterBuilder + = new AzureMonitorExporterBuilder().connectionString(connectionString).httpPipeline(httpPipeline); + + new AzureMonitor(azureMonitorExporterBuilder).configure(sdkBuilder); return sdkBuilder.addPropertiesSupplier(() -> configuration).build().getOpenTelemetrySdk(); } From e7254181b87b5843a7d0e7b471ca56c71ae6a508 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Tue, 3 Sep 2024 17:36:44 +0200 Subject: [PATCH 02/53] Update following discussions with Trask --- .../README.md | 87 +------------ .../opentelemetry/exporter/ReadmeSamples.java | 114 ------------------ 2 files changed, 2 insertions(+), 199 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index a899555e28db5..e9a556e86846b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -30,11 +30,9 @@ search for your resource. On the overview page of your resource, you will find t right corner. -### Setup OpenTelemetry SDK to work with Azure Monitor exporter +### Setup THE OpenTelemetry SDK to work with Azure Monitor exporter -#### Use the Azure Monitor OpenTelemetry Exporter with the OpenTelemetry SDK autoconfiguration - -The following example shows how to configure the OpenTelemetry SDK auto-configuration with the Azure Monitor exporter: +The following code shows how to configure the OpenTelemetry SDK auto-configuration with the Azure Monitor exporter: ```java readme-sample-autoconfigure AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); new AzureMonitor("{connection-string}") @@ -42,87 +40,6 @@ new AzureMonitor("{connection-string}") OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); ``` -#### Use the Azure Monitor OpenTelemetry Exporter with the OpenTelemetry SDK builder - -The following snippet shows how to configure the OpenTelemetry SDK with the Azure Monitor exporter: - -```java readme-sample-sdk-builder -public void exporterAndOpenTelemetrySdkBuilder() { - SdkTracerProvider tracerProvider = buildTracerProvider(); - SdkLoggerProvider loggerProvider = buildLoggerProvider(); - SdkMeterProvider meterProvider = buildMeterProvider(); - OpenTelemetry openTelemetry = OpenTelemetrySdk.builder() - .setTracerProvider(tracerProvider) - .setLoggerProvider(loggerProvider) - .setMeterProvider(meterProvider) - .build(); -} - -private SdkTracerProvider buildTracerProvider() { - SdkTracerProviderBuilder tracerProviderBuilder = SdkTracerProvider.builder(); - SpanExporter azureSpanExporter = new AzureMonitorExporterBuilder().connectionString("{connection-string}").buildTraceExporter(); - return tracerProviderBuilder.addSpanProcessor(BatchSpanProcessor.builder(azureSpanExporter).build()).build(); -} - -private SdkLoggerProvider buildLoggerProvider() { - SdkLoggerProviderBuilder loggerProviderBuilder = SdkLoggerProvider.builder(); - LogRecordExporter azureLogRecordExporter = new AzureMonitorExporterBuilder() - .connectionString("{connection-string}") - .buildLogRecordExporter(); - return loggerProviderBuilder.addLogRecordProcessor(BatchLogRecordProcessor.builder(azureLogRecordExporter).build()).build(); -} - -private SdkMeterProvider buildMeterProvider() { - MetricExporter azureMetricExporter = new AzureMonitorExporterBuilder().connectionString("{connection-string}").buildMetricExporter(); - SdkMeterProviderBuilder meterProviderBuilder = SdkMeterProvider.builder(); - PeriodicMetricReader periodicMetricReader = createPeriodicMetricReader(azureMetricExporter); - return meterProviderBuilder.registerMetricReader(periodicMetricReader).build(); -} - -private PeriodicMetricReader createPeriodicMetricReader(MetricExporter metricExporter) { - PeriodicMetricReaderBuilder metricReaderBuilder = - PeriodicMetricReader.builder(metricExporter); - Duration oneMinute = Duration.ofMinutes(1); - metricReaderBuilder.setInterval(oneMinute); - return metricReaderBuilder.build(); -} -``` - -### Direct Azure exporters usage - -We have previously see how to configure Opentelemetry SDK to work with Azure Monitor exporter. The OpenTelemetry SDK will then export the telemetry data (OpenTelemetry spans, log records and metrics) to Azure. - -You can also use the Azure Monitor `SpanExporter`, `LogRecordExporter` and `MetricExporter` exporters directly to export telemetry data to Azure Monitor. - -The following code snippet shows how to directly export spans to Azure. -```java readme-sample-direct-span-export -SpanExporter azureSpanExporter = new AzureMonitorExporterBuilder() - .connectionString("{connection-string}") - .buildTraceExporter(); - -Collection spanData = getSpanDataCollection(); -azureSpanExporter.export(spanData); -``` - -To directly export log records to Azure. -```java readme-sample-direct-log-record-export -LogRecordExporter azureLogRecordExporter = new AzureMonitorExporterBuilder() - .connectionString("{connection-string}") - .buildLogRecordExporter(); - -Collection logRecords = getLogRecordCollection(); -azureLogRecordExporter.export(logRecords); -``` - -To directly export metrics to Azure. -```java readme-sample-direct-metric-export -MetricExporter azureMetricExporter = new AzureMonitorExporterBuilder() - .connectionString("{connection-string}") - .buildMetricExporter(); - -azureMetricExporter.export(getMetricDataCollection()); -``` - ### Use the Azure Monitor OpenTelemetry Exporter with OpenTelemetry APIs The following example shows how create a span: diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java index 58841e217bafc..47bc54bcfee8f 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java @@ -45,16 +45,6 @@ */ public class ReadmeSamples { - /** - * Sample for creating Azure Monitor exporter builder. - */ - public void createExporterBuilder() { - // BEGIN: readme-sample-createExporterBuilder - AzureMonitorExporterBuilder azureMonitorExporterBuilder = new AzureMonitorExporterBuilder() - .connectionString("{connection-string}"); - // END: readme-sample-createExporterBuilder - } - /** * Sample for setting up exporter to export traces to Azure Monitor */ @@ -90,15 +80,6 @@ public void setupExporter() { // END: readme-sample-createSpans } - /** - * Method to make the sample compilable but is not visible in README code snippet. - * - * @return An empty collection. - */ - private Collection getSpanDataCollection() { - return Collections.emptyList(); - } - /** * Sample to use the Azure Monitor OpenTelemetry Exporter with the OpenTelemetry SDK auto-configuration */ @@ -144,99 +125,4 @@ public boolean isEndRequired() { .addSpanProcessor(spanProcessor)); // END: readme-sample-span-processor } - - /** - * Sample to directly export spans to Azure. - */ - public void directSpanExport() { - // BEGIN: readme-sample-direct-span-export - SpanExporter azureSpanExporter = new AzureMonitorExporterBuilder() - .connectionString("{connection-string}") - .buildTraceExporter(); - - Collection spanData = getSpanDataCollection(); - azureSpanExporter.export(spanData); - // END: readme-sample-direct-span-export - } - - /** - * Sample to directly export spans to Azure. - */ - public void directLogExport() { - // BEGIN: readme-sample-direct-log-record-export - LogRecordExporter azureLogRecordExporter = new AzureMonitorExporterBuilder() - .connectionString("{connection-string}") - .buildLogRecordExporter(); - - Collection logRecords = getLogRecordCollection(); - azureLogRecordExporter.export(logRecords); - // END: readme-sample-direct-log-record-export - } - - private Collection getLogRecordCollection() { - return Collections.emptyList(); - } - - /** - * Sample to directly export metrics to Azure. - */ - public void directMetricExport() { - // BEGIN: readme-sample-direct-metric-export - MetricExporter azureMetricExporter = new AzureMonitorExporterBuilder() - .connectionString("{connection-string}") - .buildMetricExporter(); - - azureMetricExporter.export(getMetricDataCollection()); - // END: readme-sample-direct-metric-export - } - - private Collection getMetricDataCollection() { - return Collections.emptyList(); - } - - - /** - * Sample to the Azure Monitor OpenTelemetry Exporter with the OpenTelemetry SDK builder - */ - // BEGIN: readme-sample-sdk-builder - public void exporterAndOpenTelemetrySdkBuilder() { - SdkTracerProvider tracerProvider = buildTracerProvider(); - SdkLoggerProvider loggerProvider = buildLoggerProvider(); - SdkMeterProvider meterProvider = buildMeterProvider(); - OpenTelemetry openTelemetry = OpenTelemetrySdk.builder() - .setTracerProvider(tracerProvider) - .setLoggerProvider(loggerProvider) - .setMeterProvider(meterProvider) - .build(); - } - - private SdkTracerProvider buildTracerProvider() { - SdkTracerProviderBuilder tracerProviderBuilder = SdkTracerProvider.builder(); - SpanExporter azureSpanExporter = new AzureMonitorExporterBuilder().connectionString("{connection-string}").buildTraceExporter(); - return tracerProviderBuilder.addSpanProcessor(BatchSpanProcessor.builder(azureSpanExporter).build()).build(); - } - - private SdkLoggerProvider buildLoggerProvider() { - SdkLoggerProviderBuilder loggerProviderBuilder = SdkLoggerProvider.builder(); - LogRecordExporter azureLogRecordExporter = new AzureMonitorExporterBuilder() - .connectionString("{connection-string}") - .buildLogRecordExporter(); - return loggerProviderBuilder.addLogRecordProcessor(BatchLogRecordProcessor.builder(azureLogRecordExporter).build()).build(); - } - - private SdkMeterProvider buildMeterProvider() { - MetricExporter azureMetricExporter = new AzureMonitorExporterBuilder().connectionString("{connection-string}").buildMetricExporter(); - SdkMeterProviderBuilder meterProviderBuilder = SdkMeterProvider.builder(); - PeriodicMetricReader periodicMetricReader = createPeriodicMetricReader(azureMetricExporter); - return meterProviderBuilder.registerMetricReader(periodicMetricReader).build(); - } - - private PeriodicMetricReader createPeriodicMetricReader(MetricExporter metricExporter) { - PeriodicMetricReaderBuilder metricReaderBuilder = - PeriodicMetricReader.builder(metricExporter); - Duration oneMinute = Duration.ofMinutes(1); - metricReaderBuilder.setInterval(oneMinute); - return metricReaderBuilder.build(); - } - // END: readme-sample-sdk-builder } From c70423ff9be8ac2b2a91663ac1b32231a78aad12 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Tue, 3 Sep 2024 17:49:18 +0200 Subject: [PATCH 03/53] Fix typo --- sdk/monitor/azure-monitor-opentelemetry-exporter/README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index e9a556e86846b..6b90dc7236a03 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -30,7 +30,7 @@ search for your resource. On the overview page of your resource, you will find t right corner. -### Setup THE OpenTelemetry SDK to work with Azure Monitor exporter +### Setup the OpenTelemetry SDK to work with Azure Monitor exporter The following code shows how to configure the OpenTelemetry SDK auto-configuration with the Azure Monitor exporter: ```java readme-sample-autoconfigure @@ -100,7 +100,6 @@ More advanced examples with OpenTelemetry APIs: * [Advanced examples - 2][advanced_examples_2] * [Event Hubs example][event_hubs_example] - ## Key concepts Some key concepts for the Azure Monitor exporter include: From 06f835279e5fd4bccc9d3017b56bb6f0c16b9b77 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 4 Sep 2024 14:09:51 +0200 Subject: [PATCH 04/53] Fix Readme CI check --- sdk/monitor/azure-monitor-opentelemetry-exporter/README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index 6b90dc7236a03..9e224424137f5 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -40,7 +40,10 @@ new AzureMonitor("{connection-string}") OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); ``` -### Use the Azure Monitor OpenTelemetry Exporter with OpenTelemetry APIs +## Examples + +The following sections provide code samples using the OpenTelemetry Azure Monitor Exporter client library and OpenTelemetry API. + The following example shows how create a span: From d80264854d8ef25eae3ee5db4d7729cb3356edd6 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 4 Sep 2024 14:21:46 +0200 Subject: [PATCH 05/53] Fix broken link --- sdk/monitor/azure-monitor-opentelemetry-exporter/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index 9e224424137f5..c926b81c76ee3 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -178,7 +178,7 @@ This project has adopted the [Microsoft Open Source Code of Conduct][coc]. For m [trace_concept]: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/overview.md#trace [advanced_examples_1]: https://github.com/Azure-Samples/ApplicationInsights-Java-Samples/tree/main/opentelemetry-api/exporter/ [advanced_examples_2]: https://github.com/open-telemetry/opentelemetry-java-examples/tree/main/sdk-usage/src/main/java/io/opentelemetry/sdk/example -[event_hubs_example]: https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/EventHubsExample.java +[event_hubs_example]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java [cla]: https://cla.microsoft.com [coc]: https://opensource.microsoft.com/codeofconduct/ [coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/ From 78e3f36914d599ca005e20c202b2bc257c843e93 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 4 Sep 2024 14:58:03 +0200 Subject: [PATCH 06/53] Add ExportOptions class --- .../monitor/opentelemetry/AzureMonitor.java | 10 +- .../exporter/AzureMonitorExporterBuilder.java | 198 ++++-------------- .../opentelemetry/exporter/ExportOptions.java | 118 +++++++++++ .../MonitorExporterClientTestBase.java | 3 +- .../implementation/utils/TestUtils.java | 7 +- 5 files changed, 164 insertions(+), 172 deletions(-) create mode 100644 sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/ExportOptions.java diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java index a262c7fdac088..3a6c2d64feeab 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java @@ -4,6 +4,7 @@ package com.azure.monitor.opentelemetry; import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterBuilder; +import com.azure.monitor.opentelemetry.exporter.ExportOptions; import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorExporterProviderKeys; import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorLogRecordExporterProvider; import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorMetricExporterProvider; @@ -35,15 +36,16 @@ public AzureMonitor() { * @param connectionString the Azure connection string to use. */ public AzureMonitor(String connectionString) { - this.azureMonitorExporterBuilder = new AzureMonitorExporterBuilder().connectionString(connectionString); + ExportOptions exportOptions = new ExportOptions().connectionString(connectionString); + this.azureMonitorExporterBuilder = new AzureMonitorExporterBuilder(exportOptions); } /** * Construct an instance of {@link AzureMonitor} - * @param azureMonitorExporterBuilder to have access to advanced configuration options in addition to the connection string. + * @param exportOptions The export options to Azure, see{@link ExportOptions}. */ - public AzureMonitor(AzureMonitorExporterBuilder azureMonitorExporterBuilder) { - this.azureMonitorExporterBuilder = azureMonitorExporterBuilder; + public AzureMonitor(ExportOptions exportOptions) { + this.azureMonitorExporterBuilder = new AzureMonitorExporterBuilder(exportOptions); } /** 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 b5357367b61da..bbc201225377c 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 @@ -4,17 +4,8 @@ package com.azure.monitor.opentelemetry.exporter; import com.azure.core.annotation.Fluent; -import com.azure.core.credential.TokenCredential; -import com.azure.core.http.HttpClient; import com.azure.core.http.HttpPipeline; -import com.azure.core.http.policy.BearerTokenAuthenticationPolicy; -import com.azure.core.http.policy.CookiePolicy; -import com.azure.core.http.policy.HttpLogDetailLevel; -import com.azure.core.http.policy.HttpLogOptions; -import com.azure.core.http.policy.HttpLoggingPolicy; -import com.azure.core.http.policy.HttpPipelinePolicy; -import com.azure.core.http.policy.UserAgentPolicy; -import com.azure.core.util.ClientOptions; +import com.azure.core.http.policy.*; import com.azure.core.util.Configuration; import com.azure.core.util.CoreUtils; import com.azure.core.util.logging.ClientLogger; @@ -31,11 +22,7 @@ import com.azure.monitor.opentelemetry.exporter.implementation.pipeline.TelemetryItemExporter; import com.azure.monitor.opentelemetry.exporter.implementation.statsbeat.Feature; import com.azure.monitor.opentelemetry.exporter.implementation.statsbeat.StatsbeatModule; -import com.azure.monitor.opentelemetry.exporter.implementation.utils.AzureMonitorHelper; -import com.azure.monitor.opentelemetry.exporter.implementation.utils.PropertyHelper; -import com.azure.monitor.opentelemetry.exporter.implementation.utils.TempDirs; -import com.azure.monitor.opentelemetry.exporter.implementation.utils.VersionGenerator; -import com.azure.monitor.opentelemetry.exporter.implementation.utils.ResourceParser; +import com.azure.monitor.opentelemetry.exporter.implementation.utils.*; import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; import io.opentelemetry.sdk.logs.export.LogRecordExporter; @@ -44,12 +31,7 @@ import io.opentelemetry.sdk.trace.export.SpanExporter; import java.io.File; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; +import java.util.*; import java.util.function.BiConsumer; import static java.util.concurrent.TimeUnit.DAYS; @@ -74,140 +56,29 @@ public final class AzureMonitorExporterBuilder { private static final Map PROPERTIES = CoreUtils.getProperties("azure-monitor-opentelemetry-exporter.properties"); - private ConnectionString connectionString; - private TokenCredential credential; - - private HttpPipeline httpPipeline; - private HttpClient httpClient; - private HttpLogOptions httpLogOptions; - private final List httpPipelinePolicies = new ArrayList<>(); - private ClientOptions clientOptions; - - private boolean frozen; - // this is only populated after the builder is frozen private TelemetryItemExporter builtTelemetryItemExporter; // this is only populated after the builder is frozen private StatsbeatModule statsbeatModule; - /** - * Creates an instance of {@link AzureMonitorExporterBuilder}. - */ - public AzureMonitorExporterBuilder() { - } - - /** - * Sets the HTTP pipeline to use for the service client. If {@code httpPipeline} is set, all other - * settings are ignored. - * - * @param httpPipeline The HTTP pipeline to use for sending service requests and receiving - * responses. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public AzureMonitorExporterBuilder httpPipeline(HttpPipeline httpPipeline) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "httpPipeline cannot be changed after any of the build methods have been called")); - } - this.httpPipeline = httpPipeline; - return this; - } - - /** - * Sets the HTTP client to use for sending and receiving requests to and from the service. - * - * @param httpClient The HTTP client to use for requests. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public AzureMonitorExporterBuilder httpClient(HttpClient httpClient) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "httpClient cannot be changed after any of the build methods have been called")); - } - this.httpClient = httpClient; - return this; - } - - /** - * Sets the logging configuration for HTTP requests and responses. - * - *

If logLevel is not provided, default value of {@link HttpLogDetailLevel#NONE} is set. - * - * @param httpLogOptions The logging configuration to use when sending and receiving HTTP - * requests/responses. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public AzureMonitorExporterBuilder httpLogOptions(HttpLogOptions httpLogOptions) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "httpLogOptions cannot be changed after any of the build methods have been called")); - } - this.httpLogOptions = httpLogOptions; - return this; - } + private boolean frozen; - /** - * Adds a policy to the set of existing policies that are executed after required policies. - * - * @param httpPipelinePolicy a policy to be added to the http pipeline. - * @return The updated {@link AzureMonitorExporterBuilder} object. - * @throws NullPointerException If {@code policy} is {@code null}. - */ - public AzureMonitorExporterBuilder addHttpPipelinePolicy(HttpPipelinePolicy httpPipelinePolicy) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "httpPipelinePolicies cannot be added after any of the build methods have been called")); - } - httpPipelinePolicies.add(Objects.requireNonNull(httpPipelinePolicy, "'policy' cannot be null.")); - return this; - } + private final ExportOptions exportOptions; /** - * Sets the client options such as application ID and custom headers to set on a request. - * - * @param clientOptions The client options. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public AzureMonitorExporterBuilder clientOptions(ClientOptions clientOptions) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "clientOptions cannot be changed after any of the build methods have been called")); - } - this.clientOptions = clientOptions; - return this; - } - - /** - * Sets the connection string to use for exporting telemetry events to Azure Monitor. - * - * @param connectionString The connection string for the Azure Monitor resource. - * @return The updated {@link AzureMonitorExporterBuilder} object. - * @throws NullPointerException If the connection string is {@code null}. - * @throws IllegalArgumentException If the connection string is invalid. + * Creates an instance of {@link AzureMonitorExporterBuilder}. */ - public AzureMonitorExporterBuilder connectionString(String connectionString) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "connectionString cannot be changed after any of the build methods have been called")); - } - this.connectionString = ConnectionString.parse(connectionString); - return this; + public AzureMonitorExporterBuilder() { + this.exportOptions = new ExportOptions(); } /** - * Sets the token credential required for authentication with the ingestion endpoint service. - * - * @param credential The Azure Identity TokenCredential. - * @return The updated {@link AzureMonitorExporterBuilder} object. + * Creates an instance of {@link AzureMonitorExporterBuilder}. + * @param exportOptions The export options. */ - public AzureMonitorExporterBuilder credential(TokenCredential credential) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "credential cannot be changed after any of the build methods have been called")); - } - this.credential = credential; - return this; + public AzureMonitorExporterBuilder(ExportOptions exportOptions) { + this.exportOptions = new ExportOptions(); } /** @@ -329,7 +200,7 @@ private Set initStatsbeatFeatures() { } private StatsbeatConnectionString getStatsbeatConnectionString() { - return StatsbeatConnectionString.create(connectionString, null, null); + return StatsbeatConnectionString.create(exportOptions.connectionString, null, null); } private SpanDataMapper createSpanDataMapper(ConfigProperties configProperties) { @@ -350,8 +221,8 @@ private BiConsumer createDefaultsPopulator(C } private ConnectionString getConnectionString(ConfigProperties configProperties) { - if (connectionString != null) { - return connectionString; + if (exportOptions.connectionString != null) { + return exportOptions.connectionString; } ConnectionString connectionString = ConnectionString.parse(configProperties.getString(APPLICATIONINSIGHTS_CONNECTION_STRING)); @@ -360,46 +231,47 @@ private ConnectionString getConnectionString(ConfigProperties configProperties) private HttpPipeline createHttpPipeline() { - if (httpPipeline != null) { - if (credential != null) { + if (exportOptions.httpPipeline != null) { + if (exportOptions.credential != null) { throw LOGGER.logExceptionAsError( new IllegalStateException("'credential' is not supported when custom 'httpPipeline' is specified")); } - if (httpClient != null) { + if (exportOptions.httpClient != null) { throw LOGGER.logExceptionAsError( new IllegalStateException("'httpClient' is not supported when custom 'httpPipeline' is specified")); } - if (httpLogOptions != null) { + if (exportOptions.httpLogOptions != null) { throw LOGGER.logExceptionAsError(new IllegalStateException( "'httpLogOptions' is not supported when custom 'httpPipeline' is specified")); } - if (!httpPipelinePolicies.isEmpty()) { + if (!exportOptions.httpPipelinePolicies.isEmpty()) { throw LOGGER.logExceptionAsError(new IllegalStateException( "'httpPipelinePolicies' is not supported when custom 'httpPipeline' is specified")); } - if (clientOptions != null) { + if (exportOptions.clientOptions != null) { throw LOGGER.logExceptionAsError(new IllegalStateException( "'clientOptions' is not supported when custom 'httpPipeline' is specified")); } - return httpPipeline; + return exportOptions.httpPipeline; } List policies = new ArrayList<>(); String clientName = PROPERTIES.getOrDefault("name", "UnknownName"); String clientVersion = PROPERTIES.getOrDefault("version", "UnknownVersion"); - String applicationId = CoreUtils.getApplicationId(clientOptions, httpLogOptions); + String applicationId = CoreUtils.getApplicationId(exportOptions.clientOptions, exportOptions.httpLogOptions); policies .add(new UserAgentPolicy(applicationId, clientName, clientVersion, Configuration.getGlobalConfiguration())); policies.add(new CookiePolicy()); - if (credential != null) { - policies.add(new BearerTokenAuthenticationPolicy(credential, APPLICATIONINSIGHTS_AUTHENTICATION_SCOPE)); + if (exportOptions.credential != null) { + policies.add(new BearerTokenAuthenticationPolicy(exportOptions.credential, + APPLICATIONINSIGHTS_AUTHENTICATION_SCOPE)); } - policies.addAll(httpPipelinePolicies); - policies.add(new HttpLoggingPolicy(httpLogOptions)); + policies.addAll(exportOptions.httpPipelinePolicies); + policies.add(new HttpLoggingPolicy(exportOptions.httpLogOptions)); return new com.azure.core.http.HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])) - .httpClient(httpClient) + .httpClient(exportOptions.httpClient) .tracer(new NoopTracer()) .build(); } @@ -422,23 +294,23 @@ private void startStatsbeatModule(StatsbeatModule statsbeatModule, ConfigPropert } private HttpPipeline createStatsbeatHttpPipeline() { - if (httpPipeline != null) { - return httpPipeline; + if (exportOptions.httpPipeline != null) { + return exportOptions.httpPipeline; } List policies = new ArrayList<>(); String clientName = PROPERTIES.getOrDefault("name", "UnknownName"); String clientVersion = PROPERTIES.getOrDefault("version", "UnknownVersion"); - String applicationId = CoreUtils.getApplicationId(clientOptions, httpLogOptions); + String applicationId = CoreUtils.getApplicationId(exportOptions.clientOptions, exportOptions.httpLogOptions); policies .add(new UserAgentPolicy(applicationId, clientName, clientVersion, Configuration.getGlobalConfiguration())); policies.add(new CookiePolicy()); - policies.addAll(httpPipelinePolicies); - policies.add(new HttpLoggingPolicy(httpLogOptions)); + policies.addAll(exportOptions.httpPipelinePolicies); + policies.add(new HttpLoggingPolicy(exportOptions.httpLogOptions)); return new com.azure.core.http.HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])) - .httpClient(httpClient) + .httpClient(exportOptions.httpClient) .tracer(new NoopTracer()) .build(); } diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/ExportOptions.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/ExportOptions.java new file mode 100644 index 0000000000000..9f7afc86a30e5 --- /dev/null +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/ExportOptions.java @@ -0,0 +1,118 @@ +package com.azure.monitor.opentelemetry.exporter; + +import com.azure.core.annotation.Fluent; +import com.azure.core.credential.TokenCredential; +import com.azure.core.http.HttpClient; +import com.azure.core.http.HttpPipeline; +import com.azure.core.http.policy.HttpLogDetailLevel; +import com.azure.core.http.policy.HttpLogOptions; +import com.azure.core.http.policy.HttpPipelinePolicy; +import com.azure.core.util.ClientOptions; +import com.azure.monitor.opentelemetry.exporter.implementation.configuration.ConnectionString; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** + * The options to configure the Azure Monitor export. + */ +@Fluent +public final class ExportOptions { + + ConnectionString connectionString; + TokenCredential credential; + + HttpPipeline httpPipeline; + HttpClient httpClient; + HttpLogOptions httpLogOptions; + + ClientOptions clientOptions; + + final List httpPipelinePolicies = new ArrayList<>(); + + /** + * Sets the HTTP pipeline to use for the service client. If {@code httpPipeline} is set, all other + * settings are ignored. + * + * @param httpPipeline The HTTP pipeline to use for sending service requests and receiving + * responses. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + public ExportOptions httpPipeline(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + return this; + } + + /** + * Sets the HTTP client to use for sending and receiving requests to and from the service. + * + * @param httpClient The HTTP client to use for requests. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + public ExportOptions httpClient(HttpClient httpClient) { + this.httpClient = httpClient; + return this; + } + + /** + * Sets the logging configuration for HTTP requests and responses. + * + *

If logLevel is not provided, default value of {@link HttpLogDetailLevel#NONE} is set. + * + * @param httpLogOptions The logging configuration to use when sending and receiving HTTP + * requests/responses. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + public ExportOptions httpLogOptions(HttpLogOptions httpLogOptions) { + this.httpLogOptions = httpLogOptions; + return this; + } + + /** + * Adds a policy to the set of existing policies that are executed after required policies. + * + * @param httpPipelinePolicy a policy to be added to the http pipeline. + * @return The updated {@link AzureMonitorExporterBuilder} object. + * @throws NullPointerException If {@code policy} is {@code null}. + */ + public ExportOptions addHttpPipelinePolicy(HttpPipelinePolicy httpPipelinePolicy) { + httpPipelinePolicies.add(Objects.requireNonNull(httpPipelinePolicy, "'policy' cannot be null.")); + return this; + } + + /** + * Sets the client options such as application ID and custom headers to set on a request. + * + * @param clientOptions The client options. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + public ExportOptions clientOptions(ClientOptions clientOptions) { + this.clientOptions = clientOptions; + return this; + } + + /** + * Sets the connection string to use for exporting telemetry events to Azure Monitor. + * + * @param connectionString The connection string for the Azure Monitor resource. + * @return The updated {@link AzureMonitorExporterBuilder} object. + * @throws NullPointerException If the connection string is {@code null}. + * @throws IllegalArgumentException If the connection string is invalid. + */ + public ExportOptions connectionString(String connectionString) { + this.connectionString = ConnectionString.parse(connectionString); + return this; + } + + /** + * Sets the token credential required for authentication with the ingestion endpoint service. + * + * @param credential The Azure Identity TokenCredential. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + public ExportOptions credential(TokenCredential credential) { + this.credential = credential; + return this; + } +} diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/MonitorExporterClientTestBase.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/MonitorExporterClientTestBase.java index d49cce40aff9f..ad59d2a40324c 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/MonitorExporterClientTestBase.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/MonitorExporterClientTestBase.java @@ -33,7 +33,8 @@ public class MonitorExporterClientTestBase extends TestProxyTestBase { AzureMonitorExporterBuilder getClientBuilder() { - return new AzureMonitorExporterBuilder().httpPipeline(getHttpPipeline(null)); + ExportOptions exportOptions = new ExportOptions().httpPipeline(getHttpPipeline(null)); + return new AzureMonitorExporterBuilder(exportOptions); } HttpPipeline getHttpPipeline(@Nullable HttpPipelinePolicy policy, HttpClient httpClient) { diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java index f0c80516a6a0f..dff8230847ff6 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java @@ -8,7 +8,7 @@ import com.azure.json.JsonReader; import com.azure.json.JsonToken; import com.azure.monitor.opentelemetry.AzureMonitor; -import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterBuilder; +import com.azure.monitor.opentelemetry.exporter.ExportOptions; import com.azure.monitor.opentelemetry.exporter.implementation.models.*; import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; @@ -79,10 +79,9 @@ public static OpenTelemetrySdk createOpenTelemetrySdk(HttpPipeline httpPipeline, String connectionString) { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitorExporterBuilder azureMonitorExporterBuilder - = new AzureMonitorExporterBuilder().connectionString(connectionString).httpPipeline(httpPipeline); + ExportOptions exportOptions = new ExportOptions().connectionString(connectionString).httpPipeline(httpPipeline); - new AzureMonitor(azureMonitorExporterBuilder).configure(sdkBuilder); + new AzureMonitor(exportOptions).configure(sdkBuilder); return sdkBuilder.addPropertiesSupplier(() -> configuration).build().getOpenTelemetrySdk(); } From e24fef128c46f84352b05d0262c29b76d90b7ec8 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 4 Sep 2024 15:15:51 +0200 Subject: [PATCH 07/53] Avoid star import --- .../exporter/AzureMonitorExporterBuilder.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) 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 bbc201225377c..c6c4d3170ac16 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 @@ -5,7 +5,11 @@ import com.azure.core.annotation.Fluent; import com.azure.core.http.HttpPipeline; -import com.azure.core.http.policy.*; +import com.azure.core.http.policy.BearerTokenAuthenticationPolicy; +import com.azure.core.http.policy.CookiePolicy; +import com.azure.core.http.policy.HttpLoggingPolicy; +import com.azure.core.http.policy.HttpPipelinePolicy; +import com.azure.core.http.policy.UserAgentPolicy; import com.azure.core.util.Configuration; import com.azure.core.util.CoreUtils; import com.azure.core.util.logging.ClientLogger; @@ -22,7 +26,11 @@ import com.azure.monitor.opentelemetry.exporter.implementation.pipeline.TelemetryItemExporter; import com.azure.monitor.opentelemetry.exporter.implementation.statsbeat.Feature; import com.azure.monitor.opentelemetry.exporter.implementation.statsbeat.StatsbeatModule; -import com.azure.monitor.opentelemetry.exporter.implementation.utils.*; +import com.azure.monitor.opentelemetry.exporter.implementation.utils.AzureMonitorHelper; +import com.azure.monitor.opentelemetry.exporter.implementation.utils.PropertyHelper; +import com.azure.monitor.opentelemetry.exporter.implementation.utils.TempDirs; +import com.azure.monitor.opentelemetry.exporter.implementation.utils.VersionGenerator; +import com.azure.monitor.opentelemetry.exporter.implementation.utils.ResourceParser; import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; import io.opentelemetry.sdk.logs.export.LogRecordExporter; @@ -31,7 +39,12 @@ import io.opentelemetry.sdk.trace.export.SpanExporter; import java.io.File; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; import java.util.function.BiConsumer; import static java.util.concurrent.TimeUnit.DAYS; From 0803c815473abea11bb29acb7d50be6ee818f6ea Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 4 Sep 2024 17:24:43 +0200 Subject: [PATCH 08/53] Fix mistake in field assignment --- .../opentelemetry/exporter/AzureMonitorExporterBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c6c4d3170ac16..3087be471ebdc 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 @@ -91,7 +91,7 @@ public AzureMonitorExporterBuilder() { * @param exportOptions The export options. */ public AzureMonitorExporterBuilder(ExportOptions exportOptions) { - this.exportOptions = new ExportOptions(); + this.exportOptions = exportOptions; } /** From ed73481b77c24502771bcc916eab46150b4e6053 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 4 Sep 2024 17:46:50 +0200 Subject: [PATCH 09/53] Ability to chain an OpenTelemetry method call from AzureMonitor --- .../README.md | 6 +++--- .../monitor/opentelemetry/AzureMonitor.java | 5 ++++- ...ConfigurationAzureMonitorExporterSample.java | 6 ++---- .../AzureMonitorMetricExporterSample.java | 17 +++++------------ .../EventHubsAzureMonitorExporterSample.java | 6 ++---- .../opentelemetry/exporter/ReadmeSamples.java | 12 +++++------- .../implementation/utils/TestUtils.java | 4 +--- 7 files changed, 22 insertions(+), 34 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index c926b81c76ee3..33bcc60b01ce1 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -35,9 +35,9 @@ right corner. The following code shows how to configure the OpenTelemetry SDK auto-configuration with the Azure Monitor exporter: ```java readme-sample-autoconfigure AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); -new AzureMonitor("{connection-string}") - .configure(sdkBuilder); -OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); + +OpenTelemetry openTelemetry = new AzureMonitor("{connection-string}") + .configure(sdkBuilder).build().getOpenTelemetrySdk(); ``` ## Examples diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java index 3a6c2d64feeab..cb2cf3ebdfea6 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java @@ -51,8 +51,10 @@ public AzureMonitor(ExportOptions exportOptions) { /** * Configures an {@link AutoConfiguredOpenTelemetrySdkBuilder} for Azure Monitor based on the options set. * @param autoConfiguredOpenTelemetrySdkBuilder the {@link AutoConfiguredOpenTelemetrySdkBuilder} object. + * @return the {@link AutoConfiguredOpenTelemetrySdkBuilder} object given in argument. */ - public void configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfiguredOpenTelemetrySdkBuilder) { + public AutoConfiguredOpenTelemetrySdkBuilder + configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfiguredOpenTelemetrySdkBuilder) { autoConfiguredOpenTelemetrySdkBuilder.addPropertiesSupplier(() -> { Map props = new HashMap<>(); props.put("otel.traces.exporter", AzureMonitorExporterProviderKeys.EXPORTER_NAME); @@ -90,5 +92,6 @@ public void configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfiguredOpenTe View.builder().setAggregation(Aggregation.drop()).build()) .registerView(InstrumentSelector.builder().setMeterName("io.opentelemetry.sdk.logs").build(), View.builder().setAggregation(Aggregation.drop()).build())); + return autoConfiguredOpenTelemetrySdkBuilder; } } diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java index 08aa0ebb9b4bf..3e9167de49beb 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java @@ -40,10 +40,8 @@ public static void main(String[] args) { private static Tracer configureAzureMonitorExporter() { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - new AzureMonitor("{connection-string}") - .configure(sdkBuilder); - - OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); + OpenTelemetry openTelemetry = new AzureMonitor("{connection-string}") + .configure(sdkBuilder).build().getOpenTelemetrySdk(); return openTelemetry.getTracer("Sample"); } 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 index 11f68f3d821be..bbcd7792d0e39 100644 --- 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 @@ -27,10 +27,8 @@ private static void sendDoubleHistogram() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - new AzureMonitor(APPINSIGHTS_CONNECTION_STRING) - .configure(sdkBuilder); - - OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); + OpenTelemetry openTelemetry = new AzureMonitor(APPINSIGHTS_CONNECTION_STRING) + .configure(sdkBuilder).build().getOpenTelemetrySdk(); Meter meter = openTelemetry.meterBuilder("OTEL.AzureMonitor.Demo").build(); DoubleHistogram histogram = meter.histogramBuilder("histogram").build(); @@ -49,10 +47,7 @@ private static void sendLongCounter() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - new AzureMonitor(APPINSIGHTS_CONNECTION_STRING) - .configure(sdkBuilder); - - OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); + OpenTelemetry openTelemetry = new AzureMonitor(APPINSIGHTS_CONNECTION_STRING).configure(sdkBuilder).build().getOpenTelemetrySdk(); Meter meter = openTelemetry.meterBuilder("OTEL.AzureMonitor.Demo").build(); LongCounter myFruitCounter = meter.counterBuilder("MyFruitCounter").build(); @@ -74,10 +69,8 @@ private static void sendGaugeMetric() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - new AzureMonitor(APPINSIGHTS_CONNECTION_STRING) - .configure(sdkBuilder); - - OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); + OpenTelemetry openTelemetry = new AzureMonitor(APPINSIGHTS_CONNECTION_STRING) + .configure(sdkBuilder).build().getOpenTelemetrySdk(); Meter meter = openTelemetry.getMeter("OTEL.AzureMonitor.Demo"); meter.gaugeBuilder("gauge") diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java index c27e98bbd0c8a..7558110c71e59 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java @@ -51,10 +51,8 @@ public static void main(String[] args) { private static Tracer configureAzureMonitorExporter() { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - new AzureMonitor("{connection-string}") - .configure(sdkBuilder); - - OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); + OpenTelemetry openTelemetry = new AzureMonitor("{connection-string}") + .configure(sdkBuilder).build().getOpenTelemetrySdk(); return openTelemetry.getTracer("Sample"); } diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java index 47bc54bcfee8f..45f3d1c7e74d0 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java @@ -54,10 +54,8 @@ public void setupExporter() { // This should be done just once when application starts up AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - new AzureMonitor("{connection-string}") - .configure(sdkBuilder); - - OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); + OpenTelemetry openTelemetry = new AzureMonitor("{connection-string}") + .configure(sdkBuilder).build().getOpenTelemetrySdk(); Tracer tracer = openTelemetry.getTracer("Sample"); // END: readme-sample-setupExporter @@ -86,9 +84,9 @@ public void setupExporter() { public void exporterAndOpenTelemetryAutoconfiguration() { // BEGIN: readme-sample-autoconfigure AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - new AzureMonitor("{connection-string}") - .configure(sdkBuilder); - OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); + + OpenTelemetry openTelemetry = new AzureMonitor("{connection-string}") + .configure(sdkBuilder).build().getOpenTelemetrySdk(); // END: readme-sample-autoconfigure } diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java index dff8230847ff6..d89f449b35fa5 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java @@ -81,9 +81,7 @@ public static OpenTelemetrySdk createOpenTelemetrySdk(HttpPipeline httpPipeline, ExportOptions exportOptions = new ExportOptions().connectionString(connectionString).httpPipeline(httpPipeline); - new AzureMonitor(exportOptions).configure(sdkBuilder); - - return sdkBuilder.addPropertiesSupplier(() -> configuration).build().getOpenTelemetrySdk(); + return new AzureMonitor(exportOptions).configure(sdkBuilder).addPropertiesSupplier(() -> configuration).build().getOpenTelemetrySdk(); } // azure-json doesn't deserialize subtypes yet, so need to convert the abstract MonitorDomain to RemoteDependencyData From ea0022b8dbb963e5832ab7839fd516ab5d2a844b Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 4 Sep 2024 18:04:31 +0200 Subject: [PATCH 10/53] Remove star import --- .../exporter/implementation/utils/TestUtils.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java index d89f449b35fa5..1f702d42dff55 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java @@ -16,7 +16,11 @@ import io.opentelemetry.sdk.resources.Resource; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public final class TestUtils { @@ -81,7 +85,10 @@ public static OpenTelemetrySdk createOpenTelemetrySdk(HttpPipeline httpPipeline, ExportOptions exportOptions = new ExportOptions().connectionString(connectionString).httpPipeline(httpPipeline); - return new AzureMonitor(exportOptions).configure(sdkBuilder).addPropertiesSupplier(() -> configuration).build().getOpenTelemetrySdk(); + return new AzureMonitor(exportOptions).configure(sdkBuilder) + .addPropertiesSupplier(() -> configuration) + .build() + .getOpenTelemetrySdk(); } // azure-json doesn't deserialize subtypes yet, so need to convert the abstract MonitorDomain to RemoteDependencyData From c4fb196b47e3ab2766864479cee3d39878a1e158 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 4 Sep 2024 18:13:52 +0200 Subject: [PATCH 11/53] Use AttributeKey.stringKey --- sdk/monitor/azure-monitor-opentelemetry-exporter/README.md | 2 +- .../azure/monitor/opentelemetry/exporter/ReadmeSamples.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index 33bcc60b01ce1..7ac7b13eee10a 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -76,7 +76,7 @@ AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySd SpanProcessor spanProcessor = new SpanProcessor() { @Override public void onStart(Context context, ReadWriteSpan span) { - span.setAttribute("random", RandomStringUtils.random(10)); + span.setAttribute(AttributeKey.stringKey("random"), RandomStringUtils.random(10)); } @Override diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java index 45f3d1c7e74d0..29acfd8b0d3aa 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java @@ -8,6 +8,7 @@ import com.azure.data.appconfiguration.ConfigurationClientBuilder; import com.azure.monitor.opentelemetry.AzureMonitor; import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.Tracer; import io.opentelemetry.context.Context; @@ -100,7 +101,7 @@ public void spanProcessor() { SpanProcessor spanProcessor = new SpanProcessor() { @Override public void onStart(Context context, ReadWriteSpan span) { - span.setAttribute("random", RandomStringUtils.random(10)); + span.setAttribute(AttributeKey.stringKey("random"), RandomStringUtils.random(10)); } @Override From 5edeb05d90a800148d54814faabe2596b2b33e2e Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 4 Sep 2024 18:25:21 +0200 Subject: [PATCH 12/53] Fix external dependency check --- .../src/main/resources/checkstyle/checkstyle-suppressions.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml b/eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml index 049262d222f1e..7ac376081102a 100644 --- a/eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml +++ b/eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml @@ -78,6 +78,9 @@ + + From 0af1eff61af8e7201b6db1e5d9b9fc737ba54356 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Thu, 5 Sep 2024 11:59:25 +0200 Subject: [PATCH 13/53] Add code sample for span creation --- .../README.md | 8 +++--- .../opentelemetry/exporter/ReadmeSamples.java | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index 7ac7b13eee10a..3a2abae1838d0 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -47,13 +47,11 @@ The following sections provide code samples using the OpenTelemetry Azure Monito The following example shows how create a span: -```java +```java readme-sample-create-span AutoConfiguredOpenTelemetrySdkBuilder otelSdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); -new AzureMonitor("{connection-string}") - .configure(otelSdkBuilder); - -OpenTelemetry openTelemetry = otelSdkBuilder.build().getOpenTelemetrySdk(); +OpenTelemetry openTelemetry = new AzureMonitor("{connection-string}") + .configure(otelSdkBuilder).build().getOpenTelemetrySdk(); Tracer tracer = openTelemetry.getTracer("Sample"); Span span = tracer.spanBuilder("spanName").startSpan(); diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java index 29acfd8b0d3aa..74634f7ab0fc6 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java @@ -91,6 +91,32 @@ public void exporterAndOpenTelemetryAutoconfiguration() { // END: readme-sample-autoconfigure } + /** + * Sample to create a span. + */ + @SuppressWarnings("try") + public void createSpan() { + // BEGIN: readme-sample-create-span + AutoConfiguredOpenTelemetrySdkBuilder otelSdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); + + OpenTelemetry openTelemetry = new AzureMonitor("{connection-string}") + .configure(otelSdkBuilder).build().getOpenTelemetrySdk(); + Tracer tracer = openTelemetry.getTracer("Sample"); + + Span span = tracer.spanBuilder("spanName").startSpan(); + + // Make the span the current span + try (Scope scope = span.makeCurrent()) { + // Your application logic here + } catch (Throwable t) { + span.recordException(t); + throw t; + } finally { + span.end(); + } + // END: readme-sample-create-span + } + /** * Sample to add a span processor to the OpenTelemetry SDK auto-configuration */ From fa86f47951c74346cb4b2d40b2ff4e29156edb91 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Thu, 5 Sep 2024 14:06:38 +0200 Subject: [PATCH 14/53] Fix linting --- .../monitor/opentelemetry/exporter/ExportOptions.java | 3 +++ .../java/com/azure/monitor/opentelemetry/package-info.java | 7 +++++++ 2 files changed, 10 insertions(+) create mode 100644 sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/package-info.java diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/ExportOptions.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/ExportOptions.java index 9f7afc86a30e5..9d1b214169cf1 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/ExportOptions.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/ExportOptions.java @@ -1,3 +1,6 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + package com.azure.monitor.opentelemetry.exporter; import com.azure.core.annotation.Fluent; diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/package-info.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/package-info.java new file mode 100644 index 0000000000000..090354228832c --- /dev/null +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/package-info.java @@ -0,0 +1,7 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +/** + * Package containing classes for configuring the OpenTelemetry autoconfiguration with the telemetry data export to Azure Monitor. + */ +package com.azure.monitor.opentelemetry; From 36d07945ae5dfb8c9e44da5f9c4d2b0cde38239f Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Thu, 5 Sep 2024 15:03:29 +0200 Subject: [PATCH 15/53] Make low/high level API explicit --- .../README.md | 4 +- .../monitor/opentelemetry/AzureMonitor.java | 99 ++++++-- .../exporter/AzureMonitorExporterBuilder.java | 213 +++++++++++++----- .../opentelemetry/exporter/ExportOptions.java | 121 ---------- ...nfigurationAzureMonitorExporterSample.java | 2 +- .../AzureMonitorMetricExporterSample.java | 6 +- .../EventHubsAzureMonitorExporterSample.java | 2 +- .../opentelemetry/exporter/ReadmeSamples.java | 6 +- .../AzureMonitorExporterBuilderTest.java | 2 +- .../MonitorExporterClientTestBase.java | 3 +- .../implementation/utils/TestUtils.java | 7 +- 11 files changed, 259 insertions(+), 206 deletions(-) delete mode 100644 sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/ExportOptions.java diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index 3a2abae1838d0..40940916047ec 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -36,7 +36,7 @@ The following code shows how to configure the OpenTelemetry SDK auto-configurati ```java readme-sample-autoconfigure AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); -OpenTelemetry openTelemetry = new AzureMonitor("{connection-string}") +OpenTelemetry openTelemetry = new AzureMonitor().connectionString("{connection-string}") .configure(sdkBuilder).build().getOpenTelemetrySdk(); ``` @@ -50,7 +50,7 @@ The following example shows how create a span: ```java readme-sample-create-span AutoConfiguredOpenTelemetrySdkBuilder otelSdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); -OpenTelemetry openTelemetry = new AzureMonitor("{connection-string}") +OpenTelemetry openTelemetry = new AzureMonitor().connectionString("{connection-string}") .configure(otelSdkBuilder).build().getOpenTelemetrySdk(); Tracer tracer = openTelemetry.getTracer("Sample"); diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java index cb2cf3ebdfea6..7893b58bfad1a 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java @@ -3,8 +3,15 @@ package com.azure.monitor.opentelemetry; +import com.azure.core.annotation.Fluent; +import com.azure.core.credential.TokenCredential; +import com.azure.core.http.HttpClient; +import com.azure.core.http.HttpPipeline; +import com.azure.core.http.policy.HttpLogDetailLevel; +import com.azure.core.http.policy.HttpLogOptions; +import com.azure.core.http.policy.HttpPipelinePolicy; +import com.azure.core.util.ClientOptions; import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterBuilder; -import com.azure.monitor.opentelemetry.exporter.ExportOptions; import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorExporterProviderKeys; import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorLogRecordExporterProvider; import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorMetricExporterProvider; @@ -18,34 +25,96 @@ import java.util.Map; /** - * Class to configure OpenTelemetry for Azure Monitor. + * Class to enable Azure Monitor for OpenTelemetry autoconfiguration. */ +@Fluent public final class AzureMonitor { - private final AzureMonitorExporterBuilder azureMonitorExporterBuilder; + private final AzureMonitorExporterBuilder azureMonitorExporterBuilder = new AzureMonitorExporterBuilder(); /** - * Construct an instance of {@link AzureMonitor} + * Sets the HTTP pipeline to use for the service client. If {@code httpPipeline} is set, all other + * settings are ignored. + * + * @param httpPipeline The HTTP pipeline to use for sending service requests and receiving + * responses. + * @return The updated {@link AzureMonitor} object. */ - public AzureMonitor() { - this.azureMonitorExporterBuilder = new AzureMonitorExporterBuilder(); + public AzureMonitor httpPipeline(HttpPipeline httpPipeline) { + azureMonitorExporterBuilder.httpPipeline(httpPipeline); + return this; } /** - * Construct an instance of {@link AzureMonitor} - * @param connectionString the Azure connection string to use. + * Sets the HTTP client to use for sending and receiving requests to and from the service. + * + * @param httpClient The HTTP client to use for requests. + * @return The updated {@link AzureMonitor} object. */ - public AzureMonitor(String connectionString) { - ExportOptions exportOptions = new ExportOptions().connectionString(connectionString); - this.azureMonitorExporterBuilder = new AzureMonitorExporterBuilder(exportOptions); + public AzureMonitor httpClient(HttpClient httpClient) { + azureMonitorExporterBuilder.httpClient(httpClient); + return this; } /** - * Construct an instance of {@link AzureMonitor} - * @param exportOptions The export options to Azure, see{@link ExportOptions}. + * Sets the logging configuration for HTTP requests and responses. + * + *

If logLevel is not provided, default value of {@link HttpLogDetailLevel#NONE} is set. + * + * @param httpLogOptions The logging configuration to use when sending and receiving HTTP + * requests/responses. + * @return The updated {@link AzureMonitor} object. */ - public AzureMonitor(ExportOptions exportOptions) { - this.azureMonitorExporterBuilder = new AzureMonitorExporterBuilder(exportOptions); + public AzureMonitor httpLogOptions(HttpLogOptions httpLogOptions) { + azureMonitorExporterBuilder.httpLogOptions(httpLogOptions); + return this; + } + + /** + * Adds a policy to the set of existing policies that are executed after required policies. + * + * @param httpPipelinePolicy a policy to be added to the http pipeline. + * @return The updated {@link AzureMonitorExporterBuilder} object. + * @throws NullPointerException If {@code policy} is {@code null}. + */ + public AzureMonitor addHttpPipelinePolicy(HttpPipelinePolicy httpPipelinePolicy) { + azureMonitorExporterBuilder.addHttpPipelinePolicy(httpPipelinePolicy); + return this; + } + + /** + * Sets the client options such as application ID and custom headers to set on a request. + * + * @param clientOptions The client options. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + public AzureMonitor clientOptions(ClientOptions clientOptions) { + azureMonitorExporterBuilder.clientOptions(clientOptions); + return this; + } + + /** + * Sets the connection string to use for exporting telemetry events to Azure Monitor. + * + * @param connectionString The connection string for the Azure Monitor resource. + * @return The updated {@link AzureMonitorExporterBuilder} object. + * @throws NullPointerException If the connection string is {@code null}. + * @throws IllegalArgumentException If the connection string is invalid. + */ + public AzureMonitor connectionString(String connectionString) { + azureMonitorExporterBuilder.connectionString(connectionString); + return this; + } + + /** + * Sets the token credential required for authentication with the ingestion endpoint service. + * + * @param credential The Azure Identity TokenCredential. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + public AzureMonitor credential(TokenCredential credential) { + azureMonitorExporterBuilder.credential(credential); + return this; } /** 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 3087be471ebdc..97bcb6be1d7ba 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 @@ -1,15 +1,11 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - package com.azure.monitor.opentelemetry.exporter; import com.azure.core.annotation.Fluent; +import com.azure.core.credential.TokenCredential; +import com.azure.core.http.HttpClient; import com.azure.core.http.HttpPipeline; -import com.azure.core.http.policy.BearerTokenAuthenticationPolicy; -import com.azure.core.http.policy.CookiePolicy; -import com.azure.core.http.policy.HttpLoggingPolicy; -import com.azure.core.http.policy.HttpPipelinePolicy; -import com.azure.core.http.policy.UserAgentPolicy; +import com.azure.core.http.policy.*; +import com.azure.core.util.ClientOptions; import com.azure.core.util.Configuration; import com.azure.core.util.CoreUtils; import com.azure.core.util.logging.ClientLogger; @@ -31,6 +27,7 @@ import com.azure.monitor.opentelemetry.exporter.implementation.utils.TempDirs; import com.azure.monitor.opentelemetry.exporter.implementation.utils.VersionGenerator; import com.azure.monitor.opentelemetry.exporter.implementation.utils.ResourceParser; +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder; import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; import io.opentelemetry.sdk.logs.export.LogRecordExporter; @@ -51,7 +48,7 @@ import static java.util.concurrent.TimeUnit.MINUTES; /** - * This class provides a fluent builder API to configure the OpenTelemetry SDK with Azure Monitor Exporters. + * Low level API to create OpenTelemetry span, log record and metric exporters for Azure. With OpenTelemetry autoconfiguration ({@link AutoConfiguredOpenTelemetrySdkBuilder}), we recommend using {@link com.azure.monitor.opentelemetry.AzureMonitor}. */ @Fluent public final class AzureMonitorExporterBuilder { @@ -69,36 +66,147 @@ public final class AzureMonitorExporterBuilder { private static final Map PROPERTIES = CoreUtils.getProperties("azure-monitor-opentelemetry-exporter.properties"); + private ConnectionString connectionString; + private TokenCredential credential; + + private HttpPipeline httpPipeline; + private HttpClient httpClient; + private HttpLogOptions httpLogOptions; + private final List httpPipelinePolicies = new ArrayList<>(); + private ClientOptions clientOptions; + + private boolean frozen; + // this is only populated after the builder is frozen private TelemetryItemExporter builtTelemetryItemExporter; // this is only populated after the builder is frozen private StatsbeatModule statsbeatModule; - private boolean frozen; - - private final ExportOptions exportOptions; - /** * Creates an instance of {@link AzureMonitorExporterBuilder}. */ public AzureMonitorExporterBuilder() { - this.exportOptions = new ExportOptions(); } /** - * Creates an instance of {@link AzureMonitorExporterBuilder}. - * @param exportOptions The export options. + * Sets the HTTP pipeline to use for the service client. If {@code httpPipeline} is set, all other + * settings are ignored. + * + * @param httpPipeline The HTTP pipeline to use for sending service requests and receiving + * responses. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + public AzureMonitorExporterBuilder httpPipeline(HttpPipeline httpPipeline) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "httpPipeline cannot be changed after any of the build methods have been called")); + } + this.httpPipeline = httpPipeline; + return this; + } + + /** + * Sets the HTTP client to use for sending and receiving requests to and from the service. + * + * @param httpClient The HTTP client to use for requests. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + public AzureMonitorExporterBuilder httpClient(HttpClient httpClient) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "httpClient cannot be changed after any of the build methods have been called")); + } + this.httpClient = httpClient; + return this; + } + + /** + * Sets the logging configuration for HTTP requests and responses. + * + *

If logLevel is not provided, default value of {@link HttpLogDetailLevel#NONE} is set. + * + * @param httpLogOptions The logging configuration to use when sending and receiving HTTP + * requests/responses. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + public AzureMonitorExporterBuilder httpLogOptions(HttpLogOptions httpLogOptions) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "httpLogOptions cannot be changed after any of the build methods have been called")); + } + this.httpLogOptions = httpLogOptions; + return this; + } + + /** + * Adds a policy to the set of existing policies that are executed after required policies. + * + * @param httpPipelinePolicy a policy to be added to the http pipeline. + * @return The updated {@link AzureMonitorExporterBuilder} object. + * @throws NullPointerException If {@code policy} is {@code null}. + */ + public AzureMonitorExporterBuilder addHttpPipelinePolicy(HttpPipelinePolicy httpPipelinePolicy) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "httpPipelinePolicies cannot be added after any of the build methods have been called")); + } + httpPipelinePolicies.add(Objects.requireNonNull(httpPipelinePolicy, "'policy' cannot be null.")); + return this; + } + + /** + * Sets the client options such as application ID and custom headers to set on a request. + * + * @param clientOptions The client options. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + public AzureMonitorExporterBuilder clientOptions(ClientOptions clientOptions) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "clientOptions cannot be changed after any of the build methods have been called")); + } + this.clientOptions = clientOptions; + return this; + } + + /** + * Sets the connection string to use for exporting telemetry events to Azure Monitor. + * + * @param connectionString The connection string for the Azure Monitor resource. + * @return The updated {@link AzureMonitorExporterBuilder} object. + * @throws NullPointerException If the connection string is {@code null}. + * @throws IllegalArgumentException If the connection string is invalid. + */ + public AzureMonitorExporterBuilder connectionString(String connectionString) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "connectionString cannot be changed after any of the build methods have been called")); + } + this.connectionString = ConnectionString.parse(connectionString); + return this; + } + + /** + * Sets the token credential required for authentication with the ingestion endpoint service. + * + * @param credential The Azure Identity TokenCredential. + * @return The updated {@link AzureMonitorExporterBuilder} object. */ - public AzureMonitorExporterBuilder(ExportOptions exportOptions) { - this.exportOptions = exportOptions; + public AzureMonitorExporterBuilder credential(TokenCredential credential) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "credential cannot be changed after any of the build methods have been called")); + } + this.credential = credential; + return this; } /** - * Creates an {@link AzureMonitorTraceExporter} based on the options set in the builder. This + * Creates an Azure Monitor trace exporter based on the options set in the builder. This * exporter is an implementation of OpenTelemetry {@link SpanExporter}. * - * @return An instance of {@link AzureMonitorTraceExporter}. + * @return An instance of {@link SpanExporter}. * @throws NullPointerException if the connection string is not set on this builder or if the * environment variable "APPLICATIONINSIGHTS_CONNECTION_STRING" is not set. */ @@ -108,11 +216,11 @@ public SpanExporter buildTraceExporter() { } /** - * Creates an {@link AzureMonitorTraceExporter} based on the options set in the builder. This + * Creates an Azure Monitor trace exporter based on the options set in the builder. This * exporter is an implementation of OpenTelemetry {@link SpanExporter}. * * @param configProperties The OpenTelemetry configuration properties. - * @return An instance of {@link AzureMonitorTraceExporter}. + * @return An instance of {@link SpanExporter}. * @throws NullPointerException if the connection string is not set on this builder or if the * environment variable "APPLICATIONINSIGHTS_CONNECTION_STRING" is not set. */ @@ -123,10 +231,10 @@ public SpanExporter buildTraceExporter(ConfigProperties configProperties) { } /** - * Creates an {@link AzureMonitorLogRecordExporter} based on the options set in the builder. This + * Creates an Azure Monitor log record exporter based on the options set in the builder. This * exporter is an implementation of OpenTelemetry {@link LogRecordExporter}. * - * @return An instance of {@link AzureMonitorLogRecordExporter}. + * @return An instance of {@link LogRecordExporter}. * @throws NullPointerException if the connection string is not set on this builder or if the * environment variable "APPLICATIONINSIGHTS_CONNECTION_STRING" is not set. */ @@ -136,11 +244,11 @@ public LogRecordExporter buildLogRecordExporter() { } /** - * Creates an {@link AzureMonitorLogRecordExporter} based on the options set in the builder. This + * Creates an Azure Monitor log record exporter based on the options set in the builder. This * exporter is an implementation of OpenTelemetry {@link LogRecordExporter}. * * @param configProperties The OpenTelemetry configuration properties. - * @return An instance of {@link AzureMonitorLogRecordExporter}. + * @return An instance of {@link LogRecordExporter}. * @throws NullPointerException if the connection string is not set on this builder or if the * environment variable "APPLICATIONINSIGHTS_CONNECTION_STRING" is not set. */ @@ -169,13 +277,13 @@ void internalBuildAndFreeze(ConfigProperties configProperties) { } /** - * Creates an {@link AzureMonitorMetricExporter} based on the options set in the builder. This + * Creates an Azure monitor metric exporter based on the options set in the builder. This * exporter is an implementation of OpenTelemetry {@link MetricExporter}. * *

When a new {@link MetricExporter} is created, it will automatically start {@link * HeartbeatExporter}. * - * @return An instance of {@link AzureMonitorMetricExporter}. + * @return An instance of {@link MetricExporter}. * @throws NullPointerException if the connection string is not set on this builder or if the * environment variable "APPLICATIONINSIGHTS_CONNECTION_STRING" is not set. */ @@ -185,7 +293,7 @@ public MetricExporter buildMetricExporter() { } /** - * Creates an {@link AzureMonitorMetricExporter} based on the options set in the builder. This + * Creates an Azure monitor metric exporter based on the options set in the builder. This * exporter is an implementation of OpenTelemetry {@link MetricExporter}. * *

When a new {@link MetricExporter} is created, it will automatically start {@link @@ -193,7 +301,7 @@ public MetricExporter buildMetricExporter() { * * * @param configProperties The OpenTelemetry configuration properties. - * @return An instance of {@link AzureMonitorMetricExporter}. + * @return An instance of {@link MetricExporter}. * @throws NullPointerException if the connection string is not set on this builder or if the * environment variable "APPLICATIONINSIGHTS_CONNECTION_STRING" is not set. */ @@ -213,7 +321,7 @@ private Set initStatsbeatFeatures() { } private StatsbeatConnectionString getStatsbeatConnectionString() { - return StatsbeatConnectionString.create(exportOptions.connectionString, null, null); + return StatsbeatConnectionString.create(connectionString, null, null); } private SpanDataMapper createSpanDataMapper(ConfigProperties configProperties) { @@ -234,8 +342,8 @@ private BiConsumer createDefaultsPopulator(C } private ConnectionString getConnectionString(ConfigProperties configProperties) { - if (exportOptions.connectionString != null) { - return exportOptions.connectionString; + if (connectionString != null) { + return connectionString; } ConnectionString connectionString = ConnectionString.parse(configProperties.getString(APPLICATIONINSIGHTS_CONNECTION_STRING)); @@ -244,47 +352,46 @@ private ConnectionString getConnectionString(ConfigProperties configProperties) private HttpPipeline createHttpPipeline() { - if (exportOptions.httpPipeline != null) { - if (exportOptions.credential != null) { + if (httpPipeline != null) { + if (credential != null) { throw LOGGER.logExceptionAsError( new IllegalStateException("'credential' is not supported when custom 'httpPipeline' is specified")); } - if (exportOptions.httpClient != null) { + if (httpClient != null) { throw LOGGER.logExceptionAsError( new IllegalStateException("'httpClient' is not supported when custom 'httpPipeline' is specified")); } - if (exportOptions.httpLogOptions != null) { + if (httpLogOptions != null) { throw LOGGER.logExceptionAsError(new IllegalStateException( "'httpLogOptions' is not supported when custom 'httpPipeline' is specified")); } - if (!exportOptions.httpPipelinePolicies.isEmpty()) { + if (!httpPipelinePolicies.isEmpty()) { throw LOGGER.logExceptionAsError(new IllegalStateException( "'httpPipelinePolicies' is not supported when custom 'httpPipeline' is specified")); } - if (exportOptions.clientOptions != null) { + if (clientOptions != null) { throw LOGGER.logExceptionAsError(new IllegalStateException( "'clientOptions' is not supported when custom 'httpPipeline' is specified")); } - return exportOptions.httpPipeline; + return httpPipeline; } List policies = new ArrayList<>(); String clientName = PROPERTIES.getOrDefault("name", "UnknownName"); String clientVersion = PROPERTIES.getOrDefault("version", "UnknownVersion"); - String applicationId = CoreUtils.getApplicationId(exportOptions.clientOptions, exportOptions.httpLogOptions); + String applicationId = CoreUtils.getApplicationId(clientOptions, httpLogOptions); policies .add(new UserAgentPolicy(applicationId, clientName, clientVersion, Configuration.getGlobalConfiguration())); policies.add(new CookiePolicy()); - if (exportOptions.credential != null) { - policies.add(new BearerTokenAuthenticationPolicy(exportOptions.credential, - APPLICATIONINSIGHTS_AUTHENTICATION_SCOPE)); + if (credential != null) { + policies.add(new BearerTokenAuthenticationPolicy(credential, APPLICATIONINSIGHTS_AUTHENTICATION_SCOPE)); } - policies.addAll(exportOptions.httpPipelinePolicies); - policies.add(new HttpLoggingPolicy(exportOptions.httpLogOptions)); + policies.addAll(httpPipelinePolicies); + policies.add(new HttpLoggingPolicy(httpLogOptions)); return new com.azure.core.http.HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])) - .httpClient(exportOptions.httpClient) + .httpClient(httpClient) .tracer(new NoopTracer()) .build(); } @@ -307,23 +414,23 @@ private void startStatsbeatModule(StatsbeatModule statsbeatModule, ConfigPropert } private HttpPipeline createStatsbeatHttpPipeline() { - if (exportOptions.httpPipeline != null) { - return exportOptions.httpPipeline; + if (httpPipeline != null) { + return httpPipeline; } List policies = new ArrayList<>(); String clientName = PROPERTIES.getOrDefault("name", "UnknownName"); String clientVersion = PROPERTIES.getOrDefault("version", "UnknownVersion"); - String applicationId = CoreUtils.getApplicationId(exportOptions.clientOptions, exportOptions.httpLogOptions); + String applicationId = CoreUtils.getApplicationId(clientOptions, httpLogOptions); policies .add(new UserAgentPolicy(applicationId, clientName, clientVersion, Configuration.getGlobalConfiguration())); policies.add(new CookiePolicy()); - policies.addAll(exportOptions.httpPipelinePolicies); - policies.add(new HttpLoggingPolicy(exportOptions.httpLogOptions)); + policies.addAll(httpPipelinePolicies); + policies.add(new HttpLoggingPolicy(httpLogOptions)); return new com.azure.core.http.HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])) - .httpClient(exportOptions.httpClient) + .httpClient(httpClient) .tracer(new NoopTracer()) .build(); } diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/ExportOptions.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/ExportOptions.java deleted file mode 100644 index 9d1b214169cf1..0000000000000 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/ExportOptions.java +++ /dev/null @@ -1,121 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -package com.azure.monitor.opentelemetry.exporter; - -import com.azure.core.annotation.Fluent; -import com.azure.core.credential.TokenCredential; -import com.azure.core.http.HttpClient; -import com.azure.core.http.HttpPipeline; -import com.azure.core.http.policy.HttpLogDetailLevel; -import com.azure.core.http.policy.HttpLogOptions; -import com.azure.core.http.policy.HttpPipelinePolicy; -import com.azure.core.util.ClientOptions; -import com.azure.monitor.opentelemetry.exporter.implementation.configuration.ConnectionString; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; - -/** - * The options to configure the Azure Monitor export. - */ -@Fluent -public final class ExportOptions { - - ConnectionString connectionString; - TokenCredential credential; - - HttpPipeline httpPipeline; - HttpClient httpClient; - HttpLogOptions httpLogOptions; - - ClientOptions clientOptions; - - final List httpPipelinePolicies = new ArrayList<>(); - - /** - * Sets the HTTP pipeline to use for the service client. If {@code httpPipeline} is set, all other - * settings are ignored. - * - * @param httpPipeline The HTTP pipeline to use for sending service requests and receiving - * responses. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public ExportOptions httpPipeline(HttpPipeline httpPipeline) { - this.httpPipeline = httpPipeline; - return this; - } - - /** - * Sets the HTTP client to use for sending and receiving requests to and from the service. - * - * @param httpClient The HTTP client to use for requests. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public ExportOptions httpClient(HttpClient httpClient) { - this.httpClient = httpClient; - return this; - } - - /** - * Sets the logging configuration for HTTP requests and responses. - * - *

If logLevel is not provided, default value of {@link HttpLogDetailLevel#NONE} is set. - * - * @param httpLogOptions The logging configuration to use when sending and receiving HTTP - * requests/responses. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public ExportOptions httpLogOptions(HttpLogOptions httpLogOptions) { - this.httpLogOptions = httpLogOptions; - return this; - } - - /** - * Adds a policy to the set of existing policies that are executed after required policies. - * - * @param httpPipelinePolicy a policy to be added to the http pipeline. - * @return The updated {@link AzureMonitorExporterBuilder} object. - * @throws NullPointerException If {@code policy} is {@code null}. - */ - public ExportOptions addHttpPipelinePolicy(HttpPipelinePolicy httpPipelinePolicy) { - httpPipelinePolicies.add(Objects.requireNonNull(httpPipelinePolicy, "'policy' cannot be null.")); - return this; - } - - /** - * Sets the client options such as application ID and custom headers to set on a request. - * - * @param clientOptions The client options. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public ExportOptions clientOptions(ClientOptions clientOptions) { - this.clientOptions = clientOptions; - return this; - } - - /** - * Sets the connection string to use for exporting telemetry events to Azure Monitor. - * - * @param connectionString The connection string for the Azure Monitor resource. - * @return The updated {@link AzureMonitorExporterBuilder} object. - * @throws NullPointerException If the connection string is {@code null}. - * @throws IllegalArgumentException If the connection string is invalid. - */ - public ExportOptions connectionString(String connectionString) { - this.connectionString = ConnectionString.parse(connectionString); - return this; - } - - /** - * Sets the token credential required for authentication with the ingestion endpoint service. - * - * @param credential The Azure Identity TokenCredential. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public ExportOptions credential(TokenCredential credential) { - this.credential = credential; - return this; - } -} diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java index 3e9167de49beb..d78e1ccbf3448 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java @@ -40,7 +40,7 @@ public static void main(String[] args) { private static Tracer configureAzureMonitorExporter() { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - OpenTelemetry openTelemetry = new AzureMonitor("{connection-string}") + OpenTelemetry openTelemetry = new AzureMonitor().connectionString("{connection-string}") .configure(sdkBuilder).build().getOpenTelemetrySdk(); return openTelemetry.getTracer("Sample"); 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 index bbcd7792d0e39..b4f23088e8dca 100644 --- 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 @@ -27,7 +27,7 @@ private static void sendDoubleHistogram() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - OpenTelemetry openTelemetry = new AzureMonitor(APPINSIGHTS_CONNECTION_STRING) + OpenTelemetry openTelemetry = new AzureMonitor().connectionString(APPINSIGHTS_CONNECTION_STRING) .configure(sdkBuilder).build().getOpenTelemetrySdk(); Meter meter = openTelemetry.meterBuilder("OTEL.AzureMonitor.Demo").build(); DoubleHistogram histogram = meter.histogramBuilder("histogram").build(); @@ -47,7 +47,7 @@ private static void sendLongCounter() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - OpenTelemetry openTelemetry = new AzureMonitor(APPINSIGHTS_CONNECTION_STRING).configure(sdkBuilder).build().getOpenTelemetrySdk(); + OpenTelemetry openTelemetry = new AzureMonitor().connectionString(APPINSIGHTS_CONNECTION_STRING).configure(sdkBuilder).build().getOpenTelemetrySdk(); Meter meter = openTelemetry.meterBuilder("OTEL.AzureMonitor.Demo").build(); LongCounter myFruitCounter = meter.counterBuilder("MyFruitCounter").build(); @@ -69,7 +69,7 @@ private static void sendGaugeMetric() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - OpenTelemetry openTelemetry = new AzureMonitor(APPINSIGHTS_CONNECTION_STRING) + OpenTelemetry openTelemetry = new AzureMonitor().connectionString(APPINSIGHTS_CONNECTION_STRING) .configure(sdkBuilder).build().getOpenTelemetrySdk(); Meter meter = openTelemetry.getMeter("OTEL.AzureMonitor.Demo"); diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java index 7558110c71e59..da9d751980fc3 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java @@ -51,7 +51,7 @@ public static void main(String[] args) { private static Tracer configureAzureMonitorExporter() { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - OpenTelemetry openTelemetry = new AzureMonitor("{connection-string}") + OpenTelemetry openTelemetry = new AzureMonitor().connectionString("{connection-string}") .configure(sdkBuilder).build().getOpenTelemetrySdk(); return openTelemetry.getTracer("Sample"); diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java index 74634f7ab0fc6..e24a87e9c152b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java @@ -55,7 +55,7 @@ public void setupExporter() { // This should be done just once when application starts up AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - OpenTelemetry openTelemetry = new AzureMonitor("{connection-string}") + OpenTelemetry openTelemetry = new AzureMonitor().connectionString("{connection-string}") .configure(sdkBuilder).build().getOpenTelemetrySdk(); Tracer tracer = openTelemetry.getTracer("Sample"); @@ -86,7 +86,7 @@ public void exporterAndOpenTelemetryAutoconfiguration() { // BEGIN: readme-sample-autoconfigure AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - OpenTelemetry openTelemetry = new AzureMonitor("{connection-string}") + OpenTelemetry openTelemetry = new AzureMonitor().connectionString("{connection-string}") .configure(sdkBuilder).build().getOpenTelemetrySdk(); // END: readme-sample-autoconfigure } @@ -99,7 +99,7 @@ public void createSpan() { // BEGIN: readme-sample-create-span AutoConfiguredOpenTelemetrySdkBuilder otelSdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - OpenTelemetry openTelemetry = new AzureMonitor("{connection-string}") + OpenTelemetry openTelemetry = new AzureMonitor().connectionString("{connection-string}") .configure(otelSdkBuilder).build().getOpenTelemetrySdk(); Tracer tracer = openTelemetry.getTracer("Sample"); diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java index 82ef1330c5dbf..023c422bacd14 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java @@ -23,7 +23,7 @@ public void testInvalidConnectionStrings(String con Class exceptionExpected) { Assertions.assertThrows(exceptionExpected, () -> { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - new AzureMonitor(connectionString).configure(sdkBuilder); + new AzureMonitor().connectionString(connectionString).configure(sdkBuilder); sdkBuilder.build(); }); } diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/MonitorExporterClientTestBase.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/MonitorExporterClientTestBase.java index ad59d2a40324c..d49cce40aff9f 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/MonitorExporterClientTestBase.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/MonitorExporterClientTestBase.java @@ -33,8 +33,7 @@ public class MonitorExporterClientTestBase extends TestProxyTestBase { AzureMonitorExporterBuilder getClientBuilder() { - ExportOptions exportOptions = new ExportOptions().httpPipeline(getHttpPipeline(null)); - return new AzureMonitorExporterBuilder(exportOptions); + return new AzureMonitorExporterBuilder().httpPipeline(getHttpPipeline(null)); } HttpPipeline getHttpPipeline(@Nullable HttpPipelinePolicy policy, HttpClient httpClient) { diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java index 1f702d42dff55..e1d83eef4caf3 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java @@ -8,7 +8,6 @@ import com.azure.json.JsonReader; import com.azure.json.JsonToken; import com.azure.monitor.opentelemetry.AzureMonitor; -import com.azure.monitor.opentelemetry.exporter.ExportOptions; import com.azure.monitor.opentelemetry.exporter.implementation.models.*; import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; @@ -83,9 +82,9 @@ public static OpenTelemetrySdk createOpenTelemetrySdk(HttpPipeline httpPipeline, String connectionString) { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - ExportOptions exportOptions = new ExportOptions().connectionString(connectionString).httpPipeline(httpPipeline); - - return new AzureMonitor(exportOptions).configure(sdkBuilder) + return new AzureMonitor().connectionString(connectionString) + .httpPipeline(httpPipeline) + .configure(sdkBuilder) .addPropertiesSupplier(() -> configuration) .build() .getOpenTelemetrySdk(); From 40c8739be2805a69fbb2f736f65e70657a1ac8ce Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Thu, 5 Sep 2024 15:18:00 +0200 Subject: [PATCH 16/53] Avoid start import --- .../exporter/AzureMonitorExporterBuilder.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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 97bcb6be1d7ba..8845ddac88916 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 @@ -4,7 +4,13 @@ import com.azure.core.credential.TokenCredential; import com.azure.core.http.HttpClient; import com.azure.core.http.HttpPipeline; -import com.azure.core.http.policy.*; +import com.azure.core.http.policy.BearerTokenAuthenticationPolicy; +import com.azure.core.http.policy.CookiePolicy; +import com.azure.core.http.policy.HttpLogDetailLevel; +import com.azure.core.http.policy.HttpLogOptions; +import com.azure.core.http.policy.HttpLoggingPolicy; +import com.azure.core.http.policy.HttpPipelinePolicy; +import com.azure.core.http.policy.UserAgentPolicy; import com.azure.core.util.ClientOptions; import com.azure.core.util.Configuration; import com.azure.core.util.CoreUtils; From 211f946fd299ebf409a10411349190506c9991a8 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Thu, 5 Sep 2024 15:31:51 +0200 Subject: [PATCH 17/53] Fix linting --- .../opentelemetry/exporter/AzureMonitorExporterBuilder.java | 3 +++ 1 file changed, 3 insertions(+) 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 8845ddac88916..d7332fdab9cf1 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 @@ -1,3 +1,6 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + package com.azure.monitor.opentelemetry.exporter; import com.azure.core.annotation.Fluent; From f2ca1f23aad2a6b37c50aaaaef8d651a64a562dd Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 11 Sep 2024 13:55:05 +0200 Subject: [PATCH 18/53] AzureMonitor.configure(otelSdkBuilder, AzureMonitor.exportOptions().connectionString("{connection-string}")); --- .../README.md | 9 +- .../monitor/opentelemetry/AzureMonitor.java | 207 +++++++++++------- ...nfigurationAzureMonitorExporterSample.java | 5 +- .../AzureMonitorMetricExporterSample.java | 14 +- .../EventHubsAzureMonitorExporterSample.java | 5 +- .../opentelemetry/exporter/ReadmeSamples.java | 17 +- .../AzureMonitorExporterBuilderTest.java | 2 +- .../implementation/utils/TestUtils.java | 6 +- 8 files changed, 167 insertions(+), 98 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index 40940916047ec..b01c7b92b5a60 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -50,8 +50,9 @@ The following example shows how create a span: ```java readme-sample-create-span AutoConfiguredOpenTelemetrySdkBuilder otelSdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); -OpenTelemetry openTelemetry = new AzureMonitor().connectionString("{connection-string}") - .configure(otelSdkBuilder).build().getOpenTelemetrySdk(); +AzureMonitor.configure(otelSdkBuilder, AzureMonitor.exportOptions().connectionString("{connection-string}")); + +OpenTelemetry openTelemetry = otelSdkBuilder.build().getOpenTelemetrySdk(); Tracer tracer = openTelemetry.getTracer("Sample"); Span span = tracer.spanBuilder("spanName").startSpan(); @@ -71,6 +72,8 @@ The following example demonstrates how to add a span processor to the OpenTeleme ```java readme-sample-span-processor AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); +AzureMonitor.configure(sdkBuilder); + SpanProcessor spanProcessor = new SpanProcessor() { @Override public void onStart(Context context, ReadWriteSpan span) { @@ -94,7 +97,7 @@ SpanProcessor spanProcessor = new SpanProcessor() { sdkBuilder.addTracerProviderCustomizer( (sdkTracerProviderBuilder, configProperties) -> sdkTracerProviderBuilder - .addSpanProcessor(spanProcessor)); + .addSpanProcessor(spanProcessor)); ``` More advanced examples with OpenTelemetry APIs: * [Advanced examples - 1][advanced_examples_1] diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java index 7893b58bfad1a..5fd0e818428bc 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java @@ -27,103 +27,133 @@ /** * Class to enable Azure Monitor for OpenTelemetry autoconfiguration. */ -@Fluent public final class AzureMonitor { - private final AzureMonitorExporterBuilder azureMonitorExporterBuilder = new AzureMonitorExporterBuilder(); - + @Fluent /** - * Sets the HTTP pipeline to use for the service client. If {@code httpPipeline} is set, all other - * settings are ignored. - * - * @param httpPipeline The HTTP pipeline to use for sending service requests and receiving - * responses. - * @return The updated {@link AzureMonitor} object. + * Options to configure the Azure Monitor export. */ - public AzureMonitor httpPipeline(HttpPipeline httpPipeline) { - azureMonitorExporterBuilder.httpPipeline(httpPipeline); - return this; - } + public static class ExportOptions { - /** - * Sets the HTTP client to use for sending and receiving requests to and from the service. - * - * @param httpClient The HTTP client to use for requests. - * @return The updated {@link AzureMonitor} object. - */ - public AzureMonitor httpClient(HttpClient httpClient) { - azureMonitorExporterBuilder.httpClient(httpClient); - return this; - } + private HttpPipeline httpPipeline; + private HttpClient httpClient; + private HttpLogOptions httpLogOptions; + private HttpPipelinePolicy httpPipelinePolicy; + private ClientOptions clientOptions; + private String connectionString; + private TokenCredential credential; - /** - * Sets the logging configuration for HTTP requests and responses. - * - *

If logLevel is not provided, default value of {@link HttpLogDetailLevel#NONE} is set. - * - * @param httpLogOptions The logging configuration to use when sending and receiving HTTP - * requests/responses. - * @return The updated {@link AzureMonitor} object. - */ - public AzureMonitor httpLogOptions(HttpLogOptions httpLogOptions) { - azureMonitorExporterBuilder.httpLogOptions(httpLogOptions); - return this; - } + private ExportOptions() {} - /** - * Adds a policy to the set of existing policies that are executed after required policies. - * - * @param httpPipelinePolicy a policy to be added to the http pipeline. - * @return The updated {@link AzureMonitorExporterBuilder} object. - * @throws NullPointerException If {@code policy} is {@code null}. - */ - public AzureMonitor addHttpPipelinePolicy(HttpPipelinePolicy httpPipelinePolicy) { - azureMonitorExporterBuilder.addHttpPipelinePolicy(httpPipelinePolicy); - return this; - } + /** + * Sets the HTTP pipeline to use for the service client. If {@code httpPipeline} is set, all other + * settings are ignored. + * + * @param httpPipeline The HTTP pipeline to use for sending service requests and receiving + * responses. + * @return The updated {@link AzureMonitor} object. + */ + public ExportOptions httpPipeline(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + return this; + } - /** - * Sets the client options such as application ID and custom headers to set on a request. - * - * @param clientOptions The client options. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public AzureMonitor clientOptions(ClientOptions clientOptions) { - azureMonitorExporterBuilder.clientOptions(clientOptions); - return this; + /** + * Sets the HTTP client to use for sending and receiving requests to and from the service. + * + * @param httpClient The HTTP client to use for requests. + * @return The updated {@link AzureMonitor} object. + */ + public ExportOptions httpClient(HttpClient httpClient) { + this.httpClient = httpClient; + return this; + } + + /** + * Sets the logging configuration for HTTP requests and responses. + * + *

If logLevel is not provided, default value of {@link HttpLogDetailLevel#NONE} is set. + * + * @param httpLogOptions The logging configuration to use when sending and receiving HTTP + * requests/responses. + * @return The updated {@link AzureMonitor} object. + */ + public ExportOptions httpLogOptions(HttpLogOptions httpLogOptions) { + this.httpLogOptions = httpLogOptions; + return this; + } + + /** + * Adds a policy to the set of existing policies that are executed after required policies. + * + * @param httpPipelinePolicy a policy to be added to the http pipeline. + * @return The updated {@link AzureMonitorExporterBuilder} object. + * @throws NullPointerException If {@code policy} is {@code null}. + */ + public ExportOptions addHttpPipelinePolicy(HttpPipelinePolicy httpPipelinePolicy) { + this.httpPipelinePolicy = httpPipelinePolicy; + return this; + } + + /** + * Sets the client options such as application ID and custom headers to set on a request. + * + * @param clientOptions The client options. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + public ExportOptions clientOptions(ClientOptions clientOptions) { + this.clientOptions = clientOptions; + return this; + } + + /** + * Sets the connection string to use for exporting telemetry events to Azure Monitor. + * + * @param connectionString The connection string for the Azure Monitor resource. + * @return The updated {@link AzureMonitorExporterBuilder} object. + * @throws NullPointerException If the connection string is {@code null}. + * @throws IllegalArgumentException If the connection string is invalid. + */ + public ExportOptions connectionString(String connectionString) { + this.connectionString = connectionString; + return this; + } + + /** + * Sets the token credential required for authentication with the ingestion endpoint service. + * + * @param credential The Azure Identity TokenCredential. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + public ExportOptions credential(TokenCredential credential) { + this.credential = credential; + return this; + } } /** - * Sets the connection string to use for exporting telemetry events to Azure Monitor. - * - * @param connectionString The connection string for the Azure Monitor resource. - * @return The updated {@link AzureMonitorExporterBuilder} object. - * @throws NullPointerException If the connection string is {@code null}. - * @throws IllegalArgumentException If the connection string is invalid. + * Creeate export options. + * @return the export options. */ - public AzureMonitor connectionString(String connectionString) { - azureMonitorExporterBuilder.connectionString(connectionString); - return this; + public static ExportOptions exportOptions() { + return new ExportOptions(); } /** - * Sets the token credential required for authentication with the ingestion endpoint service. - * - * @param credential The Azure Identity TokenCredential. - * @return The updated {@link AzureMonitorExporterBuilder} object. + * Configures an {@link AutoConfiguredOpenTelemetrySdkBuilder} for Azure Monitor based on the options set. */ - public AzureMonitor credential(TokenCredential credential) { - azureMonitorExporterBuilder.credential(credential); - return this; + public static void configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfiguredOpenTelemetrySdkBuilder) { + configure(autoConfiguredOpenTelemetrySdkBuilder, new ExportOptions()); } /** * Configures an {@link AutoConfiguredOpenTelemetrySdkBuilder} for Azure Monitor based on the options set. * @param autoConfiguredOpenTelemetrySdkBuilder the {@link AutoConfiguredOpenTelemetrySdkBuilder} object. - * @return the {@link AutoConfiguredOpenTelemetrySdkBuilder} object given in argument. */ - public AutoConfiguredOpenTelemetrySdkBuilder - configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfiguredOpenTelemetrySdkBuilder) { + public static void configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfiguredOpenTelemetrySdkBuilder, ExportOptions exportOptions) { + + AzureMonitorExporterBuilder azureMonitorExporterBuilder = createExporterBuilder(exportOptions); + autoConfiguredOpenTelemetrySdkBuilder.addPropertiesSupplier(() -> { Map props = new HashMap<>(); props.put("otel.traces.exporter", AzureMonitorExporterProviderKeys.EXPORTER_NAME); @@ -161,6 +191,31 @@ public AzureMonitor credential(TokenCredential credential) { View.builder().setAggregation(Aggregation.drop()).build()) .registerView(InstrumentSelector.builder().setMeterName("io.opentelemetry.sdk.logs").build(), View.builder().setAggregation(Aggregation.drop()).build())); - return autoConfiguredOpenTelemetrySdkBuilder; + } + + private static AzureMonitorExporterBuilder createExporterBuilder(ExportOptions exportOptions) { + AzureMonitorExporterBuilder azureMonitorExporterBuilder = new AzureMonitorExporterBuilder(); + if(exportOptions.httpPipeline != null) { + azureMonitorExporterBuilder.httpPipeline(exportOptions.httpPipeline); + } + if(exportOptions.httpClient != null) { + azureMonitorExporterBuilder.httpClient(exportOptions.httpClient); + } + if(exportOptions.httpLogOptions != null) { + azureMonitorExporterBuilder.httpLogOptions(exportOptions.httpLogOptions); + } + if(exportOptions.httpPipelinePolicy != null) { + azureMonitorExporterBuilder.addHttpPipelinePolicy(exportOptions.httpPipelinePolicy); + } + if(exportOptions.clientOptions != null) { + azureMonitorExporterBuilder.clientOptions(exportOptions.clientOptions); + } + if(exportOptions.connectionString != null) { + azureMonitorExporterBuilder.connectionString(exportOptions.connectionString); + } + if(exportOptions.credential != null) { + azureMonitorExporterBuilder.credential(exportOptions.credential); + } + return azureMonitorExporterBuilder; } } diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java index d78e1ccbf3448..a1808e4e98a2b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java @@ -40,8 +40,9 @@ public static void main(String[] args) { private static Tracer configureAzureMonitorExporter() { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - OpenTelemetry openTelemetry = new AzureMonitor().connectionString("{connection-string}") - .configure(sdkBuilder).build().getOpenTelemetrySdk(); + AzureMonitor.configure(sdkBuilder, AzureMonitor.exportOptions().connectionString("{connection-string}")); + + OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); return openTelemetry.getTracer("Sample"); } 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 index b4f23088e8dca..70d6694b12eb7 100644 --- 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 @@ -27,8 +27,9 @@ private static void sendDoubleHistogram() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - OpenTelemetry openTelemetry = new AzureMonitor().connectionString(APPINSIGHTS_CONNECTION_STRING) - .configure(sdkBuilder).build().getOpenTelemetrySdk(); + AzureMonitor.configure(sdkBuilder, AzureMonitor.exportOptions().connectionString(APPINSIGHTS_CONNECTION_STRING)); + + OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); Meter meter = openTelemetry.meterBuilder("OTEL.AzureMonitor.Demo").build(); DoubleHistogram histogram = meter.histogramBuilder("histogram").build(); @@ -47,7 +48,9 @@ private static void sendLongCounter() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - OpenTelemetry openTelemetry = new AzureMonitor().connectionString(APPINSIGHTS_CONNECTION_STRING).configure(sdkBuilder).build().getOpenTelemetrySdk(); + AzureMonitor.configure(sdkBuilder, AzureMonitor.exportOptions().connectionString(APPINSIGHTS_CONNECTION_STRING)); + + OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); Meter meter = openTelemetry.meterBuilder("OTEL.AzureMonitor.Demo").build(); LongCounter myFruitCounter = meter.counterBuilder("MyFruitCounter").build(); @@ -69,8 +72,9 @@ private static void sendGaugeMetric() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - OpenTelemetry openTelemetry = new AzureMonitor().connectionString(APPINSIGHTS_CONNECTION_STRING) - .configure(sdkBuilder).build().getOpenTelemetrySdk(); + AzureMonitor.configure(sdkBuilder, AzureMonitor.exportOptions().connectionString(APPINSIGHTS_CONNECTION_STRING)); + + OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); Meter meter = openTelemetry.getMeter("OTEL.AzureMonitor.Demo"); meter.gaugeBuilder("gauge") diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java index da9d751980fc3..a666294a33d23 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java @@ -51,8 +51,9 @@ public static void main(String[] args) { private static Tracer configureAzureMonitorExporter() { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - OpenTelemetry openTelemetry = new AzureMonitor().connectionString("{connection-string}") - .configure(sdkBuilder).build().getOpenTelemetrySdk(); + AzureMonitor.configure(sdkBuilder, AzureMonitor.exportOptions().connectionString("{connection-string}")); + + OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); return openTelemetry.getTracer("Sample"); } diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java index e24a87e9c152b..f1009a8b78a66 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java @@ -55,8 +55,9 @@ public void setupExporter() { // This should be done just once when application starts up AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - OpenTelemetry openTelemetry = new AzureMonitor().connectionString("{connection-string}") - .configure(sdkBuilder).build().getOpenTelemetrySdk(); + AzureMonitor.configure(sdkBuilder, AzureMonitor.exportOptions().connectionString("{connection-string}")); + + OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); Tracer tracer = openTelemetry.getTracer("Sample"); // END: readme-sample-setupExporter @@ -86,8 +87,9 @@ public void exporterAndOpenTelemetryAutoconfiguration() { // BEGIN: readme-sample-autoconfigure AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - OpenTelemetry openTelemetry = new AzureMonitor().connectionString("{connection-string}") - .configure(sdkBuilder).build().getOpenTelemetrySdk(); + AzureMonitor.configure(sdkBuilder, AzureMonitor.exportOptions().connectionString("{connection-string}")); + + OpenTelemetry openTelemetry =sdkBuilder.build().getOpenTelemetrySdk(); // END: readme-sample-autoconfigure } @@ -99,8 +101,9 @@ public void createSpan() { // BEGIN: readme-sample-create-span AutoConfiguredOpenTelemetrySdkBuilder otelSdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - OpenTelemetry openTelemetry = new AzureMonitor().connectionString("{connection-string}") - .configure(otelSdkBuilder).build().getOpenTelemetrySdk(); + AzureMonitor.configure(otelSdkBuilder, AzureMonitor.exportOptions().connectionString("{connection-string}")); + + OpenTelemetry openTelemetry = otelSdkBuilder.build().getOpenTelemetrySdk(); Tracer tracer = openTelemetry.getTracer("Sample"); Span span = tracer.spanBuilder("spanName").startSpan(); @@ -124,6 +127,8 @@ public void spanProcessor() { // BEGIN: readme-sample-span-processor AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); + AzureMonitor.configure(sdkBuilder); + SpanProcessor spanProcessor = new SpanProcessor() { @Override public void onStart(Context context, ReadWriteSpan span) { diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java index 023c422bacd14..e9a0d8f527a9e 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java @@ -23,7 +23,7 @@ public void testInvalidConnectionStrings(String con Class exceptionExpected) { Assertions.assertThrows(exceptionExpected, () -> { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - new AzureMonitor().connectionString(connectionString).configure(sdkBuilder); + AzureMonitor.configure(sdkBuilder, AzureMonitor.exportOptions().connectionString(connectionString)); sdkBuilder.build(); }); } diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java index e1d83eef4caf3..d610bbb15db6d 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java @@ -82,9 +82,9 @@ public static OpenTelemetrySdk createOpenTelemetrySdk(HttpPipeline httpPipeline, String connectionString) { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - return new AzureMonitor().connectionString(connectionString) - .httpPipeline(httpPipeline) - .configure(sdkBuilder) + AzureMonitor.configure(sdkBuilder, AzureMonitor.exportOptions().connectionString(connectionString).httpPipeline(httpPipeline)); + + return sdkBuilder .addPropertiesSupplier(() -> configuration) .build() .getOpenTelemetrySdk(); From 3b82a5935ed5468d358c25f37df355caa6a121fc Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 11 Sep 2024 15:26:13 +0200 Subject: [PATCH 19/53] ExportOptions exportOptions = new ExportOptions().connectionString("{connection-string}"); AzureMonitor.configure(otelSdkBuilder, exportOptions); --- .../README.md | 48 ++--- .../monitor/opentelemetry/AzureMonitor.java | 152 +-------------- .../exporter/AzureMonitorExporterBuilder.java | 176 ++++-------------- .../opentelemetry/exporter/ExportOptions.java | 149 +++++++++++++++ ...nfigurationAzureMonitorExporterSample.java | 3 +- .../AzureMonitorMetricExporterSample.java | 9 +- .../EventHubsAzureMonitorExporterSample.java | 3 +- .../opentelemetry/exporter/ReadmeSamples.java | 11 +- .../AzureMonitorExporterBuilderTest.java | 3 +- .../MonitorExporterClientTestBase.java | 2 +- .../implementation/utils/TestUtils.java | 9 +- 11 files changed, 224 insertions(+), 341 deletions(-) create mode 100644 sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/ExportOptions.java diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index b01c7b92b5a60..89a66954a9241 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -36,8 +36,10 @@ The following code shows how to configure the OpenTelemetry SDK auto-configurati ```java readme-sample-autoconfigure AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); -OpenTelemetry openTelemetry = new AzureMonitor().connectionString("{connection-string}") - .configure(sdkBuilder).build().getOpenTelemetrySdk(); +ExportOptions exportOptions = new ExportOptions().connectionString("{connection-string}"); +AzureMonitor.configure(sdkBuilder, exportOptions); + +OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); ``` ## Examples @@ -48,9 +50,17 @@ The following sections provide code samples using the OpenTelemetry Azure Monito The following example shows how create a span: ```java readme-sample-create-span + + + +``` +The following example demonstrates how to add a span processor to the OpenTelemetry SDK autoconfiguration. + +```java readme-sample-span-processor AutoConfiguredOpenTelemetrySdkBuilder otelSdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); -AzureMonitor.configure(otelSdkBuilder, AzureMonitor.exportOptions().connectionString("{connection-string}")); +ExportOptions exportOptions = new ExportOptions().connectionString("{connection-string}"); +AzureMonitor.configure(otelSdkBuilder, exportOptions); OpenTelemetry openTelemetry = otelSdkBuilder.build().getOpenTelemetrySdk(); Tracer tracer = openTelemetry.getTracer("Sample"); @@ -67,38 +77,6 @@ try (Scope scope = span.makeCurrent()) { span.end(); } ``` -The following example demonstrates how to add a span processor to the OpenTelemetry SDK autoconfiguration. - -```java readme-sample-span-processor -AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - -AzureMonitor.configure(sdkBuilder); - -SpanProcessor spanProcessor = new SpanProcessor() { - @Override - public void onStart(Context context, ReadWriteSpan span) { - span.setAttribute(AttributeKey.stringKey("random"), RandomStringUtils.random(10)); - } - - @Override - public boolean isStartRequired() { - return true; - } - - @Override - public void onEnd(ReadableSpan readableSpan) { - } - - @Override - public boolean isEndRequired() { - return false; - } -}; - -sdkBuilder.addTracerProviderCustomizer( - (sdkTracerProviderBuilder, configProperties) -> sdkTracerProviderBuilder - .addSpanProcessor(spanProcessor)); -``` More advanced examples with OpenTelemetry APIs: * [Advanced examples - 1][advanced_examples_1] * [Advanced examples - 2][advanced_examples_2] diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java index 5fd0e818428bc..95cbe0ee9f6b8 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java @@ -3,15 +3,8 @@ package com.azure.monitor.opentelemetry; -import com.azure.core.annotation.Fluent; -import com.azure.core.credential.TokenCredential; -import com.azure.core.http.HttpClient; -import com.azure.core.http.HttpPipeline; -import com.azure.core.http.policy.HttpLogDetailLevel; -import com.azure.core.http.policy.HttpLogOptions; -import com.azure.core.http.policy.HttpPipelinePolicy; -import com.azure.core.util.ClientOptions; import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterBuilder; +import com.azure.monitor.opentelemetry.exporter.ExportOptions; import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorExporterProviderKeys; import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorLogRecordExporterProvider; import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorMetricExporterProvider; @@ -29,118 +22,9 @@ */ public final class AzureMonitor { - @Fluent - /** - * Options to configure the Azure Monitor export. - */ - public static class ExportOptions { - - private HttpPipeline httpPipeline; - private HttpClient httpClient; - private HttpLogOptions httpLogOptions; - private HttpPipelinePolicy httpPipelinePolicy; - private ClientOptions clientOptions; - private String connectionString; - private TokenCredential credential; - - private ExportOptions() {} - - /** - * Sets the HTTP pipeline to use for the service client. If {@code httpPipeline} is set, all other - * settings are ignored. - * - * @param httpPipeline The HTTP pipeline to use for sending service requests and receiving - * responses. - * @return The updated {@link AzureMonitor} object. - */ - public ExportOptions httpPipeline(HttpPipeline httpPipeline) { - this.httpPipeline = httpPipeline; - return this; - } - - /** - * Sets the HTTP client to use for sending and receiving requests to and from the service. - * - * @param httpClient The HTTP client to use for requests. - * @return The updated {@link AzureMonitor} object. - */ - public ExportOptions httpClient(HttpClient httpClient) { - this.httpClient = httpClient; - return this; - } - - /** - * Sets the logging configuration for HTTP requests and responses. - * - *

If logLevel is not provided, default value of {@link HttpLogDetailLevel#NONE} is set. - * - * @param httpLogOptions The logging configuration to use when sending and receiving HTTP - * requests/responses. - * @return The updated {@link AzureMonitor} object. - */ - public ExportOptions httpLogOptions(HttpLogOptions httpLogOptions) { - this.httpLogOptions = httpLogOptions; - return this; - } - - /** - * Adds a policy to the set of existing policies that are executed after required policies. - * - * @param httpPipelinePolicy a policy to be added to the http pipeline. - * @return The updated {@link AzureMonitorExporterBuilder} object. - * @throws NullPointerException If {@code policy} is {@code null}. - */ - public ExportOptions addHttpPipelinePolicy(HttpPipelinePolicy httpPipelinePolicy) { - this.httpPipelinePolicy = httpPipelinePolicy; - return this; - } - - /** - * Sets the client options such as application ID and custom headers to set on a request. - * - * @param clientOptions The client options. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public ExportOptions clientOptions(ClientOptions clientOptions) { - this.clientOptions = clientOptions; - return this; - } - - /** - * Sets the connection string to use for exporting telemetry events to Azure Monitor. - * - * @param connectionString The connection string for the Azure Monitor resource. - * @return The updated {@link AzureMonitorExporterBuilder} object. - * @throws NullPointerException If the connection string is {@code null}. - * @throws IllegalArgumentException If the connection string is invalid. - */ - public ExportOptions connectionString(String connectionString) { - this.connectionString = connectionString; - return this; - } - - /** - * Sets the token credential required for authentication with the ingestion endpoint service. - * - * @param credential The Azure Identity TokenCredential. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public ExportOptions credential(TokenCredential credential) { - this.credential = credential; - return this; - } - } - - /** - * Creeate export options. - * @return the export options. - */ - public static ExportOptions exportOptions() { - return new ExportOptions(); - } - /** * Configures an {@link AutoConfiguredOpenTelemetrySdkBuilder} for Azure Monitor based on the options set. + * @param autoConfiguredOpenTelemetrySdkBuilder The OpenTelemetry autoconfiguration to set up. */ public static void configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfiguredOpenTelemetrySdkBuilder) { configure(autoConfiguredOpenTelemetrySdkBuilder, new ExportOptions()); @@ -149,10 +33,12 @@ public static void configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfigure /** * Configures an {@link AutoConfiguredOpenTelemetrySdkBuilder} for Azure Monitor based on the options set. * @param autoConfiguredOpenTelemetrySdkBuilder the {@link AutoConfiguredOpenTelemetrySdkBuilder} object. + * @param exportOptions Export options to Azure Monitor. */ - public static void configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfiguredOpenTelemetrySdkBuilder, ExportOptions exportOptions) { + public static void configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfiguredOpenTelemetrySdkBuilder, + ExportOptions exportOptions) { - AzureMonitorExporterBuilder azureMonitorExporterBuilder = createExporterBuilder(exportOptions); + AzureMonitorExporterBuilder azureMonitorExporterBuilder = new AzureMonitorExporterBuilder(exportOptions); autoConfiguredOpenTelemetrySdkBuilder.addPropertiesSupplier(() -> { Map props = new HashMap<>(); @@ -192,30 +78,4 @@ public static void configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfigure .registerView(InstrumentSelector.builder().setMeterName("io.opentelemetry.sdk.logs").build(), View.builder().setAggregation(Aggregation.drop()).build())); } - - private static AzureMonitorExporterBuilder createExporterBuilder(ExportOptions exportOptions) { - AzureMonitorExporterBuilder azureMonitorExporterBuilder = new AzureMonitorExporterBuilder(); - if(exportOptions.httpPipeline != null) { - azureMonitorExporterBuilder.httpPipeline(exportOptions.httpPipeline); - } - if(exportOptions.httpClient != null) { - azureMonitorExporterBuilder.httpClient(exportOptions.httpClient); - } - if(exportOptions.httpLogOptions != null) { - azureMonitorExporterBuilder.httpLogOptions(exportOptions.httpLogOptions); - } - if(exportOptions.httpPipelinePolicy != null) { - azureMonitorExporterBuilder.addHttpPipelinePolicy(exportOptions.httpPipelinePolicy); - } - if(exportOptions.clientOptions != null) { - azureMonitorExporterBuilder.clientOptions(exportOptions.clientOptions); - } - if(exportOptions.connectionString != null) { - azureMonitorExporterBuilder.connectionString(exportOptions.connectionString); - } - if(exportOptions.credential != null) { - azureMonitorExporterBuilder.credential(exportOptions.credential); - } - return azureMonitorExporterBuilder; - } } 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 d7332fdab9cf1..2427a603909d2 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 @@ -75,140 +75,27 @@ public final class AzureMonitorExporterBuilder { private static final Map PROPERTIES = CoreUtils.getProperties("azure-monitor-opentelemetry-exporter.properties"); - private ConnectionString connectionString; - private TokenCredential credential; - - private HttpPipeline httpPipeline; - private HttpClient httpClient; - private HttpLogOptions httpLogOptions; - private final List httpPipelinePolicies = new ArrayList<>(); - private ClientOptions clientOptions; - - private boolean frozen; - // this is only populated after the builder is frozen private TelemetryItemExporter builtTelemetryItemExporter; // this is only populated after the builder is frozen private StatsbeatModule statsbeatModule; + private final ExportOptions exportOptions; + /** * Creates an instance of {@link AzureMonitorExporterBuilder}. */ public AzureMonitorExporterBuilder() { + this.exportOptions = new ExportOptions(); } /** - * Sets the HTTP pipeline to use for the service client. If {@code httpPipeline} is set, all other - * settings are ignored. - * - * @param httpPipeline The HTTP pipeline to use for sending service requests and receiving - * responses. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public AzureMonitorExporterBuilder httpPipeline(HttpPipeline httpPipeline) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "httpPipeline cannot be changed after any of the build methods have been called")); - } - this.httpPipeline = httpPipeline; - return this; - } - - /** - * Sets the HTTP client to use for sending and receiving requests to and from the service. - * - * @param httpClient The HTTP client to use for requests. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public AzureMonitorExporterBuilder httpClient(HttpClient httpClient) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "httpClient cannot be changed after any of the build methods have been called")); - } - this.httpClient = httpClient; - return this; - } - - /** - * Sets the logging configuration for HTTP requests and responses. - * - *

If logLevel is not provided, default value of {@link HttpLogDetailLevel#NONE} is set. - * - * @param httpLogOptions The logging configuration to use when sending and receiving HTTP - * requests/responses. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public AzureMonitorExporterBuilder httpLogOptions(HttpLogOptions httpLogOptions) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "httpLogOptions cannot be changed after any of the build methods have been called")); - } - this.httpLogOptions = httpLogOptions; - return this; - } - - /** - * Adds a policy to the set of existing policies that are executed after required policies. - * - * @param httpPipelinePolicy a policy to be added to the http pipeline. - * @return The updated {@link AzureMonitorExporterBuilder} object. - * @throws NullPointerException If {@code policy} is {@code null}. - */ - public AzureMonitorExporterBuilder addHttpPipelinePolicy(HttpPipelinePolicy httpPipelinePolicy) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "httpPipelinePolicies cannot be added after any of the build methods have been called")); - } - httpPipelinePolicies.add(Objects.requireNonNull(httpPipelinePolicy, "'policy' cannot be null.")); - return this; - } - - /** - * Sets the client options such as application ID and custom headers to set on a request. - * - * @param clientOptions The client options. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public AzureMonitorExporterBuilder clientOptions(ClientOptions clientOptions) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "clientOptions cannot be changed after any of the build methods have been called")); - } - this.clientOptions = clientOptions; - return this; - } - - /** - * Sets the connection string to use for exporting telemetry events to Azure Monitor. - * - * @param connectionString The connection string for the Azure Monitor resource. - * @return The updated {@link AzureMonitorExporterBuilder} object. - * @throws NullPointerException If the connection string is {@code null}. - * @throws IllegalArgumentException If the connection string is invalid. - */ - public AzureMonitorExporterBuilder connectionString(String connectionString) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "connectionString cannot be changed after any of the build methods have been called")); - } - this.connectionString = ConnectionString.parse(connectionString); - return this; - } - - /** - * Sets the token credential required for authentication with the ingestion endpoint service. - * - * @param credential The Azure Identity TokenCredential. - * @return The updated {@link AzureMonitorExporterBuilder} object. + * Creates an instance of {@link AzureMonitorExporterBuilder}. + * @param exportOptions Export options to Azure Monitor. */ - public AzureMonitorExporterBuilder credential(TokenCredential credential) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "credential cannot be changed after any of the build methods have been called")); - } - this.credential = credential; - return this; + public AzureMonitorExporterBuilder(ExportOptions exportOptions) { + this.exportOptions = exportOptions; } /** @@ -272,7 +159,7 @@ public LogRecordExporter buildLogRecordExporter(ConfigProperties configPropertie // the new ConfigProperties will not get applied to StatsbeatModule because of "frozen" guard. Luckily, we're using the config properties // in StatsbeatModule for testing only. We might need to revisit this approach later. void internalBuildAndFreeze(ConfigProperties configProperties) { - if (!frozen) { + if (!exportOptions.frozen) { HttpPipeline httpPipeline = createHttpPipeline(); statsbeatModule = initStatsbeatModule(configProperties); File tempDir = TempDirs.getApplicationInsightsTempDir(LOGGER, @@ -281,7 +168,7 @@ void internalBuildAndFreeze(ConfigProperties configProperties) { builtTelemetryItemExporter = AzureMonitorHelper.createTelemetryItemExporter(httpPipeline, statsbeatModule, tempDir, LocalStorageStats.noop()); startStatsbeatModule(statsbeatModule, configProperties, tempDir); // wait till TelemetryItemExporter has been initialized before starting StatsbeatModule - frozen = true; + exportOptions.frozen = true; } } @@ -330,7 +217,7 @@ private Set initStatsbeatFeatures() { } private StatsbeatConnectionString getStatsbeatConnectionString() { - return StatsbeatConnectionString.create(connectionString, null, null); + return StatsbeatConnectionString.create(exportOptions.connectionString, null, null); } private SpanDataMapper createSpanDataMapper(ConfigProperties configProperties) { @@ -351,8 +238,8 @@ private BiConsumer createDefaultsPopulator(C } private ConnectionString getConnectionString(ConfigProperties configProperties) { - if (connectionString != null) { - return connectionString; + if (exportOptions.connectionString != null) { + return exportOptions.connectionString; } ConnectionString connectionString = ConnectionString.parse(configProperties.getString(APPLICATIONINSIGHTS_CONNECTION_STRING)); @@ -361,46 +248,47 @@ private ConnectionString getConnectionString(ConfigProperties configProperties) private HttpPipeline createHttpPipeline() { - if (httpPipeline != null) { - if (credential != null) { + if (exportOptions.httpPipeline != null) { + if (exportOptions.credential != null) { throw LOGGER.logExceptionAsError( new IllegalStateException("'credential' is not supported when custom 'httpPipeline' is specified")); } - if (httpClient != null) { + if (exportOptions.httpClient != null) { throw LOGGER.logExceptionAsError( new IllegalStateException("'httpClient' is not supported when custom 'httpPipeline' is specified")); } - if (httpLogOptions != null) { + if (exportOptions.httpLogOptions != null) { throw LOGGER.logExceptionAsError(new IllegalStateException( "'httpLogOptions' is not supported when custom 'httpPipeline' is specified")); } - if (!httpPipelinePolicies.isEmpty()) { + if (!exportOptions.httpPipelinePolicies.isEmpty()) { throw LOGGER.logExceptionAsError(new IllegalStateException( "'httpPipelinePolicies' is not supported when custom 'httpPipeline' is specified")); } - if (clientOptions != null) { + if (exportOptions.clientOptions != null) { throw LOGGER.logExceptionAsError(new IllegalStateException( "'clientOptions' is not supported when custom 'httpPipeline' is specified")); } - return httpPipeline; + return exportOptions.httpPipeline; } List policies = new ArrayList<>(); String clientName = PROPERTIES.getOrDefault("name", "UnknownName"); String clientVersion = PROPERTIES.getOrDefault("version", "UnknownVersion"); - String applicationId = CoreUtils.getApplicationId(clientOptions, httpLogOptions); + String applicationId = CoreUtils.getApplicationId(exportOptions.clientOptions, exportOptions.httpLogOptions); policies .add(new UserAgentPolicy(applicationId, clientName, clientVersion, Configuration.getGlobalConfiguration())); policies.add(new CookiePolicy()); - if (credential != null) { - policies.add(new BearerTokenAuthenticationPolicy(credential, APPLICATIONINSIGHTS_AUTHENTICATION_SCOPE)); + if (exportOptions.credential != null) { + policies.add(new BearerTokenAuthenticationPolicy(exportOptions.credential, + APPLICATIONINSIGHTS_AUTHENTICATION_SCOPE)); } - policies.addAll(httpPipelinePolicies); - policies.add(new HttpLoggingPolicy(httpLogOptions)); + policies.addAll(exportOptions.httpPipelinePolicies); + policies.add(new HttpLoggingPolicy(exportOptions.httpLogOptions)); return new com.azure.core.http.HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])) - .httpClient(httpClient) + .httpClient(exportOptions.httpClient) .tracer(new NoopTracer()) .build(); } @@ -423,23 +311,23 @@ private void startStatsbeatModule(StatsbeatModule statsbeatModule, ConfigPropert } private HttpPipeline createStatsbeatHttpPipeline() { - if (httpPipeline != null) { - return httpPipeline; + if (exportOptions.httpPipeline != null) { + return exportOptions.httpPipeline; } List policies = new ArrayList<>(); String clientName = PROPERTIES.getOrDefault("name", "UnknownName"); String clientVersion = PROPERTIES.getOrDefault("version", "UnknownVersion"); - String applicationId = CoreUtils.getApplicationId(clientOptions, httpLogOptions); + String applicationId = CoreUtils.getApplicationId(exportOptions.clientOptions, exportOptions.httpLogOptions); policies .add(new UserAgentPolicy(applicationId, clientName, clientVersion, Configuration.getGlobalConfiguration())); policies.add(new CookiePolicy()); - policies.addAll(httpPipelinePolicies); - policies.add(new HttpLoggingPolicy(httpLogOptions)); + policies.addAll(exportOptions.httpPipelinePolicies); + policies.add(new HttpLoggingPolicy(exportOptions.httpLogOptions)); return new com.azure.core.http.HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])) - .httpClient(httpClient) + .httpClient(exportOptions.httpClient) .tracer(new NoopTracer()) .build(); } diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/ExportOptions.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/ExportOptions.java new file mode 100644 index 0000000000000..505cade5fea80 --- /dev/null +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/ExportOptions.java @@ -0,0 +1,149 @@ +package com.azure.monitor.opentelemetry.exporter; + +import com.azure.core.annotation.Fluent; +import com.azure.core.credential.TokenCredential; +import com.azure.core.http.HttpClient; +import com.azure.core.http.HttpPipeline; +import com.azure.core.http.policy.HttpLogDetailLevel; +import com.azure.core.http.policy.HttpLogOptions; +import com.azure.core.http.policy.HttpPipelinePolicy; +import com.azure.core.util.ClientOptions; +import com.azure.core.util.logging.ClientLogger; +import com.azure.monitor.opentelemetry.AzureMonitor; +import com.azure.monitor.opentelemetry.exporter.implementation.configuration.ConnectionString; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** + * Options to configure the Azure Monitor export. + */ +@Fluent +public class ExportOptions { + + private static final ClientLogger LOGGER = new ClientLogger(ExportOptions.class); + + HttpPipeline httpPipeline; + HttpClient httpClient; + HttpLogOptions httpLogOptions; + final List httpPipelinePolicies = new ArrayList<>(); + ClientOptions clientOptions; + ConnectionString connectionString; + TokenCredential credential; + + boolean frozen; + + /** + * Sets the HTTP pipeline to use for the service client. If {@code httpPipeline} is set, all other + * settings are ignored. + * + * @param httpPipeline The HTTP pipeline to use for sending service requests and receiving + * responses. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + public ExportOptions httpPipeline(HttpPipeline httpPipeline) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "httpPipeline cannot be changed after any of the build methods have been called")); + } + this.httpPipeline = httpPipeline; + return this; + } + + /** + * Sets the HTTP client to use for sending and receiving requests to and from the service. + * + * @param httpClient The HTTP client to use for requests. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + public ExportOptions httpClient(HttpClient httpClient) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "httpClient cannot be changed after any of the build methods have been called")); + } + this.httpClient = httpClient; + return this; + } + + /** + * Sets the logging configuration for HTTP requests and responses. + * + *

If logLevel is not provided, default value of {@link HttpLogDetailLevel#NONE} is set. + * + * @param httpLogOptions The logging configuration to use when sending and receiving HTTP + * requests/responses. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + public ExportOptions httpLogOptions(HttpLogOptions httpLogOptions) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "httpLogOptions cannot be changed after any of the build methods have been called")); + } + this.httpLogOptions = httpLogOptions; + return this; + } + + /** + * Adds a policy to the set of existing policies that are executed after required policies. + * + * @param httpPipelinePolicy a policy to be added to the http pipeline. + * @return The updated {@link AzureMonitorExporterBuilder} object. + * @throws NullPointerException If {@code policy} is {@code null}. + */ + public ExportOptions addHttpPipelinePolicy(HttpPipelinePolicy httpPipelinePolicy) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "httpPipelinePolicies cannot be added after any of the build methods have been called")); + } + httpPipelinePolicies.add(Objects.requireNonNull(httpPipelinePolicy, "'policy' cannot be null.")); + return this; + } + + /** + * Sets the client options such as application ID and custom headers to set on a request. + * + * @param clientOptions The client options. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + public ExportOptions clientOptions(ClientOptions clientOptions) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "clientOptions cannot be changed after any of the build methods have been called")); + } + this.clientOptions = clientOptions; + return this; + } + + /** + * Sets the connection string to use for exporting telemetry events to Azure Monitor. + * + * @param connectionString The connection string for the Azure Monitor resource. + * @return The updated {@link AzureMonitorExporterBuilder} object. + * @throws NullPointerException If the connection string is {@code null}. + * @throws IllegalArgumentException If the connection string is invalid. + */ + public ExportOptions connectionString(String connectionString) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "connectionString cannot be changed after any of the build methods have been called")); + } + this.connectionString = ConnectionString.parse(connectionString); + return this; + } + + /** + * Sets the token credential required for authentication with the ingestion endpoint service. + * + * @param credential The Azure Identity TokenCredential. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + public ExportOptions credential(TokenCredential credential) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "credential cannot be changed after any of the build methods have been called")); + } + this.credential = credential; + return this; + } +} diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java index a1808e4e98a2b..48ea2a1ad708e 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java @@ -40,7 +40,8 @@ public static void main(String[] args) { private static Tracer configureAzureMonitorExporter() { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(sdkBuilder, AzureMonitor.exportOptions().connectionString("{connection-string}")); + ExportOptions exportOptions = new ExportOptions().connectionString("{connection-string}"); + AzureMonitor.configure(sdkBuilder, exportOptions); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); 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 index 70d6694b12eb7..c76558dd50ac3 100644 --- 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 @@ -27,7 +27,8 @@ private static void sendDoubleHistogram() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(sdkBuilder, AzureMonitor.exportOptions().connectionString(APPINSIGHTS_CONNECTION_STRING)); + ExportOptions exportOptions = new ExportOptions().connectionString(APPINSIGHTS_CONNECTION_STRING); + AzureMonitor.configure(sdkBuilder, exportOptions); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); Meter meter = openTelemetry.meterBuilder("OTEL.AzureMonitor.Demo").build(); @@ -48,7 +49,8 @@ private static void sendLongCounter() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(sdkBuilder, AzureMonitor.exportOptions().connectionString(APPINSIGHTS_CONNECTION_STRING)); + ExportOptions exportOptions = new ExportOptions().connectionString(APPINSIGHTS_CONNECTION_STRING); + AzureMonitor.configure(sdkBuilder, exportOptions); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); Meter meter = openTelemetry.meterBuilder("OTEL.AzureMonitor.Demo").build(); @@ -72,7 +74,8 @@ private static void sendGaugeMetric() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(sdkBuilder, AzureMonitor.exportOptions().connectionString(APPINSIGHTS_CONNECTION_STRING)); + ExportOptions exportOptions = new ExportOptions().connectionString(APPINSIGHTS_CONNECTION_STRING); + AzureMonitor.configure(sdkBuilder, exportOptions); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); Meter meter = openTelemetry.getMeter("OTEL.AzureMonitor.Demo"); diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java index a666294a33d23..9587a99290d0c 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java @@ -51,7 +51,8 @@ public static void main(String[] args) { private static Tracer configureAzureMonitorExporter() { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(sdkBuilder, AzureMonitor.exportOptions().connectionString("{connection-string}")); + ExportOptions exportOptions = new ExportOptions().connectionString("{connection-string}"); + AzureMonitor.configure(sdkBuilder, exportOptions); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java index f1009a8b78a66..0aa7e842543ba 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java @@ -55,7 +55,8 @@ public void setupExporter() { // This should be done just once when application starts up AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(sdkBuilder, AzureMonitor.exportOptions().connectionString("{connection-string}")); + ExportOptions exportOptions = new ExportOptions().connectionString("{connection-string}"); + AzureMonitor.configure(sdkBuilder, exportOptions); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); @@ -87,9 +88,10 @@ public void exporterAndOpenTelemetryAutoconfiguration() { // BEGIN: readme-sample-autoconfigure AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(sdkBuilder, AzureMonitor.exportOptions().connectionString("{connection-string}")); + ExportOptions exportOptions = new ExportOptions().connectionString("{connection-string}"); + AzureMonitor.configure(sdkBuilder, exportOptions); - OpenTelemetry openTelemetry =sdkBuilder.build().getOpenTelemetrySdk(); + OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); // END: readme-sample-autoconfigure } @@ -101,7 +103,8 @@ public void createSpan() { // BEGIN: readme-sample-create-span AutoConfiguredOpenTelemetrySdkBuilder otelSdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(otelSdkBuilder, AzureMonitor.exportOptions().connectionString("{connection-string}")); + ExportOptions exportOptions = new ExportOptions().connectionString("{connection-string}"); + AzureMonitor.configure(otelSdkBuilder, exportOptions); OpenTelemetry openTelemetry = otelSdkBuilder.build().getOpenTelemetrySdk(); Tracer tracer = openTelemetry.getTracer("Sample"); diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java index e9a0d8f527a9e..e2a4568673bd3 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java @@ -23,7 +23,8 @@ public void testInvalidConnectionStrings(String con Class exceptionExpected) { Assertions.assertThrows(exceptionExpected, () -> { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(sdkBuilder, AzureMonitor.exportOptions().connectionString(connectionString)); + ExportOptions exportOptions = new ExportOptions().connectionString(connectionString); + AzureMonitor.configure(sdkBuilder, exportOptions); sdkBuilder.build(); }); } diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/MonitorExporterClientTestBase.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/MonitorExporterClientTestBase.java index d49cce40aff9f..a93782f8d64fd 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/MonitorExporterClientTestBase.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/MonitorExporterClientTestBase.java @@ -33,7 +33,7 @@ public class MonitorExporterClientTestBase extends TestProxyTestBase { AzureMonitorExporterBuilder getClientBuilder() { - return new AzureMonitorExporterBuilder().httpPipeline(getHttpPipeline(null)); + return new AzureMonitorExporterBuilder(new ExportOptions().httpPipeline(getHttpPipeline(null))); } HttpPipeline getHttpPipeline(@Nullable HttpPipelinePolicy policy, HttpClient httpClient) { diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java index d610bbb15db6d..2fadc869208b1 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java @@ -8,6 +8,7 @@ import com.azure.json.JsonReader; import com.azure.json.JsonToken; import com.azure.monitor.opentelemetry.AzureMonitor; +import com.azure.monitor.opentelemetry.exporter.ExportOptions; import com.azure.monitor.opentelemetry.exporter.implementation.models.*; import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; @@ -82,12 +83,10 @@ public static OpenTelemetrySdk createOpenTelemetrySdk(HttpPipeline httpPipeline, String connectionString) { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(sdkBuilder, AzureMonitor.exportOptions().connectionString(connectionString).httpPipeline(httpPipeline)); + ExportOptions exportOptions = new ExportOptions().connectionString(connectionString).httpPipeline(httpPipeline); + AzureMonitor.configure(sdkBuilder, exportOptions); - return sdkBuilder - .addPropertiesSupplier(() -> configuration) - .build() - .getOpenTelemetrySdk(); + return sdkBuilder.addPropertiesSupplier(() -> configuration).build().getOpenTelemetrySdk(); } // azure-json doesn't deserialize subtypes yet, so need to convert the abstract MonitorDomain to RemoteDependencyData From 7f0bb687ecad26578aec26001b39adbc74bf2d7e Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Mon, 16 Sep 2024 10:48:34 +0200 Subject: [PATCH 20/53] Change API --- .../README.md | 45 +++- .../monitor/opentelemetry/AzureMonitor.java | 30 ++- .../exporter/AzureMonitorExporterBuilder.java | 255 +++++++++++++++--- .../opentelemetry/exporter/ExportOptions.java | 149 ---------- ...nfigurationAzureMonitorExporterSample.java | 3 +- .../AzureMonitorMetricExporterSample.java | 9 +- .../EventHubsAzureMonitorExporterSample.java | 3 +- .../opentelemetry/exporter/ReadmeSamples.java | 31 +-- .../AzureMonitorExporterBuilderTest.java | 3 +- .../MonitorExporterClientTestBase.java | 2 +- .../implementation/utils/TestUtils.java | 7 +- 11 files changed, 289 insertions(+), 248 deletions(-) delete mode 100644 sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/ExportOptions.java diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index 89a66954a9241..3ee1927d756f5 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -36,8 +36,7 @@ The following code shows how to configure the OpenTelemetry SDK auto-configurati ```java readme-sample-autoconfigure AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); -ExportOptions exportOptions = new ExportOptions().connectionString("{connection-string}"); -AzureMonitor.configure(sdkBuilder, exportOptions); +AzureMonitor.configure(sdkBuilder, "{connection-string}"); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); ``` @@ -50,17 +49,9 @@ The following sections provide code samples using the OpenTelemetry Azure Monito The following example shows how create a span: ```java readme-sample-create-span - - - -``` -The following example demonstrates how to add a span processor to the OpenTelemetry SDK autoconfiguration. - -```java readme-sample-span-processor AutoConfiguredOpenTelemetrySdkBuilder otelSdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); -ExportOptions exportOptions = new ExportOptions().connectionString("{connection-string}"); -AzureMonitor.configure(otelSdkBuilder, exportOptions); +AzureMonitor.configure(otelSdkBuilder, "{connection-string}"); OpenTelemetry openTelemetry = otelSdkBuilder.build().getOpenTelemetrySdk(); Tracer tracer = openTelemetry.getTracer("Sample"); @@ -77,6 +68,38 @@ try (Scope scope = span.makeCurrent()) { span.end(); } ``` +The following example demonstrates how to add a span processor to the OpenTelemetry SDK autoconfiguration. + +```java readme-sample-span-processor +AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); + +AzureMonitor.configure(sdkBuilder); + +SpanProcessor spanProcessor = new SpanProcessor() { + @Override + public void onStart(Context context, ReadWriteSpan span) { + span.setAttribute(AttributeKey.stringKey("random"), RandomStringUtils.random(10)); + } + + @Override + public boolean isStartRequired() { + return true; + } + + @Override + public void onEnd(ReadableSpan readableSpan) { + } + + @Override + public boolean isEndRequired() { + return false; + } +}; + +sdkBuilder.addTracerProviderCustomizer( + (sdkTracerProviderBuilder, configProperties) -> sdkTracerProviderBuilder + .addSpanProcessor(spanProcessor)); +``` More advanced examples with OpenTelemetry APIs: * [Advanced examples - 1][advanced_examples_1] * [Advanced examples - 2][advanced_examples_2] diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java index 95cbe0ee9f6b8..a071ea366cf9e 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java @@ -3,8 +3,14 @@ package com.azure.monitor.opentelemetry; +import com.azure.core.annotation.Fluent; +import com.azure.core.credential.TokenCredential; +import com.azure.core.http.HttpClient; +import com.azure.core.http.HttpPipeline; +import com.azure.core.http.policy.HttpLogOptions; +import com.azure.core.http.policy.HttpPipelinePolicy; +import com.azure.core.util.ClientOptions; import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterBuilder; -import com.azure.monitor.opentelemetry.exporter.ExportOptions; import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorExporterProviderKeys; import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorLogRecordExporterProvider; import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorMetricExporterProvider; @@ -27,19 +33,29 @@ public final class AzureMonitor { * @param autoConfiguredOpenTelemetrySdkBuilder The OpenTelemetry autoconfiguration to set up. */ public static void configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfiguredOpenTelemetrySdkBuilder) { - configure(autoConfiguredOpenTelemetrySdkBuilder, new ExportOptions()); + AzureMonitorExporterBuilder azureMonitorExporterBuilder = new AzureMonitorExporterBuilder(); + configure(autoConfiguredOpenTelemetrySdkBuilder, azureMonitorExporterBuilder); } /** * Configures an {@link AutoConfiguredOpenTelemetrySdkBuilder} for Azure Monitor based on the options set. - * @param autoConfiguredOpenTelemetrySdkBuilder the {@link AutoConfiguredOpenTelemetrySdkBuilder} object. - * @param exportOptions Export options to Azure Monitor. + * @param autoConfiguredOpenTelemetrySdkBuilder The OpenTelemetry autoconfiguration to set up. + * @param connectionString The connection string to connect to an Applicacation Insights resource. */ public static void configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfiguredOpenTelemetrySdkBuilder, - ExportOptions exportOptions) { - - AzureMonitorExporterBuilder azureMonitorExporterBuilder = new AzureMonitorExporterBuilder(exportOptions); + String connectionString) { + AzureMonitorExporterBuilder azureMonitorExporterBuilder + = new AzureMonitorExporterBuilder().connectionString(connectionString); + configure(autoConfiguredOpenTelemetrySdkBuilder, azureMonitorExporterBuilder); + } + /** + * Configures an {@link AutoConfiguredOpenTelemetrySdkBuilder} for Azure Monitor based on the options set. + * @param autoConfiguredOpenTelemetrySdkBuilder the {@link AutoConfiguredOpenTelemetrySdkBuilder} object. + * @param azureMonitorExporterBuilder Advanced configuration to send the data to Azure Monitor. + */ + public static void configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfiguredOpenTelemetrySdkBuilder, + AzureMonitorExporterBuilder azureMonitorExporterBuilder) { autoConfiguredOpenTelemetrySdkBuilder.addPropertiesSupplier(() -> { Map props = new HashMap<>(); props.put("otel.traces.exporter", AzureMonitorExporterProviderKeys.EXPORTER_NAME); 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 2427a603909d2..53fe4f8614f1d 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 @@ -4,19 +4,17 @@ package com.azure.monitor.opentelemetry.exporter; import com.azure.core.annotation.Fluent; +import com.azure.core.client.traits.ConnectionStringTrait; +import com.azure.core.client.traits.HttpTrait; +import com.azure.core.client.traits.TokenCredentialTrait; import com.azure.core.credential.TokenCredential; import com.azure.core.http.HttpClient; import com.azure.core.http.HttpPipeline; -import com.azure.core.http.policy.BearerTokenAuthenticationPolicy; -import com.azure.core.http.policy.CookiePolicy; -import com.azure.core.http.policy.HttpLogDetailLevel; -import com.azure.core.http.policy.HttpLogOptions; -import com.azure.core.http.policy.HttpLoggingPolicy; -import com.azure.core.http.policy.HttpPipelinePolicy; -import com.azure.core.http.policy.UserAgentPolicy; +import com.azure.core.http.policy.*; import com.azure.core.util.ClientOptions; import com.azure.core.util.Configuration; import com.azure.core.util.CoreUtils; +import com.azure.core.util.builder.ClientBuilderUtil; import com.azure.core.util.logging.ClientLogger; import com.azure.monitor.opentelemetry.exporter.implementation.LogDataMapper; import com.azure.monitor.opentelemetry.exporter.implementation.MetricDataMapper; @@ -60,7 +58,8 @@ * Low level API to create OpenTelemetry span, log record and metric exporters for Azure. With OpenTelemetry autoconfiguration ({@link AutoConfiguredOpenTelemetrySdkBuilder}), we recommend using {@link com.azure.monitor.opentelemetry.AzureMonitor}. */ @Fluent -public final class AzureMonitorExporterBuilder { +public final class AzureMonitorExporterBuilder implements ConnectionStringTrait, + TokenCredentialTrait, HttpTrait { private static final ClientLogger LOGGER = new ClientLogger(AzureMonitorExporterBuilder.class); @@ -75,27 +74,193 @@ public final class AzureMonitorExporterBuilder { private static final Map PROPERTIES = CoreUtils.getProperties("azure-monitor-opentelemetry-exporter.properties"); + private ConnectionString connectionString; + private TokenCredential credential; + + private HttpPipeline httpPipeline; + private HttpClient httpClient; + private HttpLogOptions httpLogOptions; + private final List httpPipelinePolicies = new ArrayList<>(); + private ClientOptions clientOptions; + private RetryOptions retryOptions; + private RetryPolicy retryPolicy; + + private boolean frozen; + // this is only populated after the builder is frozen private TelemetryItemExporter builtTelemetryItemExporter; // this is only populated after the builder is frozen private StatsbeatModule statsbeatModule; - private final ExportOptions exportOptions; - /** * Creates an instance of {@link AzureMonitorExporterBuilder}. */ public AzureMonitorExporterBuilder() { - this.exportOptions = new ExportOptions(); } /** - * Creates an instance of {@link AzureMonitorExporterBuilder}. - * @param exportOptions Export options to Azure Monitor. + * Sets the HTTP pipeline to use for the service client. If {@code httpPipeline} is set, all other + * settings are ignored. + * + * @param httpPipeline The HTTP pipeline to use for sending service requests and receiving + * responses. + * @return The updated {@link AzureMonitorExporterBuilder} object. */ - public AzureMonitorExporterBuilder(ExportOptions exportOptions) { - this.exportOptions = exportOptions; + @Override + public AzureMonitorExporterBuilder pipeline(HttpPipeline httpPipeline) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "httpPipeline cannot be changed after any of the build methods have been called")); + } + this.httpPipeline = httpPipeline; + return this; + } + + /** + * Sets the HTTP client to use for sending and receiving requests to and from the service. + * + * @param httpClient The HTTP client to use for requests. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + @Override + public AzureMonitorExporterBuilder httpClient(HttpClient httpClient) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "httpClient cannot be changed after any of the build methods have been called")); + } + this.httpClient = httpClient; + return this; + } + + /** + * Sets the logging configuration for HTTP requests and responses. + * + *

If logLevel is not provided, default value of {@link HttpLogDetailLevel#NONE} is set. + * + * @param httpLogOptions The logging configuration to use when sending and receiving HTTP + * requests/responses. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + @Override + public AzureMonitorExporterBuilder httpLogOptions(HttpLogOptions httpLogOptions) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "httpLogOptions cannot be changed after any of the build methods have been called")); + } + this.httpLogOptions = httpLogOptions; + return this; + } + + /** + * Adds a policy to the set of existing policies that are executed after required policies. + * + * @param httpPipelinePolicy a policy to be added to the http pipeline. + * @return The updated {@link AzureMonitorExporterBuilder} object. + * @throws NullPointerException If {@code policy} is {@code null}. + */ + @Override + public AzureMonitorExporterBuilder addPolicy(HttpPipelinePolicy httpPipelinePolicy) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "httpPipelinePolicy cannot be added after any of the build methods have been called")); + } + httpPipelinePolicies.add(Objects.requireNonNull(httpPipelinePolicy, "'policy' cannot be null.")); + return this; + } + + /** + * Sets the {@link RetryOptions} for all the requests made through the client. + * + *

Note: It is important to understand the precedence order of the HttpTrait APIs. In + * particular, if a {@link HttpPipeline} is specified, this takes precedence over all other APIs in the trait, and + * they will be ignored. If no {@link HttpPipeline} is specified, a HTTP pipeline will be constructed internally + * based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this + * trait that are also ignored if an {@link HttpPipeline} is specified, so please be sure to refer to the + * documentation of types that implement this trait to understand the full set of implications.

+ *

+ * Setting this is mutually exclusive with using {@link #retryPolicy(RetryPolicy)}. + * + * @param retryOptions The {@link RetryOptions} to use for all the requests made through the client. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + + @Override + public AzureMonitorExporterBuilder retryOptions(RetryOptions retryOptions) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "retryOptions cannot be changed after any of the build methods have been called")); + } + this.retryOptions = retryOptions; + return this; + } + + /** + * Sets the request {@link RetryPolicy} for all the requests made through the client. The default + * {@link RetryPolicy} will be used in the pipeline, if not provided. + * Setting this is mutually exclusive with using {@link #retryOptions(RetryOptions)}. + * + * @param retryPolicy {@link RetryPolicy}. + * + * @return The updated {@link AzureMonitorExporterBuilder}. + */ + public AzureMonitorExporterBuilder retryPolicy(RetryPolicy retryPolicy) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "retryPolicy cannot be changed after any of the build methods have been called")); + } + this.retryPolicy = retryPolicy; + return this; + } + + /** + * Sets the client options such as application ID and custom headers to set on a request. + * + * @param clientOptions The client options. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + @Override + public AzureMonitorExporterBuilder clientOptions(ClientOptions clientOptions) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "clientOptions cannot be changed after any of the build methods have been called")); + } + this.clientOptions = clientOptions; + return this; + } + + /** + * Sets the connection string to use for exporting telemetry events to Azure Monitor. + * + * @param connectionString The connection string for the Azure Monitor resource. + * @return The updated {@link AzureMonitorExporterBuilder} object. + * @throws NullPointerException If the connection string is {@code null}. + * @throws IllegalArgumentException If the connection string is invalid. + */ + @Override + public AzureMonitorExporterBuilder connectionString(String connectionString) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "connectionString cannot be changed after any of the build methods have been called")); + } + this.connectionString = ConnectionString.parse(connectionString); + return this; + } + + /** + * Sets the token credential required for authentication with the ingestion endpoint service. + * + * @param credential The Azure Identity TokenCredential. + * @return The updated {@link AzureMonitorExporterBuilder} object. + */ + @Override + public AzureMonitorExporterBuilder credential(TokenCredential credential) { + if (frozen) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "credential cannot be changed after any of the build methods have been called")); + } + this.credential = credential; + return this; } /** @@ -159,7 +324,7 @@ public LogRecordExporter buildLogRecordExporter(ConfigProperties configPropertie // the new ConfigProperties will not get applied to StatsbeatModule because of "frozen" guard. Luckily, we're using the config properties // in StatsbeatModule for testing only. We might need to revisit this approach later. void internalBuildAndFreeze(ConfigProperties configProperties) { - if (!exportOptions.frozen) { + if (!frozen) { HttpPipeline httpPipeline = createHttpPipeline(); statsbeatModule = initStatsbeatModule(configProperties); File tempDir = TempDirs.getApplicationInsightsTempDir(LOGGER, @@ -168,7 +333,7 @@ void internalBuildAndFreeze(ConfigProperties configProperties) { builtTelemetryItemExporter = AzureMonitorHelper.createTelemetryItemExporter(httpPipeline, statsbeatModule, tempDir, LocalStorageStats.noop()); startStatsbeatModule(statsbeatModule, configProperties, tempDir); // wait till TelemetryItemExporter has been initialized before starting StatsbeatModule - exportOptions.frozen = true; + frozen = true; } } @@ -217,7 +382,7 @@ private Set initStatsbeatFeatures() { } private StatsbeatConnectionString getStatsbeatConnectionString() { - return StatsbeatConnectionString.create(exportOptions.connectionString, null, null); + return StatsbeatConnectionString.create(connectionString, null, null); } private SpanDataMapper createSpanDataMapper(ConfigProperties configProperties) { @@ -238,8 +403,8 @@ private BiConsumer createDefaultsPopulator(C } private ConnectionString getConnectionString(ConfigProperties configProperties) { - if (exportOptions.connectionString != null) { - return exportOptions.connectionString; + if (connectionString != null) { + return connectionString; } ConnectionString connectionString = ConnectionString.parse(configProperties.getString(APPLICATIONINSIGHTS_CONNECTION_STRING)); @@ -248,47 +413,57 @@ private ConnectionString getConnectionString(ConfigProperties configProperties) private HttpPipeline createHttpPipeline() { - if (exportOptions.httpPipeline != null) { - if (exportOptions.credential != null) { + if (httpPipeline != null) { + if (credential != null) { throw LOGGER.logExceptionAsError( new IllegalStateException("'credential' is not supported when custom 'httpPipeline' is specified")); } - if (exportOptions.httpClient != null) { + if (httpClient != null) { throw LOGGER.logExceptionAsError( new IllegalStateException("'httpClient' is not supported when custom 'httpPipeline' is specified")); } - if (exportOptions.httpLogOptions != null) { + if (httpLogOptions != null) { throw LOGGER.logExceptionAsError(new IllegalStateException( "'httpLogOptions' is not supported when custom 'httpPipeline' is specified")); } - if (!exportOptions.httpPipelinePolicies.isEmpty()) { + if (!httpPipelinePolicies.isEmpty()) { throw LOGGER.logExceptionAsError(new IllegalStateException( "'httpPipelinePolicies' is not supported when custom 'httpPipeline' is specified")); } - if (exportOptions.clientOptions != null) { + if (clientOptions != null) { throw LOGGER.logExceptionAsError(new IllegalStateException( "'clientOptions' is not supported when custom 'httpPipeline' is specified")); } - return exportOptions.httpPipeline; + if (retryPolicy != null) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "'retryPolicy' is not supported when custom 'httpPipeline' is specified")); + } + if (retryOptions != null) { + throw LOGGER.logExceptionAsError(new IllegalStateException( + "'retryOptions' is not supported when custom 'httpPipeline' is specified")); + } + return httpPipeline; } List policies = new ArrayList<>(); String clientName = PROPERTIES.getOrDefault("name", "UnknownName"); String clientVersion = PROPERTIES.getOrDefault("version", "UnknownVersion"); - String applicationId = CoreUtils.getApplicationId(exportOptions.clientOptions, exportOptions.httpLogOptions); + String applicationId = CoreUtils.getApplicationId(clientOptions, httpLogOptions); policies .add(new UserAgentPolicy(applicationId, clientName, clientVersion, Configuration.getGlobalConfiguration())); policies.add(new CookiePolicy()); - if (exportOptions.credential != null) { - policies.add(new BearerTokenAuthenticationPolicy(exportOptions.credential, - APPLICATIONINSIGHTS_AUTHENTICATION_SCOPE)); + if (credential != null) { + policies.add(new BearerTokenAuthenticationPolicy(credential, APPLICATIONINSIGHTS_AUTHENTICATION_SCOPE)); } - policies.addAll(exportOptions.httpPipelinePolicies); - policies.add(new HttpLoggingPolicy(exportOptions.httpLogOptions)); + + policies.add(ClientBuilderUtil.validateAndGetRetryPolicy(retryPolicy, retryOptions, new RetryPolicy())); + + policies.addAll(httpPipelinePolicies); + policies.add(new HttpLoggingPolicy(httpLogOptions)); return new com.azure.core.http.HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])) - .httpClient(exportOptions.httpClient) + .httpClient(httpClient) .tracer(new NoopTracer()) .build(); } @@ -311,23 +486,23 @@ private void startStatsbeatModule(StatsbeatModule statsbeatModule, ConfigPropert } private HttpPipeline createStatsbeatHttpPipeline() { - if (exportOptions.httpPipeline != null) { - return exportOptions.httpPipeline; + if (httpPipeline != null) { + return httpPipeline; } List policies = new ArrayList<>(); String clientName = PROPERTIES.getOrDefault("name", "UnknownName"); String clientVersion = PROPERTIES.getOrDefault("version", "UnknownVersion"); - String applicationId = CoreUtils.getApplicationId(exportOptions.clientOptions, exportOptions.httpLogOptions); + String applicationId = CoreUtils.getApplicationId(clientOptions, httpLogOptions); policies .add(new UserAgentPolicy(applicationId, clientName, clientVersion, Configuration.getGlobalConfiguration())); policies.add(new CookiePolicy()); - policies.addAll(exportOptions.httpPipelinePolicies); - policies.add(new HttpLoggingPolicy(exportOptions.httpLogOptions)); + policies.addAll(httpPipelinePolicies); + policies.add(new HttpLoggingPolicy(httpLogOptions)); return new com.azure.core.http.HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])) - .httpClient(exportOptions.httpClient) + .httpClient(httpClient) .tracer(new NoopTracer()) .build(); } diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/ExportOptions.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/ExportOptions.java deleted file mode 100644 index 505cade5fea80..0000000000000 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/ExportOptions.java +++ /dev/null @@ -1,149 +0,0 @@ -package com.azure.monitor.opentelemetry.exporter; - -import com.azure.core.annotation.Fluent; -import com.azure.core.credential.TokenCredential; -import com.azure.core.http.HttpClient; -import com.azure.core.http.HttpPipeline; -import com.azure.core.http.policy.HttpLogDetailLevel; -import com.azure.core.http.policy.HttpLogOptions; -import com.azure.core.http.policy.HttpPipelinePolicy; -import com.azure.core.util.ClientOptions; -import com.azure.core.util.logging.ClientLogger; -import com.azure.monitor.opentelemetry.AzureMonitor; -import com.azure.monitor.opentelemetry.exporter.implementation.configuration.ConnectionString; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; - -/** - * Options to configure the Azure Monitor export. - */ -@Fluent -public class ExportOptions { - - private static final ClientLogger LOGGER = new ClientLogger(ExportOptions.class); - - HttpPipeline httpPipeline; - HttpClient httpClient; - HttpLogOptions httpLogOptions; - final List httpPipelinePolicies = new ArrayList<>(); - ClientOptions clientOptions; - ConnectionString connectionString; - TokenCredential credential; - - boolean frozen; - - /** - * Sets the HTTP pipeline to use for the service client. If {@code httpPipeline} is set, all other - * settings are ignored. - * - * @param httpPipeline The HTTP pipeline to use for sending service requests and receiving - * responses. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public ExportOptions httpPipeline(HttpPipeline httpPipeline) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "httpPipeline cannot be changed after any of the build methods have been called")); - } - this.httpPipeline = httpPipeline; - return this; - } - - /** - * Sets the HTTP client to use for sending and receiving requests to and from the service. - * - * @param httpClient The HTTP client to use for requests. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public ExportOptions httpClient(HttpClient httpClient) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "httpClient cannot be changed after any of the build methods have been called")); - } - this.httpClient = httpClient; - return this; - } - - /** - * Sets the logging configuration for HTTP requests and responses. - * - *

If logLevel is not provided, default value of {@link HttpLogDetailLevel#NONE} is set. - * - * @param httpLogOptions The logging configuration to use when sending and receiving HTTP - * requests/responses. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public ExportOptions httpLogOptions(HttpLogOptions httpLogOptions) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "httpLogOptions cannot be changed after any of the build methods have been called")); - } - this.httpLogOptions = httpLogOptions; - return this; - } - - /** - * Adds a policy to the set of existing policies that are executed after required policies. - * - * @param httpPipelinePolicy a policy to be added to the http pipeline. - * @return The updated {@link AzureMonitorExporterBuilder} object. - * @throws NullPointerException If {@code policy} is {@code null}. - */ - public ExportOptions addHttpPipelinePolicy(HttpPipelinePolicy httpPipelinePolicy) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "httpPipelinePolicies cannot be added after any of the build methods have been called")); - } - httpPipelinePolicies.add(Objects.requireNonNull(httpPipelinePolicy, "'policy' cannot be null.")); - return this; - } - - /** - * Sets the client options such as application ID and custom headers to set on a request. - * - * @param clientOptions The client options. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public ExportOptions clientOptions(ClientOptions clientOptions) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "clientOptions cannot be changed after any of the build methods have been called")); - } - this.clientOptions = clientOptions; - return this; - } - - /** - * Sets the connection string to use for exporting telemetry events to Azure Monitor. - * - * @param connectionString The connection string for the Azure Monitor resource. - * @return The updated {@link AzureMonitorExporterBuilder} object. - * @throws NullPointerException If the connection string is {@code null}. - * @throws IllegalArgumentException If the connection string is invalid. - */ - public ExportOptions connectionString(String connectionString) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "connectionString cannot be changed after any of the build methods have been called")); - } - this.connectionString = ConnectionString.parse(connectionString); - return this; - } - - /** - * Sets the token credential required for authentication with the ingestion endpoint service. - * - * @param credential The Azure Identity TokenCredential. - * @return The updated {@link AzureMonitorExporterBuilder} object. - */ - public ExportOptions credential(TokenCredential credential) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "credential cannot be changed after any of the build methods have been called")); - } - this.credential = credential; - return this; - } -} diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java index 48ea2a1ad708e..1b64142893d7b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java @@ -40,8 +40,7 @@ public static void main(String[] args) { private static Tracer configureAzureMonitorExporter() { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - ExportOptions exportOptions = new ExportOptions().connectionString("{connection-string}"); - AzureMonitor.configure(sdkBuilder, exportOptions); + AzureMonitor.configure(sdkBuilder, "{connection-string}"); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); 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 index c76558dd50ac3..acc80e71b74bf 100644 --- 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 @@ -27,8 +27,7 @@ private static void sendDoubleHistogram() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - ExportOptions exportOptions = new ExportOptions().connectionString(APPINSIGHTS_CONNECTION_STRING); - AzureMonitor.configure(sdkBuilder, exportOptions); + AzureMonitor.configure(sdkBuilder, APPINSIGHTS_CONNECTION_STRING); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); Meter meter = openTelemetry.meterBuilder("OTEL.AzureMonitor.Demo").build(); @@ -49,8 +48,7 @@ private static void sendLongCounter() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - ExportOptions exportOptions = new ExportOptions().connectionString(APPINSIGHTS_CONNECTION_STRING); - AzureMonitor.configure(sdkBuilder, exportOptions); + AzureMonitor.configure(sdkBuilder, APPINSIGHTS_CONNECTION_STRING); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); Meter meter = openTelemetry.meterBuilder("OTEL.AzureMonitor.Demo").build(); @@ -74,8 +72,7 @@ private static void sendGaugeMetric() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - ExportOptions exportOptions = new ExportOptions().connectionString(APPINSIGHTS_CONNECTION_STRING); - AzureMonitor.configure(sdkBuilder, exportOptions); + AzureMonitor.configure(sdkBuilder, APPINSIGHTS_CONNECTION_STRING); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); Meter meter = openTelemetry.getMeter("OTEL.AzureMonitor.Demo"); diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java index 9587a99290d0c..5fd61130a04de 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java @@ -51,8 +51,7 @@ public static void main(String[] args) { private static Tracer configureAzureMonitorExporter() { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - ExportOptions exportOptions = new ExportOptions().connectionString("{connection-string}"); - AzureMonitor.configure(sdkBuilder, exportOptions); + AzureMonitor.configure(sdkBuilder, "{connection-string}"); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java index 0aa7e842543ba..637331d4c003d 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java @@ -13,29 +13,13 @@ import io.opentelemetry.api.trace.Tracer; import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; -import io.opentelemetry.sdk.OpenTelemetrySdk; + import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder; -import io.opentelemetry.sdk.logs.SdkLoggerProvider; -import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder; -import io.opentelemetry.sdk.logs.data.LogRecordData; -import io.opentelemetry.sdk.logs.export.BatchLogRecordProcessor; -import io.opentelemetry.sdk.logs.export.LogRecordExporter; -import io.opentelemetry.sdk.metrics.SdkMeterProvider; -import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder; -import io.opentelemetry.sdk.metrics.data.MetricData; -import io.opentelemetry.sdk.metrics.export.MetricExporter; -import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader; -import io.opentelemetry.sdk.metrics.export.PeriodicMetricReaderBuilder; + import io.opentelemetry.sdk.trace.*; -import io.opentelemetry.sdk.trace.data.SpanData; -import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; -import io.opentelemetry.sdk.trace.export.SpanExporter; -import org.apache.commons.lang3.RandomStringUtils; -import java.time.Duration; -import java.util.Collection; -import java.util.Collections; +import org.apache.commons.lang3.RandomStringUtils; /** * WARNING: MODIFYING THIS FILE WILL REQUIRE CORRESPONDING UPDATES TO README.md FILE. LINE NUMBERS @@ -55,8 +39,7 @@ public void setupExporter() { // This should be done just once when application starts up AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - ExportOptions exportOptions = new ExportOptions().connectionString("{connection-string}"); - AzureMonitor.configure(sdkBuilder, exportOptions); + AzureMonitor.configure(sdkBuilder, "{connection-string}"); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); @@ -88,8 +71,7 @@ public void exporterAndOpenTelemetryAutoconfiguration() { // BEGIN: readme-sample-autoconfigure AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - ExportOptions exportOptions = new ExportOptions().connectionString("{connection-string}"); - AzureMonitor.configure(sdkBuilder, exportOptions); + AzureMonitor.configure(sdkBuilder, "{connection-string}"); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); // END: readme-sample-autoconfigure @@ -103,8 +85,7 @@ public void createSpan() { // BEGIN: readme-sample-create-span AutoConfiguredOpenTelemetrySdkBuilder otelSdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - ExportOptions exportOptions = new ExportOptions().connectionString("{connection-string}"); - AzureMonitor.configure(otelSdkBuilder, exportOptions); + AzureMonitor.configure(otelSdkBuilder, "{connection-string}"); OpenTelemetry openTelemetry = otelSdkBuilder.build().getOpenTelemetrySdk(); Tracer tracer = openTelemetry.getTracer("Sample"); diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java index e2a4568673bd3..8058e4911ec23 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java @@ -23,8 +23,7 @@ public void testInvalidConnectionStrings(String con Class exceptionExpected) { Assertions.assertThrows(exceptionExpected, () -> { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - ExportOptions exportOptions = new ExportOptions().connectionString(connectionString); - AzureMonitor.configure(sdkBuilder, exportOptions); + AzureMonitor.configure(sdkBuilder, connectionString); sdkBuilder.build(); }); } diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/MonitorExporterClientTestBase.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/MonitorExporterClientTestBase.java index a93782f8d64fd..9c666fe655f68 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/MonitorExporterClientTestBase.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/MonitorExporterClientTestBase.java @@ -33,7 +33,7 @@ public class MonitorExporterClientTestBase extends TestProxyTestBase { AzureMonitorExporterBuilder getClientBuilder() { - return new AzureMonitorExporterBuilder(new ExportOptions().httpPipeline(getHttpPipeline(null))); + return new AzureMonitorExporterBuilder().pipeline(getHttpPipeline(null)); } HttpPipeline getHttpPipeline(@Nullable HttpPipelinePolicy policy, HttpClient httpClient) { diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java index 2fadc869208b1..d1d80f3f13400 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java @@ -8,7 +8,7 @@ import com.azure.json.JsonReader; import com.azure.json.JsonToken; import com.azure.monitor.opentelemetry.AzureMonitor; -import com.azure.monitor.opentelemetry.exporter.ExportOptions; +import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterBuilder; import com.azure.monitor.opentelemetry.exporter.implementation.models.*; import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; @@ -83,8 +83,9 @@ public static OpenTelemetrySdk createOpenTelemetrySdk(HttpPipeline httpPipeline, String connectionString) { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - ExportOptions exportOptions = new ExportOptions().connectionString(connectionString).httpPipeline(httpPipeline); - AzureMonitor.configure(sdkBuilder, exportOptions); + AzureMonitorExporterBuilder azureMonitorExporterBuilder + = new AzureMonitorExporterBuilder().connectionString(connectionString).pipeline(httpPipeline); + AzureMonitor.configure(sdkBuilder, azureMonitorExporterBuilder); return sdkBuilder.addPropertiesSupplier(() -> configuration).build().getOpenTelemetrySdk(); } From da6cbe5af01b1e71640a65b987620b0b983365c6 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Mon, 16 Sep 2024 11:42:46 +0200 Subject: [PATCH 21/53] Rename buildTraceExporter into buildSpanExporter --- .../com/azure/monitor/opentelemetry/AzureMonitor.java | 9 +-------- .../exporter/AzureMonitorExporterBuilder.java | 8 ++++---- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java index a071ea366cf9e..e61297c5a3110 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java @@ -3,13 +3,6 @@ package com.azure.monitor.opentelemetry; -import com.azure.core.annotation.Fluent; -import com.azure.core.credential.TokenCredential; -import com.azure.core.http.HttpClient; -import com.azure.core.http.HttpPipeline; -import com.azure.core.http.policy.HttpLogOptions; -import com.azure.core.http.policy.HttpPipelinePolicy; -import com.azure.core.util.ClientOptions; import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterBuilder; import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorExporterProviderKeys; import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorLogRecordExporterProvider; @@ -66,7 +59,7 @@ public static void configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfigure }); autoConfiguredOpenTelemetrySdkBuilder.addSpanExporterCustomizer((spanExporter, configProperties) -> { if (spanExporter instanceof AzureMonitorSpanExporterProvider.MarkerSpanExporter) { - spanExporter = azureMonitorExporterBuilder.buildTraceExporter(configProperties); + spanExporter = azureMonitorExporterBuilder.buildSpanExporter(configProperties); } return spanExporter; }); 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 53fe4f8614f1d..734d2f31646f7 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 @@ -271,13 +271,13 @@ public AzureMonitorExporterBuilder credential(TokenCredential credential) { * @throws NullPointerException if the connection string is not set on this builder or if the * environment variable "APPLICATIONINSIGHTS_CONNECTION_STRING" is not set. */ - public SpanExporter buildTraceExporter() { + public SpanExporter buildSpanExporter() { ConfigProperties defaultConfig = DefaultConfigProperties.create(Collections.emptyMap()); - return buildTraceExporter(defaultConfig); + return buildSpanExporter(defaultConfig); } /** - * Creates an Azure Monitor trace exporter based on the options set in the builder. This + * Creates an Azure Monitor span exporter based on the options set in the builder. This * exporter is an implementation of OpenTelemetry {@link SpanExporter}. * * @param configProperties The OpenTelemetry configuration properties. @@ -285,7 +285,7 @@ public SpanExporter buildTraceExporter() { * @throws NullPointerException if the connection string is not set on this builder or if the * environment variable "APPLICATIONINSIGHTS_CONNECTION_STRING" is not set. */ - public SpanExporter buildTraceExporter(ConfigProperties configProperties) { + public SpanExporter buildSpanExporter(ConfigProperties configProperties) { internalBuildAndFreeze(configProperties); return new AzureMonitorTraceExporter(createSpanDataMapper(configProperties), builtTelemetryItemExporter, statsbeatModule); From 3e4efa32a7adb910e2b19579342bed528e60945b Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Mon, 16 Sep 2024 11:51:08 +0200 Subject: [PATCH 22/53] Update Readme --- .../azure-monitor-opentelemetry-exporter/README.md | 12 +++++++++--- .../opentelemetry/exporter/ReadmeSamples.java | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index 3ee1927d756f5..5194d614ebb34 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -32,12 +32,18 @@ right corner. ### Setup the OpenTelemetry SDK to work with Azure Monitor exporter -The following code shows how to configure the OpenTelemetry SDK auto-configuration with the Azure Monitor exporter: -```java readme-sample-autoconfigure +If you have set the Application Insights connection string with the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable, you configure OpenTelemetry SDK auto-configuration for Azure in the following way: + +```java readme-sample-autoconfigure-env-variable AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); +AzureMonitor.configure(sdkBuilder); +OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); +``` +You can also se the connection string in the code: +```java readme-sample-autoconfigure +AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); AzureMonitor.configure(sdkBuilder, "{connection-string}"); - OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); ``` diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java index 637331d4c003d..114aec760db58 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java @@ -64,15 +64,25 @@ public void setupExporter() { // END: readme-sample-createSpans } + + /** + * Sample to use the Azure Monitor OpenTelemetry Exporter with the OpenTelemetry SDK auto-configuration when the connection string is set with the APPLICATIONINSIGHTS_CONNECTION_STRING + */ + public void exporterAndOpenTelemetryAutoconfigurationEnvVariable() { + // BEGIN: readme-sample-autoconfigure-env-variable + AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); + AzureMonitor.configure(sdkBuilder); + OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); + // END: readme-sample-autoconfigure-env-variable + } + /** * Sample to use the Azure Monitor OpenTelemetry Exporter with the OpenTelemetry SDK auto-configuration */ public void exporterAndOpenTelemetryAutoconfiguration() { // BEGIN: readme-sample-autoconfigure AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(sdkBuilder, "{connection-string}"); - OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); // END: readme-sample-autoconfigure } From 40c19006744d070d5d19d7ff3b3c3674cc1b8232 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Mon, 16 Sep 2024 12:18:43 +0200 Subject: [PATCH 23/53] Avoid start import --- .../exporter/AzureMonitorExporterBuilder.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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 734d2f31646f7..810a66fe58f53 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 @@ -10,7 +10,15 @@ import com.azure.core.credential.TokenCredential; import com.azure.core.http.HttpClient; import com.azure.core.http.HttpPipeline; -import com.azure.core.http.policy.*; +import com.azure.core.http.policy.BearerTokenAuthenticationPolicy; +import com.azure.core.http.policy.CookiePolicy; +import com.azure.core.http.policy.HttpLogDetailLevel; +import com.azure.core.http.policy.HttpLogOptions; +import com.azure.core.http.policy.HttpLoggingPolicy; +import com.azure.core.http.policy.HttpPipelinePolicy; +import com.azure.core.http.policy.RetryOptions; +import com.azure.core.http.policy.RetryPolicy; +import com.azure.core.http.policy.UserAgentPolicy; import com.azure.core.util.ClientOptions; import com.azure.core.util.Configuration; import com.azure.core.util.CoreUtils; From 2eba79424487824d101588ea2c91418af2284da4 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Mon, 16 Sep 2024 12:31:54 +0200 Subject: [PATCH 24/53] Avoid start import --- .../azure/monitor/opentelemetry/exporter/ReadmeSamples.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java index 114aec760db58..a67cdead8276f 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java @@ -17,8 +17,9 @@ import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder; -import io.opentelemetry.sdk.trace.*; - +import io.opentelemetry.sdk.trace.ReadWriteSpan; +import io.opentelemetry.sdk.trace.ReadableSpan; +import io.opentelemetry.sdk.trace.SpanProcessor; import org.apache.commons.lang3.RandomStringUtils; /** From 7370644f6c88152b4512a7a2b18a83f871edd2d2 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Mon, 16 Sep 2024 12:35:30 +0200 Subject: [PATCH 25/53] Remove apache import --- sdk/monitor/azure-monitor-opentelemetry-exporter/README.md | 2 +- .../azure/monitor/opentelemetry/exporter/ReadmeSamples.java | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index 5194d614ebb34..4e240de2c9ff3 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -84,7 +84,7 @@ AzureMonitor.configure(sdkBuilder); SpanProcessor spanProcessor = new SpanProcessor() { @Override public void onStart(Context context, ReadWriteSpan span) { - span.setAttribute(AttributeKey.stringKey("random"), RandomStringUtils.random(10)); + span.setAttribute(AttributeKey.stringKey("attributeKey"), "attributeValue"); } @Override diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java index a67cdead8276f..f54d5f5bea8bd 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java @@ -20,7 +20,6 @@ import io.opentelemetry.sdk.trace.ReadWriteSpan; import io.opentelemetry.sdk.trace.ReadableSpan; import io.opentelemetry.sdk.trace.SpanProcessor; -import org.apache.commons.lang3.RandomStringUtils; /** * WARNING: MODIFYING THIS FILE WILL REQUIRE CORRESPONDING UPDATES TO README.md FILE. LINE NUMBERS @@ -127,7 +126,7 @@ public void spanProcessor() { SpanProcessor spanProcessor = new SpanProcessor() { @Override public void onStart(Context context, ReadWriteSpan span) { - span.setAttribute(AttributeKey.stringKey("random"), RandomStringUtils.random(10)); + span.setAttribute(AttributeKey.stringKey("attributeKey"), "attributeValue"); } @Override From 2fca53ca9d4f7b659b080c0491ae3ad2fad5b673 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Mon, 16 Sep 2024 14:11:23 +0200 Subject: [PATCH 26/53] Add private constructor --- .../java/com/azure/monitor/opentelemetry/AzureMonitor.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java index e61297c5a3110..774ca73c2671b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java @@ -21,6 +21,9 @@ */ public final class AzureMonitor { + + private AzureMonitor() { } + /** * Configures an {@link AutoConfiguredOpenTelemetrySdkBuilder} for Azure Monitor based on the options set. * @param autoConfiguredOpenTelemetrySdkBuilder The OpenTelemetry autoconfiguration to set up. From 8801ac0625b2cfb5c3b5350e1fce2741ad30023c Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Tue, 17 Sep 2024 11:17:38 +0200 Subject: [PATCH 27/53] Update sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilder.java Co-authored-by: Trask Stalnaker --- .../opentelemetry/exporter/AzureMonitorExporterBuilder.java | 1 - 1 file changed, 1 deletion(-) 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 810a66fe58f53..9da562c3ba3be 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 @@ -192,7 +192,6 @@ public AzureMonitorExporterBuilder addPolicy(HttpPipelinePolicy httpPipelinePoli * @param retryOptions The {@link RetryOptions} to use for all the requests made through the client. * @return The updated {@link AzureMonitorExporterBuilder} object. */ - @Override public AzureMonitorExporterBuilder retryOptions(RetryOptions retryOptions) { if (frozen) { From f837230888c2b4a919c6cd7a050e9153a9cb50bc Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Tue, 17 Sep 2024 11:17:46 +0200 Subject: [PATCH 28/53] Update sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilder.java Co-authored-by: Trask Stalnaker --- .../opentelemetry/exporter/AzureMonitorExporterBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 9da562c3ba3be..fe118e99db2cf 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 @@ -181,7 +181,7 @@ public AzureMonitorExporterBuilder addPolicy(HttpPipelinePolicy httpPipelinePoli * Sets the {@link RetryOptions} for all the requests made through the client. * *

Note: It is important to understand the precedence order of the HttpTrait APIs. In - * particular, if a {@link HttpPipeline} is specified, this takes precedence over all other APIs in the trait, and + * particular, if an {@link HttpPipeline} is specified, this takes precedence over all other APIs in the trait, and * they will be ignored. If no {@link HttpPipeline} is specified, a HTTP pipeline will be constructed internally * based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this * trait that are also ignored if an {@link HttpPipeline} is specified, so please be sure to refer to the From 1675d38f8cc05af42cfd2c5d044ef40eb27428de Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Tue, 17 Sep 2024 11:18:01 +0200 Subject: [PATCH 29/53] Update sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilder.java Co-authored-by: Trask Stalnaker --- .../opentelemetry/exporter/AzureMonitorExporterBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 fe118e99db2cf..4f7bdda826c0d 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 @@ -182,7 +182,7 @@ public AzureMonitorExporterBuilder addPolicy(HttpPipelinePolicy httpPipelinePoli * *

Note: It is important to understand the precedence order of the HttpTrait APIs. In * particular, if an {@link HttpPipeline} is specified, this takes precedence over all other APIs in the trait, and - * they will be ignored. If no {@link HttpPipeline} is specified, a HTTP pipeline will be constructed internally + * they will be ignored. If no {@link HttpPipeline} is specified, an HTTP pipeline will be constructed internally * based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this * trait that are also ignored if an {@link HttpPipeline} is specified, so please be sure to refer to the * documentation of types that implement this trait to understand the full set of implications.

From a757481dba854a505d7188b3318f3bde79f3d38e Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Tue, 17 Sep 2024 12:27:02 +0200 Subject: [PATCH 30/53] Move ExternalDependencyExposedCheck --- .../main/resources/checkstyle/checkstyle-suppressions.xml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml b/eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml index 7ac376081102a..66cd575eb1c1a 100644 --- a/eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml +++ b/eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml @@ -78,8 +78,6 @@ - @@ -141,6 +139,9 @@ the main ServiceBusClientBuilder. --> + + From 9028e57a8e9a261a26b4a748353e9d4ab01e2056 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Tue, 17 Sep 2024 12:35:12 +0200 Subject: [PATCH 31/53] AzureMonitor now uses the AutoConfigurationCustomizer interface --- .../monitor/opentelemetry/AzureMonitor.java | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java index 774ca73c2671b..8f4703cc8678b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java @@ -8,7 +8,7 @@ import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorLogRecordExporterProvider; import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorMetricExporterProvider; import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorSpanExporterProvider; -import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder; +import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer; import io.opentelemetry.sdk.metrics.Aggregation; import io.opentelemetry.sdk.metrics.InstrumentSelector; import io.opentelemetry.sdk.metrics.View; @@ -21,38 +21,37 @@ */ public final class AzureMonitor { - - private AzureMonitor() { } + private AzureMonitor() { + } /** - * Configures an {@link AutoConfiguredOpenTelemetrySdkBuilder} for Azure Monitor based on the options set. - * @param autoConfiguredOpenTelemetrySdkBuilder The OpenTelemetry autoconfiguration to set up. + * Configures an {@link AutoConfigurationCustomizer} for Azure Monitor based on the options set. + * @param autoConfigurationCustomizer The OpenTelemetry autoconfiguration to set up. */ - public static void configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfiguredOpenTelemetrySdkBuilder) { + public static void configure(AutoConfigurationCustomizer autoConfigurationCustomizer) { AzureMonitorExporterBuilder azureMonitorExporterBuilder = new AzureMonitorExporterBuilder(); - configure(autoConfiguredOpenTelemetrySdkBuilder, azureMonitorExporterBuilder); + configure(autoConfigurationCustomizer, azureMonitorExporterBuilder); } /** - * Configures an {@link AutoConfiguredOpenTelemetrySdkBuilder} for Azure Monitor based on the options set. - * @param autoConfiguredOpenTelemetrySdkBuilder The OpenTelemetry autoconfiguration to set up. - * @param connectionString The connection string to connect to an Applicacation Insights resource. + * Configures an {@link AutoConfigurationCustomizer} for Azure Monitor based on the options set. + * @param autoConfigurationCustomizer The OpenTelemetry autoconfiguration to set up. + * @param connectionString The connection string to connect to an Application Insights resource. */ - public static void configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfiguredOpenTelemetrySdkBuilder, - String connectionString) { + public static void configure(AutoConfigurationCustomizer autoConfigurationCustomizer, String connectionString) { AzureMonitorExporterBuilder azureMonitorExporterBuilder = new AzureMonitorExporterBuilder().connectionString(connectionString); - configure(autoConfiguredOpenTelemetrySdkBuilder, azureMonitorExporterBuilder); + configure(autoConfigurationCustomizer, azureMonitorExporterBuilder); } /** - * Configures an {@link AutoConfiguredOpenTelemetrySdkBuilder} for Azure Monitor based on the options set. - * @param autoConfiguredOpenTelemetrySdkBuilder the {@link AutoConfiguredOpenTelemetrySdkBuilder} object. + * Configures an {@link AutoConfigurationCustomizer} for Azure Monitor based on the options set. + * @param autoConfigurationCustomizer the {@link AutoConfigurationCustomizer} object. * @param azureMonitorExporterBuilder Advanced configuration to send the data to Azure Monitor. */ - public static void configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfiguredOpenTelemetrySdkBuilder, + public static void configure(AutoConfigurationCustomizer autoConfigurationCustomizer, AzureMonitorExporterBuilder azureMonitorExporterBuilder) { - autoConfiguredOpenTelemetrySdkBuilder.addPropertiesSupplier(() -> { + autoConfigurationCustomizer.addPropertiesSupplier(() -> { Map props = new HashMap<>(); props.put("otel.traces.exporter", AzureMonitorExporterProviderKeys.EXPORTER_NAME); props.put("otel.metrics.exporter", AzureMonitorExporterProviderKeys.EXPORTER_NAME); @@ -60,19 +59,19 @@ public static void configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfigure props.put(AzureMonitorExporterProviderKeys.INTERNAL_USING_AZURE_MONITOR_EXPORTER_BUILDER, "true"); return props; }); - autoConfiguredOpenTelemetrySdkBuilder.addSpanExporterCustomizer((spanExporter, configProperties) -> { + autoConfigurationCustomizer.addSpanExporterCustomizer((spanExporter, configProperties) -> { if (spanExporter instanceof AzureMonitorSpanExporterProvider.MarkerSpanExporter) { spanExporter = azureMonitorExporterBuilder.buildSpanExporter(configProperties); } return spanExporter; }); - autoConfiguredOpenTelemetrySdkBuilder.addMetricExporterCustomizer((metricExporter, configProperties) -> { + autoConfigurationCustomizer.addMetricExporterCustomizer((metricExporter, configProperties) -> { if (metricExporter instanceof AzureMonitorMetricExporterProvider.MarkerMetricExporter) { metricExporter = azureMonitorExporterBuilder.buildMetricExporter(configProperties); } return metricExporter; }); - autoConfiguredOpenTelemetrySdkBuilder.addLogRecordExporterCustomizer((logRecordExporter, configProperties) -> { + autoConfigurationCustomizer.addLogRecordExporterCustomizer((logRecordExporter, configProperties) -> { if (logRecordExporter instanceof AzureMonitorLogRecordExporterProvider.MarkerLogRecordExporter) { logRecordExporter = azureMonitorExporterBuilder.buildLogRecordExporter(configProperties); } @@ -83,7 +82,7 @@ public static void configure(AutoConfiguredOpenTelemetrySdkBuilder autoConfigure // QuickPulse quickPulse = QuickPulse.create(getHttpPipeline()); // return sdkTracerProviderBuilder.addSpanProcessor( // ne - autoConfiguredOpenTelemetrySdkBuilder + autoConfigurationCustomizer .addMeterProviderCustomizer((sdkMeterProviderBuilder, config) -> sdkMeterProviderBuilder .registerView(InstrumentSelector.builder().setMeterName("io.opentelemetry.sdk.trace").build(), View.builder().setAggregation(Aggregation.drop()).build()) From 35e2a5fa727140eaacf2e522dcf81deab9548a1c Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Tue, 17 Sep 2024 14:21:51 +0200 Subject: [PATCH 32/53] Remove methods without a ConfigProperties argument in AzureMonitorExporterBuilder --- .../exporter/AzureMonitorExporterBuilder.java | 41 ------------------- 1 file changed, 41 deletions(-) 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 4f7bdda826c0d..8fe105054f9ab 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 @@ -270,19 +270,6 @@ public AzureMonitorExporterBuilder credential(TokenCredential credential) { return this; } - /** - * Creates an Azure Monitor trace exporter based on the options set in the builder. This - * exporter is an implementation of OpenTelemetry {@link SpanExporter}. - * - * @return An instance of {@link SpanExporter}. - * @throws NullPointerException if the connection string is not set on this builder or if the - * environment variable "APPLICATIONINSIGHTS_CONNECTION_STRING" is not set. - */ - public SpanExporter buildSpanExporter() { - ConfigProperties defaultConfig = DefaultConfigProperties.create(Collections.emptyMap()); - return buildSpanExporter(defaultConfig); - } - /** * Creates an Azure Monitor span exporter based on the options set in the builder. This * exporter is an implementation of OpenTelemetry {@link SpanExporter}. @@ -298,18 +285,6 @@ public SpanExporter buildSpanExporter(ConfigProperties configProperties) { statsbeatModule); } - /** - * Creates an Azure Monitor log record exporter based on the options set in the builder. This - * exporter is an implementation of OpenTelemetry {@link LogRecordExporter}. - * - * @return An instance of {@link LogRecordExporter}. - * @throws NullPointerException if the connection string is not set on this builder or if the - * environment variable "APPLICATIONINSIGHTS_CONNECTION_STRING" is not set. - */ - public LogRecordExporter buildLogRecordExporter() { - ConfigProperties defaultConfig = DefaultConfigProperties.create(Collections.emptyMap()); - return buildLogRecordExporter(defaultConfig); - } /** * Creates an Azure Monitor log record exporter based on the options set in the builder. This @@ -344,22 +319,6 @@ void internalBuildAndFreeze(ConfigProperties configProperties) { } } - /** - * Creates an Azure monitor metric exporter based on the options set in the builder. This - * exporter is an implementation of OpenTelemetry {@link MetricExporter}. - * - *

When a new {@link MetricExporter} is created, it will automatically start {@link - * HeartbeatExporter}. - * - * @return An instance of {@link MetricExporter}. - * @throws NullPointerException if the connection string is not set on this builder or if the - * environment variable "APPLICATIONINSIGHTS_CONNECTION_STRING" is not set. - */ - public MetricExporter buildMetricExporter() { - ConfigProperties defaultConfig = DefaultConfigProperties.create(Collections.emptyMap()); - return buildMetricExporter(defaultConfig); - } - /** * Creates an Azure monitor metric exporter based on the options set in the builder. This * exporter is an implementation of OpenTelemetry {@link MetricExporter}. From 14262fdff08a8eb92efe2efb809bce40f6ee51fe Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Tue, 17 Sep 2024 14:27:55 +0200 Subject: [PATCH 33/53] Remove empty line --- .../opentelemetry/exporter/AzureMonitorExporterBuilder.java | 1 - 1 file changed, 1 deletion(-) 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 8fe105054f9ab..aea0b0c0421d5 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 @@ -285,7 +285,6 @@ public SpanExporter buildSpanExporter(ConfigProperties configProperties) { statsbeatModule); } - /** * Creates an Azure Monitor log record exporter based on the options set in the builder. This * exporter is an implementation of OpenTelemetry {@link LogRecordExporter}. From a45ec2ca840187271ecba99c339ea8df260836ba Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Tue, 17 Sep 2024 15:46:19 +0200 Subject: [PATCH 34/53] Remove unused import --- .../opentelemetry/exporter/AzureMonitorExporterBuilder.java | 1 - 1 file changed, 1 deletion(-) 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 aea0b0c0421d5..0edd1055ff6c6 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 @@ -44,7 +44,6 @@ import com.azure.monitor.opentelemetry.exporter.implementation.utils.ResourceParser; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder; import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; -import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; import io.opentelemetry.sdk.logs.export.LogRecordExporter; import io.opentelemetry.sdk.metrics.export.MetricExporter; import io.opentelemetry.sdk.resources.Resource; From dc3528b31c84d766ce1b82ce6c455b8a38af17ce Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Tue, 17 Sep 2024 15:56:39 +0200 Subject: [PATCH 35/53] Update Hjavadoc of the HttpTrait methods --- .../exporter/AzureMonitorExporterBuilder.java | 79 ++++++++++++++----- 1 file changed, 59 insertions(+), 20 deletions(-) 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 0edd1055ff6c6..13ff963b48ac2 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 @@ -22,6 +22,7 @@ import com.azure.core.util.ClientOptions; import com.azure.core.util.Configuration; import com.azure.core.util.CoreUtils; +import com.azure.core.util.HttpClientOptions; import com.azure.core.util.builder.ClientBuilderUtil; import com.azure.core.util.logging.ClientLogger; import com.azure.monitor.opentelemetry.exporter.implementation.LogDataMapper; @@ -107,15 +108,20 @@ public AzureMonitorExporterBuilder() { } /** - * Sets the HTTP pipeline to use for the service client. If {@code httpPipeline} is set, all other - * settings are ignored. + * Sets the {@link HttpPipeline} to use for the service client. * - * @param httpPipeline The HTTP pipeline to use for sending service requests and receiving - * responses. + *

Note: It is important to understand the precedence order of the HttpTrait APIs. In + * particular, if a {@link HttpPipeline} is specified, this takes precedence over all other APIs in the trait, and + * they will be ignored. If no {@link HttpPipeline} is specified, a HTTP pipeline will be constructed internally + * based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this + * trait that are also ignored if an {@link HttpPipeline} is specified, so please be sure to refer to the + * documentation of types that implement this trait to understand the full set of implications.

+ * + * @param pipeline {@link HttpPipeline} to use for sending service requests and receiving responses. * @return The updated {@link AzureMonitorExporterBuilder} object. */ @Override - public AzureMonitorExporterBuilder pipeline(HttpPipeline httpPipeline) { + public AzureMonitorExporterBuilder pipeline(HttpPipeline pipeline) { if (frozen) { throw LOGGER.logExceptionAsError(new IllegalStateException( "httpPipeline cannot be changed after any of the build methods have been called")); @@ -124,10 +130,18 @@ public AzureMonitorExporterBuilder pipeline(HttpPipeline httpPipeline) { return this; } + /** - * Sets the HTTP client to use for sending and receiving requests to and from the service. + * Sets the {@link HttpClient} to use for sending and receiving requests to and from the service. * - * @param httpClient The HTTP client to use for requests. + *

Note: It is important to understand the precedence order of the HttpTrait APIs. In + * particular, if a {@link HttpPipeline} is specified, this takes precedence over all other APIs in the trait, and + * they will be ignored. If no {@link HttpPipeline} is specified, a HTTP pipeline will be constructed internally + * based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this + * trait that are also ignored if an {@link HttpPipeline} is specified, so please be sure to refer to the + * documentation of types that implement this trait to understand the full set of implications.

+ * + * @param httpClient The {@link HttpClient} to use for requests. * @return The updated {@link AzureMonitorExporterBuilder} object. */ @Override @@ -141,38 +155,51 @@ public AzureMonitorExporterBuilder httpClient(HttpClient httpClient) { } /** - * Sets the logging configuration for HTTP requests and responses. + * Sets the {@link HttpLogOptions logging configuration} to use when sending and receiving requests to and from + * the service. If a {@code logLevel} is not provided, default value of {@link HttpLogDetailLevel#NONE} is set. * - *

If logLevel is not provided, default value of {@link HttpLogDetailLevel#NONE} is set. + *

Note: It is important to understand the precedence order of the HttpTrait APIs. In + * particular, if a {@link HttpPipeline} is specified, this takes precedence over all other APIs in the trait, and + * they will be ignored. If no {@link HttpPipeline} is specified, a HTTP pipeline will be constructed internally + * based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this + * trait that are also ignored if an {@link HttpPipeline} is specified, so please be sure to refer to the + * documentation of types that implement this trait to understand the full set of implications.

* - * @param httpLogOptions The logging configuration to use when sending and receiving HTTP - * requests/responses. + * @param logOptions The {@link HttpLogOptions logging configuration} to use when sending and receiving requests to + * and from the service. * @return The updated {@link AzureMonitorExporterBuilder} object. */ @Override - public AzureMonitorExporterBuilder httpLogOptions(HttpLogOptions httpLogOptions) { + public AzureMonitorExporterBuilder httpLogOptions(HttpLogOptions logOptions) { if (frozen) { throw LOGGER.logExceptionAsError(new IllegalStateException( "httpLogOptions cannot be changed after any of the build methods have been called")); } - this.httpLogOptions = httpLogOptions; + this.httpLogOptions = logOptions; return this; } /** - * Adds a policy to the set of existing policies that are executed after required policies. + * Adds a {@link HttpPipelinePolicy pipeline policy} to apply on each request sent. + * + *

Note: It is important to understand the precedence order of the HttpTrait APIs. In + * particular, if a {@link HttpPipeline} is specified, this takes precedence over all other APIs in the trait, and + * they will be ignored. If no {@link HttpPipeline} is specified, a HTTP pipeline will be constructed internally + * based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this + * trait that are also ignored if an {@link HttpPipeline} is specified, so please be sure to refer to the + * documentation of types that implement this trait to understand the full set of implications.

* - * @param httpPipelinePolicy a policy to be added to the http pipeline. + * @param pipelinePolicy A {@link HttpPipelinePolicy pipeline policy}. + * @throws NullPointerException If {@code pipelinePolicy} is {@code null}. * @return The updated {@link AzureMonitorExporterBuilder} object. - * @throws NullPointerException If {@code policy} is {@code null}. */ @Override - public AzureMonitorExporterBuilder addPolicy(HttpPipelinePolicy httpPipelinePolicy) { + public AzureMonitorExporterBuilder addPolicy(HttpPipelinePolicy pipelinePolicy) { if (frozen) { throw LOGGER.logExceptionAsError(new IllegalStateException( "httpPipelinePolicy cannot be added after any of the build methods have been called")); } - httpPipelinePolicies.add(Objects.requireNonNull(httpPipelinePolicy, "'policy' cannot be null.")); + httpPipelinePolicies.add(Objects.requireNonNull(pipelinePolicy, "'policy' cannot be null.")); return this; } @@ -220,10 +247,22 @@ public AzureMonitorExporterBuilder retryPolicy(RetryPolicy retryPolicy) { } /** - * Sets the client options such as application ID and custom headers to set on a request. + * Allows for setting common properties such as application ID, headers, proxy configuration, etc. Note that it is + * recommended that this method be called with an instance of the {@link HttpClientOptions} + * class (a subclass of the {@link ClientOptions} base class). The HttpClientOptions subclass provides more + * configuration options suitable for HTTP clients, which is applicable for any class that implements this HttpTrait + * interface. + * + *

Note: It is important to understand the precedence order of the HttpTrait APIs. In + * particular, if a {@link HttpPipeline} is specified, this takes precedence over all other APIs in the trait, and + * they will be ignored. If no {@link HttpPipeline} is specified, a HTTP pipeline will be constructed internally + * based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this + * trait that are also ignored if an {@link HttpPipeline} is specified, so please be sure to refer to the + * documentation of types that implement this trait to understand the full set of implications.

* - * @param clientOptions The client options. + * @param clientOptions A configured instance of {@link HttpClientOptions}. * @return The updated {@link AzureMonitorExporterBuilder} object. + * @see HttpClientOptions */ @Override public AzureMonitorExporterBuilder clientOptions(ClientOptions clientOptions) { From b5f50f8cfd73fb48929913a85f4cdf8c22abdc0c Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Tue, 17 Sep 2024 17:10:08 +0200 Subject: [PATCH 36/53] configure method name renamed to customize, to look like https://github.com/open-telemetry/opentelemetry-java/blob/main/sdk-extensions/autoconfigure-spi/src/main/java/io/opentelemetry/sdk/autoconfigure/spi/AutoConfigurationCustomizerProvider.java --- .../README.md | 8 ++++---- .../monitor/opentelemetry/AzureMonitor.java | 19 ++++++++++--------- ...nfigurationAzureMonitorExporterSample.java | 2 +- .../AzureMonitorMetricExporterSample.java | 6 +++--- .../EventHubsAzureMonitorExporterSample.java | 2 +- .../opentelemetry/exporter/ReadmeSamples.java | 10 +++++----- .../AzureMonitorExporterBuilderTest.java | 2 +- .../implementation/utils/TestUtils.java | 2 +- 8 files changed, 26 insertions(+), 25 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index 4e240de2c9ff3..6e704832757ec 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -36,14 +36,14 @@ If you have set the Application Insights connection string with the `APPLICATION ```java readme-sample-autoconfigure-env-variable AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); -AzureMonitor.configure(sdkBuilder); +AzureMonitor.customize(sdkBuilder); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); ``` You can also se the connection string in the code: ```java readme-sample-autoconfigure AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); -AzureMonitor.configure(sdkBuilder, "{connection-string}"); +AzureMonitor.customize(sdkBuilder, "{connection-string}"); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); ``` @@ -57,7 +57,7 @@ The following example shows how create a span: ```java readme-sample-create-span AutoConfiguredOpenTelemetrySdkBuilder otelSdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); -AzureMonitor.configure(otelSdkBuilder, "{connection-string}"); +AzureMonitor.customize(otelSdkBuilder, "{connection-string}"); OpenTelemetry openTelemetry = otelSdkBuilder.build().getOpenTelemetrySdk(); Tracer tracer = openTelemetry.getTracer("Sample"); @@ -79,7 +79,7 @@ The following example demonstrates how to add a span processor to the OpenTeleme ```java readme-sample-span-processor AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); -AzureMonitor.configure(sdkBuilder); +AzureMonitor.customize(sdkBuilder); SpanProcessor spanProcessor = new SpanProcessor() { @Override diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java index 8f4703cc8678b..5c08905850a85 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java @@ -25,32 +25,33 @@ private AzureMonitor() { } /** - * Configures an {@link AutoConfigurationCustomizer} for Azure Monitor based on the options set. + * Customizes an {@link AutoConfigurationCustomizer} for Azure Monitor. The connection string to the Application Insights resource is expected to be configured with the APPLICATIONINSIGHTS_CONNECTION_STRING environment variable. + * * @param autoConfigurationCustomizer The OpenTelemetry autoconfiguration to set up. */ - public static void configure(AutoConfigurationCustomizer autoConfigurationCustomizer) { + public static void customize(AutoConfigurationCustomizer autoConfigurationCustomizer) { AzureMonitorExporterBuilder azureMonitorExporterBuilder = new AzureMonitorExporterBuilder(); - configure(autoConfigurationCustomizer, azureMonitorExporterBuilder); + customize(autoConfigurationCustomizer, azureMonitorExporterBuilder); } /** - * Configures an {@link AutoConfigurationCustomizer} for Azure Monitor based on the options set. + * Customizes an {@link AutoConfigurationCustomizer} for Azure Monitor. * @param autoConfigurationCustomizer The OpenTelemetry autoconfiguration to set up. * @param connectionString The connection string to connect to an Application Insights resource. */ - public static void configure(AutoConfigurationCustomizer autoConfigurationCustomizer, String connectionString) { + public static void customize(AutoConfigurationCustomizer autoConfigurationCustomizer, String connectionString) { AzureMonitorExporterBuilder azureMonitorExporterBuilder = new AzureMonitorExporterBuilder().connectionString(connectionString); - configure(autoConfigurationCustomizer, azureMonitorExporterBuilder); + customize(autoConfigurationCustomizer, azureMonitorExporterBuilder); } /** - * Configures an {@link AutoConfigurationCustomizer} for Azure Monitor based on the options set. + * Customizes an {@link AutoConfigurationCustomizer} for Azure Monitor. * @param autoConfigurationCustomizer the {@link AutoConfigurationCustomizer} object. * @param azureMonitorExporterBuilder Advanced configuration to send the data to Azure Monitor. */ - public static void configure(AutoConfigurationCustomizer autoConfigurationCustomizer, - AzureMonitorExporterBuilder azureMonitorExporterBuilder) { + public static void customize(AutoConfigurationCustomizer autoConfigurationCustomizer, + AzureMonitorExporterBuilder azureMonitorExporterBuilder) { autoConfigurationCustomizer.addPropertiesSupplier(() -> { Map props = new HashMap<>(); props.put("otel.traces.exporter", AzureMonitorExporterProviderKeys.EXPORTER_NAME); diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java index 1b64142893d7b..0e07e883032fd 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationAzureMonitorExporterSample.java @@ -40,7 +40,7 @@ public static void main(String[] args) { private static Tracer configureAzureMonitorExporter() { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(sdkBuilder, "{connection-string}"); + AzureMonitor.customize(sdkBuilder, "{connection-string}"); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); 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 index acc80e71b74bf..f3fdbe1d644e1 100644 --- 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 @@ -27,7 +27,7 @@ private static void sendDoubleHistogram() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(sdkBuilder, APPINSIGHTS_CONNECTION_STRING); + AzureMonitor.customize(sdkBuilder, APPINSIGHTS_CONNECTION_STRING); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); Meter meter = openTelemetry.meterBuilder("OTEL.AzureMonitor.Demo").build(); @@ -48,7 +48,7 @@ private static void sendLongCounter() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(sdkBuilder, APPINSIGHTS_CONNECTION_STRING); + AzureMonitor.customize(sdkBuilder, APPINSIGHTS_CONNECTION_STRING); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); Meter meter = openTelemetry.meterBuilder("OTEL.AzureMonitor.Demo").build(); @@ -72,7 +72,7 @@ private static void sendGaugeMetric() { try { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(sdkBuilder, APPINSIGHTS_CONNECTION_STRING); + AzureMonitor.customize(sdkBuilder, APPINSIGHTS_CONNECTION_STRING); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); Meter meter = openTelemetry.getMeter("OTEL.AzureMonitor.Demo"); diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java index 5fd61130a04de..b1ac4116950de 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/EventHubsAzureMonitorExporterSample.java @@ -51,7 +51,7 @@ public static void main(String[] args) { private static Tracer configureAzureMonitorExporter() { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(sdkBuilder, "{connection-string}"); + AzureMonitor.customize(sdkBuilder, "{connection-string}"); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java index f54d5f5bea8bd..3af326148c2f6 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java @@ -39,7 +39,7 @@ public void setupExporter() { // This should be done just once when application starts up AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(sdkBuilder, "{connection-string}"); + AzureMonitor.customize(sdkBuilder, "{connection-string}"); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); @@ -71,7 +71,7 @@ public void setupExporter() { public void exporterAndOpenTelemetryAutoconfigurationEnvVariable() { // BEGIN: readme-sample-autoconfigure-env-variable AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(sdkBuilder); + AzureMonitor.customize(sdkBuilder); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); // END: readme-sample-autoconfigure-env-variable } @@ -82,7 +82,7 @@ public void exporterAndOpenTelemetryAutoconfigurationEnvVariable() { public void exporterAndOpenTelemetryAutoconfiguration() { // BEGIN: readme-sample-autoconfigure AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(sdkBuilder, "{connection-string}"); + AzureMonitor.customize(sdkBuilder, "{connection-string}"); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); // END: readme-sample-autoconfigure } @@ -95,7 +95,7 @@ public void createSpan() { // BEGIN: readme-sample-create-span AutoConfiguredOpenTelemetrySdkBuilder otelSdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(otelSdkBuilder, "{connection-string}"); + AzureMonitor.customize(otelSdkBuilder, "{connection-string}"); OpenTelemetry openTelemetry = otelSdkBuilder.build().getOpenTelemetrySdk(); Tracer tracer = openTelemetry.getTracer("Sample"); @@ -121,7 +121,7 @@ public void spanProcessor() { // BEGIN: readme-sample-span-processor AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(sdkBuilder); + AzureMonitor.customize(sdkBuilder); SpanProcessor spanProcessor = new SpanProcessor() { @Override diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java index 8058e4911ec23..cbb336856b319 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilderTest.java @@ -23,7 +23,7 @@ public void testInvalidConnectionStrings(String con Class exceptionExpected) { Assertions.assertThrows(exceptionExpected, () -> { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - AzureMonitor.configure(sdkBuilder, connectionString); + AzureMonitor.customize(sdkBuilder, connectionString); sdkBuilder.build(); }); } diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java index d1d80f3f13400..95b08854e5926 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java @@ -85,7 +85,7 @@ public static OpenTelemetrySdk createOpenTelemetrySdk(HttpPipeline httpPipeline, AzureMonitorExporterBuilder azureMonitorExporterBuilder = new AzureMonitorExporterBuilder().connectionString(connectionString).pipeline(httpPipeline); - AzureMonitor.configure(sdkBuilder, azureMonitorExporterBuilder); + AzureMonitor.customize(sdkBuilder, azureMonitorExporterBuilder); return sdkBuilder.addPropertiesSupplier(() -> configuration).build().getOpenTelemetrySdk(); } From 718ccacc9e4fbfd416b62fc6e1cd253e28992cc0 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 18 Sep 2024 10:28:40 +0200 Subject: [PATCH 37/53] Update sdk/monitor/azure-monitor-opentelemetry-exporter/README.md Co-authored-by: Trask Stalnaker --- sdk/monitor/azure-monitor-opentelemetry-exporter/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index 6e704832757ec..85986e86c8e0b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -32,7 +32,7 @@ right corner. ### Setup the OpenTelemetry SDK to work with Azure Monitor exporter -If you have set the Application Insights connection string with the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable, you configure OpenTelemetry SDK auto-configuration for Azure in the following way: +If you have set the Application Insights connection string with the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable, you can configure OpenTelemetry SDK auto-configuration for Azure in the following way: ```java readme-sample-autoconfigure-env-variable AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); From 68ad45d7aaf81a0eb830a6e2dbce7545dd15dd79 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 18 Sep 2024 10:28:53 +0200 Subject: [PATCH 38/53] Update eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml Co-authored-by: Trask Stalnaker --- .../src/main/resources/checkstyle/checkstyle-suppressions.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml b/eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml index 66cd575eb1c1a..97cefbd83f3d9 100644 --- a/eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml +++ b/eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml @@ -78,7 +78,6 @@ - From 87d4c5b23c8c0d61ec48fc81653957c108d3856d Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 18 Sep 2024 10:29:16 +0200 Subject: [PATCH 39/53] Update sdk/monitor/azure-monitor-opentelemetry-exporter/README.md Co-authored-by: Trask Stalnaker --- sdk/monitor/azure-monitor-opentelemetry-exporter/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index 85986e86c8e0b..b77a6419c56d7 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -40,7 +40,7 @@ AzureMonitor.customize(sdkBuilder); OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); ``` -You can also se the connection string in the code: +You can also set the connection string in the code: ```java readme-sample-autoconfigure AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); AzureMonitor.customize(sdkBuilder, "{connection-string}"); From 2542a2ba90325f426d4645cfd934b6482f824b03 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 18 Sep 2024 10:29:33 +0200 Subject: [PATCH 40/53] Update sdk/monitor/azure-monitor-opentelemetry-exporter/README.md Co-authored-by: Trask Stalnaker --- sdk/monitor/azure-monitor-opentelemetry-exporter/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index b77a6419c56d7..afb1bada72c89 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -51,7 +51,6 @@ OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); The following sections provide code samples using the OpenTelemetry Azure Monitor Exporter client library and OpenTelemetry API. - The following example shows how create a span: ```java readme-sample-create-span From 44baff837034b1db3c427393788e074d9d022210 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 18 Sep 2024 10:30:03 +0200 Subject: [PATCH 41/53] Update sdk/monitor/azure-monitor-opentelemetry-exporter/README.md Co-authored-by: Trask Stalnaker --- sdk/monitor/azure-monitor-opentelemetry-exporter/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index afb1bada72c89..c8ff06ffdc965 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -49,7 +49,7 @@ OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk(); ## Examples -The following sections provide code samples using the OpenTelemetry Azure Monitor Exporter client library and OpenTelemetry API. +The following sections provide code samples using the OpenTelemetry Azure Monitor Exporter client library and OpenTelemetry SDK. The following example shows how create a span: From 4bc5cf2745172b907ff1dbd1efbb6870d7717bd1 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 18 Sep 2024 10:38:08 +0200 Subject: [PATCH 42/53] Remove star import --- .../exporter/implementation/utils/TestUtils.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java index 95b08854e5926..5d753c63c50b1 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/utils/TestUtils.java @@ -9,7 +9,13 @@ import com.azure.json.JsonToken; import com.azure.monitor.opentelemetry.AzureMonitor; import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterBuilder; -import com.azure.monitor.opentelemetry.exporter.implementation.models.*; +import com.azure.monitor.opentelemetry.exporter.implementation.models.MessageData; +import com.azure.monitor.opentelemetry.exporter.implementation.models.MetricDataPoint; +import com.azure.monitor.opentelemetry.exporter.implementation.models.MetricsData; +import com.azure.monitor.opentelemetry.exporter.implementation.models.MonitorBase; +import com.azure.monitor.opentelemetry.exporter.implementation.models.MonitorDomain; +import com.azure.monitor.opentelemetry.exporter.implementation.models.RemoteDependencyData; +import com.azure.monitor.opentelemetry.exporter.implementation.models.TelemetryItem; import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder; From 9f3e674cc235643fc3bfe9246d5dd8a2ab7eb3f1 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 18 Sep 2024 10:42:21 +0200 Subject: [PATCH 43/53] Use constant for attribute key --- sdk/monitor/azure-monitor-opentelemetry-exporter/README.md | 5 ++++- .../azure/monitor/opentelemetry/exporter/ReadmeSamples.java | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index c8ff06ffdc965..3b08749fb0d19 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -81,9 +81,12 @@ AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySd AzureMonitor.customize(sdkBuilder); SpanProcessor spanProcessor = new SpanProcessor() { + + private static final AttributeKey ATTRIBUTE_KEY = AttributeKey.stringKey("attributeKey"); + @Override public void onStart(Context context, ReadWriteSpan span) { - span.setAttribute(AttributeKey.stringKey("attributeKey"), "attributeValue"); + span.setAttribute(ATTRIBUTE_KEY, "attributeValue"); } @Override diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java index 3af326148c2f6..879685a1c6f81 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java @@ -124,9 +124,12 @@ public void spanProcessor() { AzureMonitor.customize(sdkBuilder); SpanProcessor spanProcessor = new SpanProcessor() { + + private static final AttributeKey ATTRIBUTE_KEY = AttributeKey.stringKey("attributeKey"); + @Override public void onStart(Context context, ReadWriteSpan span) { - span.setAttribute(AttributeKey.stringKey("attributeKey"), "attributeValue"); + span.setAttribute(ATTRIBUTE_KEY, "attributeValue"); } @Override From 993e554e3ba540e546b57fde17db84b5ecf8acd1 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 18 Sep 2024 18:19:44 +0200 Subject: [PATCH 44/53] Fix ReadmeSamples.java:[128,55] Illegal static declaration in inner class --- .../README.md | 58 ++++++++++--------- .../opentelemetry/exporter/ReadmeSamples.java | 12 ++-- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index 3b08749fb0d19..4c37c042f0094 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -76,37 +76,39 @@ try (Scope scope = span.makeCurrent()) { The following example demonstrates how to add a span processor to the OpenTelemetry SDK autoconfiguration. ```java readme-sample-span-processor -AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); + private static final AttributeKey ATTRIBUTE_KEY = AttributeKey.stringKey("attributeKey"); -AzureMonitor.customize(sdkBuilder); +public void spanProcessor() { + AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); -SpanProcessor spanProcessor = new SpanProcessor() { + AzureMonitor.customize(sdkBuilder); - private static final AttributeKey ATTRIBUTE_KEY = AttributeKey.stringKey("attributeKey"); - - @Override - public void onStart(Context context, ReadWriteSpan span) { - span.setAttribute(ATTRIBUTE_KEY, "attributeValue"); - } - - @Override - public boolean isStartRequired() { - return true; - } - - @Override - public void onEnd(ReadableSpan readableSpan) { - } - - @Override - public boolean isEndRequired() { - return false; - } -}; - -sdkBuilder.addTracerProviderCustomizer( - (sdkTracerProviderBuilder, configProperties) -> sdkTracerProviderBuilder - .addSpanProcessor(spanProcessor)); + SpanProcessor spanProcessor = new SpanProcessor() { + + @Override + public void onStart(Context context, ReadWriteSpan span) { + span.setAttribute(ATTRIBUTE_KEY, "attributeValue"); + } + + @Override + public boolean isStartRequired() { + return true; + } + + @Override + public void onEnd(ReadableSpan readableSpan) { + } + + @Override + public boolean isEndRequired() { + return false; + } + }; + + sdkBuilder.addTracerProviderCustomizer( + (sdkTracerProviderBuilder, configProperties) -> sdkTracerProviderBuilder + .addSpanProcessor(spanProcessor)); +} ``` More advanced examples with OpenTelemetry APIs: * [Advanced examples - 1][advanced_examples_1] diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java index 879685a1c6f81..6888ae19a0d09 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/samples/java/com/azure/monitor/opentelemetry/exporter/ReadmeSamples.java @@ -114,19 +114,16 @@ public void createSpan() { // END: readme-sample-create-span } - /** - * Sample to add a span processor to the OpenTelemetry SDK auto-configuration - */ + // BEGIN: readme-sample-span-processor + private static final AttributeKey ATTRIBUTE_KEY = AttributeKey.stringKey("attributeKey"); + public void spanProcessor() { - // BEGIN: readme-sample-span-processor AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); AzureMonitor.customize(sdkBuilder); SpanProcessor spanProcessor = new SpanProcessor() { - private static final AttributeKey ATTRIBUTE_KEY = AttributeKey.stringKey("attributeKey"); - @Override public void onStart(Context context, ReadWriteSpan span) { span.setAttribute(ATTRIBUTE_KEY, "attributeValue"); @@ -150,6 +147,7 @@ public boolean isEndRequired() { sdkBuilder.addTracerProviderCustomizer( (sdkTracerProviderBuilder, configProperties) -> sdkTracerProviderBuilder .addSpanProcessor(spanProcessor)); - // END: readme-sample-span-processor } + // END: readme-sample-span-processor + } From 92ef3f5c39c100cf62424579941bb4b43cffda4e Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 18 Sep 2024 18:33:12 +0200 Subject: [PATCH 45/53] Try to fix code snippet issue --- sdk/monitor/azure-monitor-opentelemetry-exporter/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index 4c37c042f0094..ee0135f6ca759 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -76,7 +76,7 @@ try (Scope scope = span.makeCurrent()) { The following example demonstrates how to add a span processor to the OpenTelemetry SDK autoconfiguration. ```java readme-sample-span-processor - private static final AttributeKey ATTRIBUTE_KEY = AttributeKey.stringKey("attributeKey"); +private static final AttributeKey ATTRIBUTE_KEY = AttributeKey.stringKey("attributeKey"); public void spanProcessor() { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); From 51d6774174d2d5132d52f42ee3b586f157f6c1f5 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 18 Sep 2024 18:46:44 +0200 Subject: [PATCH 46/53] spotless --- .../main/java/com/azure/monitor/opentelemetry/AzureMonitor.java | 2 +- .../opentelemetry/exporter/AzureMonitorExporterBuilder.java | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java index 5c08905850a85..5baf17c068c50 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/AzureMonitor.java @@ -51,7 +51,7 @@ public static void customize(AutoConfigurationCustomizer autoConfigurationCustom * @param azureMonitorExporterBuilder Advanced configuration to send the data to Azure Monitor. */ public static void customize(AutoConfigurationCustomizer autoConfigurationCustomizer, - AzureMonitorExporterBuilder azureMonitorExporterBuilder) { + AzureMonitorExporterBuilder azureMonitorExporterBuilder) { autoConfigurationCustomizer.addPropertiesSupplier(() -> { Map props = new HashMap<>(); props.put("otel.traces.exporter", AzureMonitorExporterProviderKeys.EXPORTER_NAME); 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 13ff963b48ac2..6edc1c520e630 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 @@ -130,7 +130,6 @@ public AzureMonitorExporterBuilder pipeline(HttpPipeline pipeline) { return this; } - /** * Sets the {@link HttpClient} to use for sending and receiving requests to and from the service. * From 4edb2c9c52ba152f04e7ac409268d15072f89936 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Thu, 19 Sep 2024 11:02:28 +0200 Subject: [PATCH 47/53] Fix --- .../opentelemetry/exporter/AzureMonitorExporterBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 6edc1c520e630..30c115fc2e4be 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 @@ -126,7 +126,7 @@ public AzureMonitorExporterBuilder pipeline(HttpPipeline pipeline) { throw LOGGER.logExceptionAsError(new IllegalStateException( "httpPipeline cannot be changed after any of the build methods have been called")); } - this.httpPipeline = httpPipeline; + this.httpPipeline = pipeline; return this; } From afe3c1c209efe20e8adc4a208efc2ee20e691ef6 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Thu, 19 Sep 2024 11:14:25 +0200 Subject: [PATCH 48/53] Remove retryPolicy method --- .../exporter/AzureMonitorExporterBuilder.java | 29 ++----------------- 1 file changed, 3 insertions(+), 26 deletions(-) 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 30c115fc2e4be..ab408f7d0c18f 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 @@ -23,7 +23,6 @@ import com.azure.core.util.Configuration; import com.azure.core.util.CoreUtils; import com.azure.core.util.HttpClientOptions; -import com.azure.core.util.builder.ClientBuilderUtil; import com.azure.core.util.logging.ClientLogger; import com.azure.monitor.opentelemetry.exporter.implementation.LogDataMapper; import com.azure.monitor.opentelemetry.exporter.implementation.MetricDataMapper; @@ -91,7 +90,6 @@ public final class AzureMonitorExporterBuilder implements ConnectionStringTrait< private final List httpPipelinePolicies = new ArrayList<>(); private ClientOptions clientOptions; private RetryOptions retryOptions; - private RetryPolicy retryPolicy; private boolean frozen; @@ -212,7 +210,6 @@ public AzureMonitorExporterBuilder addPolicy(HttpPipelinePolicy pipelinePolicy) * trait that are also ignored if an {@link HttpPipeline} is specified, so please be sure to refer to the * documentation of types that implement this trait to understand the full set of implications.

*

- * Setting this is mutually exclusive with using {@link #retryPolicy(RetryPolicy)}. * * @param retryOptions The {@link RetryOptions} to use for all the requests made through the client. * @return The updated {@link AzureMonitorExporterBuilder} object. @@ -227,24 +224,6 @@ public AzureMonitorExporterBuilder retryOptions(RetryOptions retryOptions) { return this; } - /** - * Sets the request {@link RetryPolicy} for all the requests made through the client. The default - * {@link RetryPolicy} will be used in the pipeline, if not provided. - * Setting this is mutually exclusive with using {@link #retryOptions(RetryOptions)}. - * - * @param retryPolicy {@link RetryPolicy}. - * - * @return The updated {@link AzureMonitorExporterBuilder}. - */ - public AzureMonitorExporterBuilder retryPolicy(RetryPolicy retryPolicy) { - if (frozen) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "retryPolicy cannot be changed after any of the build methods have been called")); - } - this.retryPolicy = retryPolicy; - return this; - } - /** * Allows for setting common properties such as application ID, headers, proxy configuration, etc. Note that it is * recommended that this method be called with an instance of the {@link HttpClientOptions} @@ -436,10 +415,6 @@ private HttpPipeline createHttpPipeline() { throw LOGGER.logExceptionAsError(new IllegalStateException( "'clientOptions' is not supported when custom 'httpPipeline' is specified")); } - if (retryPolicy != null) { - throw LOGGER.logExceptionAsError(new IllegalStateException( - "'retryPolicy' is not supported when custom 'httpPipeline' is specified")); - } if (retryOptions != null) { throw LOGGER.logExceptionAsError(new IllegalStateException( "'retryOptions' is not supported when custom 'httpPipeline' is specified")); @@ -460,7 +435,9 @@ private HttpPipeline createHttpPipeline() { policies.add(new BearerTokenAuthenticationPolicy(credential, APPLICATIONINSIGHTS_AUTHENTICATION_SCOPE)); } - policies.add(ClientBuilderUtil.validateAndGetRetryPolicy(retryPolicy, retryOptions, new RetryPolicy())); + if(retryOptions != null) { + policies.add(new RetryPolicy(retryOptions)); + } policies.addAll(httpPipelinePolicies); policies.add(new HttpLoggingPolicy(httpLogOptions)); From 939490c3985e0fd8fccb61b9fd989151910a2cd0 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Thu, 19 Sep 2024 11:24:57 +0200 Subject: [PATCH 49/53] spotless --- .../opentelemetry/exporter/AzureMonitorExporterBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 ab408f7d0c18f..9e03322107225 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 @@ -435,7 +435,7 @@ private HttpPipeline createHttpPipeline() { policies.add(new BearerTokenAuthenticationPolicy(credential, APPLICATIONINSIGHTS_AUTHENTICATION_SCOPE)); } - if(retryOptions != null) { + if (retryOptions != null) { policies.add(new RetryPolicy(retryOptions)); } From edfedfe6f3f6ba0711b73f18f2f457a216556e63 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Thu, 19 Sep 2024 11:39:23 +0200 Subject: [PATCH 50/53] Fix Javadoc --- .../opentelemetry/exporter/AzureMonitorExporterBuilder.java | 2 -- 1 file changed, 2 deletions(-) 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 9e03322107225..7bb1ab3659696 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 @@ -209,7 +209,6 @@ public AzureMonitorExporterBuilder addPolicy(HttpPipelinePolicy pipelinePolicy) * based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this * trait that are also ignored if an {@link HttpPipeline} is specified, so please be sure to refer to the * documentation of types that implement this trait to understand the full set of implications.

- *

* * @param retryOptions The {@link RetryOptions} to use for all the requests made through the client. * @return The updated {@link AzureMonitorExporterBuilder} object. @@ -341,7 +340,6 @@ void internalBuildAndFreeze(ConfigProperties configProperties) { *

When a new {@link MetricExporter} is created, it will automatically start {@link * HeartbeatExporter}. * - * * @param configProperties The OpenTelemetry configuration properties. * @return An instance of {@link MetricExporter}. * @throws NullPointerException if the connection string is not set on this builder or if the From 2a3636a20f88a6e1794eed8aeca55848b4763241 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Thu, 19 Sep 2024 21:16:15 +0200 Subject: [PATCH 51/53] Bump some projects to the last exporter version to make them compile --- .../azure-messaging-eventhubs-stress/pom.xml | 2 +- .../eventhubs/stress/util/TelemetryHelper.java | 6 ++---- .../azure-messaging-servicebus-stress/pom.xml | 2 +- .../servicebus/stress/util/TelemetryHelper.java | 6 ++---- .../spring-cloud-azure-starter-monitor/pom.xml | 2 +- .../AzureSpringMonitorAutoConfiguration.java | 16 ++++++++-------- sdk/template/azure-template-stress/pom.xml | 2 +- .../template/stress/util/TelemetryHelper.java | 6 ++---- 8 files changed, 18 insertions(+), 24 deletions(-) diff --git a/sdk/eventhubs/azure-messaging-eventhubs-stress/pom.xml b/sdk/eventhubs/azure-messaging-eventhubs-stress/pom.xml index 4b6dd8bd846dc..928471551cd2d 100644 --- a/sdk/eventhubs/azure-messaging-eventhubs-stress/pom.xml +++ b/sdk/eventhubs/azure-messaging-eventhubs-stress/pom.xml @@ -50,7 +50,7 @@ com.azure azure-monitor-opentelemetry-exporter - 1.0.0-beta.28 + 1.0.0-beta.29 io.opentelemetry.instrumentation diff --git a/sdk/eventhubs/azure-messaging-eventhubs-stress/src/main/java/com/azure/messaging/eventhubs/stress/util/TelemetryHelper.java b/sdk/eventhubs/azure-messaging-eventhubs-stress/src/main/java/com/azure/messaging/eventhubs/stress/util/TelemetryHelper.java index 66c4e7273a697..1463a38ddc3f8 100644 --- a/sdk/eventhubs/azure-messaging-eventhubs-stress/src/main/java/com/azure/messaging/eventhubs/stress/util/TelemetryHelper.java +++ b/sdk/eventhubs/azure-messaging-eventhubs-stress/src/main/java/com/azure/messaging/eventhubs/stress/util/TelemetryHelper.java @@ -8,7 +8,7 @@ import com.azure.messaging.eventhubs.EventHubClientBuilder; import com.azure.messaging.eventhubs.models.CloseContext; import com.azure.messaging.eventhubs.models.InitializationContext; -import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterBuilder; +import com.azure.monitor.opentelemetry.AzureMonitor; import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; @@ -96,9 +96,7 @@ private static OpenTelemetry init() { } AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - new AzureMonitorExporterBuilder() - .connectionString(applicationInsightsConnectionString) - .install(sdkBuilder); + AzureMonitor.customize(sdkBuilder, applicationInsightsConnectionString); String instanceId = System.getenv("CONTAINER_NAME"); OpenTelemetry otel = sdkBuilder diff --git a/sdk/servicebus/azure-messaging-servicebus-stress/pom.xml b/sdk/servicebus/azure-messaging-servicebus-stress/pom.xml index b8ec6d178f780..6721bed101cca 100644 --- a/sdk/servicebus/azure-messaging-servicebus-stress/pom.xml +++ b/sdk/servicebus/azure-messaging-servicebus-stress/pom.xml @@ -52,7 +52,7 @@ com.azure azure-monitor-opentelemetry-exporter - 1.0.0-beta.28 + 1.0.0-beta.29 io.opentelemetry.instrumentation diff --git a/sdk/servicebus/azure-messaging-servicebus-stress/src/main/java/com/azure/messaging/servicebus/stress/util/TelemetryHelper.java b/sdk/servicebus/azure-messaging-servicebus-stress/src/main/java/com/azure/messaging/servicebus/stress/util/TelemetryHelper.java index 85ed2ebb17c95..8dcf19ffc0cfe 100644 --- a/sdk/servicebus/azure-messaging-servicebus-stress/src/main/java/com/azure/messaging/servicebus/stress/util/TelemetryHelper.java +++ b/sdk/servicebus/azure-messaging-servicebus-stress/src/main/java/com/azure/messaging/servicebus/stress/util/TelemetryHelper.java @@ -7,7 +7,7 @@ import com.azure.core.util.logging.LoggingEventBuilder; import com.azure.messaging.servicebus.ServiceBusClientBuilder; import com.azure.messaging.servicebus.ServiceBusException; -import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterBuilder; +import com.azure.monitor.opentelemetry.AzureMonitor; import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; @@ -75,9 +75,7 @@ private static OpenTelemetry init() { } AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); if (applicationInsightsConnectionString != null) { - new AzureMonitorExporterBuilder() - .connectionString(applicationInsightsConnectionString) - .install(sdkBuilder); + AzureMonitor.customize(sdkBuilder, applicationInsightsConnectionString); } String instanceId = System.getenv("CONTAINER_NAME"); diff --git a/sdk/spring/spring-cloud-azure-starter-monitor/pom.xml b/sdk/spring/spring-cloud-azure-starter-monitor/pom.xml index 7f40259d7069e..672419e78ca0d 100644 --- a/sdk/spring/spring-cloud-azure-starter-monitor/pom.xml +++ b/sdk/spring/spring-cloud-azure-starter-monitor/pom.xml @@ -88,7 +88,7 @@ com.azure azure-monitor-opentelemetry-exporter - 1.0.0-beta.28 + 1.0.0-beta.29 diff --git a/sdk/spring/spring-cloud-azure-starter-monitor/src/main/java/com/azure/spring/cloud/autoconfigure/monitor/AzureSpringMonitorAutoConfiguration.java b/sdk/spring/spring-cloud-azure-starter-monitor/src/main/java/com/azure/spring/cloud/autoconfigure/monitor/AzureSpringMonitorAutoConfiguration.java index fc68f89d75783..5a49e10d3dace 100644 --- a/sdk/spring/spring-cloud-azure-starter-monitor/src/main/java/com/azure/spring/cloud/autoconfigure/monitor/AzureSpringMonitorAutoConfiguration.java +++ b/sdk/spring/spring-cloud-azure-starter-monitor/src/main/java/com/azure/spring/cloud/autoconfigure/monitor/AzureSpringMonitorAutoConfiguration.java @@ -4,6 +4,7 @@ package com.azure.spring.cloud.autoconfigure.monitor; import com.azure.core.http.HttpPipeline; +import com.azure.monitor.opentelemetry.AzureMonitor; import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterBuilder; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder; @@ -104,15 +105,14 @@ public void customize(AutoConfigurationCustomizer autoConfigurationCustomizer) { throw new WrongConnectionStringException(); } - if (autoConfigurationCustomizer instanceof AutoConfiguredOpenTelemetrySdkBuilder) { - AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = (AutoConfiguredOpenTelemetrySdkBuilder) autoConfigurationCustomizer; + AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = (AutoConfiguredOpenTelemetrySdkBuilder) autoConfigurationCustomizer; - AzureMonitorExporterBuilder azureMonitorExporterBuilder = new AzureMonitorExporterBuilder().connectionString(connectionString); - HttpPipeline providedHttpPipeline = httpPipeline.getIfAvailable(); - if (providedHttpPipeline != null) { - azureMonitorExporterBuilder = azureMonitorExporterBuilder.httpPipeline(providedHttpPipeline); - } - azureMonitorExporterBuilder.install(sdkBuilder); + HttpPipeline providedHttpPipeline = httpPipeline.getIfAvailable(); + if (providedHttpPipeline != null) { + AzureMonitorExporterBuilder azureMonitorExporterBuilder = new AzureMonitorExporterBuilder().connectionString(connectionString).pipeline(providedHttpPipeline); + AzureMonitor.customize(sdkBuilder, azureMonitorExporterBuilder); + } else { + AzureMonitor.customize(sdkBuilder, connectionString); } } diff --git a/sdk/template/azure-template-stress/pom.xml b/sdk/template/azure-template-stress/pom.xml index 32d31fb01cccb..f322b18230b0e 100644 --- a/sdk/template/azure-template-stress/pom.xml +++ b/sdk/template/azure-template-stress/pom.xml @@ -71,7 +71,7 @@ com.azure azure-monitor-opentelemetry-exporter - 1.0.0-beta.28 + 1.0.0-beta.29 io.opentelemetry.instrumentation diff --git a/sdk/template/azure-template-stress/src/main/java/com/azure/sdk/template/stress/util/TelemetryHelper.java b/sdk/template/azure-template-stress/src/main/java/com/azure/sdk/template/stress/util/TelemetryHelper.java index 5eab79af92bfb..f7d0873779aa7 100644 --- a/sdk/template/azure-template-stress/src/main/java/com/azure/sdk/template/stress/util/TelemetryHelper.java +++ b/sdk/template/azure-template-stress/src/main/java/com/azure/sdk/template/stress/util/TelemetryHelper.java @@ -5,7 +5,7 @@ import com.azure.core.util.CoreUtils; import com.azure.core.util.logging.ClientLogger; -import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterBuilder; +import com.azure.monitor.opentelemetry.AzureMonitor; import com.azure.perf.test.core.PerfStressOptions; import com.azure.sdk.template.stress.StressOptions; import io.opentelemetry.api.GlobalOpenTelemetry; @@ -87,9 +87,7 @@ public static void init() { AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); String applicationInsightsConnectionString = System.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING"); if (applicationInsightsConnectionString != null) { - new AzureMonitorExporterBuilder() - .connectionString(applicationInsightsConnectionString) - .install(sdkBuilder); + AzureMonitor.customize(sdkBuilder, applicationInsightsConnectionString); } else { System.setProperty("otel.traces.exporter", "none"); System.setProperty("otel.logs.exporter", "none"); From 28050d3dbaf1fcceeb608d1b47aa7a3de1a15efb Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Thu, 19 Sep 2024 22:10:51 +0200 Subject: [PATCH 52/53] Add unreleased to version_client.txt --- eng/versioning/version_client.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/eng/versioning/version_client.txt b/eng/versioning/version_client.txt index 7aaaaeae2697b..dc91b8220c358 100644 --- a/eng/versioning/version_client.txt +++ b/eng/versioning/version_client.txt @@ -492,6 +492,8 @@ unreleased_com.azure:azure-messaging-servicebus;7.18.0-beta.2 unreleased_com.azure:azure-messaging-eventhubs;5.19.0-beta.3 unreleased_com.azure:azure-messaging-eventhubs-checkpointstore-blob;1.20.0-beta.3 +unreleased_com.azure:azure-monitor-opentelemetry-exporter;1.0.0-beta.29 + # Released Beta dependencies: Copy the entry from above, prepend "beta_", remove the current # version and set the version to the released beta. Released beta dependencies are only valid # for dependency versions. These entries are specifically for when we've released a beta for From f335fa002c18435016ae4266a19e22969c29edd5 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Fri, 20 Sep 2024 15:22:26 +0200 Subject: [PATCH 53/53] Update checkstyle-suppressions after having rebased --- .../src/main/resources/checkstyle/checkstyle-suppressions.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml b/eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml index 97cefbd83f3d9..9278d5d1c3ce5 100644 --- a/eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml +++ b/eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml @@ -75,7 +75,7 @@ - +