Repository navigation
Expand file tree
/
Copy pathclient.ts
More file actions
212 lines (180 loc) · 8.67 KB
/
Copy pathclient.ts
File metadata and controls
212 lines (180 loc) · 8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { SERVER_ADDRESS } from '@sentry/conventions/attributes';
import * as os from 'node:os';
import type { Tracer } from '@opentelemetry/api';
import { trace } from '@opentelemetry/api';
import type { ServerRuntimeClientOptions } from '@sentry/core/server';
import {
_INTERNAL_clearAiProviderSkips,
_INTERNAL_setDeferSegmentSpanCapture,
applySdkMetadata,
debug,
SDK_VERSION,
} from '@sentry/core';
import { ServerRuntimeClient } from '@sentry/core/server';
import {
type AsyncLocalStorageLookup,
registerPrepareSpanScope,
type SentryTracerProvider,
setOpenTelemetryContextAsyncContextStrategy,
} from '@sentry/opentelemetry';
import { registerDiagnosticsChannelInjection } from '@sentry/server-runtime-injection/register';
import { setAsyncLocalStorageAsyncContextStrategy } from '@sentry/server-utils';
import { isMainThread, threadId } from 'worker_threads';
import { DEBUG_BUILD } from '../debug-build';
import type { NodeClientOptions } from '../types';
const DEFAULT_CLIENT_REPORT_FLUSH_INTERVAL_MS = 60_000; // 60s was chosen arbitrarily
// Treeshakable guard to remove all code related to runtime diagnostics-channel injection. Set to
// `false` at build time by the Sentry bundler plugins' `bundleSizeOptimizations.excludeChannelInjection`.
declare const __SENTRY_CHANNEL_INJECTION__: boolean | undefined;
/** A client for using Sentry with Node & OpenTelemetry. */
export class NodeClient extends ServerRuntimeClient<NodeClientOptions> {
public traceProvider: SentryTracerProvider | undefined;
public asyncLocalStorageLookup: AsyncLocalStorageLookup | undefined;
private _tracer: Tracer | undefined;
private _clientReportInterval: NodeJS.Timeout | undefined;
private _clientReportOnExitFlushListener: (() => void) | undefined;
private _onExitFlushListener: (() => void) | undefined;
public constructor(options: NodeClientOptions) {
const serverName =
options.includeServerName === false
? undefined
: options.serverName || global.process.env.SENTRY_NAME || os.hostname();
const clientOptions: ServerRuntimeClientOptions = {
...options,
platform: 'node',
// Use provided runtime or default to 'node' with current process version
runtime: options.runtime || { name: 'node', version: global.process.version },
serverName,
};
applySdkMetadata(clientOptions, 'node');
debug.log(`Initializing Sentry: process: ${process.pid}, thread: ${isMainThread ? 'main' : `worker-${threadId}`}.`);
super(clientOptions);
// The log, metric and span buffers drain on `flush`. Their timers are unref'd, so a natural exit
// would drop what they hold.
this._onExitFlushListener = () => {
this.emit('flush');
};
if (serverName) {
this.on('beforeCaptureLog', log => {
log.attributes = {
...log.attributes,
[SERVER_ADDRESS]: serverName,
};
});
}
process.on('beforeExit', this._onExitFlushListener);
// Enable deferred segment-span transaction capture here, in the constructor, rather than in
// `initOtel`. Every client runs its constructor exactly once, whereas `initOtel` only runs on
// `Sentry.init()` and only fully wires up the first client (a second `init` loses the
// `setGlobalTracerProvider` race and bails early, and a manually constructed `NodeClient` never
// runs `initOtel` at all). Anchoring on the constructor means every client — first, second, or
// manual — defers correctly. It's unconditional and cheap: clients on the OpenTelemetry SDK
// provider path produce OTel spans that never reach `SentrySpan`, so the strategy is simply never
// consulted for them.
_INTERNAL_setDeferSegmentSpanCapture(this);
// Same constructor anchoring as above: every client must continue incoming (remote) traces,
// also manually constructed ones that never run `initOtel`.
registerPrepareSpanScope(this);
// Install the channel-based (orchestrion diagnostics-channel) instrumentation hooks here, in the
// constructor, so that every client installs them — not only the one built by the Node SDK's
// `init()`. Downstream SDKs construct a client without going through that path, and would
// otherwise never install the hooks. Registration is idempotent (a global marker guards it), so
// a second construction is harmless. The channel integrations capture errors as well as spans,
// so this is independent of tracing. Opt out at runtime with `enableRuntimeChannelInjection:
// false`, or at build time via the bundler plugins'
// `bundleSizeOptimizations.excludeChannelInjection` (which tree-shakes this whole block away).
if (
(typeof __SENTRY_CHANNEL_INJECTION__ === 'undefined' || __SENTRY_CHANNEL_INJECTION__) &&
options.enableRuntimeChannelInjection !== false
) {
registerDiagnosticsChannelInjection();
}
}
/** @inheritDoc */
public init(): void {
// Must run before `super.init()`: channel-based integrations capture the strategy's
// AsyncLocalStorage via `getTracingChannelBinding()` during integration setup.
if (this.getOptions().enableOpenTelemetrySetup) {
this.asyncLocalStorageLookup = setOpenTelemetryContextAsyncContextStrategy();
} else {
this.asyncLocalStorageLookup = { asyncLocalStorage: setAsyncLocalStorageAsyncContextStrategy() };
}
super.init();
}
/** Get the OTEL tracer. */
public get tracer(): Tracer {
if (this._tracer) {
return this._tracer;
}
const name = '@sentry/node';
const version = SDK_VERSION;
const tracer = trace.getTracer(name, version);
this._tracer = tracer;
return tracer;
}
/** @inheritDoc */
// @ts-expect-error - PromiseLike is a subset of Promise
public async flush(timeout?: number): PromiseLike<boolean> {
await this.traceProvider?.forceFlush();
if (this.getOptions().sendClientReports) {
this._flushOutcomes();
}
return super.flush(timeout);
}
/** @inheritDoc */
// @ts-expect-error - PromiseLike is a subset of Promise
public async close(timeout?: number | undefined): PromiseLike<boolean> {
if (this._clientReportInterval) {
clearInterval(this._clientReportInterval);
}
if (this._clientReportOnExitFlushListener) {
process.off('beforeExit', this._clientReportOnExitFlushListener);
}
if (this._onExitFlushListener) {
process.off('beforeExit', this._onExitFlushListener);
}
const allEventsSent = await super.close(timeout);
if (this.traceProvider) {
await this.traceProvider.shutdown();
}
return allEventsSent;
}
/**
* Will start tracking client reports for this client.
*
* NOTICE: This method will create an interval that is periodically called and attach a `process.on('beforeExit')`
* hook. To clean up these resources, call `.close()` when you no longer intend to use the client. Not doing so will
* result in a memory leak.
*/
// The reason client reports need to be manually activated with this method instead of just enabling them in a
// constructor, is that if users periodically and unboundedly create new clients, we will create more and more
// intervals and beforeExit listeners, thus leaking memory. In these situations, users are required to call
// `client.close()` in order to dispose of the acquired resources.
// We assume that calling this method in Sentry.init() is a sensible default, because calling Sentry.init() over and
// over again would also result in memory leaks.
// Note: We have experimented with using `FinalizationRegisty` to clear the interval when the client is garbage
// collected, but it did not work, because the cleanup function never got called.
public startClientReportTracking(): void {
const clientOptions = this.getOptions();
if (clientOptions.sendClientReports) {
this._clientReportOnExitFlushListener = () => {
this._flushOutcomes();
};
this._clientReportInterval = setInterval(() => {
DEBUG_BUILD && debug.log('Flushing client reports based on interval.');
this._flushOutcomes();
}, clientOptions.clientReportFlushInterval ?? DEFAULT_CLIENT_REPORT_FLUSH_INTERVAL_MS)
// Unref is critical for not preventing the process from exiting because the interval is active.
.unref();
process.on('beforeExit', this._clientReportOnExitFlushListener);
}
}
/** @inheritDoc */
protected _setupIntegrations(): void {
// Clear AI provider skip registrations before setting up integrations
// This ensures a clean state between different client initializations
// (e.g., when LangChain skips OpenAI in one client, but a subsequent client uses OpenAI standalone)
_INTERNAL_clearAiProviderSkips();
super._setupIntegrations();
}
}