Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API change of the Azure Monitor exporter #42134

Merged
merged 4 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -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.AzureMonitor;
import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporter;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
Expand Down Expand Up @@ -96,7 +96,7 @@ private static OpenTelemetry init() {
}
AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder();

AzureMonitor.customize(sdkBuilder, applicationInsightsConnectionString);
AzureMonitorExporter.customize(sdkBuilder, applicationInsightsConnectionString);
lmolkova marked this conversation as resolved.
Show resolved Hide resolved

String instanceId = System.getenv("CONTAINER_NAME");
OpenTelemetry otel = sdkBuilder
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) {
jeanbisutti marked this conversation as resolved.
Show resolved Hide resolved
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) {
jeanbisutti marked this conversation as resolved.
Show resolved Hide resolved
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}.
* Providers configuration options for Azure Monitor exporter..
*/
@Fluent
public final class AzureMonitorExporterBuilder implements ConnectionStringTrait<AzureMonitorExporterBuilder>,
TokenCredentialTrait<AzureMonitorExporterBuilder>, HttpTrait<AzureMonitorExporterBuilder> {
public final class AzureMonitorExporterOptions {
jeanbisutti marked this conversation as resolved.
Show resolved Hide resolved

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"));
lmolkova marked this conversation as resolved.
Show resolved Hide resolved
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) {
jeanbisutti marked this conversation as resolved.
Show resolved Hide resolved
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) {
lmolkova marked this conversation as resolved.
Show resolved Hide resolved
internalBuildAndFreeze(configProperties);
HeartbeatExporter.start(MINUTES.toSeconds(15), createDefaultsPopulator(configProperties),
builtTelemetryItemExporter::send);
Expand Down
Loading
Loading