/
CreateConfigurationSettingLoggingExporterSample.java
72 lines (63 loc) · 2.71 KB
/
CreateConfigurationSettingLoggingExporterSample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.core.tracing.opentelemetry;
import com.azure.data.appconfiguration.ConfigurationClient;
import com.azure.data.appconfiguration.ConfigurationClientBuilder;
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.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
/**
* Sample to demonstrate using {@link LoggingSpanExporter} to export telemetry events when creating a configuration
* 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.
*/
public static void main(String[] args) {
doClientWork();
}
/**
* Configure the OpenTelemetry {@link LoggingSpanExporter} to enable tracing.
*
* @return The OpenTelemetry {@link Tracer} instance.
*/
private static Tracer configureLoggingExporter() {
// Tracer provider configured to export spans with SimpleSpanProcessor using
// the logging exporter.
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.
*/
private static void doClientWork() {
ConfigurationClient client = new ConfigurationClientBuilder()
.connectionString(CONNECTION_STRING)
.buildClient();
Span userParentSpan = TRACER.spanBuilder("user-parent-span").startSpan();
final Scope scope = userParentSpan.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");
} finally {
userParentSpan.end();
scope.close();
}
}
}