Skip to content

Commit

Permalink
OTel flush timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobat committed Oct 18, 2023
1 parent c0bacff commit fb85771
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.quarkus.opentelemetry.deployment;

import static org.hamcrest.Matchers.is;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.opentelemetry.OpenTelemetryDestroyer;
import io.quarkus.opentelemetry.deployment.common.TestSpanExporter;
import io.quarkus.opentelemetry.deployment.common.TestSpanExporterProvider;
import io.quarkus.test.QuarkusDevModeTest;
import io.restassured.RestAssured;

public class OpenTelemetryDestroyerTest {

@RegisterExtension
final static QuarkusDevModeTest TEST = new QuarkusDevModeTest()
.withApplicationRoot((jar) -> jar
.addClasses(TestSpanExporter.class,
TestSpanExporterProvider.class,
HelloResource.class)
.addAsResource(new StringAsset(TestSpanExporterProvider.class.getCanonicalName()),
"META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider")
.add(new StringAsset(
"quarkus.otel.traces.exporter=test-span-exporter\n" +
"quarkus.otel.experimental.shutdown-wait-time=PT60S\n"),
"application.properties"));

@Test
void getShutdownWaitTime() {
RestAssured.when()
.get("/hello").then()
.statusCode(200)
.body(is("PT1M"));
}

@Path("/hello")
public static class HelloResource {
@GET
public String hello() {
return OpenTelemetryDestroyer.getShutdownWaitTime().toString();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
package io.quarkus.opentelemetry;

import static java.util.concurrent.TimeUnit.MILLISECONDS;

import java.time.Duration;
import java.util.Map;

import jakarta.enterprise.context.spi.CreationalContext;

import org.eclipse.microprofile.config.ConfigProvider;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.quarkus.arc.BeanDestroyer;
import io.smallrye.config.SmallRyeConfig;

public class OpenTelemetryDestroyer implements BeanDestroyer<OpenTelemetry> {
@Override
public void destroy(OpenTelemetry openTelemetry, CreationalContext<OpenTelemetry> creationalContext,
Map<String, Object> params) {
if (openTelemetry instanceof OpenTelemetrySdk) {
// between flush and shutdown we will wait shutdown-wait-time, at the most.
var waitTime = getShutdownWaitTime().dividedBy(2);
var openTelemetrySdk = ((OpenTelemetrySdk) openTelemetry);
openTelemetrySdk.getSdkTracerProvider().forceFlush();
openTelemetrySdk.getSdkTracerProvider().shutdown();
openTelemetrySdk.getSdkTracerProvider().forceFlush().join(waitTime.toMillis(), MILLISECONDS);
openTelemetrySdk.getSdkTracerProvider().shutdown().join(waitTime.toMillis(), MILLISECONDS);
}
}

public static Duration getShutdownWaitTime() {
var config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class);
var waitTime = config.getOptionalValue("quarkus.otel.experimental.shutdown-wait-time", Duration.class)
.orElse(Duration.ofSeconds(1));
return waitTime;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.opentelemetry.runtime.config.runtime;

import java.time.Duration;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -63,4 +64,11 @@ public interface OTelRuntimeConfig {
*/
@WithName("experimental.resource.disabled-keys")
Optional<List<String>> experimentalResourceDisabledKeys();

/**
* The maximum amount of time Quarkus will wait for the OpenTelemetry SDK to flush unsent spans and shutdown.
*/
@WithName("experimental.shutdown-wait-time")
@WithDefault("1s")
Duration experimentalShutdownWaitTime();
}

0 comments on commit fb85771

Please sign in to comment.