Repository navigation
Expand file tree
/
Copy pathhttp.ts
More file actions
225 lines (201 loc) · 7.54 KB
/
Copy pathhttp.ts
File metadata and controls
225 lines (201 loc) · 7.54 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
import { getCurrentHub } from '@sentry/core';
import { Integration, Span, Transaction } from '@sentry/types';
import { fill, parseSemver } from '@sentry/utils';
import * as http from 'http';
import * as https from 'https';
const NODE_VERSION = parseSemver(process.versions.node);
/** http module integration */
export class Http implements Integration {
/**
* @inheritDoc
*/
public static id: string = 'Http';
/**
* @inheritDoc
*/
public name: string = Http.id;
/**
* @inheritDoc
*/
private readonly _breadcrumbs: boolean;
/**
* @inheritDoc
*/
private readonly _tracing: boolean;
/**
* @inheritDoc
*/
public constructor(options: { breadcrumbs?: boolean; tracing?: boolean } = {}) {
this._breadcrumbs = typeof options.breadcrumbs === 'undefined' ? true : options.breadcrumbs;
this._tracing = typeof options.tracing === 'undefined' ? false : options.tracing;
}
/**
* @inheritDoc
*/
public setupOnce(): void {
// No need to instrument if we don't want to track anything
if (!this._breadcrumbs && !this._tracing) {
return;
}
const wrappedHandlerMaker = _createWrappedHandlerMaker(this._breadcrumbs, this._tracing);
const httpModule = require('http');
fill(httpModule, 'get', wrappedHandlerMaker);
fill(httpModule, 'request', wrappedHandlerMaker);
// NOTE: Prior to Node 9, `https` used internals of `http` module, thus we don't patch it.
// If we do, we'd get double breadcrumbs and double spans for `https` calls.
// It has been changed in Node 9, so for all versions equal and above, we patch `https` separately.
if (NODE_VERSION.major && NODE_VERSION.major > 8) {
const httpsModule = require('https');
fill(httpsModule, 'get', wrappedHandlerMaker);
fill(httpsModule, 'request', wrappedHandlerMaker);
}
}
}
type OriginalHandler = () => http.ClientRequest;
type WrappedHandler = (options: string | http.ClientRequestArgs) => http.ClientRequest;
type WrappedHandlerMaker = (originalHandler: OriginalHandler) => WrappedHandler;
/**
* Function which creates a function which creates wrapped versions of internal `request` and `get` calls within `http`
* and `https` modules. (NB: Not a typo - this is a creator^2!)
*
* @param breadcrumbsEnabled Whether or not to record outgoing requests as breadcrumbs
* @param tracingEnabled Whether or not to record outgoing requests as tracing spans
*
* @returns A function which accepts the exiting handler and returns a wrapped handler
*/
function _createWrappedHandlerMaker(breadcrumbsEnabled: boolean, tracingEnabled: boolean): WrappedHandlerMaker {
return function wrappedHandlerMaker(originalHandler: OriginalHandler): WrappedHandler {
return function wrappedHandler(
this: typeof http | typeof https,
options: string | http.ClientRequestArgs,
): http.ClientRequest {
const requestUrl = extractUrl(options);
// we don't want to record requests to Sentry as either breadcrumbs or spans, so just use the original handler
if (isSentryRequest(requestUrl)) {
return originalHandler.apply(this, arguments);
}
let span: Span | undefined;
let transaction: Transaction | undefined;
const scope = getCurrentHub().getScope();
if (scope && tracingEnabled) {
transaction = scope.getTransaction();
if (transaction) {
span = transaction.startChild({
description: `${typeof options === 'string' || !options.method ? 'GET' : options.method} ${requestUrl}`,
op: 'request',
});
}
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
return originalHandler
.apply(this, arguments)
.once('response', function(this: http.IncomingMessage, res: http.ServerResponse): void {
if (breadcrumbsEnabled) {
addRequestBreadcrumb('response', requestUrl, this, res);
}
if (tracingEnabled && span) {
span.setHttpStatus(res.statusCode);
cleanDescription(options, this, span);
span.finish();
}
})
.once('error', function(this: http.IncomingMessage): void {
if (breadcrumbsEnabled) {
addRequestBreadcrumb('error', requestUrl, this);
}
if (tracingEnabled && span) {
span.setHttpStatus(500);
cleanDescription(options, this, span);
span.finish();
}
});
};
};
}
/**
* Captures Breadcrumb based on provided request/response pair
*/
function addRequestBreadcrumb(event: string, url: string, req: http.IncomingMessage, res?: http.ServerResponse): void {
if (!getCurrentHub().getIntegration(Http)) {
return;
}
getCurrentHub().addBreadcrumb(
{
category: 'http',
data: {
method: req.method,
status_code: res && res.statusCode,
url,
},
type: 'http',
},
{
event,
request: req,
response: res,
},
);
}
/**
* Assemble a URL to be used for breadcrumbs and spans.
*
* @param url URL string or object containing the component parts
* @returns Fully-formed URL
*/
export function extractUrl(url: string | http.ClientRequestArgs): string {
if (typeof url === 'string') {
return url;
}
const protocol = url.protocol || '';
const hostname = url.hostname || url.host || '';
// Don't log standard :80 (http) and :443 (https) ports to reduce the noise
const port = !url.port || url.port === 80 || url.port === 443 ? '' : `:${url.port}`;
const path = url.path ? url.path : '/';
// internal routes end up with too many slashes
return `${protocol}//${hostname}${port}${path}`.replace('///', '/');
}
/**
* Handle an edge case with urls in the span description. Runs just before the span closes because it relies on
* data from the response object.
*
* @param requestOptions Configuration data for the request
* @param response Response object
* @param span Span representing the request
*/
function cleanDescription(
requestOptions: string | http.ClientRequestArgs,
response: http.IncomingMessage,
span: Span,
): void {
// There are some libraries which don't pass the request protocol in the options object, so attempt to retrieve it
// from the response and run the URL processing again. We only do this in the presence of a (non-empty) host value,
// because if we're missing both, it's likely we're dealing with an internal route, in which case we don't want to be
// jamming a random `http:` on the front of it.
if (typeof requestOptions !== 'string' && !Object.keys(requestOptions).includes('protocol') && requestOptions.host) {
// Neither http.IncomingMessage nor any of its ancestors have an `agent` property in their type definitions, and
// http.Agent doesn't have a `protocol` property in its type definition. Nonetheless, at least one request library
// (superagent) arranges things that way, so might as well give it a shot.
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
requestOptions.protocol = (response as any).agent.protocol;
span.description = `${requestOptions.method || 'GET'} ${extractUrl(requestOptions)}`;
} catch (error) {
// well, we tried
}
}
}
/**
* Checks whether given url points to Sentry server
* @param url url to verify
*/
function isSentryRequest(url: string): boolean {
const client = getCurrentHub().getClient();
if (!url || !client) {
return false;
}
const dsn = client.getDsn();
if (!dsn) {
return false;
}
return url.indexOf(dsn.host) !== -1;
}