Skip to content

Commit a8badb8

Browse files
committed
move node-version- and server-name-adding code to client
1 parent 2a1689e commit a8badb8

File tree

8 files changed

+98
-74
lines changed

8 files changed

+98
-74
lines changed

packages/node/src/client.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { BaseClient, Scope, SDK_VERSION } from '@sentry/core';
22
import { SessionFlusher } from '@sentry/hub';
33
import { Event, EventHint, Severity, SeverityLevel } from '@sentry/types';
44
import { logger, resolvedSyncPromise } from '@sentry/utils';
5+
import * as os from 'os';
56
import { TextEncoder } from 'util';
67

78
import { eventFromMessage, eventFromUnknownInput } from './eventbuilder';
@@ -139,9 +140,15 @@ export class NodeClient extends BaseClient<NodeClientOptions> {
139140
*/
140141
protected _prepareEvent(event: Event, hint: EventHint, scope?: Scope): PromiseLike<Event | null> {
141142
event.platform = event.platform || 'node';
142-
if (this.getOptions().serverName) {
143-
event.server_name = this.getOptions().serverName;
144-
}
143+
event.contexts = {
144+
...event.contexts,
145+
runtime: {
146+
name: 'node',
147+
version: global.process.version,
148+
},
149+
};
150+
event.server_name =
151+
event.server_name || this.getOptions().serverName || global.process.env.SENTRY_NAME || os.hostname();
145152
return super._prepareEvent(event, hint, scope);
146153
}
147154

packages/node/src/handlers.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
import * as cookie from 'cookie';
1515
import * as domain from 'domain';
1616
import * as http from 'http';
17-
import * as os from 'os';
1817
import * as url from 'url';
1918

2019
import { NodeClient } from './client';
@@ -290,10 +289,8 @@ export function extractRequestData(
290289
export interface ParseRequestOptions {
291290
ip?: boolean;
292291
request?: boolean | string[];
293-
serverName?: boolean;
294292
transaction?: boolean | TransactionNamingScheme;
295293
user?: boolean | string[];
296-
version?: boolean;
297294
}
298295

299296
/**
@@ -309,23 +306,11 @@ export function parseRequest(event: Event, req: ExpressRequest, options?: ParseR
309306
options = {
310307
ip: false,
311308
request: true,
312-
serverName: true,
313309
transaction: true,
314310
user: true,
315-
version: true,
316311
...options,
317312
};
318313

319-
if (options.version) {
320-
event.contexts = {
321-
...event.contexts,
322-
runtime: {
323-
name: 'node',
324-
version: global.process.version,
325-
},
326-
};
327-
}
328-
329314
if (options.request) {
330315
// if the option value is `true`, use the default set of keys by not passing anything to `extractRequestData()`
331316
const extractedRequestData = Array.isArray(options.request)
@@ -337,10 +322,6 @@ export function parseRequest(event: Event, req: ExpressRequest, options?: ParseR
337322
};
338323
}
339324

340-
if (options.serverName && !event.server_name) {
341-
event.server_name = global.process.env.SENTRY_NAME || os.hostname();
342-
}
343-
344325
if (options.user) {
345326
const extractedUser = req.user && isPlainObject(req.user) ? extractUserData(req.user, options.user) : {};
346327

packages/node/test/client.test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Scope, SessionFlusher } from '@sentry/hub';
2+
import { Event, EventHint } from '@sentry/types';
3+
import * as os from 'os';
24

35
import { NodeClient } from '../src';
46
import { getDefaultNodeClientOptions } from './helper/node-client-options';
@@ -187,6 +189,81 @@ describe('NodeClient', () => {
187189
expect(requestSession!.status).toEqual('ok');
188190
});
189191
});
192+
193+
describe('_prepareEvent', () => {
194+
test('adds platform to event', () => {
195+
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN });
196+
client = new NodeClient(options);
197+
198+
const event: Event = {};
199+
const hint: EventHint = {};
200+
(client as any)._prepareEvent(event, hint);
201+
202+
expect(event.platform).toEqual('node');
203+
});
204+
205+
test('adds runtime context to event', () => {
206+
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN });
207+
client = new NodeClient(options);
208+
209+
const event: Event = {};
210+
const hint: EventHint = {};
211+
(client as any)._prepareEvent(event, hint);
212+
213+
expect(event.contexts?.runtime).toEqual({
214+
name: 'node',
215+
version: process.version,
216+
});
217+
});
218+
219+
test('adds server name to event when value passed in options', () => {
220+
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, serverName: 'foo' });
221+
client = new NodeClient(options);
222+
223+
const event: Event = {};
224+
const hint: EventHint = {};
225+
(client as any)._prepareEvent(event, hint);
226+
227+
expect(event.server_name).toEqual('foo');
228+
});
229+
230+
test('adds server name to event when value given in env', () => {
231+
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN });
232+
client = new NodeClient(options);
233+
process.env.SENTRY_NAME = 'foo';
234+
235+
const event: Event = {};
236+
const hint: EventHint = {};
237+
(client as any)._prepareEvent(event, hint);
238+
239+
expect(event.server_name).toEqual('foo');
240+
241+
delete process.env.SENTRY_NAME;
242+
});
243+
244+
test('adds hostname as event server name when no value given', () => {
245+
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN });
246+
client = new NodeClient(options);
247+
248+
const event: Event = {};
249+
const hint: EventHint = {};
250+
(client as any)._prepareEvent(event, hint);
251+
252+
expect(event.server_name).toEqual(os.hostname());
253+
});
254+
255+
test("doesn't clobber existing server name", () => {
256+
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, serverName: 'bar' });
257+
client = new NodeClient(options);
258+
259+
const event: Event = { server_name: 'foo' };
260+
const hint: EventHint = {};
261+
(client as any)._prepareEvent(event, hint);
262+
263+
expect(event.server_name).toEqual('foo');
264+
expect(event.server_name).not.toEqual('bar');
265+
});
266+
});
190267
});
191268

192269
describe('flush/close', () => {

packages/node/test/handlers.test.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as sentryCore from '@sentry/core';
22
import * as sentryHub from '@sentry/hub';
33
import { Hub } from '@sentry/hub';
44
import { Transaction } from '@sentry/tracing';
5-
import { Baggage, Runtime } from '@sentry/types';
5+
import { Baggage } from '@sentry/types';
66
import { SentryError } from '@sentry/utils';
77
import * as http from 'http';
88
import * as net from 'net';
@@ -55,25 +55,6 @@ describe('parseRequest', () => {
5555
};
5656
});
5757

58-
describe('parseRequest.contexts runtime', () => {
59-
test('runtime name must contain node', () => {
60-
const parsedRequest: Event = parseRequest({}, mockReq as ExpressRequest);
61-
expect((parsedRequest.contexts!.runtime as Runtime).name).toEqual('node');
62-
});
63-
64-
test('runtime version must contain current node version', () => {
65-
const parsedRequest: Event = parseRequest({}, mockReq as ExpressRequest);
66-
expect((parsedRequest.contexts!.runtime as Runtime).version).toEqual(process.version);
67-
});
68-
69-
test('runtime disbaled by options', () => {
70-
const parsedRequest: Event = parseRequest({}, mockReq as ExpressRequest, {
71-
version: false,
72-
});
73-
expect(parsedRequest).not.toHaveProperty('contexts.runtime');
74-
});
75-
});
76-
7758
describe('parseRequest.user properties', () => {
7859
const DEFAULT_USER_KEYS = ['id', 'username', 'email'];
7960
const CUSTOM_USER_KEYS = ['custom_property'];

packages/serverless/src/awslambda.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,6 @@ function enhanceScopeWithEnvironmentData(scope: Scope, context: Context, startTi
180180
scope.setTag('server_name', process.env._AWS_XRAY_DAEMON_ADDRESS || process.env.SENTRY_NAME || hostname());
181181
scope.setTag('url', `awslambda:///${context.functionName}`);
182182

183-
scope.setContext('runtime', {
184-
name: 'node',
185-
version: global.process.version,
186-
});
187-
188183
scope.setContext('aws.lambda', {
189184
aws_request_id: context.awsRequestId,
190185
function_name: context.functionName,

packages/serverless/src/gcpfunction/general.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Scope } from '@sentry/node';
22
import { Context as SentryContext } from '@sentry/types';
33
import type { Request, Response } from 'express';
4-
import { hostname } from 'os';
54

65
export interface HttpFunction {
76
(req: Request, res: Response): any; // eslint-disable-line @typescript-eslint/no-explicit-any
@@ -54,11 +53,6 @@ export interface WrapperOptions {
5453
* @param context event context
5554
*/
5655
export function configureScopeWithContext(scope: Scope, context: Context): void {
57-
scope.setContext('runtime', {
58-
name: 'node',
59-
version: global.process.version,
60-
});
61-
scope.setTag('server_name', process.env.SENTRY_NAME || hostname());
6256
scope.setContext('gcp.function.context', { ...context } as SentryContext);
6357
}
6458

packages/serverless/test/awslambda.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ function expectScopeSettings() {
4646
// @ts-ignore see "Why @ts-ignore" note
4747
expect(Sentry.fakeScope.setTag).toBeCalledWith('url', 'awslambda:///functionName');
4848
// @ts-ignore see "Why @ts-ignore" note
49-
expect(Sentry.fakeScope.setContext).toBeCalledWith('runtime', { name: 'node', version: expect.anything() });
50-
// @ts-ignore see "Why @ts-ignore" note
5149
expect(Sentry.fakeScope.setContext).toBeCalledWith(
5250
'aws.lambda',
5351
expect.objectContaining({
@@ -181,7 +179,7 @@ describe('AWSLambda', () => {
181179

182180
describe('wrapHandler() on sync handler', () => {
183181
test('successful execution', async () => {
184-
expect.assertions(10);
182+
expect.assertions(9);
185183

186184
const handler: Handler = (_event, _context, callback) => {
187185
callback(null, 42);
@@ -197,7 +195,7 @@ describe('AWSLambda', () => {
197195
});
198196

199197
test('unsuccessful execution', async () => {
200-
expect.assertions(10);
198+
expect.assertions(9);
201199

202200
const error = new Error('sorry');
203201
const handler: Handler = (_event, _context, callback) => {
@@ -264,7 +262,7 @@ describe('AWSLambda', () => {
264262
});
265263

266264
test('capture error', async () => {
267-
expect.assertions(10);
265+
expect.assertions(9);
268266

269267
const error = new Error('wat');
270268
const handler: Handler = (_event, _context, _callback) => {
@@ -294,7 +292,7 @@ describe('AWSLambda', () => {
294292

295293
describe('wrapHandler() on async handler', () => {
296294
test('successful execution', async () => {
297-
expect.assertions(10);
295+
expect.assertions(9);
298296

299297
const handler: Handler = async (_event, _context) => {
300298
return 42;
@@ -321,7 +319,7 @@ describe('AWSLambda', () => {
321319
});
322320

323321
test('capture error', async () => {
324-
expect.assertions(10);
322+
expect.assertions(9);
325323

326324
const error = new Error('wat');
327325
const handler: Handler = async (_event, _context) => {
@@ -359,7 +357,7 @@ describe('AWSLambda', () => {
359357

360358
describe('wrapHandler() on async handler with a callback method (aka incorrect usage)', () => {
361359
test('successful execution', async () => {
362-
expect.assertions(10);
360+
expect.assertions(9);
363361

364362
const handler: Handler = async (_event, _context, _callback) => {
365363
return 42;
@@ -386,7 +384,7 @@ describe('AWSLambda', () => {
386384
});
387385

388386
test('capture error', async () => {
389-
expect.assertions(10);
387+
expect.assertions(9);
390388

391389
const error = new Error('wat');
392390
const handler: Handler = async (_event, _context, _callback) => {

packages/serverless/test/gcpfunction.test.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ describe('GCPFunction', () => {
212212
});
213213

214214
test('wrapHttpFunction request data', async () => {
215-
expect.assertions(7);
215+
expect.assertions(6);
216216

217217
const handler: HttpFunction = (_req, res) => {
218218
res.end();
@@ -223,7 +223,6 @@ describe('GCPFunction', () => {
223223
Sentry.fakeScope.addEventProcessor.mockImplementation(cb => cb(event));
224224
await handleHttp(wrappedHandler);
225225
expect(event.transaction).toEqual('POST /path');
226-
expect(event.contexts?.runtime).toEqual({ name: 'node', version: expect.anything() });
227226
expect(event.request?.method).toEqual('POST');
228227
expect(event.request?.url).toEqual('http://hostname/path?q=query');
229228
expect(event.request?.query_string).toEqual('q=query');
@@ -362,16 +361,12 @@ describe('GCPFunction', () => {
362361
});
363362

364363
test('wrapEventFunction scope data', async () => {
365-
expect.assertions(3);
364+
expect.assertions(1);
366365

367366
const handler: EventFunction = (_data, _context) => 42;
368367
const wrappedHandler = wrapEventFunction(handler);
369368
await handleEvent(wrappedHandler);
370369
// @ts-ignore see "Why @ts-ignore" note
371-
expect(Sentry.fakeScope.setContext).toBeCalledWith('runtime', { name: 'node', version: expect.anything() });
372-
// @ts-ignore see "Why @ts-ignore" note
373-
expect(Sentry.fakeScope.setTag).toBeCalledWith('server_name', expect.anything());
374-
// @ts-ignore see "Why @ts-ignore" note
375370
expect(Sentry.fakeScope.setContext).toBeCalledWith('gcp.function.context', {
376371
eventType: 'event.type',
377372
resource: 'some.resource',
@@ -466,16 +461,12 @@ describe('GCPFunction', () => {
466461
});
467462

468463
test('wrapCloudEventFunction scope data', async () => {
469-
expect.assertions(3);
464+
expect.assertions(1);
470465

471466
const handler: CloudEventFunction = _context => 42;
472467
const wrappedHandler = wrapCloudEventFunction(handler);
473468
await handleCloudEvent(wrappedHandler);
474469
// @ts-ignore see "Why @ts-ignore" note
475-
expect(Sentry.fakeScope.setContext).toBeCalledWith('runtime', { name: 'node', version: expect.anything() });
476-
// @ts-ignore see "Why @ts-ignore" note
477-
expect(Sentry.fakeScope.setTag).toBeCalledWith('server_name', expect.anything());
478-
// @ts-ignore see "Why @ts-ignore" note
479470
expect(Sentry.fakeScope.setContext).toBeCalledWith('gcp.function.context', { type: 'event.type' });
480471
});
481472

0 commit comments

Comments
 (0)