Skip to content

Commit 3c14bb5

Browse files
authored
Update to latest otel (#2246)
* Update instrumentation, pull in jboss mdc * Update to new APIs * Remove deprecated method usage: * submodule * more * Remove redundant shutdown hook * small * fix * spotless * More instrumentation * Updates * Spotless
1 parent a105c16 commit 3c14bb5

File tree

20 files changed

+360
-142
lines changed

20 files changed

+360
-142
lines changed

agent/agent-bootstrap/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ dependencies {
1717
implementation("ch.qos.logback:logback-classic")
1818
implementation("ch.qos.logback.contrib:logback-json-classic")
1919

20+
implementation("io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-executors-bootstrap:$otelInstrumentationAlphaVersion")
2021
implementation("io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-jaxrs-common-bootstrap:$otelInstrumentationAlphaVersion")
2122
implementation("io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-kafka-clients-0.11-bootstrap:$otelInstrumentationAlphaVersion")
23+
implementation("io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-servlet-common-bootstrap:$otelInstrumentationAlphaVersion")
2224
implementation("io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-undertow-1.4-bootstrap:$otelInstrumentationAlphaVersion")
2325

2426
// not using gson because it has dependency on java.sql.*, which is not available in Java 9+ bootstrap class loader

agent/agent-bootstrap/gradle.lockfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ io.opentelemetry.instrumentation:opentelemetry-instrumentation-api-semconv:1.13.
1414
io.opentelemetry.instrumentation:opentelemetry-instrumentation-api:1.13.0+ai.patches-alpha=runtimeClasspath
1515
io.opentelemetry.instrumentation:opentelemetry-instrumentation-appender-api-internal:1.13.0+ai.patches-alpha=runtimeClasspath
1616
io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom-alpha:1.13.0+ai.patches-alpha=runtimeClasspath
17+
io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-executors-bootstrap:1.13.0+ai.patches-alpha=runtimeClasspath
1718
io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-jaxrs-common-bootstrap:1.13.0+ai.patches-alpha=runtimeClasspath
1819
io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-kafka-clients-0.11-bootstrap:1.13.0+ai.patches-alpha=runtimeClasspath
1920
io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-servlet-common-bootstrap:1.13.0+ai.patches-alpha=runtimeClasspath

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/exporter/AzureMonitorMetricExporter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private List<TelemetryItem> convertOtelMetricToAzureMonitorMetric(MetricData met
119119

120120
for (PointData pointData : metricData.getData().getPoints()) {
121121
MetricTelemetryBuilder builder = telemetryClient.newMetricTelemetryBuilder();
122-
builder.setTime(FormattedTime.offSetDateTimeFromEpochNanos(pointData.getStartEpochNanos()));
122+
builder.setTime(FormattedTime.offSetDateTimeFromEpochNanos(pointData.getEpochNanos()));
123123
updateMetricPointBuilder(builder, metricData, pointData);
124124
telemetryItems.add(builder.build());
125125
}

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/exporter/LoggerExporter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private void trackMessage(LogData log) {
136136
// set message-specific properties
137137
setLoggerProperties(
138138
telemetryBuilder,
139-
log.getInstrumentationLibraryInfo().getName(),
139+
log.getInstrumentationScopeInfo().getName(),
140140
attributes.get(SemanticAttributes.THREAD_NAME),
141141
log.getSeverity());
142142

@@ -160,7 +160,7 @@ private void trackMessageAsException(LogData log, String stack) {
160160
// set exception-specific properties
161161
setLoggerProperties(
162162
telemetryBuilder,
163-
log.getInstrumentationLibraryInfo().getName(),
163+
log.getInstrumentationScopeInfo().getName(),
164164
attributes.get(SemanticAttributes.THREAD_NAME),
165165
log.getSeverity());
166166

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/AiComponentInstaller.java

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
package com.microsoft.applicationinsights.agent.internal.init;
2323

24-
import static java.util.concurrent.TimeUnit.SECONDS;
25-
2624
import com.azure.monitor.opentelemetry.exporter.implementation.utils.Strings;
2725
import com.azure.monitor.opentelemetry.exporter.implementation.utils.TempDirs;
2826
import com.microsoft.applicationinsights.agent.bootstrap.BytecodeUtil;
@@ -53,7 +51,6 @@
5351
import com.microsoft.applicationinsights.profiler.config.ServiceProfilerServiceConfig;
5452
import io.opentelemetry.instrumentation.api.aisdk.AiAppId;
5553
import io.opentelemetry.instrumentation.api.aisdk.AiLazyConfiguration;
56-
import io.opentelemetry.sdk.common.CompletableResultCode;
5754
import java.io.File;
5855
import java.lang.instrument.Instrumentation;
5956
import java.net.URL;
@@ -168,7 +165,6 @@ private static AppIdSupplier start() {
168165

169166
// this is currently used by Micrometer instrumentation in addition to 2.x SDK
170167
BytecodeUtil.setDelegate(new BytecodeUtilImpl());
171-
Runtime.getRuntime().addShutdownHook(new ShutdownHook(telemetryClient));
172168

173169
RpConfiguration rpConfiguration = MainEntryPoint.getRpConfiguration();
174170
if (rpConfiguration != null) {
@@ -239,41 +235,5 @@ private static boolean hasConnectionString(Configuration config) {
239235
return !Strings.isNullOrEmpty(config.connectionString);
240236
}
241237

242-
private static class ShutdownHook extends Thread {
243-
private final TelemetryClient telemetryClient;
244-
245-
public ShutdownHook(TelemetryClient telemetryClient) {
246-
this.telemetryClient = telemetryClient;
247-
}
248-
249-
@Override
250-
public void run() {
251-
startupLogger.debug("running shutdown hook");
252-
CompletableResultCode otelFlush = OpenTelemetryConfigurer.flush();
253-
CompletableResultCode result = new CompletableResultCode();
254-
otelFlush.whenComplete(
255-
() -> {
256-
CompletableResultCode batchingClientFlush = telemetryClient.forceFlush();
257-
batchingClientFlush.whenComplete(
258-
() -> {
259-
if (otelFlush.isSuccess() && batchingClientFlush.isSuccess()) {
260-
result.succeed();
261-
} else {
262-
result.fail();
263-
}
264-
});
265-
});
266-
result.join(5, SECONDS);
267-
if (result.isSuccess()) {
268-
startupLogger.debug("flushing telemetry on shutdown completed successfully");
269-
} else if (Thread.interrupted()) {
270-
startupLogger.debug("interrupted while flushing telemetry on shutdown");
271-
} else {
272-
startupLogger.debug(
273-
"flushing telemetry on shutdown has taken more than 5 seconds, shutting down anyways...");
274-
}
275-
}
276-
}
277-
278238
private AiComponentInstaller() {}
279239
}

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/ConfigOverride.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import com.microsoft.applicationinsights.agent.internal.configuration.Configuration;
2727
import com.microsoft.applicationinsights.agent.internal.legacyheaders.DelegatingPropagatorProvider;
2828
import io.opentelemetry.instrumentation.api.config.Config;
29-
import io.opentelemetry.instrumentation.api.config.ConfigBuilder;
3029
import java.util.ArrayList;
3130
import java.util.HashMap;
3231
import java.util.List;
@@ -114,9 +113,10 @@ static Config getConfig(Configuration config) {
114113
properties.put("otel.instrumentation.jaxrs-annotations.enabled", "false");
115114
}
116115
if (!config.preview.captureControllerSpans) {
117-
properties.put("otel.instrumentation.common.experimental.suppress-controller-spans", "true");
116+
properties.put(
117+
"otel.instrumentation.common.experimental.controller-telemetry.enabled", "false");
118118
}
119-
properties.put("otel.instrumentation.common.experimental.suppress-view-spans", "true");
119+
properties.put("otel.instrumentation.common.experimental.view-telemetry.enabled", "false");
120120
properties.put(
121121
"otel.instrumentation.common.experimental.suppress-messaging-receive-spans", "true");
122122
// this is needed to capture kafka.record.queue_time_ms
@@ -200,7 +200,7 @@ static Config getConfig(Configuration config) {
200200
properties.put("otel.service.name", config.role.name);
201201
}
202202

203-
return new ConfigBuilder().readProperties(properties).build();
203+
return Config.builder().addProperties(properties).build();
204204
}
205205

206206
private static void setHttpHeaderConfiguration(

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/MetricExporterCustomizer.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/OpenTelemetryConfigurer.java

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.auto.service.AutoService;
2626
import com.microsoft.applicationinsights.agent.internal.configuration.Configuration;
2727
import com.microsoft.applicationinsights.agent.internal.configuration.Configuration.ProcessorConfig;
28+
import com.microsoft.applicationinsights.agent.internal.exporter.AzureMonitorMetricExporter;
2829
import com.microsoft.applicationinsights.agent.internal.exporter.Exporter;
2930
import com.microsoft.applicationinsights.agent.internal.exporter.LoggerExporter;
3031
import com.microsoft.applicationinsights.agent.internal.legacyheaders.AiLegacyHeaderSpanProcessor;
@@ -47,6 +48,8 @@
4748
import io.opentelemetry.sdk.logs.SdkLogEmitterProviderBuilder;
4849
import io.opentelemetry.sdk.logs.export.BatchLogProcessor;
4950
import io.opentelemetry.sdk.logs.export.LogExporter;
51+
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder;
52+
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
5053
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
5154
import io.opentelemetry.sdk.trace.data.SpanData;
5255
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
@@ -62,9 +65,6 @@
6265
@AutoService(AutoConfigurationCustomizerProvider.class)
6366
public class OpenTelemetryConfigurer implements AutoConfigurationCustomizerProvider {
6467

65-
private static volatile BatchSpanProcessor batchSpanProcessor;
66-
private static volatile BatchLogProcessor batchLogProcessor;
67-
6868
@Override
6969
public void customize(AutoConfigurationCustomizer autoConfiguration) {
7070
TelemetryClient telemetryClient = TelemetryClient.getActive();
@@ -75,28 +75,21 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) {
7575

7676
Configuration configuration = MainEntryPoint.getConfiguration();
7777

78-
autoConfiguration.addTracerProviderCustomizer(
79-
(builder, config) -> configureTracing(builder, config, configuration));
80-
autoConfiguration.addLogEmitterProviderCustomizer(
81-
(builder, config) -> configureLogging(builder, configuration));
82-
}
83-
84-
public static CompletableResultCode flush() {
85-
List<CompletableResultCode> results = new ArrayList<>();
86-
if (batchSpanProcessor != null) {
87-
results.add(batchSpanProcessor.forceFlush());
88-
}
89-
if (batchLogProcessor != null) {
90-
results.add(batchLogProcessor.forceFlush());
91-
}
92-
return CompletableResultCode.ofAll(results);
78+
autoConfiguration
79+
.addTracerProviderCustomizer(
80+
(builder, config) -> configureTracing(builder, telemetryClient, config, configuration))
81+
.addLogEmitterProviderCustomizer(
82+
(builder, config) -> configureLogging(builder, telemetryClient, configuration))
83+
.addMeterProviderCustomizer(
84+
(builder, config) -> configureMetrics(builder, telemetryClient, configuration));
9385
}
9486

9587
@SuppressFBWarnings(
9688
value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD",
9789
justification = "this method is only called once during initialization")
9890
private static SdkTracerProviderBuilder configureTracing(
9991
SdkTracerProviderBuilder tracerProvider,
92+
TelemetryClient telemetryClient,
10093
ConfigProperties config,
10194
Configuration configuration) {
10295

@@ -151,25 +144,28 @@ private static SdkTracerProviderBuilder configureTracing(
151144
String tracesExporter = config.getString("otel.traces.exporter");
152145
if ("none".equals(tracesExporter)) { // "none" is the default set in ConfigOverride
153146
SpanExporter spanExporter =
154-
createSpanExporter(configuration, configuration.preview.captureHttpServer4xxAsError);
147+
createSpanExporter(
148+
telemetryClient, configuration, configuration.preview.captureHttpServer4xxAsError);
155149

156150
// using BatchSpanProcessor in order to get off of the application thread as soon as possible
157-
batchSpanProcessor =
151+
BatchSpanProcessor batchSpanProcessor =
158152
BatchSpanProcessor.builder(spanExporter)
159153
.setScheduleDelay(getBatchProcessorDelay())
160154
.build();
161155

162-
tracerProvider.addSpanProcessor(batchSpanProcessor);
156+
tracerProvider.addSpanProcessor(
157+
new TelemetryClientFlushingSpanProcessor(batchSpanProcessor, telemetryClient));
163158
}
164159

165160
return tracerProvider;
166161
}
167162

168163
private static SpanExporter createSpanExporter(
169-
Configuration configuration, boolean captureHttpServer4xxAsError) {
164+
TelemetryClient telemetryClient,
165+
Configuration configuration,
166+
boolean captureHttpServer4xxAsError) {
170167

171-
SpanExporter spanExporter =
172-
new Exporter(TelemetryClient.getActive(), captureHttpServer4xxAsError);
168+
SpanExporter spanExporter = new Exporter(telemetryClient, captureHttpServer4xxAsError);
173169
List<ProcessorConfig> processorConfigs = getSpanProcessorConfigs(configuration);
174170
// NOTE if changing the span processor to something async, flush it in the shutdown hook before
175171
// flushing TelemetryClient
@@ -208,27 +204,32 @@ private static List<ProcessorConfig> getSpanProcessorConfigs(Configuration confi
208204
}
209205

210206
private static SdkLogEmitterProviderBuilder configureLogging(
211-
SdkLogEmitterProviderBuilder builder, Configuration configuration) {
207+
SdkLogEmitterProviderBuilder builder,
208+
TelemetryClient telemetryClient,
209+
Configuration configuration) {
212210

213-
LogExporter logExporter = createLogExporter(configuration);
211+
LogExporter logExporter = createLogExporter(telemetryClient, configuration);
214212

215213
// using BatchLogProcessor in order to get off of the application thread as soon as possible
216-
batchLogProcessor =
214+
BatchLogProcessor batchLogProcessor =
217215
BatchLogProcessor.builder(logExporter).setScheduleDelay(getBatchProcessorDelay()).build();
218216

217+
TelemetryClientFlushingLogProcessor telemetryClientFlushingLogProcessor =
218+
new TelemetryClientFlushingLogProcessor(batchLogProcessor, telemetryClient);
219+
219220
// inherited attributes log processor also handles operation name, ikey and role name attributes
220221
// and these all need access to Span.current(), so must be run before passing off to the
221222
// BatchLogProcessor
222223
return builder.addLogProcessor(
223224
new InheritedAttributesLogProcessor(
224-
configuration.preview.inheritedAttributes, batchLogProcessor));
225+
configuration.preview.inheritedAttributes, telemetryClientFlushingLogProcessor));
225226
}
226227

227-
private static LogExporter createLogExporter(Configuration configuration) {
228+
private static LogExporter createLogExporter(
229+
TelemetryClient telemetryClient, Configuration configuration) {
228230
LogExporter logExporter =
229231
new LoggerExporter(
230-
TelemetryClient.getActive(),
231-
configuration.preview.captureLoggingLevelAsCustomDimension);
232+
telemetryClient, configuration.preview.captureLoggingLevelAsCustomDimension);
232233
List<ProcessorConfig> processorConfigs = getLogProcessorConfigs(configuration);
233234
if (!processorConfigs.isEmpty()) {
234235
// Reversing the order of processors before passing it Log processor
@@ -271,6 +272,16 @@ private static Duration getBatchProcessorDelay() {
271272
}
272273
}
273274

275+
private static SdkMeterProviderBuilder configureMetrics(
276+
SdkMeterProviderBuilder builder,
277+
TelemetryClient telemetryClient,
278+
Configuration configuration) {
279+
return builder.registerMetricReader(
280+
PeriodicMetricReader.builder(new AzureMonitorMetricExporter(telemetryClient))
281+
.setInterval(Duration.ofSeconds(configuration.preview.metricIntervalSeconds))
282+
.build());
283+
}
284+
274285
private static class BackCompatHttpUrlProcessor implements SpanExporter {
275286

276287
private final SpanExporter delegate;

0 commit comments

Comments
 (0)