-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathhttp.ts
More file actions
283 lines (238 loc) · 8.47 KB
/
Copy pathhttp.ts
File metadata and controls
283 lines (238 loc) · 8.47 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
import type { ClientRequest, IncomingMessage, ServerResponse } from 'http';
import type { Span } from '@opentelemetry/api';
import { SpanKind } from '@opentelemetry/api';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { addBreadcrumb, defineIntegration, hasTracingEnabled, isSentryRequestUrl } from '@sentry/core';
import { _INTERNAL, getClient, getSpanKind, setSpanMetadata } from '@sentry/opentelemetry';
import type { EventProcessor, Hub, Integration, IntegrationFn } from '@sentry/types';
import { stringMatchesSomePattern } from '@sentry/utils';
import { getIsolationScope, setIsolationScope } from '../sdk/api';
import type { NodeExperimentalClient } from '../types';
import { addOriginToSpan } from '../utils/addOriginToSpan';
import { getRequestUrl } from '../utils/getRequestUrl';
interface HttpOptions {
/**
* Whether breadcrumbs should be recorded for requests.
* Defaults to true
*/
breadcrumbs?: boolean;
/**
* Do not capture spans or breadcrumbs for outgoing HTTP requests to URLs where the given callback returns `true`.
* This controls both span & breadcrumb creation - spans will be non recording if tracing is disabled.
*/
ignoreOutgoingRequests?: (url: string) => boolean;
/**
* Do not capture spans or breadcrumbs for incoming HTTP requests to URLs where the given callback returns `true`.
* This controls both span & breadcrumb creation - spans will be non recording if tracing is disabled.
*/
ignoreIncomingRequests?: (url: string) => boolean;
}
const _httpIntegration = ((options: HttpOptions = {}) => {
const _breadcrumbs = typeof options.breadcrumbs === 'undefined' ? true : options.breadcrumbs;
const _ignoreOutgoingRequests = options.ignoreOutgoingRequests;
const _ignoreIncomingRequests = options.ignoreIncomingRequests;
return {
name: 'Http',
setupOnce() {
const instrumentations = [
new HttpInstrumentation({
ignoreOutgoingRequestHook: request => {
const url = getRequestUrl(request);
if (!url) {
return false;
}
if (isSentryRequestUrl(url, getClient())) {
return true;
}
if (_ignoreOutgoingRequests && _ignoreOutgoingRequests(url)) {
return true;
}
return false;
},
ignoreIncomingRequestHook: request => {
const url = getRequestUrl(request);
const method = request.method?.toUpperCase();
// We do not capture OPTIONS/HEAD requests as transactions
if (method === 'OPTIONS' || method === 'HEAD') {
return true;
}
if (_ignoreIncomingRequests && _ignoreIncomingRequests(url)) {
return true;
}
return false;
},
requireParentforOutgoingSpans: true,
requireParentforIncomingSpans: false,
requestHook: (span, req) => {
_updateSpan(span, req);
// Update the isolation scope, isolate this request
if (getSpanKind(span) === SpanKind.SERVER) {
setIsolationScope(getIsolationScope().clone());
}
},
responseHook: (span, res) => {
if (_breadcrumbs) {
_addRequestBreadcrumb(span, res);
}
},
}),
];
registerInstrumentations({
instrumentations,
});
},
};
}) satisfies IntegrationFn;
export const httpIntegration = defineIntegration(_httpIntegration);
interface OldHttpOptions {
/**
* Whether breadcrumbs should be recorded for requests
* Defaults to true
*/
breadcrumbs?: boolean;
/**
* Whether tracing spans should be created for requests
* Defaults to false
*/
spans?: boolean;
/**
* Do not capture spans or breadcrumbs for outgoing HTTP requests to URLs matching the given patterns.
*/
ignoreOutgoingRequests?: (string | RegExp)[];
}
/**
* Http instrumentation based on @opentelemetry/instrumentation-http.
* This instrumentation does two things:
* * Create breadcrumbs for outgoing requests
* * Create spans for outgoing requests
*
* Note that this integration is also needed for the Express integration to work!
*
* @deprecated Use `httpIntegration()` instead.
*/
export class Http implements Integration {
/**
* @inheritDoc
*/
public static id: string = 'Http';
/**
* @inheritDoc
*/
public name: string;
/**
* If spans for HTTP requests should be captured.
*/
public shouldCreateSpansForRequests: boolean;
private _unload?: () => void;
private readonly _breadcrumbs: boolean;
// If this is undefined, use default behavior based on client settings
private readonly _spans: boolean | undefined;
private _ignoreOutgoingRequests: (string | RegExp)[];
/**
* @inheritDoc
*/
public constructor(options: OldHttpOptions = {}) {
// eslint-disable-next-line deprecation/deprecation
this.name = Http.id;
this._breadcrumbs = typeof options.breadcrumbs === 'undefined' ? true : options.breadcrumbs;
this._spans = typeof options.spans === 'undefined' ? undefined : options.spans;
this._ignoreOutgoingRequests = options.ignoreOutgoingRequests || [];
// Properly set in setupOnce based on client settings
this.shouldCreateSpansForRequests = false;
}
/**
* @inheritDoc
*/
// eslint-disable-next-line deprecation/deprecation
public setupOnce(_addGlobalEventProcessor: (callback: EventProcessor) => void, _getCurrentHub: () => Hub): void {
// No need to instrument if we don't want to track anything
if (!this._breadcrumbs && this._spans === false) {
return;
}
const client = getClient<NodeExperimentalClient>();
const clientOptions = client?.getOptions();
// This is used in the sampler function
this.shouldCreateSpansForRequests =
typeof this._spans === 'boolean' ? this._spans : hasTracingEnabled(clientOptions);
// Register instrumentations we care about
this._unload = registerInstrumentations({
instrumentations: [
new HttpInstrumentation({
ignoreOutgoingRequestHook: request => {
const url = getRequestUrl(request);
if (!url) {
return false;
}
if (isSentryRequestUrl(url, getClient())) {
return true;
}
if (this._ignoreOutgoingRequests.length && stringMatchesSomePattern(url, this._ignoreOutgoingRequests)) {
return true;
}
return false;
},
ignoreIncomingRequestHook: request => {
const method = request.method?.toUpperCase();
// We do not capture OPTIONS/HEAD requests as transactions
if (method === 'OPTIONS' || method === 'HEAD') {
return true;
}
return false;
},
requireParentforOutgoingSpans: true,
requireParentforIncomingSpans: false,
requestHook: (span, req) => {
_updateSpan(span, req);
// Update the isolation scope, isolate this request
if (getSpanKind(span) === SpanKind.SERVER) {
setIsolationScope(getIsolationScope().clone());
}
},
responseHook: (span, res) => {
if (this._breadcrumbs) {
_addRequestBreadcrumb(span, res);
}
},
}),
],
});
}
/**
* Unregister this integration.
*/
public unregister(): void {
this._unload?.();
}
}
/** Update the span with data we need. */
function _updateSpan(span: Span, request: ClientRequest | IncomingMessage): void {
addOriginToSpan(span, 'auto.http.otel.http');
if (getSpanKind(span) === SpanKind.SERVER) {
setSpanMetadata(span, { request });
}
}
/** Add a breadcrumb for outgoing requests. */
function _addRequestBreadcrumb(span: Span, response: IncomingMessage | ServerResponse): void {
if (getSpanKind(span) !== SpanKind.CLIENT) {
return;
}
const data = _INTERNAL.getRequestSpanData(span);
addBreadcrumb(
{
category: 'http',
data: {
status_code: response.statusCode,
...data,
},
type: 'http',
},
{
event: 'response',
// TODO FN: Do we need access to `request` here?
// If we do, we'll have to use the `applyCustomAttributesOnSpan` hook instead,
// but this has worse context semantics than request/responseHook.
response,
},
);
}