Skip to content

Commit 088ea88

Browse files
committed
feat(node): Use ServerRuntimeClient
1 parent 4e107aa commit 088ea88

File tree

7 files changed

+21
-290
lines changed

7 files changed

+21
-290
lines changed

packages/node/src/client.ts

Lines changed: 12 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,10 @@
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';
225
import * as os from 'os';
236
import { TextEncoder } from 'util';
247

25-
import { eventFromMessage, eventFromUnknownInput } from './eventbuilder';
268
import type { NodeClientOptions } from './types';
279

2810
/**
@@ -31,7 +13,7 @@ import type { NodeClientOptions } from './types';
3113
* @see NodeClientOptions for documentation on configuration options.
3214
* @see SentryClient for usage documentation.
3315
*/
34-
export class NodeClient extends BaseClient<NodeClientOptions> {
16+
export class NodeClient extends ServerRuntimeClient<NodeClientOptions> {
3517
protected _sessionFlusher: SessionFlusher | undefined;
3618

3719
/**
@@ -57,10 +39,14 @@ export class NodeClient extends BaseClient<NodeClientOptions> {
5739
...options.transportOptions,
5840
};
5941

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+
};
6248

63-
super(options);
49+
super(clientOptions);
6450
}
6551

6652
/**
@@ -133,104 +119,6 @@ export class NodeClient extends BaseClient<NodeClientOptions> {
133119
}
134120
}
135121

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-
234122
/**
235123
* Method responsible for capturing/ending a request session by calling `incrementSessionStatusCount` to increment
236124
* appropriate session aggregates bucket
@@ -242,30 +130,4 @@ export class NodeClient extends BaseClient<NodeClientOptions> {
242130
this._sessionFlusher.incrementSessionStatusCount();
243131
}
244132
}
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-
}
271133
}

packages/node/src/eventbuilder.ts

Lines changed: 0 additions & 131 deletions
This file was deleted.

packages/node/src/integrations/linkederrors.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { Event, EventHint, EventProcessor, Hub, Integration } from '@sentry/types';
2-
import { applyAggregateErrorsToEvent } from '@sentry/utils';
2+
import { applyAggregateErrorsToEvent, exceptionFromError } from '@sentry/utils';
33

4-
import { exceptionFromError } from '../eventbuilder';
54
import { ContextLines } from './contextlines';
65

76
const DEFAULT_KEY = 'cause';

packages/node/test/client.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ describe('NodeClient', () => {
233233

234234
test('adds server name to event when value given in env', () => {
235235
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN });
236-
client = new NodeClient(options);
237236
process.env.SENTRY_NAME = 'foo';
237+
client = new NodeClient(options);
238238

239239
const event: Event = {};
240240
const hint: EventHint = {};

packages/node/test/context-lines.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { StackFrame } from '@sentry/types';
2+
import { parseStackFrames } from '@sentry/utils';
23
import * as fs from 'fs';
34

4-
import { parseStackFrames } from '../src/eventbuilder';
55
import { ContextLines, resetFileContentCache } from '../src/integrations/contextlines';
66
import { defaultStackParser } from '../src/sdk';
77
import { getError } from './helper/error';

packages/node/test/eventbuilders.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Client } from '@sentry/types';
2+
import { eventFromUnknownInput } from '@sentry/utils';
23

3-
import { defaultStackParser, Scope } from '../src';
4-
import { eventFromUnknownInput } from '../src/eventbuilder';
4+
import { defaultStackParser, getCurrentHub, Scope } from '../src';
55

66
const testScope = new Scope();
77

@@ -55,7 +55,7 @@ describe('eventFromUnknownInput', () => {
5555
},
5656
};
5757

58-
eventFromUnknownInput(defaultStackParser, deepObject);
58+
eventFromUnknownInput(getCurrentHub, defaultStackParser, deepObject);
5959

6060
const serializedObject = (testScope as any)._extra.__serialized__;
6161
expect(serializedObject).toBeDefined();

packages/node/test/stacktrace.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
* @license MIT
1111
*/
1212

13-
import { parseStackFrames } from '../src/eventbuilder';
13+
import { parseStackFrames } from '@sentry/utils';
14+
1415
import { defaultStackParser as stackParser } from '../src/sdk';
1516

1617
function testBasic() {
@@ -32,7 +33,7 @@ describe('Stack parsing', () => {
3233
const last = frames.length - 1;
3334
expect(frames[last].filename).toEqual(__filename);
3435
expect(frames[last].function).toEqual('testBasic');
35-
expect(frames[last].lineno).toEqual(17);
36+
expect(frames[last].lineno).toEqual(18);
3637
expect(frames[last].colno).toEqual(10);
3738
});
3839

0 commit comments

Comments
 (0)