Skip to content

Commit

Permalink
API change of the Azure Monitor exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanbisutti committed Sep 30, 2024
1 parent 65d97c1 commit cac5605
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@
files="com.azure.containers.containerregistry.ContainerRegistryContentClient.java"/>

<suppress checks="com.azure.tools.checkstyle.checks.ExternalDependencyExposedCheck"
files="com.azure.monitor.opentelemetry.AzureMonitor.java"/>
files="com.azure.monitor.opentelemetry.exporter.AzureMonitorExporter.java"/>

<suppress checks="com.azure.tools.checkstyle.checks.ExternalDependencyExposedCheck"
files="com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterBuilder"/>
files="com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterOptions"/>

<!-- JavadocInlineTagCheck Suppression for now, which need code owner's attention -->
<suppress checks="com.azure.tools.checkstyle.checks.JavadocInlineTagCheck"
Expand Down
8 changes: 4 additions & 4 deletions sdk/monitor/azure-monitor-opentelemetry-exporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.customize(sdkBuilder);
AzureMonitorExporter.customize(sdkBuilder);
OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk();
```

You can also set the connection string in the code:
```java readme-sample-autoconfigure
AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder();
AzureMonitor.customize(sdkBuilder, "{connection-string}");
AzureMonitorExporter.customize(sdkBuilder, "{connection-string}");
OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk();
```

Expand All @@ -56,7 +56,7 @@ The following example shows how create a span:
```java readme-sample-create-span
AutoConfiguredOpenTelemetrySdkBuilder otelSdkBuilder = AutoConfiguredOpenTelemetrySdk.builder();

AzureMonitor.customize(otelSdkBuilder, "{connection-string}");
AzureMonitorExporter.customize(otelSdkBuilder, "{connection-string}");

OpenTelemetry openTelemetry = otelSdkBuilder.build().getOpenTelemetrySdk();
Tracer tracer = openTelemetry.getTracer("Sample");
Expand All @@ -82,7 +82,7 @@ private static final AttributeKey<String> ATTRIBUTE_KEY = AttributeKey.stringKey
public void spanProcessor() {
AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder();

AzureMonitor.customize(sdkBuilder);
AzureMonitorExporter.customize(sdkBuilder);

SpanProcessor spanProcessor = new SpanProcessor() {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.monitor.opentelemetry;
package com.azure.monitor.opentelemetry.exporter;

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;
Expand All @@ -19,9 +18,9 @@
/**
* Class to enable Azure Monitor for OpenTelemetry autoconfiguration.
*/
public final class AzureMonitor {
public final class AzureMonitorExporter {

private AzureMonitor() {
private AzureMonitorExporter() {
}

/**
Expand All @@ -30,8 +29,8 @@ private AzureMonitor() {
* @param autoConfigurationCustomizer The OpenTelemetry autoconfiguration to set up.
*/
public static void customize(AutoConfigurationCustomizer autoConfigurationCustomizer) {
AzureMonitorExporterBuilder azureMonitorExporterBuilder = new AzureMonitorExporterBuilder();
customize(autoConfigurationCustomizer, azureMonitorExporterBuilder);
AzureMonitorExporterOptions exporterOptions = new AzureMonitorExporterOptions();
customize(autoConfigurationCustomizer, exporterOptions);
}

/**
Expand All @@ -40,18 +39,18 @@ public static void customize(AutoConfigurationCustomizer autoConfigurationCustom
* @param connectionString The connection string to connect to an Application Insights resource.
*/
public static void customize(AutoConfigurationCustomizer autoConfigurationCustomizer, String connectionString) {
AzureMonitorExporterBuilder azureMonitorExporterBuilder
= new AzureMonitorExporterBuilder().connectionString(connectionString);
customize(autoConfigurationCustomizer, azureMonitorExporterBuilder);
AzureMonitorExporterOptions exporterOptions
= new AzureMonitorExporterOptions().connectionString(connectionString);
customize(autoConfigurationCustomizer, exporterOptions);
}

/**
* Customizes an {@link AutoConfigurationCustomizer} for Azure Monitor.
* @param autoConfigurationCustomizer the {@link AutoConfigurationCustomizer} object.
* @param azureMonitorExporterBuilder Advanced configuration to send the data to Azure Monitor.
* @param exporterOptions Advanced configuration to send the data to Azure Monitor.
*/
public static void customize(AutoConfigurationCustomizer autoConfigurationCustomizer,
AzureMonitorExporterBuilder azureMonitorExporterBuilder) {
AzureMonitorExporterOptions exporterOptions) {
autoConfigurationCustomizer.addPropertiesSupplier(() -> {
Map<String, String> props = new HashMap<>();
props.put("otel.traces.exporter", AzureMonitorExporterProviderKeys.EXPORTER_NAME);
Expand All @@ -62,19 +61,19 @@ public static void customize(AutoConfigurationCustomizer autoConfigurationCustom
});
autoConfigurationCustomizer.addSpanExporterCustomizer((spanExporter, configProperties) -> {
if (spanExporter instanceof AzureMonitorSpanExporterProvider.MarkerSpanExporter) {
spanExporter = azureMonitorExporterBuilder.buildSpanExporter(configProperties);
spanExporter = exporterOptions.buildSpanExporter(configProperties);
}
return spanExporter;
});
autoConfigurationCustomizer.addMetricExporterCustomizer((metricExporter, configProperties) -> {
if (metricExporter instanceof AzureMonitorMetricExporterProvider.MarkerMetricExporter) {
metricExporter = azureMonitorExporterBuilder.buildMetricExporter(configProperties);
metricExporter = exporterOptions.buildMetricExporter(configProperties);
}
return metricExporter;
});
autoConfigurationCustomizer.addLogRecordExporterCustomizer((logRecordExporter, configProperties) -> {
if (logRecordExporter instanceof AzureMonitorLogRecordExporterProvider.MarkerLogRecordExporter) {
logRecordExporter = azureMonitorExporterBuilder.buildLogRecordExporter(configProperties);
logRecordExporter = exporterOptions.buildLogRecordExporter(configProperties);
}
return logRecordExporter;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
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;
Expand Down Expand Up @@ -42,7 +39,6 @@
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.logs.export.LogRecordExporter;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
Expand All @@ -62,13 +58,12 @@
import static java.util.concurrent.TimeUnit.MINUTES;

/**
* 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}.
* Class allowing you to create options to configure the Azure Monitor export.
*/
@Fluent
public final class AzureMonitorExporterBuilder implements ConnectionStringTrait<AzureMonitorExporterBuilder>,
TokenCredentialTrait<AzureMonitorExporterBuilder>, HttpTrait<AzureMonitorExporterBuilder> {
public final class AzureMonitorExporterOptions {

private static final ClientLogger LOGGER = new ClientLogger(AzureMonitorExporterBuilder.class);
private static final ClientLogger LOGGER = new ClientLogger(AzureMonitorExporterOptions.class);

private static final String APPLICATIONINSIGHTS_CONNECTION_STRING = "APPLICATIONINSIGHTS_CONNECTION_STRING";
private static final String APPLICATIONINSIGHTS_AUTHENTICATION_SCOPE = "https://monitor.azure.com//.default";
Expand Down Expand Up @@ -100,9 +95,9 @@ public final class AzureMonitorExporterBuilder implements ConnectionStringTrait<
private StatsbeatModule statsbeatModule;

/**
* Creates an instance of {@link AzureMonitorExporterBuilder}.
* Creates an instance of {@link AzureMonitorExporterOptions}.
*/
public AzureMonitorExporterBuilder() {
public AzureMonitorExporterOptions() {
}

/**
Expand All @@ -116,10 +111,9 @@ public AzureMonitorExporterBuilder() {
* documentation of types that implement this trait to understand the full set of implications.</p>
*
* @param pipeline {@link HttpPipeline} to use for sending service requests and receiving responses.
* @return The updated {@link AzureMonitorExporterBuilder} object.
* @return The updated {@link AzureMonitorExporterOptions} object.
*/
@Override
public AzureMonitorExporterBuilder pipeline(HttpPipeline pipeline) {
public AzureMonitorExporterOptions pipeline(HttpPipeline pipeline) {
if (frozen) {
throw LOGGER.logExceptionAsError(new IllegalStateException(
"httpPipeline cannot be changed after any of the build methods have been called"));
Expand All @@ -139,10 +133,9 @@ public AzureMonitorExporterBuilder pipeline(HttpPipeline pipeline) {
* documentation of types that implement this trait to understand the full set of implications.</p>
*
* @param httpClient The {@link HttpClient} to use for requests.
* @return The updated {@link AzureMonitorExporterBuilder} object.
* @return The updated {@link AzureMonitorExporterOptions} object.
*/
@Override
public AzureMonitorExporterBuilder httpClient(HttpClient httpClient) {
public AzureMonitorExporterOptions httpClient(HttpClient httpClient) {
if (frozen) {
throw LOGGER.logExceptionAsError(new IllegalStateException(
"httpClient cannot be changed after any of the build methods have been called"));
Expand All @@ -164,10 +157,9 @@ public AzureMonitorExporterBuilder httpClient(HttpClient httpClient) {
*
* @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.
* @return The updated {@link AzureMonitorExporterOptions} object.
*/
@Override
public AzureMonitorExporterBuilder httpLogOptions(HttpLogOptions logOptions) {
public AzureMonitorExporterOptions httpLogOptions(HttpLogOptions logOptions) {
if (frozen) {
throw LOGGER.logExceptionAsError(new IllegalStateException(
"httpLogOptions cannot be changed after any of the build methods have been called"));
Expand All @@ -188,10 +180,9 @@ public AzureMonitorExporterBuilder httpLogOptions(HttpLogOptions logOptions) {
*
* @param pipelinePolicy A {@link HttpPipelinePolicy pipeline policy}.
* @throws NullPointerException If {@code pipelinePolicy} is {@code null}.
* @return The updated {@link AzureMonitorExporterBuilder} object.
* @return The updated {@link AzureMonitorExporterOptions} object.
*/
@Override
public AzureMonitorExporterBuilder addPolicy(HttpPipelinePolicy pipelinePolicy) {
public AzureMonitorExporterOptions addPolicy(HttpPipelinePolicy pipelinePolicy) {
if (frozen) {
throw LOGGER.logExceptionAsError(new IllegalStateException(
"httpPipelinePolicy cannot be added after any of the build methods have been called"));
Expand All @@ -211,10 +202,9 @@ public AzureMonitorExporterBuilder addPolicy(HttpPipelinePolicy pipelinePolicy)
* documentation of types that implement this trait to understand the full set of implications.</p>
*
* @param retryOptions The {@link RetryOptions} to use for all the requests made through the client.
* @return The updated {@link AzureMonitorExporterBuilder} object.
* @return The updated {@link AzureMonitorExporterOptions} object.
*/
@Override
public AzureMonitorExporterBuilder retryOptions(RetryOptions retryOptions) {
public AzureMonitorExporterOptions retryOptions(RetryOptions retryOptions) {
if (frozen) {
throw LOGGER.logExceptionAsError(new IllegalStateException(
"retryOptions cannot be changed after any of the build methods have been called"));
Expand All @@ -238,11 +228,10 @@ public AzureMonitorExporterBuilder retryOptions(RetryOptions retryOptions) {
* documentation of types that implement this trait to understand the full set of implications.</p>
*
* @param clientOptions A configured instance of {@link HttpClientOptions}.
* @return The updated {@link AzureMonitorExporterBuilder} object.
* @return The updated {@link AzureMonitorExporterOptions} object.
* @see HttpClientOptions
*/
@Override
public AzureMonitorExporterBuilder clientOptions(ClientOptions clientOptions) {
public AzureMonitorExporterOptions clientOptions(ClientOptions clientOptions) {
if (frozen) {
throw LOGGER.logExceptionAsError(new IllegalStateException(
"clientOptions cannot be changed after any of the build methods have been called"));
Expand All @@ -255,12 +244,11 @@ public AzureMonitorExporterBuilder clientOptions(ClientOptions clientOptions) {
* 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.
* @return The updated {@link AzureMonitorExporterOptions} object.
* @throws NullPointerException If the connection string is {@code null}.
* @throws IllegalArgumentException If the connection string is invalid.
*/
@Override
public AzureMonitorExporterBuilder connectionString(String connectionString) {
public AzureMonitorExporterOptions connectionString(String connectionString) {
if (frozen) {
throw LOGGER.logExceptionAsError(new IllegalStateException(
"connectionString cannot be changed after any of the build methods have been called"));
Expand All @@ -273,10 +261,9 @@ public AzureMonitorExporterBuilder connectionString(String connectionString) {
* Sets the token credential required for authentication with the ingestion endpoint service.
*
* @param credential The Azure Identity TokenCredential.
* @return The updated {@link AzureMonitorExporterBuilder} object.
* @return The updated {@link AzureMonitorExporterOptions} object.
*/
@Override
public AzureMonitorExporterBuilder credential(TokenCredential credential) {
public AzureMonitorExporterOptions credential(TokenCredential credential) {
if (frozen) {
throw LOGGER.logExceptionAsError(new IllegalStateException(
"credential cannot be changed after any of the build methods have been called"));
Expand All @@ -285,31 +272,13 @@ public AzureMonitorExporterBuilder credential(TokenCredential credential) {
return 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.
* @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 configProperties) {
SpanExporter buildSpanExporter(ConfigProperties configProperties) {
internalBuildAndFreeze(configProperties);
return new AzureMonitorTraceExporter(createSpanDataMapper(configProperties), builtTelemetryItemExporter,
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}.
*
* @param configProperties The OpenTelemetry configuration properties.
* @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 configProperties) {
LogRecordExporter buildLogRecordExporter(ConfigProperties configProperties) {
internalBuildAndFreeze(configProperties);
return new AzureMonitorLogRecordExporter(
new LogDataMapper(true, false, createDefaultsPopulator(configProperties)), builtTelemetryItemExporter);
Expand All @@ -333,19 +302,7 @@ 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}.
*
* <p>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
* environment variable "APPLICATIONINSIGHTS_CONNECTION_STRING" is not set.
*/
public MetricExporter buildMetricExporter(ConfigProperties configProperties) {
MetricExporter buildMetricExporter(ConfigProperties configProperties) {
internalBuildAndFreeze(configProperties);
HeartbeatExporter.start(MINUTES.toSeconds(15), createDefaultsPopulator(configProperties),
builtTelemetryItemExporter::send);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package com.azure.monitor.opentelemetry.exporter.implementation;

import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterBuilder;
import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterOptions;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.logs.ConfigurableLogRecordExporterProvider;
import io.opentelemetry.sdk.common.CompletableResultCode;
Expand All @@ -20,7 +20,7 @@ public LogRecordExporter createExporter(ConfigProperties configProperties) {
return AzureMonitorLogRecordExporterProvider.MarkerLogRecordExporter.INSTANCE;
}
throw new IllegalStateException(
getName() + " currently only supports usage via " + AzureMonitorExporterBuilder.class.getName());
getName() + " currently only supports usage via " + AzureMonitorExporterOptions.class.getName());
}

@Override
Expand Down
Loading

0 comments on commit cac5605

Please sign in to comment.