-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36538 from brunobat/otel-flush-timeout
OTel flush timeout
- Loading branch information
Showing
3 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
...loyment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryDestroyerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
19 changes: 17 additions & 2 deletions
19
.../opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/OpenTelemetryDestroyer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters