Skip to content

Commit cb11d4c

Browse files
author
Luca Forstner
committed
Extract into method and only start tracking in Sentry.init by default
1 parent 2b03c5b commit cb11d4c

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

packages/core/src/baseclient.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,8 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
877877
* Sends client reports as an envelope.
878878
*/
879879
protected _flushOutcomes(): void {
880+
DEBUG_BUILD && logger.log('Flushing outcomes...');
881+
880882
const outcomes = this._clearOutcomes();
881883

882884
if (outcomes.length === 0) {

packages/node/src/sdk/client.ts

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,6 @@ export class NodeClient extends ServerRuntimeClient<NodeClientOptions> {
3333
);
3434

3535
super(clientOptions);
36-
37-
if (clientOptions.sendClientReports !== false) {
38-
// There is one mild concern here, being that if users periodically and unboundedly create new clients, we will
39-
// create more and more intervals and beforeExit listeners, which may leak memory. In these situations, users are
40-
// required to call `client.close()` in order to dispose of the acquired resources.
41-
// Users are already confronted with the same reality with the SessionFlusher at the time of writing this so the
42-
// working theory is that this should be fine.
43-
// Note: We have experimented with using `FinalizationRegisty` to clear the interval when the client is garbage
44-
// collected, but it did not work, because the cleanup function never got called.
45-
this._clientReportInterval = setInterval(() => {
46-
DEBUG_BUILD && logger.log('Flushing client reports based on interval.');
47-
this._flushOutcomes();
48-
}, CLIENT_REPORT_FLUSH_INTERVAL_MS)
49-
// Unref is critical, otherwise we stop the process from exiting by itself
50-
.unref();
51-
52-
this._clientReportOnExitFlushListener = () => {
53-
this._flushOutcomes();
54-
};
55-
56-
process.on('beforeExit', this._clientReportOnExitFlushListener);
57-
}
5836
}
5937

6038
/** Get the OTEL tracer. */
@@ -99,4 +77,36 @@ export class NodeClient extends ServerRuntimeClient<NodeClientOptions> {
9977

10078
return super.close(timeout);
10179
}
80+
81+
/**
82+
* Will start tracking client reports for this client.
83+
*
84+
* NOTICE: This method will create an interval that is periodically called and attach a `process.on('beforeExit')`
85+
* hook. To clean up these resources, call `.close()` when you no longer intend to use the client. Not doing so will
86+
* result in a memory leak.
87+
*/
88+
// The reason client reports need to be manually activated with this method instead of just enabling them in a
89+
// constructor, is that if users periodically and unboundedly create new clients, we will create more and more
90+
// intervals and beforeExit listeners, thus leaking memory. In these situations, users are required to call
91+
// `client.close()` in order to dispose of the acquired resources.
92+
// We assume that calling this method in Sentry.init() is a sensible default, because calling Sentry.init() over and
93+
// over again would also result in memory leaks.
94+
// Note: We have experimented with using `FinalizationRegisty` to clear the interval when the client is garbage
95+
// collected, but it did not work, because the cleanup function never got called.
96+
public startClientReportTracking(): void {
97+
if (this.getOptions().sendClientReports !== false) {
98+
this._clientReportOnExitFlushListener = () => {
99+
this._flushOutcomes();
100+
};
101+
102+
this._clientReportInterval = setInterval(() => {
103+
DEBUG_BUILD && logger.log('Flushing client reports based on interval.');
104+
this._flushOutcomes();
105+
}, CLIENT_REPORT_FLUSH_INTERVAL_MS)
106+
// Unref is critical for not preventing the process from exiting because the interval is active.
107+
.unref();
108+
109+
process.on('beforeExit', this._clientReportOnExitFlushListener);
110+
}
111+
}
102112
}

packages/node/src/sdk/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ function _init(
154154
startSessionTracking();
155155
}
156156

157+
client.startClientReportTracking();
158+
157159
updateScopeFromEnvVariables();
158160

159161
if (options.spotlight) {

0 commit comments

Comments
 (0)