Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 11 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ static GcpObservability grpcInit(
/** 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!");
Expand All @@ -135,8 +144,7 @@ public void close() {
if (config.isEnableCloudMonitoring() || config.isEnableCloudTracing()) {
try {
// Sleeping before shutdown to ensure all metrics and traces are flushed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restore this comment. If you don't think it makes sense here any more (I'm assuming that's why you deleted it), then at least move it to close() just before the closeWithSleepTime() call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved and restored the comment.

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.util.concurrent.TimeUnit;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put it in Lexicographical order.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved


@RunWith(JUnit4.class)
public class GcpObservabilityTest {
Expand Down Expand Up @@ -196,9 +197,9 @@ public void run() {
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);
Expand All @@ -208,9 +209,11 @@ public void run() {
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) {
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
fail("Encountered exception: " + e);
}
verify(sink).close();
}
}

Expand Down
Loading