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

Commit 72d915c

Browse files
authored
Exporter/Stackdriver: Expose unregister API. (#1918)
* Exporter/Stackdriver: Expose unregister API. * Add threadsafe annotation.
1 parent 62ab074 commit 72d915c

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## Unreleased
2+
- Make `StackdriverStatsExporter.unregister()` a public API.
23

34
## 0.22.1 - 2019-05-21
45
- Increase the buffer size for the trace export batch to 2500 (previously it was 32).

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.util.Map;
4848
import javax.annotation.Nullable;
4949
import javax.annotation.concurrent.GuardedBy;
50+
import javax.annotation.concurrent.ThreadSafe;
5051

5152
/**
5253
* Exporter to Stackdriver Monitoring Client API v3.
@@ -67,6 +68,7 @@
6768
*
6869
* @since 0.9
6970
*/
71+
@ThreadSafe
7072
public final class StackdriverStatsExporter {
7173

7274
@VisibleForTesting static final Object monitor = new Object();
@@ -420,9 +422,14 @@ static MetricServiceClient createMetricServiceClient(
420422
return MetricServiceClient.create(settingsBuilder.build());
421423
}
422424

423-
// Resets exporter to null. Used only for unit tests.
424-
@VisibleForTesting
425-
static void unsafeResetExporter() {
425+
/**
426+
* Unregisters the {@link StackdriverStatsExporter} and stops metrics exporting.
427+
*
428+
* <p>Unexported data will be flushed before the exporter is stopped.
429+
*
430+
* @since 0.23
431+
*/
432+
public static void unregister() {
426433
synchronized (monitor) {
427434
if (instance != null) {
428435
instance.intervalMetricReader.stop();

exporters/stats/stackdriver/src/test/java/io/opencensus/exporter/stats/stackdriver/StackdriverStatsExporterTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,20 @@ public void createExporterTwice() throws IOException {
115115
thrown.expectMessage("Stackdriver stats exporter is already created.");
116116
StackdriverStatsExporter.createAndRegister(CONFIGURATION);
117117
} finally {
118-
StackdriverStatsExporter.unsafeResetExporter();
118+
StackdriverStatsExporter.unregister();
119+
}
120+
}
121+
122+
@Test
123+
public void unregister() throws IOException {
124+
// unregister has no effect if exporter is not yet registered.
125+
StackdriverStatsExporter.unregister();
126+
try {
127+
StackdriverStatsExporter.createAndRegister(CONFIGURATION);
128+
StackdriverStatsExporter.unregister();
129+
StackdriverStatsExporter.createAndRegister(CONFIGURATION);
130+
} finally {
131+
StackdriverStatsExporter.unregister();
119132
}
120133
}
121134

0 commit comments

Comments
 (0)