gcp-observability: Optimize GcpObservabilityTest.enableObservability execution time (#11783)

This commit is contained in:
Boddu Harshavardhan 2025-01-24 16:50:41 +05:30 committed by GitHub
parent bf8eb24a30
commit 67351c0c53
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 5 deletions

View File

@ -127,6 +127,15 @@ public final class GcpObservability implements AutoCloseable {
/** Un-initialize/shutdown grpc-observability. */
@Override
public void close() {
closeWithSleepTime(2 * METRICS_EXPORT_INTERVAL, TimeUnit.SECONDS);
}
/**
* Method to close along with sleep time explicitly.
*
* @param sleepTime sleepTime
*/
void closeWithSleepTime(long sleepTime, TimeUnit timeUnit) {
synchronized (GcpObservability.class) {
if (instance == null) {
throw new IllegalStateException("GcpObservability already closed!");
@ -135,8 +144,7 @@ public final class GcpObservability implements AutoCloseable {
if (config.isEnableCloudMonitoring() || config.isEnableCloudTracing()) {
try {
// Sleeping before shutdown to ensure all metrics and traces are flushed
Thread.sleep(
TimeUnit.MILLISECONDS.convert(2 * METRICS_EXPORT_INTERVAL, TimeUnit.SECONDS));
timeUnit.sleep(sleepTime);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
logger.log(Level.SEVERE, "Caught exception during sleep", e);

View File

@ -45,6 +45,7 @@ import io.grpc.gcp.observability.logging.Sink;
import io.opencensus.trace.samplers.Samplers;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -196,9 +197,9 @@ public class GcpObservabilityTest {
mock(InternalLoggingServerInterceptor.Factory.class);
when(serverInterceptorFactory.create()).thenReturn(serverInterceptor);
try (GcpObservability unused =
GcpObservability.grpcInit(
sink, config, channelInterceptorFactory, serverInterceptorFactory)) {
try {
GcpObservability gcpObservability = GcpObservability.grpcInit(
sink, config, channelInterceptorFactory, serverInterceptorFactory);
List<?> configurators = InternalConfiguratorRegistry.getConfigurators();
assertThat(configurators).hasSize(1);
ObservabilityConfigurator configurator = (ObservabilityConfigurator) configurators.get(0);
@ -208,9 +209,11 @@ public class GcpObservabilityTest {
assertThat(list.get(2)).isInstanceOf(ConditionalClientInterceptor.class);
assertThat(configurator.serverInterceptors).hasSize(1);
assertThat(configurator.tracerFactories).hasSize(2);
gcpObservability.closeWithSleepTime(3000, TimeUnit.MILLISECONDS);
} catch (Exception e) {
fail("Encountered exception: " + e);
}
verify(sink).close();
}
}