-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathhttpServerIntegration.ts
More file actions
439 lines (377 loc) · 16.1 KB
/
httpServerIntegration.ts
File metadata and controls
439 lines (377 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import type { ChannelListener } from 'node:diagnostics_channel';
import { subscribe } from 'node:diagnostics_channel';
import type { EventEmitter } from 'node:events';
import type { IncomingMessage, RequestOptions, Server, ServerResponse } from 'node:http';
import type { Socket } from 'node:net';
import { context, createContextKey, propagation } from '@opentelemetry/api';
import type { AggregationCounts, Client, Integration, IntegrationFn, Scope } from '@sentry/core';
import {
addNonEnumerableProperty,
debug,
generateSpanId,
getClient,
getCurrentScope,
getIsolationScope,
httpRequestToRequestData,
safeUnref,
stripUrlQueryAndFragment,
withIsolationScope,
} from '@sentry/core';
import { DEBUG_BUILD } from '../../debug-build';
import type { NodeClient } from '../../sdk/client';
import { MAX_BODY_BYTE_LENGTH } from './constants';
type ServerEmit = typeof Server.prototype.emit;
// Inlining this type to not depend on newer TS types
interface WeakRefImpl<T> {
deref(): T | undefined;
}
type StartSpanCallback = (next: () => boolean) => boolean;
type RequestWithOptionalStartSpanCallback = IncomingMessage & {
_startSpanCallback?: WeakRefImpl<StartSpanCallback>;
};
const HTTP_SERVER_INSTRUMENTED_KEY = createContextKey('sentry_http_server_instrumented');
const INTEGRATION_NAME = 'Http.Server';
const clientToRequestSessionAggregatesMap = new Map<
Client,
{ [timestampRoundedToSeconds: string]: { exited: number; crashed: number; errored: number } }
>();
// We keep track of emit functions we wrapped, to avoid double wrapping
// We do this instead of putting a non-enumerable property on the function, because
// sometimes the property seems to be migrated to forks of the emit function, which we do not want to happen
// This was the case in the nestjs-distributed-tracing E2E test
const wrappedEmitFns = new WeakSet<ServerEmit>();
export interface HttpServerIntegrationOptions {
/**
* Whether the integration should create [Sessions](https://docs.sentry.io/product/releases/health/#sessions) for incoming requests to track the health and crash-free rate of your releases in Sentry.
* Read more about Release Health: https://docs.sentry.io/product/releases/health/
*
* Defaults to `true`.
*/
sessions?: boolean;
/**
* Number of milliseconds until sessions tracked with `trackIncomingRequestsAsSessions` will be flushed as a session aggregate.
*
* Defaults to `60000` (60s).
*/
sessionFlushingDelayMS?: number;
/**
* Do not capture the request body for incoming HTTP requests to URLs where the given callback returns `true`.
* This can be useful for long running requests where the body is not needed and we want to avoid capturing it.
*
* @param url Contains the entire URL, including query string (if any), protocol, host, etc. of the incoming request.
* @param request Contains the {@type RequestOptions} object used to make the incoming request.
*/
ignoreRequestBody?: (url: string, request: RequestOptions) => boolean;
/**
* Controls the maximum size of incoming HTTP request bodies attached to events.
*
* Available options:
* - 'none': No request bodies will be attached
* - 'small': Request bodies up to 1,000 bytes will be attached
* - 'medium': Request bodies up to 10,000 bytes will be attached (default)
* - 'always': Request bodies will always be attached
*
* Note that even with 'always' setting, bodies exceeding 1MB will never be attached
* for performance and security reasons.
*
* @default 'medium'
*/
maxRequestBodySize?: 'none' | 'small' | 'medium' | 'always';
}
/**
* Add a callback to the request object that will be called when the request is started.
* The callback will receive the next function to continue processing the request.
*/
export function addStartSpanCallback(request: RequestWithOptionalStartSpanCallback, callback: StartSpanCallback): void {
addNonEnumerableProperty(request, '_startSpanCallback', new WeakRef(callback));
}
const _httpServerIntegration = ((options: HttpServerIntegrationOptions = {}) => {
const _options = {
sessions: options.sessions ?? true,
sessionFlushingDelayMS: options.sessionFlushingDelayMS ?? 60_000,
maxRequestBodySize: options.maxRequestBodySize ?? 'medium',
ignoreRequestBody: options.ignoreRequestBody,
};
return {
name: INTEGRATION_NAME,
setupOnce() {
const onHttpServerRequestStart = ((_data: unknown) => {
const data = _data as { server: Server };
instrumentServer(data.server, _options);
}) satisfies ChannelListener;
subscribe('http.server.request.start', onHttpServerRequestStart);
},
afterAllSetup(client) {
if (DEBUG_BUILD && client.getIntegrationByName('Http')) {
debug.warn(
'It seems that you have manually added `httpServerIntegration` while `httpIntegration` is also present. Make sure to remove `httpServerIntegration` when adding `httpIntegration`.',
);
}
},
};
}) satisfies IntegrationFn;
/**
* This integration handles request isolation, trace continuation and other core Sentry functionality around incoming http requests
* handled via the node `http` module.
*/
export const httpServerIntegration = _httpServerIntegration as (
options?: HttpServerIntegrationOptions,
) => Integration & {
name: 'HttpServer';
setupOnce: () => void;
};
/**
* Instrument a server to capture incoming requests.
*
*/
function instrumentServer(
server: Server,
{
ignoreRequestBody,
maxRequestBodySize,
sessions,
sessionFlushingDelayMS,
}: {
ignoreRequestBody?: (url: string, request: IncomingMessage) => boolean;
maxRequestBodySize: 'small' | 'medium' | 'always' | 'none';
sessions: boolean;
sessionFlushingDelayMS: number;
},
): void {
// eslint-disable-next-line @typescript-eslint/unbound-method
const originalEmit: ServerEmit = server.emit;
if (wrappedEmitFns.has(originalEmit)) {
return;
}
const newEmit = new Proxy(originalEmit, {
apply(target, thisArg, args: [event: string, ...args: unknown[]]) {
// Only traces request events
if (args[0] !== 'request') {
return target.apply(thisArg, args);
}
const client = getClient<NodeClient>();
// Make sure we do not double execute our wrapper code, for edge cases...
// Without this check, if we double-wrap emit, for whatever reason, you'd get two http.server spans (one the children of the other)
if (context.active().getValue(HTTP_SERVER_INSTRUMENTED_KEY) || !client) {
return target.apply(thisArg, args);
}
DEBUG_BUILD && debug.log(INTEGRATION_NAME, 'Handling incoming request');
const isolationScope = getIsolationScope().clone();
const request = args[1] as IncomingMessage;
const response = args[2] as ServerResponse & { socket: Socket };
const normalizedRequest = httpRequestToRequestData(request);
// request.ip is non-standard but some frameworks set this
const ipAddress = (request as { ip?: string }).ip || request.socket?.remoteAddress;
const url = request.url || '/';
if (maxRequestBodySize !== 'none' && !ignoreRequestBody?.(url, request)) {
patchRequestToCaptureBody(request, isolationScope, maxRequestBodySize);
}
// Update the isolation scope, isolate this request
isolationScope.setSDKProcessingMetadata({ normalizedRequest, ipAddress });
// attempt to update the scope's `transactionName` based on the request URL
// Ideally, framework instrumentations coming after the HttpInstrumentation
// update the transactionName once we get a parameterized route.
const httpMethod = (request.method || 'GET').toUpperCase();
const httpTargetWithoutQueryFragment = stripUrlQueryAndFragment(url);
const bestEffortTransactionName = `${httpMethod} ${httpTargetWithoutQueryFragment}`;
isolationScope.setTransactionName(bestEffortTransactionName);
if (sessions && client) {
recordRequestSession(client, {
requestIsolationScope: isolationScope,
response,
sessionFlushingDelayMS: sessionFlushingDelayMS ?? 60_000,
});
}
return withIsolationScope(isolationScope, () => {
// Set a new propagationSpanId for this request
// We rely on the fact that `withIsolationScope()` will implicitly also fork the current scope
// This way we can save an "unnecessary" `withScope()` invocation
getCurrentScope().getPropagationContext().propagationSpanId = generateSpanId();
const ctx = propagation
.extract(context.active(), normalizedRequest.headers)
.setValue(HTTP_SERVER_INSTRUMENTED_KEY, true);
return context.with(ctx, () => {
// This is used (optionally) by the httpServerSpansIntegration to attach _startSpanCallback to the request object
client.emit('httpServerRequest', request, response, normalizedRequest);
const callback = (request as RequestWithOptionalStartSpanCallback)._startSpanCallback?.deref();
if (callback) {
return callback(() => target.apply(thisArg, args));
}
return target.apply(thisArg, args);
});
});
},
});
wrappedEmitFns.add(newEmit);
server.emit = newEmit;
}
/**
* Starts a session and tracks it in the context of a given isolation scope.
* When the passed response is finished, the session is put into a task and is
* aggregated with other sessions that may happen in a certain time window
* (sessionFlushingDelayMs).
*
* The sessions are always aggregated by the client that is on the current scope
* at the time of ending the response (if there is one).
*/
// Exported for unit tests
export function recordRequestSession(
client: Client,
{
requestIsolationScope,
response,
sessionFlushingDelayMS,
}: {
requestIsolationScope: Scope;
response: EventEmitter;
sessionFlushingDelayMS?: number;
},
): void {
requestIsolationScope.setSDKProcessingMetadata({
requestSession: { status: 'ok' },
});
response.once('close', () => {
const requestSession = requestIsolationScope.getScopeData().sdkProcessingMetadata.requestSession;
if (client && requestSession) {
DEBUG_BUILD && debug.log(`Recorded request session with status: ${requestSession.status}`);
const roundedDate = new Date();
roundedDate.setSeconds(0, 0);
const dateBucketKey = roundedDate.toISOString();
const existingClientAggregate = clientToRequestSessionAggregatesMap.get(client);
const bucket = existingClientAggregate?.[dateBucketKey] || { exited: 0, crashed: 0, errored: 0 };
bucket[({ ok: 'exited', crashed: 'crashed', errored: 'errored' } as const)[requestSession.status]]++;
if (existingClientAggregate) {
existingClientAggregate[dateBucketKey] = bucket;
} else {
DEBUG_BUILD && debug.log('Opened new request session aggregate.');
const newClientAggregate = { [dateBucketKey]: bucket };
clientToRequestSessionAggregatesMap.set(client, newClientAggregate);
const flushPendingClientAggregates = (): void => {
clearTimeout(timeout);
unregisterClientFlushHook();
clientToRequestSessionAggregatesMap.delete(client);
const aggregatePayload: AggregationCounts[] = Object.entries(newClientAggregate).map(
([timestamp, value]) => ({
started: timestamp,
exited: value.exited,
errored: value.errored,
crashed: value.crashed,
}),
);
client.sendSession({ aggregates: aggregatePayload });
};
const unregisterClientFlushHook = client.on('flush', () => {
DEBUG_BUILD && debug.log('Sending request session aggregate due to client flush');
flushPendingClientAggregates();
});
const timeout = safeUnref(
setTimeout(() => {
DEBUG_BUILD && debug.log('Sending request session aggregate due to flushing schedule');
flushPendingClientAggregates();
}, sessionFlushingDelayMS),
);
}
}
});
}
/**
* This method patches the request object to capture the body.
* Instead of actually consuming the streamed body ourselves, which has potential side effects,
* we monkey patch `req.on('data')` to intercept the body chunks.
* This way, we only read the body if the user also consumes the body, ensuring we do not change any behavior in unexpected ways.
*/
function patchRequestToCaptureBody(
req: IncomingMessage,
isolationScope: Scope,
maxIncomingRequestBodySize: 'small' | 'medium' | 'always',
): void {
let bodyByteLength = 0;
const chunks: Buffer[] = [];
DEBUG_BUILD && debug.log(INTEGRATION_NAME, 'Patching request.on');
/**
* We need to keep track of the original callbacks, in order to be able to remove listeners again.
* Since `off` depends on having the exact same function reference passed in, we need to be able to map
* original listeners to our wrapped ones.
*/
const callbackMap = new WeakMap();
const maxBodySize =
maxIncomingRequestBodySize === 'small'
? 1_000
: maxIncomingRequestBodySize === 'medium'
? 10_000
: MAX_BODY_BYTE_LENGTH;
try {
// eslint-disable-next-line @typescript-eslint/unbound-method
req.on = new Proxy(req.on, {
apply: (target, thisArg, args: Parameters<typeof req.on>) => {
const [event, listener, ...restArgs] = args;
if (event === 'data') {
DEBUG_BUILD &&
debug.log(INTEGRATION_NAME, `Handling request.on("data") with maximum body size of ${maxBodySize}b`);
const callback = new Proxy(listener, {
apply: (target, thisArg, args: Parameters<typeof listener>) => {
try {
const chunk = args[0] as Buffer | string;
const bufferifiedChunk = Buffer.from(chunk);
if (bodyByteLength < maxBodySize) {
chunks.push(bufferifiedChunk);
bodyByteLength += bufferifiedChunk.byteLength;
} else if (DEBUG_BUILD) {
debug.log(
INTEGRATION_NAME,
`Dropping request body chunk because maximum body length of ${maxBodySize}b is exceeded.`,
);
}
} catch (err) {
DEBUG_BUILD && debug.error(INTEGRATION_NAME, 'Encountered error while storing body chunk.');
}
return Reflect.apply(target, thisArg, args);
},
});
callbackMap.set(listener, callback);
return Reflect.apply(target, thisArg, [event, callback, ...restArgs]);
}
return Reflect.apply(target, thisArg, args);
},
});
// Ensure we also remove callbacks correctly
// eslint-disable-next-line @typescript-eslint/unbound-method
req.off = new Proxy(req.off, {
apply: (target, thisArg, args: Parameters<typeof req.off>) => {
const [, listener] = args;
const callback = callbackMap.get(listener);
if (callback) {
callbackMap.delete(listener);
const modifiedArgs = args.slice();
modifiedArgs[1] = callback;
return Reflect.apply(target, thisArg, modifiedArgs);
}
return Reflect.apply(target, thisArg, args);
},
});
req.on('end', () => {
try {
const body = Buffer.concat(chunks).toString('utf-8');
if (body) {
// Using Buffer.byteLength here, because the body may contain characters that are not 1 byte long
const bodyByteLength = Buffer.byteLength(body, 'utf-8');
const truncatedBody =
bodyByteLength > maxBodySize
? `${Buffer.from(body)
.subarray(0, maxBodySize - 3)
.toString('utf-8')}...`
: body;
isolationScope.setSDKProcessingMetadata({ normalizedRequest: { data: truncatedBody } });
}
} catch (error) {
if (DEBUG_BUILD) {
debug.error(INTEGRATION_NAME, 'Error building captured request body', error);
}
}
});
} catch (error) {
if (DEBUG_BUILD) {
debug.error(INTEGRATION_NAME, 'Error patching request to capture body', error);
}
}
}