Skip to content

Commit

Permalink
gcp-o11y: add sleep in Observability close()
Browse files Browse the repository at this point in the history
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)].
  • Loading branch information
DNVindhya authored and ejona86 committed Mar 22, 2023
1 parent 2e41c9a commit 882a27b
Showing 1 changed file with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,16 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/** The main class for gRPC Google Cloud Platform Observability features. */
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/8869")
public final class GcpObservability implements AutoCloseable {

private static final Logger logger = Logger.getLogger(GcpObservability.class.getName());
private static final int METRICS_EXPORT_INTERVAL = 30;
@VisibleForTesting
static final ImmutableSet<String> SERVICES_TO_EXCLUDE = ImmutableSet.of(
Expand Down Expand Up @@ -117,6 +122,16 @@ public void close() {
throw new IllegalStateException("GcpObservability already closed!");
}
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;
}
}
Expand Down

0 comments on commit 882a27b

Please sign in to comment.