Skip to content

Commit 7c521e3

Browse files
authored
ref(node): Move non-handler code out of handlers module (#5190)
Currently, the `handlers` module includes not just our middleware handlers but also a bunch of code for extracting context data and applying it to events. That latter code is indeed used in the handlers, but it's also used in a number of other places in the repo. This pulls that code into its own module in `@sentry/utils`, to make it possible to use in a platform-agnostic context. Key changes: - A new `requestData` module in `@sentry/utils` to hold the data extraction code (and a new test file for its tests). - Wrapper functions in the `handlers` module for backwards compatibility. (IOW, calling `Handlers.someFunc()` still works, but it's now just a passthrough to `someFunc` from the new module.) - Deprecation notices in the docstrings of the wrappers, and TODOs so we remember to make the switch in v8. - Wrapper `describe`s in the tests, to run the tests against both the real functions and their wrappers, to make sure the wrappers work identically to the real functions. (This means that for the moment, the test file needs to live in `@sentry/node`, since it depends on the wrappers as well as the real functions. Once the wrappers are removed, the test file can be moved to `@sentry/utils`.) - `parseRequest` has been renamed `addRequestDataToEvent` for greater accuracy and clarity for the reader. (The corresponding options type has also been renamed.) - Two pieces of context data which have until now been set by `parseRequest`, but which have nothing to do with the request and are Node-only (namely `runtime` and `server_name`) are now set by the Node client. As a result, they no longer need to be set separately in the serverless package. (The exception is `server_name` in the AWS integration, because it pulls from a non-standard location.) Note: The original impetus for this change is the need to call `parseRequest`/`addRequestDataToEvent` in the nextjs SDK, in code which can run either in Node or the browser, which means it can't be exported from `@sentry/node`.
1 parent d48f6fd commit 7c521e3

File tree

17 files changed

+843
-718
lines changed

17 files changed

+843
-718
lines changed

packages/nextjs/src/utils/instrumentServer.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* eslint-disable max-lines */
22
import {
3+
addRequestDataToEvent,
34
captureException,
45
configureScope,
56
deepReadDirSync,
67
getCurrentHub,
7-
Handlers,
88
startTransaction,
99
} from '@sentry/node';
1010
import { extractTraceparentData, getActiveTransaction, hasTracingEnabled } from '@sentry/tracing';
@@ -22,8 +22,6 @@ import { default as createNextServer } from 'next';
2222
import * as querystring from 'querystring';
2323
import * as url from 'url';
2424

25-
const { parseRequest } = Handlers;
26-
2725
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2826
type PlainObject<T = any> = { [key: string]: T };
2927

@@ -246,7 +244,7 @@ function makeWrappedReqHandler(origReqHandler: ReqHandler): WrappedReqHandler {
246244
const currentScope = getCurrentHub().getScope();
247245

248246
if (currentScope) {
249-
currentScope.addEventProcessor(event => parseRequest(event, nextReq));
247+
currentScope.addEventProcessor(event => addRequestDataToEvent(event, nextReq));
250248

251249
// We only want to record page and API requests
252250
if (hasTracingEnabled() && shouldTraceRequest(nextReq.url, publicDirFiles)) {

packages/nextjs/src/utils/withSentry.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { captureException, flush, getCurrentHub, Handlers, startTransaction } from '@sentry/node';
1+
import { addRequestDataToEvent, captureException, flush, getCurrentHub, startTransaction } from '@sentry/node';
22
import { extractTraceparentData, hasTracingEnabled } from '@sentry/tracing';
33
import { Transaction } from '@sentry/types';
44
import {
@@ -12,8 +12,6 @@ import {
1212
import * as domain from 'domain';
1313
import { NextApiHandler, NextApiRequest, NextApiResponse } from 'next';
1414

15-
const { parseRequest } = Handlers;
16-
1715
// This is the same as the `NextApiHandler` type, except instead of having a return type of `void | Promise<void>`, it's
1816
// only `Promise<void>`, because wrapped handlers are always async
1917
export type WrappedNextApiHandler = (req: NextApiRequest, res: NextApiResponse) => Promise<void>;
@@ -43,7 +41,7 @@ export const withSentry = (origHandler: NextApiHandler): WrappedNextApiHandler =
4341
const currentScope = getCurrentHub().getScope();
4442

4543
if (currentScope) {
46-
currentScope.addEventProcessor(event => parseRequest(event, req));
44+
currentScope.addEventProcessor(event => addRequestDataToEvent(event, req));
4745

4846
if (hasTracingEnabled()) {
4947
// If there is a trace header set, extract the data from it (parentSpanId, traceId, and sampling decision)

packages/node/src/client.ts

+9-3
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,14 @@ 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 = this.getOptions().serverName || global.process.env.SENTRY_NAME || os.hostname();
145151
return super._prepareEvent(event, hint, scope);
146152
}
147153

0 commit comments

Comments
 (0)