Skip to content

Commit

Permalink
Minor tracing samples improvements (Azure#36226)
Browse files Browse the repository at this point in the history
* tracing samples - flush otel, remove deprecated jaeger, use autoconfigure
  • Loading branch information
lmolkova authored Sep 27, 2023
1 parent 4ed2f41 commit a562e65
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 149 deletions.
2 changes: 2 additions & 0 deletions .vscode/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
"sdk/core/azure-xml/**",
"sdk/cosmos/azure-cosmos-dotnet-benchmark/**",
"sdk/core/azure-core-tracing-opentelemetry/**",
"sdk/core/azure-core-tracing-opentelemetry-samples/**",
"sdk/cosmos/azure-cosmos-benchmark/**",
"sdk/core/azure-json-gson/**",
"sdk/cosmos/azure-cosmos-spark_3-1_2-12/**",
Expand Down Expand Up @@ -339,6 +340,7 @@
"odata",
"ODBC",
"okhttp",
"OTLP",
"OLTP",
"onboarded",
"Onco",
Expand Down
1 change: 0 additions & 1 deletion eng/versioning/external_dependencies.txt
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ io.opentelemetry:opentelemetry-sdk;1.28.0
io.opentelemetry:opentelemetry-sdk-metrics;1.28.0
io.opentelemetry:opentelemetry-sdk-logs;1.28.0
io.opentelemetry:opentelemetry-exporter-logging;1.28.0
io.opentelemetry:opentelemetry-exporter-jaeger;1.28.0
io.opentelemetry:opentelemetry-exporter-otlp;1.28.0
io.opentelemetry:opentelemetry-api-logs;1.26.0-alpha
io.opentelemetry:opentelemetry-sdk-testing;1.28.0
Expand Down
3 changes: 2 additions & 1 deletion sdk/core/azure-core-metrics-opentelemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ SdkMeterProvider meterProvider = SdkMeterProvider.builder()
.registerMetricReader(PeriodicMetricReader.builder(OtlpGrpcMetricExporter.builder().build()).build())
.build();

OpenTelemetrySdk openTelemetry = OpenTelemetrySdk.builder().setMeterProvider(meterProvider).build();
// Pass OpenTelemetry instance to MetricsOptions.
MetricsOptions customMetricsOptions = new OpenTelemetryMetricsOptions()
.setOpenTelemetry(OpenTelemetrySdk.builder().setMeterProvider(meterProvider).build());
.setOpenTelemetry(openTelemetry);

// configure Azure Client to use customMetricsOptions - it will use meterProvider
// to create meters and instruments
Expand Down
1 change: 0 additions & 1 deletion sdk/core/azure-core-metrics-opentelemetry/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@
<include>io.opentelemetry:opentelemetry-sdk-testing:[1.28.0]</include> <!-- {x-include-update;io.opentelemetry:opentelemetry-sdk-testing;external_dependency} -->
<include>io.opentelemetry:opentelemetry-exporter-logging:[1.28.0]</include> <!-- {x-include-update;io.opentelemetry:opentelemetry-exporter-logging;external_dependency} -->
<include>io.opentelemetry:opentelemetry-exporter-otlp:[1.28.0]</include> <!-- {x-include-update;io.opentelemetry:opentelemetry-exporter-otlp;external_dependency} -->
<include>io.opentelemetry:opentelemetry-exporter-jaeger:[1.28.0]</include> <!-- {x-include-update;io.opentelemetry:opentelemetry-exporter-jaeger;external_dependency} -->
</includes>
</bannedDependencies>
</rules>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public OpenTelemetryMeterProvider() {
* .registerMetricReader&#40;PeriodicMetricReader.builder&#40;OtlpGrpcMetricExporter.builder&#40;&#41;.build&#40;&#41;&#41;.build&#40;&#41;&#41;
* .build&#40;&#41;;
*
* OpenTelemetry openTelemetry = OpenTelemetrySdk.builder&#40;&#41;
* OpenTelemetrySdk openTelemetry = OpenTelemetrySdk.builder&#40;&#41;
* .setTracerProvider&#40;tracerProvider&#41;
* .setMeterProvider&#40;meterProvider&#41;
* .setPropagators&#40;ContextPropagators.create&#40;W3CTraceContextPropagator.getInstance&#40;&#41;&#41;&#41;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.azure.core.util.metrics.Meter;
import com.azure.core.util.metrics.MeterProvider;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
Expand Down Expand Up @@ -93,9 +92,10 @@ public void readmeSampleCustomSdkConfiguration() {
.registerMetricReader(PeriodicMetricReader.builder(OtlpGrpcMetricExporter.builder().build()).build())
.build();

OpenTelemetrySdk openTelemetry = OpenTelemetrySdk.builder().setMeterProvider(meterProvider).build();
// Pass OpenTelemetry instance to MetricsOptions.
MetricsOptions customMetricsOptions = new OpenTelemetryMetricsOptions()
.setOpenTelemetry(OpenTelemetrySdk.builder().setMeterProvider(meterProvider).build());
.setOpenTelemetry(openTelemetry);

// configure Azure Client to use customMetricsOptions - it will use meterProvider
// to create meters and instruments
Expand All @@ -108,6 +108,7 @@ public void readmeSampleCustomSdkConfiguration() {
sampleClient.methodCall("get items");

// END: readme-sample-customConfiguration
openTelemetry.close();
}

/**
Expand All @@ -125,7 +126,7 @@ public void configureClientLibraryToUseCustomMeter() {
.registerMetricReader(PeriodicMetricReader.builder(OtlpGrpcMetricExporter.builder().build()).build())
.build();

OpenTelemetry openTelemetry = OpenTelemetrySdk.builder()
OpenTelemetrySdk openTelemetry = OpenTelemetrySdk.builder()
.setTracerProvider(tracerProvider)
.setMeterProvider(meterProvider)
.setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance()))
Expand Down Expand Up @@ -157,6 +158,7 @@ public void configureClientLibraryToUseCustomMeter() {
span.end();

// END: com.azure.core.util.metrics.OpenTelemetryMeterProvider.createMeter#custom
openTelemetry.close();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions sdk/core/azure-core-tracing-opentelemetry-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-jaeger</artifactId>
<version>1.28.0</version> <!-- {x-version-update;io.opentelemetry:opentelemetry-exporter-jaeger;external_dependency} -->
<artifactId>opentelemetry-sdk-extension-autoconfigure</artifactId>
<version>1.28.0</version> <!-- {x-version-update;io.opentelemetry:opentelemetry-sdk-extension-autoconfigure;external_dependency} -->
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

package com.azure.core.tracing.opentelemetry.samples;

import com.azure.core.tracing.opentelemetry.OpenTelemetryTracingOptions;
import com.azure.core.util.ClientOptions;
import com.azure.core.util.TracingOptions;
import com.azure.data.appconfiguration.ConfigurationClient;
import com.azure.data.appconfiguration.ConfigurationClientBuilder;
import io.opentelemetry.api.trace.Span;
Expand All @@ -18,54 +21,52 @@
* in App Configuration through the {@link ConfigurationClient}.
*/
public class CreateConfigurationSettingLoggingExporterSample {
private static final Tracer TRACER = configureLoggingExporter();
private static final String CONNECTION_STRING = "<YOUR_CONNECTION_STRING>";

/**
* The main method to run the application.
*
* @param args Ignored args.
*/
@SuppressWarnings("try")
public static void main(String[] args) {
configureLoggingExporter();
OpenTelemetrySdk openTelemetry = configureTracing();

// In this sample we configured OpenTelemetry without registering global instance, so we need to pass it explicitly to the Azure SDK.
// If we used ApplicationInsights or OpenTelemetry agent, or registered global instance, we would not need to pass it explicitly.
TracingOptions tracingOptions = new OpenTelemetryTracingOptions().setOpenTelemetry(openTelemetry);

ConfigurationClient client = new ConfigurationClientBuilder()
.connectionString(CONNECTION_STRING)
.clientOptions(new ClientOptions().setTracingOptions(tracingOptions))
.buildClient();

doClientWork(client);
Tracer tracer = openTelemetry.getTracer("sample");

Span span = tracer.spanBuilder("my-span").startSpan();
try (Scope s = span.makeCurrent()) {
// current span propagates into synchronous calls automatically. ApplicationInsights or OpenTelemetry agent
// also propagate context through async reactor calls.
client.setConfigurationSetting("hello", "text", "World");
} finally {
span.end();
}

openTelemetry.close();
}

/**
* Configure the OpenTelemetry {@link LoggingSpanExporter} to enable tracing.
*
* @return The OpenTelemetry {@link Tracer} instance.
* Configure the OpenTelemetry to print traces with {@link LoggingSpanExporter}.
*/
private static Tracer configureLoggingExporter() {
private static OpenTelemetrySdk configureTracing() {
// configure OpenTelemetry explicitly or with io.opentelemetry:opentelemetry-sdk-extension-autoconfigure package
SdkTracerProvider tracerProvider =
SdkTracerProvider.builder()
.addSpanProcessor(BatchSpanProcessor.builder(LoggingSpanExporter.create()).build())
.build();

return OpenTelemetrySdk.builder()
.setTracerProvider(tracerProvider)
.buildAndRegisterGlobal()
.getTracer("AppConfig-Sample");
}

/**
* Creates the {@link ConfigurationClient} and creates a configuration in Azure App Configuration with distributed
* tracing enabled and using the Logging exporter to export telemetry events.
*/
@SuppressWarnings("try")
private static void doClientWork(ConfigurationClient client) {
Span span = TRACER.spanBuilder("my-span").startSpan();
try (Scope s = span.makeCurrent()) {
// current span propagates into synchronous calls automatically. ApplicationInsights or OpenTelemetry agent
// also propagate context through async reactor calls.
client.setConfigurationSetting("hello", "text", "World");
} finally {
span.end();
}
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import com.azure.security.keyvault.secrets.SecretClient;
import com.azure.security.keyvault.secrets.SecretClientBuilder;
import com.azure.security.keyvault.secrets.models.KeyVaultSecret;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import reactor.util.context.Context;

import static com.azure.core.util.tracing.Tracer.PARENT_TRACE_CONTEXT_KEY;
Expand All @@ -26,7 +26,8 @@ public class ListKeyVaultSecretsAutoConfigurationSample {
private static final String VAULT_URL = "<YOUR_VAULT_URL>";
@SuppressWarnings("try")
public void syncClient() {
Tracer tracer = configureTracing();
OpenTelemetrySdk openTelemetry = AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk();
Tracer tracer = openTelemetry.getTracer("sample");

// BEGIN: readme-sample-context-auto-propagation
SecretClient secretClient = new SecretClientBuilder()
Expand All @@ -46,10 +47,12 @@ public void syncClient() {
}

// END: readme-sample-context-auto-propagation
openTelemetry.close();
}

public void asyncClient() {
Tracer tracer = configureTracing();
OpenTelemetrySdk openTelemetry = AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk();
Tracer tracer = openTelemetry.getTracer("sample");

// BEGIN: readme-sample-context-manual-propagation
SecretAsyncClient secretAsyncClient = new SecretClientBuilder()
Expand All @@ -73,20 +76,7 @@ public void asyncClient() {
} finally {
span.end();
}

// END: readme-sample-context-manual-propagation
}

/**
* Configure the OpenTelemetry {@link LoggingSpanExporter} to enable tracing.
*
* @return The OpenTelemetry {@link Tracer} instance.
*/
private static Tracer configureTracing() {
// configure OpenTelemetry SDK using io.opentelemetry:opentelemetry-sdk-extension-autoconfigure:
// OpenTelemetrySdk sdk = AutoConfiguredOpenTelemetrySdk.initialize()
// .getOpenTelemetrySdk();

return GlobalOpenTelemetry.getTracer("Async-List-KV-Secrets-Sample");
openTelemetry.close();
}
}

This file was deleted.

Loading

0 comments on commit a562e65

Please sign in to comment.