1
- import type { Scope } from '@sentry/core' ;
2
- import {
3
- addTracingExtensions ,
4
- BaseClient ,
5
- createCheckInEnvelope ,
6
- getDynamicSamplingContextFromClient ,
7
- SDK_VERSION ,
8
- SessionFlusher ,
9
- } from '@sentry/core' ;
10
- import type {
11
- CheckIn ,
12
- DynamicSamplingContext ,
13
- Event ,
14
- EventHint ,
15
- MonitorConfig ,
16
- SerializedCheckIn ,
17
- Severity ,
18
- SeverityLevel ,
19
- TraceContext ,
20
- } from '@sentry/types' ;
21
- import { logger , resolvedSyncPromise , uuid4 } from '@sentry/utils' ;
1
+ import type { Scope , ServerRuntimeClientOptions } from '@sentry/core' ;
2
+ import { SDK_VERSION , ServerRuntimeClient , SessionFlusher } from '@sentry/core' ;
3
+ import type { Event , EventHint } from '@sentry/types' ;
4
+ import { logger } from '@sentry/utils' ;
22
5
import * as os from 'os' ;
23
6
import { TextEncoder } from 'util' ;
24
7
25
- import { eventFromMessage , eventFromUnknownInput } from './eventbuilder' ;
26
8
import type { NodeClientOptions } from './types' ;
27
9
28
10
/**
@@ -31,7 +13,7 @@ import type { NodeClientOptions } from './types';
31
13
* @see NodeClientOptions for documentation on configuration options.
32
14
* @see SentryClient for usage documentation.
33
15
*/
34
- export class NodeClient extends BaseClient < NodeClientOptions > {
16
+ export class NodeClient extends ServerRuntimeClient < NodeClientOptions > {
35
17
protected _sessionFlusher : SessionFlusher | undefined ;
36
18
37
19
/**
@@ -57,10 +39,14 @@ export class NodeClient extends BaseClient<NodeClientOptions> {
57
39
...options . transportOptions ,
58
40
} ;
59
41
60
- // The Node client always supports tracing
61
- addTracingExtensions ( ) ;
42
+ const clientOptions : ServerRuntimeClientOptions = {
43
+ ...options ,
44
+ platform : 'node' ,
45
+ runtime : { name : 'node' , version : global . process . version } ,
46
+ serverName : options . serverName || global . process . env . SENTRY_NAME || os . hostname ( ) ,
47
+ } ;
62
48
63
- super ( options ) ;
49
+ super ( clientOptions ) ;
64
50
}
65
51
66
52
/**
@@ -133,104 +119,6 @@ export class NodeClient extends BaseClient<NodeClientOptions> {
133
119
}
134
120
}
135
121
136
- /**
137
- * @inheritDoc
138
- */
139
- // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
140
- public eventFromException ( exception : any , hint ?: EventHint ) : PromiseLike < Event > {
141
- return resolvedSyncPromise ( eventFromUnknownInput ( this . _options . stackParser , exception , hint ) ) ;
142
- }
143
-
144
- /**
145
- * @inheritDoc
146
- */
147
- public eventFromMessage (
148
- message : string ,
149
- // eslint-disable-next-line deprecation/deprecation
150
- level : Severity | SeverityLevel = 'info' ,
151
- hint ?: EventHint ,
152
- ) : PromiseLike < Event > {
153
- return resolvedSyncPromise (
154
- eventFromMessage ( this . _options . stackParser , message , level , hint , this . _options . attachStacktrace ) ,
155
- ) ;
156
- }
157
-
158
- /**
159
- * Create a cron monitor check in and send it to Sentry.
160
- *
161
- * @param checkIn An object that describes a check in.
162
- * @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want
163
- * to create a monitor automatically when sending a check in.
164
- * @returns A string representing the id of the check in.
165
- */
166
- public captureCheckIn ( checkIn : CheckIn , monitorConfig ?: MonitorConfig , scope ?: Scope ) : string {
167
- const id = checkIn . status !== 'in_progress' && checkIn . checkInId ? checkIn . checkInId : uuid4 ( ) ;
168
- if ( ! this . _isEnabled ( ) ) {
169
- __DEBUG_BUILD__ && logger . warn ( 'SDK not enabled, will not capture checkin.' ) ;
170
- return id ;
171
- }
172
-
173
- const options = this . getOptions ( ) ;
174
- const { release, environment, tunnel } = options ;
175
-
176
- const serializedCheckIn : SerializedCheckIn = {
177
- check_in_id : id ,
178
- monitor_slug : checkIn . monitorSlug ,
179
- status : checkIn . status ,
180
- release,
181
- environment,
182
- } ;
183
-
184
- if ( checkIn . status !== 'in_progress' ) {
185
- serializedCheckIn . duration = checkIn . duration ;
186
- }
187
-
188
- if ( monitorConfig ) {
189
- serializedCheckIn . monitor_config = {
190
- schedule : monitorConfig . schedule ,
191
- checkin_margin : monitorConfig . checkinMargin ,
192
- max_runtime : monitorConfig . maxRuntime ,
193
- timezone : monitorConfig . timezone ,
194
- } ;
195
- }
196
-
197
- const [ dynamicSamplingContext , traceContext ] = this . _getTraceInfoFromScope ( scope ) ;
198
- if ( traceContext ) {
199
- serializedCheckIn . contexts = {
200
- trace : traceContext ,
201
- } ;
202
- }
203
-
204
- const envelope = createCheckInEnvelope (
205
- serializedCheckIn ,
206
- dynamicSamplingContext ,
207
- this . getSdkMetadata ( ) ,
208
- tunnel ,
209
- this . getDsn ( ) ,
210
- ) ;
211
-
212
- __DEBUG_BUILD__ && logger . info ( 'Sending checkin:' , checkIn . monitorSlug , checkIn . status ) ;
213
- void this . _sendEnvelope ( envelope ) ;
214
- return id ;
215
- }
216
-
217
- /**
218
- * @inheritDoc
219
- */
220
- protected _prepareEvent ( event : Event , hint : EventHint , scope ?: Scope ) : PromiseLike < Event | null > {
221
- event . platform = event . platform || 'node' ;
222
- event . contexts = {
223
- ...event . contexts ,
224
- runtime : event . contexts ?. runtime || {
225
- name : 'node' ,
226
- version : global . process . version ,
227
- } ,
228
- } ;
229
- event . server_name =
230
- event . server_name || this . getOptions ( ) . serverName || global . process . env . SENTRY_NAME || os . hostname ( ) ;
231
- return super . _prepareEvent ( event , hint , scope ) ;
232
- }
233
-
234
122
/**
235
123
* Method responsible for capturing/ending a request session by calling `incrementSessionStatusCount` to increment
236
124
* appropriate session aggregates bucket
@@ -242,30 +130,4 @@ export class NodeClient extends BaseClient<NodeClientOptions> {
242
130
this . _sessionFlusher . incrementSessionStatusCount ( ) ;
243
131
}
244
132
}
245
-
246
- /** Extract trace information from scope */
247
- private _getTraceInfoFromScope (
248
- scope : Scope | undefined ,
249
- ) : [ dynamicSamplingContext : Partial < DynamicSamplingContext > | undefined , traceContext : TraceContext | undefined ] {
250
- if ( ! scope ) {
251
- return [ undefined , undefined ] ;
252
- }
253
-
254
- const span = scope . getSpan ( ) ;
255
- if ( span ) {
256
- return [ span ?. transaction ?. getDynamicSamplingContext ( ) , span ?. getTraceContext ( ) ] ;
257
- }
258
-
259
- const { traceId, spanId, parentSpanId, dsc } = scope . getPropagationContext ( ) ;
260
- const traceContext : TraceContext = {
261
- trace_id : traceId ,
262
- span_id : spanId ,
263
- parent_span_id : parentSpanId ,
264
- } ;
265
- if ( dsc ) {
266
- return [ dsc , traceContext ] ;
267
- }
268
-
269
- return [ getDynamicSamplingContextFromClient ( traceId , this , scope ) , traceContext ] ;
270
- }
271
133
}
0 commit comments