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

Commit 85e2fc2

Browse files
authored
Add support for unregister stats exporter whenever the exporter is not needed anymore (#397)
1 parent 34dd809 commit 85e2fc2

File tree

12 files changed

+56
-2
lines changed

12 files changed

+56
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file.
1616
- Enforce `--strictNullChecks` and `--noUnusedLocals` Compiler Options on [opencensus-instrumentation-http], [opencensus-instrumentation-grpc] and [opencensus-propagation-tracecontext] packages.
1717
- Enforce `--strictNullChecks` and `--noUnusedLocals` Compiler Options on [opencensus-exporter-zipkin] packages.
1818
- Add NoRecordRootSpan, NoRecordSpan and NoRecordSpanBase.
19+
- Add an API `globalStats.unregisterExporter()`.
1920

2021
## 0.0.9 - 2019-02-12
2122
- Add Metrics API.

packages/opencensus-core/src/exporters/console-exporter.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,7 @@ export class ConsoleStatsExporter implements types.StatsEventListener {
106106
start(): void {
107107
// TODO(mayurkale): dependency with PR#253.
108108
}
109+
110+
/** Stops the exporter. */
111+
stop(): void {}
109112
}

packages/opencensus-core/src/exporters/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ export interface StatsEventListener {
5454
* batched data to backend.
5555
*/
5656
start(): void;
57+
58+
/** Stops the exporter. */
59+
stop(): void;
5760
}
5861

5962
export type ExporterConfig = configTypes.BufferConfig;

packages/opencensus-core/src/stats/stats.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ export class BaseStats implements Stats {
107107
exporter.start();
108108
}
109109

110+
/**
111+
* Unregisters an exporter. It should be called whenever the exporter is not
112+
* needed anymore.
113+
* @param exporter An stats exporter
114+
*/
115+
unregisterExporter(exporter: StatsEventListener): void {
116+
if (exporter) {
117+
this.statsEventListeners = this.statsEventListeners.filter(
118+
currentExporter => currentExporter !== exporter);
119+
exporter.stop();
120+
}
121+
}
122+
110123
/**
111124
* Creates a measure of type Double.
112125
* @param name The measure name

packages/opencensus-core/src/stats/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ export interface Stats {
8686
* @param exporter An stats exporter
8787
*/
8888
registerExporter(exporter: StatsEventListener): void;
89+
90+
/**
91+
* Unregisters an exporter. It should be called whenever the exporter is not
92+
* needed anymore.
93+
* @param exporter An stats exporter
94+
*/
95+
unregisterExporter(exporter: StatsEventListener): void;
8996
}
9097

9198
/**

packages/opencensus-core/test/test-stats.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class TestExporter implements StatsEventListener {
3535
// TODO(mayurkale): dependency with PR#253.
3636
}
3737

38+
stop(): void {}
39+
3840
clean() {
3941
this.registeredViews = [];
4042
this.recordedMeasurements = [];
@@ -143,6 +145,21 @@ describe('Stats', () => {
143145
});
144146
});
145147

148+
describe('unregisterExporter()', () => {
149+
const testExporter = new TestExporter();
150+
151+
it('should unregister the exporter', () => {
152+
globalStats.registerExporter(testExporter);
153+
const measure = globalStats.createMeasureInt64(measureName, measureUnit);
154+
const view = new BaseView(
155+
viewName, measure, AggregationType.LAST_VALUE, tagKeys, description);
156+
globalStats.unregisterExporter(testExporter);
157+
globalStats.registerView(view);
158+
159+
assert.strictEqual(testExporter.registeredViews.length, 0);
160+
});
161+
});
162+
146163
describe('record()', () => {
147164
let measure: Measure;
148165
const testExporter = new TestExporter();

packages/opencensus-exporter-prometheus/src/prometheus-stats.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ export class PrometheusStatsExporter implements StatsEventListener {
9595
// // it to backend (dependency with PR#253).
9696
}
9797

98+
/** Stops the exporter. */
99+
stop(): void {
100+
this.stopServer();
101+
}
102+
98103
private getLabelValues(columns: TagKey[], tags: Map<TagKey, TagValue>):
99104
labelValues {
100105
const labels: labelValues = {};

packages/opencensus-exporter-stackdriver/src/stackdriver-monitoring.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export class StackdriverStatsExporter implements StatsEventListener {
199199
* Clear the interval timer to stop uploading metrics. It should be called
200200
* whenever the exporter is not needed anymore.
201201
*/
202-
close() {
202+
stop() {
203203
clearInterval(this.timer);
204204
}
205205

packages/opencensus-exporter-stackdriver/test/test-stackdriver-monitoring.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('Stackdriver Stats Exporter', () => {
7171
});
7272

7373
afterEach(() => {
74-
exporter.close();
74+
exporter.stop();
7575
mockLogger.cleanAll();
7676
globalStats.clear();
7777
});

packages/opencensus-exporter-zpages/src/zpages.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ export class ZpagesExporter implements Exporter, StatsEventListener {
134134
// TODO(mayurkale): dependency with PR#253.
135135
}
136136

137+
/** Stops the exporter. */
138+
stop(): void {}
139+
137140
/**
138141
* Send a trace to traces array
139142
* @param trace the rootSpan to be sent to the array list

packages/opencensus-instrumentation-grpc/test/test-grpc.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class TestExporter implements StatsEventListener {
4343
}
4444

4545
start(): void {}
46+
stop(): void {}
4647

4748
clean() {
4849
this.registeredViews = [];

packages/opencensus-instrumentation-http/test/test-http.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class TestExporter implements StatsEventListener {
4545
}
4646

4747
start(): void {}
48+
stop(): void {}
4849

4950
clean() {
5051
this.registeredViews = [];

0 commit comments

Comments
 (0)