Skip to content

Commit

Permalink
Better handling of the shutdown hook
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrille-leclerc committed Jul 29, 2024
1 parent 78d8dcc commit 97b8cc2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
<changelist>999999-SNAPSHOT</changelist>
<gitHubRepo>jenkinsci/opentelemetry-api-plugin</gitHubRepo>
<opentelemetry.version>1.40.0</opentelemetry.version>
<opentelemetry-instrumentation.version>2.5.0</opentelemetry-instrumentation.version>
<opentelemetry-semconv.version>1.26.0-alpha</opentelemetry-semconv.version>
<opentelemetry-instrumentation.version>2.6.0</opentelemetry-instrumentation.version>
<opentelemetry-semconv.version>1.25.0-alpha</opentelemetry-semconv.version>

<jenkins.version>2.440.3</jenkins.version>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class ReconfigurableOpenTelemetry implements ExtendedOpenTelemetry, OpenT
Resource resource = Resource.empty();
ConfigProperties config = ConfigPropertiesUtils.emptyConfig();
OpenTelemetry openTelemetryImpl = OpenTelemetry.noop();
Thread shutdownHook;
final ReconfigurableMeterProvider meterProviderImpl = new ReconfigurableMeterProvider();
final ReconfigurableTracerProvider traceProviderImpl = new ReconfigurableTracerProvider();
final ReconfigurableLoggerProvider loggerProviderImpl = new ReconfigurableLoggerProvider();
Expand All @@ -82,14 +83,14 @@ public ReconfigurableOpenTelemetry() {
logger.log(Level.WARNING, "GlobalEventLoggerProvider already set", e);
}

logger.log(Level.FINE, () -> "Initialize " +
logger.log(Level.FINE, () -> "Configure " +
"GlobalOpenTelemetry with instance " + Optional.of(GlobalOpenTelemetry.get()).map(ot -> ot + "@" + System.identityHashCode(ot)) + "and " +
"GlobalEventLoggerProvide with instance " + Optional.of(GlobalEventLoggerProvider.get()).map(elp -> elp + "@" + System.identityHashCode(elp)));
}

@Initializer(after = InitMilestone.EXTENSIONS_AUGMENTED, before = InitMilestone.SYSTEM_CONFIG_LOADED)
public void init() {
logger.log(Level.INFO, "OpenTelemetry initialized as NoOp");
logger.log(Level.INFO, "OpenTelemetry configured as NoOp");
}

/**
Expand All @@ -112,7 +113,7 @@ public void configure(@NonNull Map<String, String> openTelemetryProperties, Reso
logger.log(Level.FINE, "initializeOtlp");

// OPENTELEMETRY SDK
AutoConfiguredOpenTelemetrySdkBuilder autoConfiguredOpenTelemetrySdkBuilder = AutoConfiguredOpenTelemetrySdk
OpenTelemetrySdk openTelemetrySdk = AutoConfiguredOpenTelemetrySdk
.builder()
// properties
.addPropertiesSupplier(() -> openTelemetryProperties)
Expand All @@ -129,15 +130,27 @@ public void configure(@NonNull Map<String, String> openTelemetryProperties, Reso
.putAll(openTelemetryResource).build();
return this.resource;
}
);
if (disableShutdownHook) {
autoConfiguredOpenTelemetrySdkBuilder.disableShutdownHook();
}
OpenTelemetrySdk openTelemetrySdk = autoConfiguredOpenTelemetrySdkBuilder
)
.disableShutdownHook()
.build()
.getOpenTelemetrySdk();
setOpenTelemetryImpl(openTelemetrySdk);

if (disableShutdownHook) {
if (shutdownHook == null) {
// nothing to do, no shutdownhook registered
} else {
Runtime.getRuntime().removeShutdownHook(shutdownHook);
}
} else {
if (shutdownHook == null) {
shutdownHook = new Thread(this::close);
Runtime.getRuntime().addShutdownHook(shutdownHook);
} else {
// nothing to do, shutdown hook already registered
}
}

logger.log(Level.INFO, () -> "OpenTelemetry initialized: " + ConfigPropertiesUtils.prettyPrintOtelSdkConfig(this.config, this.resource));

} else { // NO-OP
Expand Down Expand Up @@ -263,4 +276,18 @@ protected void postOpenTelemetrySdkConfiguration() {
openTelemetryLifecycleListener.afterConfiguration(this.config);
});
}

static class ShutdownHook extends Thread {
final OpenTelemetrySdk openTelemetrySdk;

public ShutdownHook(OpenTelemetrySdk openTelemetrySdk) {
super("OpenTelemetry SDK Shutdown Hook");
this.openTelemetrySdk = openTelemetrySdk;
}

@Override
public void run() {
openTelemetrySdk.close();
}

Check warning on line 291 in src/main/java/io/jenkins/plugins/opentelemetry/api/ReconfigurableOpenTelemetry.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 102-291 are not covered by tests
}
}

0 comments on commit 97b8cc2

Please sign in to comment.