Skip to content

Commit

Permalink
AzureMonitor.configure(otelSdkBuilder, AzureMonitor.exportOptions().c…
Browse files Browse the repository at this point in the history
…onnectionString("{connection-string}"));
  • Loading branch information
jeanbisutti committed Sep 11, 2024
1 parent 648f645 commit 2aaf035
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 99 deletions.
11 changes: 7 additions & 4 deletions sdk/monitor/azure-monitor-opentelemetry-exporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,17 @@ 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();

// Make the span the current span
try (Scope scope = span.makeCurrent()) {
// Your application logic here
} catch (Throwable t) {
} catch (Throwable t) {
span.recordException(t);
throw t;
} finally {
Expand All @@ -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) {
Expand All @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>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.
*
* <p>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<String, String> props = new HashMap<>();
props.put("otel.traces.exporter", AzureMonitorExporterProviderKeys.EXPORTER_NAME);
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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();
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public <T extends RuntimeException> void testInvalidConnectionStrings(String con
Class<T> exceptionExpected) {
Assertions.assertThrows(exceptionExpected, () -> {
AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder();
new AzureMonitor().connectionString(connectionString).configure(sdkBuilder);
AzureMonitor.configure(sdkBuilder, AzureMonitor.exportOptions().connectionString(connectionString));
sdkBuilder.build();
});
}
Expand Down
Loading

0 comments on commit 2aaf035

Please sign in to comment.