-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
net-breadcrumbs.ts
215 lines (187 loc) · 6.14 KB
/
net-breadcrumbs.ts
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
/* eslint-disable deprecation/deprecation */
import { getCurrentHub } from '@sentry/core';
import { Integration, Span } from '@sentry/types';
import { fill } from '@sentry/utils';
import { ClientRequest, ClientRequestConstructorOptions, IncomingMessage, net } from 'electron';
import * as urlModule from 'url';
import { OrBool, OrFalse } from '../../common/types';
type ShouldTraceFn = (method: string, url: string) => boolean;
interface NetOptions {
/**
* Whether breadcrumbs should be captured for net requests
*
* Defaults to: true
*/
breadcrumbs: boolean;
/**
* Whether to capture transaction spans for net requests
*
* true | false | (method: string, url: string) => boolean
* Defaults to: true
*/
tracing: ShouldTraceFn;
/**
* Whether to add 'sentry-trace' headers to outgoing requests
*
* true | false | (method: string, url: string) => boolean
* Defaults to: true
*/
tracingOrigins: ShouldTraceFn;
}
const DEFAULT_OPTIONS: NetOptions = {
breadcrumbs: true,
tracing: (_method, _url) => true,
tracingOrigins: (_method, _url) => true,
};
/** Converts all user supplied options to T | false */
export function normalizeOptions(options: Partial<OrBool<NetOptions>>): Partial<OrFalse<NetOptions>> {
return (Object.keys(options) as (keyof NetOptions)[]).reduce((obj, k) => {
if (typeof options[k] === 'function' || options[k] === false) {
obj[k] = options[k] as boolean & (false | ShouldTraceFn);
}
return obj;
}, {} as Partial<OrFalse<NetOptions>>);
}
/** http module integration */
export class Net implements Integration {
/** @inheritDoc */
public static id: string = 'Net';
/** @inheritDoc */
public name: string = Net.id;
private readonly _options: OrFalse<NetOptions>;
/** @inheritDoc */
public constructor(options: Partial<OrBool<NetOptions>> = {}) {
this._options = {
...DEFAULT_OPTIONS,
...normalizeOptions(options),
};
}
/** @inheritDoc */
public setupOnce(): void {
// No need to instrument if we don't want to track anything
if (this._options.breadcrumbs || this._options.tracing) {
fill(net, 'request', createWrappedRequestFactory(this._options));
}
}
}
/**
* Trimmed down version of the code from Electron here:
* https://github.com/electron/electron/blob/f3df76dbdc58cb704637b89357e1400791c92cfe/lib/browser/api/net.ts#L209-L269
*
* We want to match the final URL that Electron uses
*/
function parseOptions(optionsIn: ClientRequestConstructorOptions | string): { method: string; url: string } {
const { method, options } =
typeof optionsIn === 'string'
? { method: 'GET', options: urlModule.parse(optionsIn) }
: { method: (optionsIn.method || 'GET').toUpperCase(), options: optionsIn };
let url = 'url' in options ? options.url : undefined;
if (!url) {
const urlObj: urlModule.UrlObject = {};
urlObj.protocol = options.protocol || 'http:';
if (options.host) {
urlObj.host = options.host;
} else {
if (options.hostname) {
urlObj.hostname = options.hostname;
} else {
urlObj.hostname = 'localhost';
}
if (options.port) {
urlObj.port = options.port;
}
}
const pathObj = urlModule.parse(options.path || '/');
urlObj.pathname = pathObj.pathname;
urlObj.search = pathObj.search;
urlObj.hash = pathObj.hash;
url = urlModule.format(urlObj);
}
return {
method,
url,
};
}
type RequestOptions = string | ClientRequestConstructorOptions;
type RequestMethod = (opt: RequestOptions) => ClientRequest;
type WrappedRequestMethodFactory = (original: RequestMethod) => RequestMethod;
/** */
function createWrappedRequestFactory(options: OrFalse<NetOptions>): WrappedRequestMethodFactory {
return function wrappedRequestMethodFactory(originalRequestMethod: RequestMethod): RequestMethod {
return function requestMethod(this: typeof net, reqOptions: RequestOptions): ClientRequest {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const netModule = this;
const { url, method } = parseOptions(reqOptions);
const request = originalRequestMethod.apply(netModule, [reqOptions]) as ClientRequest;
if (url.match(/sentry_key/) || request.getHeader('x-sentry-auth')) {
return request;
}
let span: Span | undefined;
const scope = getCurrentHub().getScope();
if (scope && options.tracing && options.tracing(method, url)) {
const parentSpan = scope.getSpan();
if (parentSpan) {
span = parentSpan.startChild({
description: `${method} ${url}`,
op: 'http.client',
});
if (options.tracingOrigins && options.tracingOrigins(method, url)) {
request.setHeader('sentry-trace', span.toTraceparent());
}
}
}
return request
.once('response', function (this: ClientRequest, res: IncomingMessage): void {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const req = this;
if (options.breadcrumbs) {
addRequestBreadcrumb('response', method, url, req, res);
}
if (span) {
if (res.statusCode) {
span.setHttpStatus(res.statusCode);
}
span.finish();
}
})
.once('error', function (this: ClientRequest, _error: Error): void {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const req = this;
if (options.breadcrumbs) {
addRequestBreadcrumb('error', method, url, req, undefined);
}
if (span) {
span.setHttpStatus(500);
span.finish();
}
});
};
};
}
/**
* Captures Breadcrumb based on provided request/response pair
*/
function addRequestBreadcrumb(
event: string,
method: string,
url: string,
req: ClientRequest,
res?: IncomingMessage,
): void {
getCurrentHub().addBreadcrumb(
{
type: 'http',
category: 'electron.net',
data: {
url,
method: method,
status_code: res && res.statusCode,
},
},
{
event,
request: req,
response: res,
},
);
}