Skip to content

Commit 5cee1d9

Browse files
committed
move node-version- and server-name-adding code to client
1 parent 0d07854 commit 5cee1d9

File tree

10 files changed

+102
-95
lines changed

10 files changed

+102
-95
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';
@@ -292,10 +291,8 @@ export function extractRequestData(
292291
export interface ParseRequestOptions {
293292
ip?: boolean;
294293
request?: boolean | string[];
295-
serverName?: boolean;
296294
transaction?: boolean | TransactionNamingScheme;
297295
user?: boolean | string[];
298-
version?: boolean;
299296
}
300297

301298
/**
@@ -311,23 +308,11 @@ export function parseRequest(event: Event, req: ExpressRequest, options?: ParseR
311308
options = {
312309
ip: false,
313310
request: true,
314-
serverName: true,
315311
transaction: true,
316312
user: true,
317-
version: true,
318313
...options,
319314
};
320315

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

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

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 { isBaggageEmpty, isBaggageMutable, 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/cloud_events.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ import { captureException, flush, getCurrentHub, startTransaction } from '@sentr
22
import { logger } from '@sentry/utils';
33

44
import { domainify, getActiveDomain, proxyFunction } from '../utils';
5-
import {
6-
CloudEventFunction,
7-
CloudEventFunctionWithCallback,
8-
configureScopeWithContext,
9-
WrapperOptions,
10-
} from './general';
5+
import { CloudEventFunction, CloudEventFunctionWithCallback, WrapperOptions } from './general';
116

127
export type CloudEventFunctionWrapperOptions = WrapperOptions;
138

@@ -44,7 +39,7 @@ function _wrapCloudEventFunction(
4439
// since functions-framework creates a domain for each incoming request.
4540
// So adding of event processors every time should not lead to memory bloat.
4641
getCurrentHub().configureScope(scope => {
47-
configureScopeWithContext(scope, context);
42+
scope.setContext('gcp.function.context', { ...context });
4843
// We put the transaction on the scope so users can attach children to it
4944
scope.setSpan(transaction);
5045
});

packages/serverless/src/gcpfunction/events.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { captureException, flush, getCurrentHub, startTransaction } from '@sentr
22
import { logger } from '@sentry/utils';
33

44
import { domainify, getActiveDomain, proxyFunction } from '../utils';
5-
import { configureScopeWithContext, EventFunction, EventFunctionWithCallback, WrapperOptions } from './general';
5+
import { EventFunction, EventFunctionWithCallback, WrapperOptions } from './general';
66

77
export type EventFunctionWrapperOptions = WrapperOptions;
88

@@ -39,7 +39,7 @@ function _wrapEventFunction(
3939
// since functions-framework creates a domain for each incoming request.
4040
// So adding of event processors every time should not lead to memory bloat.
4141
getCurrentHub().configureScope(scope => {
42-
configureScopeWithContext(scope, context);
42+
scope.setContext('gcp.function.context', { ...context });
4343
// We put the transaction on the scope so users can attach children to it
4444
scope.setSpan(transaction);
4545
});

packages/serverless/src/gcpfunction/general.ts

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

63
export interface HttpFunction {
74
(req: Request, res: Response): any; // eslint-disable-line @typescript-eslint/no-explicit-any
@@ -47,19 +44,4 @@ export interface WrapperOptions {
4744
flushTimeout: number;
4845
}
4946

50-
/**
51-
* Enhances the scope with additional event information.
52-
*
53-
* @param scope scope
54-
* @param context event context
55-
*/
56-
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());
62-
scope.setContext('gcp.function.context', { ...context } as SentryContext);
63-
}
64-
6547
export type { Request, Response };

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);
@@ -201,7 +199,7 @@ describe('AWSLambda', () => {
201199
});
202200

203201
test('unsuccessful execution', async () => {
204-
expect.assertions(10);
202+
expect.assertions(9);
205203

206204
const error = new Error('sorry');
207205
const handler: Handler = (_event, _context, callback) => {
@@ -273,7 +271,7 @@ describe('AWSLambda', () => {
273271
});
274272

275273
test('capture error', async () => {
276-
expect.assertions(10);
274+
expect.assertions(9);
277275

278276
const error = new Error('wat');
279277
const handler: Handler = (_event, _context, _callback) => {
@@ -304,7 +302,7 @@ describe('AWSLambda', () => {
304302

305303
describe('wrapHandler() on async handler', () => {
306304
test('successful execution', async () => {
307-
expect.assertions(10);
305+
expect.assertions(9);
308306

309307
const handler: Handler = async (_event, _context) => {
310308
return 42;
@@ -335,7 +333,7 @@ describe('AWSLambda', () => {
335333
});
336334

337335
test('capture error', async () => {
338-
expect.assertions(10);
336+
expect.assertions(9);
339337

340338
const error = new Error('wat');
341339
const handler: Handler = async (_event, _context) => {
@@ -377,7 +375,7 @@ describe('AWSLambda', () => {
377375

378376
describe('wrapHandler() on async handler with a callback method (aka incorrect usage)', () => {
379377
test('successful execution', async () => {
380-
expect.assertions(10);
378+
expect.assertions(9);
381379

382380
const handler: Handler = async (_event, _context, _callback) => {
383381
return 42;
@@ -408,7 +406,7 @@ describe('AWSLambda', () => {
408406
});
409407

410408
test('capture error', async () => {
411-
expect.assertions(10);
409+
expect.assertions(9);
412410

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

0 commit comments

Comments
 (0)