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

Commit 81225af

Browse files
authored
fix: Shutdown Stackdriver MetricServiceClient properly (#2091)
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.
1 parent 0899c0b commit 81225af

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Unreleased
22

3+
- fix: Shutdown `MetricServiceClient` properly on `StackdriverStatsExporter.unregister()` (#2007)
4+
35
## 0.28.3 - 2021-01-12
46

57
- fix: Return public access to unsafe `ContextUtils` api. Remove bincompat issue from 0.27.1. (#2072)

exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverStatsExporter.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ public final class StackdriverStatsExporter {
7777
@Nullable
7878
private static StackdriverStatsExporter instance = null;
7979

80+
@GuardedBy("monitor")
81+
@Nullable
82+
private static MetricServiceClient metricServiceClient = null;
83+
8084
private static final String EXPORTER_SPAN_NAME = "ExportMetricsToStackdriver";
8185

8286
// See io.grpc.internal.GrpcUtil.USER_AGENT_KEY
@@ -392,10 +396,13 @@ private static void createInternal(
392396
throws IOException {
393397
synchronized (monitor) {
394398
checkState(instance == null, "Stackdriver stats exporter is already created.");
395-
MetricServiceClient client =
396-
stub == null
397-
? createMetricServiceClient(credentials, deadline)
398-
: MetricServiceClient.create(stub);
399+
final MetricServiceClient client;
400+
if (stub == null) {
401+
metricServiceClient = createMetricServiceClient(credentials, deadline);
402+
client = metricServiceClient;
403+
} else {
404+
client = MetricServiceClient.create(stub);
405+
}
399406
instance =
400407
new StackdriverStatsExporter(
401408
projectId,
@@ -445,6 +452,10 @@ public static void unregister() {
445452
instance.intervalMetricReader.stop();
446453
}
447454
instance = null;
455+
if (metricServiceClient != null) {
456+
metricServiceClient.close();
457+
metricServiceClient = null;
458+
}
448459
}
449460
}
450461
}

0 commit comments

Comments
 (0)