-
Notifications
You must be signed in to change notification settings - Fork 225
/
http2.js
320 lines (283 loc) · 10.1 KB
/
http2.js
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
/*
* Copyright Elasticsearch B.V. and other contributors where applicable.
* Licensed under the BSD 2-Clause License; you may not use this file except in
* compliance with the BSD 2-Clause License.
*/
'use strict';
var eos = require('end-of-stream');
var shimmer = require('../shimmer');
var symbols = require('../../symbols');
var { parseUrl } = require('../../parsers');
var { getHTTPDestination } = require('../context');
// Return true iff this is an HTTP 1.0 or 1.1 request.
//
// @param {http2.Http2ServerRequest} req
function reqIsHTTP1(req) {
return (
req &&
typeof req.httpVersion === 'string' &&
req.httpVersion.startsWith('1.')
);
}
module.exports = function (http2, agent, { enabled }) {
if (agent._conf.instrumentIncomingHTTPRequests) {
agent.logger.debug('shimming http2.createServer function');
shimmer.wrap(http2, 'createServer', wrapCreateServer);
shimmer.wrap(http2, 'createSecureServer', wrapCreateServer);
}
if (!enabled) return http2;
var ins = agent._instrumentation;
agent.logger.debug('shimming http2.connect function');
shimmer.wrap(http2, 'connect', wrapConnect);
return http2;
// The `createServer` function will unpatch itself after patching
// the first server prototype it patches.
function wrapCreateServer(original) {
return function wrappedCreateServer(options, handler) {
var server = original.apply(this, arguments);
shimmer.wrap(server.constructor.prototype, 'emit', wrapEmit);
wrappedCreateServer[symbols.unwrap]();
return server;
};
}
function wrapEmit(original) {
var patched = false;
return function wrappedEmit(event, stream, headers) {
if (event === 'stream') {
if (!patched) {
patched = true;
var proto = stream.constructor.prototype;
shimmer.wrap(proto, 'pushStream', wrapPushStream);
shimmer.wrap(proto, 'respondWithFile', wrapRespondWith);
shimmer.wrap(proto, 'respondWithFD', wrapRespondWith);
shimmer.wrap(proto, 'respond', wrapHeaders);
shimmer.wrap(proto, 'end', wrapEnd);
}
agent.logger.debug(
'intercepted stream event call to http2.Server.prototype.emit',
);
const traceparent =
headers.traceparent || headers['elastic-apm-traceparent'];
const tracestate = headers.tracestate;
const trans = agent.startTransaction(null, 'request', {
childOf: traceparent,
tracestate,
});
// `trans.req` and `trans.res` are fake representations of Node.js's
// core `http.IncomingMessage` and `http.ServerResponse` objects,
// sufficient for `parsers.getContextFromRequest()` and
// `parsers.getContextFromResponse()`, respectively.
// `remoteAddress` is fetched now, rather than at stream end, because
// this `stream.session.socket` is a proxy object that can throw
// `ERR_HTTP2_SOCKET_UNBOUND` if the Http2Session has been destroyed.
trans.req = {
headers,
socket: {
remoteAddress: stream.session.socket.remoteAddress,
},
method: headers[':method'],
url: headers[':path'],
httpVersion: '2.0',
};
trans.res = {
statusCode: 200,
headersSent: false,
finished: false,
headers: null,
};
ins.bindEmitter(stream);
eos(stream, function () {
trans.end();
});
} else if (event === 'request' && reqIsHTTP1(stream)) {
// http2.createSecureServer() supports a `allowHTTP1: true` option.
// When true, an incoming client request that supports HTTP/1.x but not
// HTTP/2 will be allowed. It will result in a 'request' event being
// emitted. We wrap that here.
//
// Note that, a HTTP/2 request results in a 'stream' event (wrapped
// above) *and* a 'request' event. We do not want to wrap the
// compatibility 'request' event in this case. Hence the `reqIsHTTP1`
// guard.
const req = stream;
const res = headers;
agent.logger.debug(
'intercepted request event call to http2.Server.prototype.emit for %s',
req.url,
);
const traceparent =
req.headers.traceparent || req.headers['elastic-apm-traceparent'];
const tracestate = req.headers.tracestate;
const trans = agent.startTransaction(null, null, {
childOf: traceparent,
tracestate,
});
trans.type = 'request';
trans.req = req;
trans.res = res;
ins.bindEmitter(req);
ins.bindEmitter(res);
eos(res, function (err) {
if (trans.ended) return;
if (!err) {
trans.end();
return;
}
if (agent._conf.errorOnAbortedRequests) {
var duration = trans._timer.elapsed();
if (duration > agent._conf.abortedErrorThreshold * 1000) {
agent.captureError(
'Socket closed with active HTTP request (>' +
agent._conf.abortedErrorThreshold +
' sec)',
{
request: req,
extra: { abortTime: duration },
},
);
}
}
// Handle case where res.end is called after an error occurred on the
// stream (e.g. if the underlying socket was prematurely closed)
const end = res.end;
res.end = function () {
const result = end.apply(this, arguments);
trans.end();
return result;
};
});
}
return original.apply(this, arguments);
};
}
function updateHeaders(headers) {
var trans = agent._instrumentation.currTransaction();
if (trans && !trans.ended) {
var status = headers[':status'] || 200;
trans.result = 'HTTP ' + status.toString()[0] + 'xx';
trans.res.statusCode = status;
trans._setOutcomeFromHttpStatusCode(status);
trans.res.headers = mergeHeaders(trans.res.headers, headers);
trans.res.headersSent = true;
}
}
function wrapHeaders(original) {
return function (headers) {
updateHeaders(headers);
return original.apply(this, arguments);
};
}
function wrapRespondWith(original) {
return function (_, headers) {
updateHeaders(headers);
return original.apply(this, arguments);
};
}
function wrapEnd(original) {
return function (headers) {
var trans = agent._instrumentation.currTransaction();
// `trans.res` might be removed, because before
// https://github.com/nodejs/node/pull/20084 (e.g. in node v10.0.0) the
// 'end' event could be called multiple times for the same Http2Stream,
// and the `trans.res` ref is removed when the Transaction is ended.
if (trans && trans.res) {
trans.res.finished = true;
}
return original.apply(this, arguments);
};
}
function wrapPushStream(original) {
return function wrappedPushStream(...args) {
// Note: Break the run context so that the wrapped `stream.respond` et al
// for this pushStream do not overwrite outer transaction state.
var callback = args.pop();
args.push(agent._instrumentation.bindFunctionToEmptyRunContext(callback));
return original.apply(this, args);
};
}
function mergeHeaders(source, target) {
if (source === null) return target;
var result = Object.assign({}, target);
var keys = Object.keys(source);
for (let i = 0; i < keys.length; i++) {
var key = keys[i];
if (typeof target[key] === 'undefined') {
result[key] = source[key];
} else if (Array.isArray(target[key])) {
result[key].push(source[key]);
} else {
result[key] = [source[key]].concat(target[key]);
}
}
return result;
}
function wrapConnect(orig) {
return function (host) {
const ret = orig.apply(this, arguments);
shimmer.wrap(ret, 'request', (orig) => wrapRequest(orig, host));
return ret;
};
}
function wrapRequest(orig, host) {
return function (headers) {
agent.logger.debug('intercepted call to http2.request');
var method = headers[':method'] || 'GET';
const span = ins.createSpan(null, 'external', 'http', method, {
exitSpan: true,
});
const parentRunContext = ins.currRunContext();
var parent =
span ||
parentRunContext.currSpan() ||
parentRunContext.currTransaction();
if (parent) {
const newHeaders = Object.assign({}, headers);
parent.propagateTraceContextHeaders(
newHeaders,
function (carrier, name, value) {
carrier[name] = value;
},
);
arguments[0] = newHeaders;
}
if (!span) {
return orig.apply(this, arguments);
}
const spanRunContext = parentRunContext.enterSpan(span);
var req = ins.withRunContext(spanRunContext, orig, this, ...arguments);
ins.bindEmitterToRunContext(parentRunContext, req);
var urlObj = parseUrl(headers[':path']);
var path = urlObj.pathname;
var url = host + path;
span.name = method + ' ' + host;
var statusCode;
req.on('response', (headers) => {
statusCode = headers[':status'];
});
req.on('end', () => {
agent.logger.debug('intercepted http2 client end event');
span.setHttpContext({
method,
status_code: statusCode,
url,
});
span._setOutcomeFromHttpStatusCode(statusCode);
// The `getHTTPDestination` function might throw in case an
// invalid URL is given to the `URL()` function. Until we can
// be 100% sure this doesn't happen, we better catch it here.
// For details, see:
// https://github.com/elastic/apm-agent-nodejs/issues/1769
try {
span._setDestinationContext(getHTTPDestination(url));
} catch (e) {
agent.logger.error(
'Could not set destination context: %s',
e.message,
);
}
span.end();
});
return req;
};
}
};