Skip to content
This repository has been archived by the owner on Dec 23, 2023. It is now read-only.

Commit

Permalink
fix: Shutdown Stackdriver MetricServiceClient properly (#2091)
Browse files Browse the repository at this point in the history
Using the default configuration of StackdriverStatsConfiguration, a
MetricServiceClient is initialized by the StackdriverStatsExporter.

This client was never closed, this will be done now when the
.unregister() method is called on the exporter.

If a custom MetricServiceStub is given by the user, it will *not* be
closed as the user should be in charge of it.
  • Loading branch information
janhicken committed Jan 13, 2022
1 parent 0899c0b commit 81225af
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,7 @@
## Unreleased

- fix: Shutdown `MetricServiceClient` properly on `StackdriverStatsExporter.unregister()` (#2007)

## 0.28.3 - 2021-01-12

- fix: Return public access to unsafe `ContextUtils` api. Remove bincompat issue from 0.27.1. (#2072)
Expand Down
Expand Up @@ -77,6 +77,10 @@ public final class StackdriverStatsExporter {
@Nullable
private static StackdriverStatsExporter instance = null;

@GuardedBy("monitor")
@Nullable
private static MetricServiceClient metricServiceClient = null;

private static final String EXPORTER_SPAN_NAME = "ExportMetricsToStackdriver";

// See io.grpc.internal.GrpcUtil.USER_AGENT_KEY
Expand Down Expand Up @@ -392,10 +396,13 @@ private static void createInternal(
throws IOException {
synchronized (monitor) {
checkState(instance == null, "Stackdriver stats exporter is already created.");
MetricServiceClient client =
stub == null
? createMetricServiceClient(credentials, deadline)
: MetricServiceClient.create(stub);
final MetricServiceClient client;
if (stub == null) {
metricServiceClient = createMetricServiceClient(credentials, deadline);
client = metricServiceClient;
} else {
client = MetricServiceClient.create(stub);
}
instance =
new StackdriverStatsExporter(
projectId,
Expand Down Expand Up @@ -445,6 +452,10 @@ public static void unregister() {
instance.intervalMetricReader.stop();
}
instance = null;
if (metricServiceClient != null) {
metricServiceClient.close();
metricServiceClient = null;
}
}
}
}

0 comments on commit 81225af

Please sign in to comment.