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. */ /** Un-initialize/shutdown grpc-observability. */
@Override @Override
public void close() { 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) { synchronized (GcpObservability.class) {
if (instance == null) { if (instance == null) {
throw new IllegalStateException("GcpObservability already closed!"); throw new IllegalStateException("GcpObservability already closed!");
@ -135,8 +144,7 @@ public final class GcpObservability implements AutoCloseable {
if (config.isEnableCloudMonitoring() || config.isEnableCloudTracing()) { if (config.isEnableCloudMonitoring() || config.isEnableCloudTracing()) {
try { try {
// Sleeping before shutdown to ensure all metrics and traces are flushed // Sleeping before shutdown to ensure all metrics and traces are flushed
Thread.sleep( timeUnit.sleep(sleepTime);
TimeUnit.MILLISECONDS.convert(2 * METRICS_EXPORT_INTERVAL, TimeUnit.SECONDS));
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
logger.log(Level.SEVERE, "Caught exception during sleep", e); 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 io.opencensus.trace.samplers.Samplers;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -196,9 +197,9 @@ public class GcpObservabilityTest {
mock(InternalLoggingServerInterceptor.Factory.class); mock(InternalLoggingServerInterceptor.Factory.class);
when(serverInterceptorFactory.create()).thenReturn(serverInterceptor); when(serverInterceptorFactory.create()).thenReturn(serverInterceptor);
try (GcpObservability unused = try {
GcpObservability.grpcInit( GcpObservability gcpObservability = GcpObservability.grpcInit(
sink, config, channelInterceptorFactory, serverInterceptorFactory)) { sink, config, channelInterceptorFactory, serverInterceptorFactory);
List<?> configurators = InternalConfiguratorRegistry.getConfigurators(); List<?> configurators = InternalConfiguratorRegistry.getConfigurators();
assertThat(configurators).hasSize(1); assertThat(configurators).hasSize(1);
ObservabilityConfigurator configurator = (ObservabilityConfigurator) configurators.get(0); ObservabilityConfigurator configurator = (ObservabilityConfigurator) configurators.get(0);
@ -208,9 +209,11 @@ public class GcpObservabilityTest {
assertThat(list.get(2)).isInstanceOf(ConditionalClientInterceptor.class); assertThat(list.get(2)).isInstanceOf(ConditionalClientInterceptor.class);
assertThat(configurator.serverInterceptors).hasSize(1); assertThat(configurator.serverInterceptors).hasSize(1);
assertThat(configurator.tracerFactories).hasSize(2); assertThat(configurator.tracerFactories).hasSize(2);
gcpObservability.closeWithSleepTime(3000, TimeUnit.MILLISECONDS);
} catch (Exception e) { } catch (Exception e) {
fail("Encountered exception: " + e); fail("Encountered exception: " + e);
} }
verify(sink).close();
} }
} }