gcp-o11y: add sleep in Observability close()

This commit adds sleep in `close()` for metrics and/or traces to be
flushed before closing observability.

Currently sleep is set to 2 * [Metrics export interval (30 secs)].
This commit is contained in:
DNVindhya 2023-03-22 08:40:05 -07:00 committed by GitHub
parent 9039d4dcff
commit 783de5dfc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 0 deletions

View File

@ -53,11 +53,16 @@ import io.opencensus.trace.config.TraceConfig;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** The main class for gRPC Google Cloud Platform Observability features. */ /** The main class for gRPC Google Cloud Platform Observability features. */
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/8869") @ExperimentalApi("https://github.com/grpc/grpc-java/issues/8869")
public final class GcpObservability implements AutoCloseable { public final class GcpObservability implements AutoCloseable {
private static final Logger logger = Logger.getLogger(GcpObservability.class.getName());
private static final int METRICS_EXPORT_INTERVAL = 30; private static final int METRICS_EXPORT_INTERVAL = 30;
@VisibleForTesting @VisibleForTesting
static final ImmutableSet<String> SERVICES_TO_EXCLUDE = ImmutableSet.of( static final ImmutableSet<String> SERVICES_TO_EXCLUDE = ImmutableSet.of(
@ -117,6 +122,16 @@ public final class GcpObservability implements AutoCloseable {
throw new IllegalStateException("GcpObservability already closed!"); throw new IllegalStateException("GcpObservability already closed!");
} }
sink.close(); sink.close();
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));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
logger.log(Level.SEVERE, "Caught exception during sleep", e);
}
}
instance = null; instance = null;
} }
} }