Skip to content

Commit c41e9e9

Browse files
committed
http: add perf_hooks detail
1. http: add perf_hooks detail for http request and client 2. dns,net: move hasObserver out of startPerf and stopPerf
1 parent be1ca70 commit c41e9e9

File tree

9 files changed

+159
-94
lines changed

9 files changed

+159
-94
lines changed

doc/api/perf_hooks.md

+16
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,22 @@ property will be an {Object} with two properties:
484484
* `perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_ALL_EXTERNAL_MEMORY`
485485
* `perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_SCHEDULE_IDLE`
486486

487+
### HTTP ('http') Details
488+
489+
When `performanceEntry.type` is equal to `'http'`, the
490+
`performanceEntry.detail` property will be an {Object} containing
491+
additional information.
492+
493+
If `performanceEntry.name` is equal to `HttpClient`, the `detail`
494+
will contain the following properties: `req`, `res`. And the `req` property
495+
will be an {Object} containing `method`, `url`, `headers`, the `res` property
496+
will be an {Object} containing `statusCode`, `statusMessage`, `headers`.
497+
498+
If `performanceEntry.name` is equal to `HttpRequest`, the `detail`
499+
will contain the following properties: `req`, `res`. And the `req` property
500+
will be an {Object} containing `method`, `url`, `headers`, the `res` property
501+
will be an {Object} containing `statusCode`, `statusMessage`, `headers`.
502+
487503
### HTTP/2 ('http2') Details
488504

489505
When `performanceEntry.type` is equal to `'http2'`, the

lib/_http_client.js

+25-6
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const Agent = require('_http_agent');
5757
const { Buffer } = require('buffer');
5858
const { defaultTriggerAsyncIdScope } = require('internal/async_hooks');
5959
const { URL, urlToHttpOptions, searchParamsSymbol } = require('internal/url');
60-
const { kOutHeaders, kNeedDrain, emitStatistics } = require('internal/http');
60+
const { kOutHeaders, kNeedDrain } = require('internal/http');
6161
const { connResetException, codes } = require('internal/errors');
6262
const {
6363
ERR_HTTP_HEADERS_SENT,
@@ -77,6 +77,8 @@ const {
7777

7878
const {
7979
hasObserver,
80+
startPerf,
81+
stopPerf,
8082
} = require('internal/perf/observe');
8183

8284
const kClientRequestStatistics = Symbol('ClientRequestStatistics');
@@ -344,10 +346,17 @@ ClientRequest.prototype._finish = function _finish() {
344346
DTRACE_HTTP_CLIENT_REQUEST(this, this.socket);
345347
FunctionPrototypeCall(OutgoingMessage.prototype._finish, this);
346348
if (hasObserver('http')) {
347-
this[kClientRequestStatistics] = {
348-
startTime: process.hrtime(),
349-
type: 'HttpClient',
350-
};
349+
startPerf(this, kClientRequestStatistics, {
350+
type: 'http',
351+
name: 'HttpClient',
352+
detail: {
353+
req: {
354+
method: this.method,
355+
url: `${this.protocol}//${this.host}${this.path}`,
356+
headers: typeof this.getHeaders === 'function' ? this.getHeaders() : {},
357+
},
358+
},
359+
});
351360
}
352361
};
353362

@@ -616,7 +625,17 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
616625
}
617626

618627
DTRACE_HTTP_CLIENT_RESPONSE(socket, req);
619-
emitStatistics(req[kClientRequestStatistics]);
628+
if (req[kClientRequestStatistics] && hasObserver('http')) {
629+
stopPerf(req, kClientRequestStatistics, {
630+
detail: {
631+
res: {
632+
statusCode: res.statusCode,
633+
statusMessage: res.statusMessage,
634+
headers: res.headers,
635+
},
636+
},
637+
});
638+
}
620639
req.res = res;
621640
res.req = req;
622641

lib/_http_server.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ const { OutgoingMessage } = require('_http_outgoing');
5151
const {
5252
kOutHeaders,
5353
kNeedDrain,
54-
emitStatistics
5554
} = require('internal/http');
5655
const {
5756
defaultTriggerAsyncIdScope,
@@ -93,6 +92,8 @@ const kServerResponse = Symbol('ServerResponse');
9392
const kServerResponseStatistics = Symbol('ServerResponseStatistics');
9493

9594
const {
95+
startPerf,
96+
stopPerf,
9697
hasObserver,
9798
} = require('internal/perf/observe');
9899

@@ -191,20 +192,36 @@ function ServerResponse(req) {
191192
req.headers.te);
192193
this.shouldKeepAlive = false;
193194
}
194-
195195
if (hasObserver('http')) {
196-
this[kServerResponseStatistics] = {
197-
startTime: process.hrtime(),
198-
type: 'HttpRequest',
199-
};
196+
startPerf(this, kServerResponseStatistics, {
197+
type: 'http',
198+
name: 'HttpRequest',
199+
detail: {
200+
req: {
201+
method: req.method,
202+
url: req.url,
203+
headers: req.headers,
204+
},
205+
},
206+
});
200207
}
201208
}
202209
ObjectSetPrototypeOf(ServerResponse.prototype, OutgoingMessage.prototype);
203210
ObjectSetPrototypeOf(ServerResponse, OutgoingMessage);
204211

205212
ServerResponse.prototype._finish = function _finish() {
206213
DTRACE_HTTP_SERVER_RESPONSE(this.socket);
207-
emitStatistics(this[kServerResponseStatistics]);
214+
if (this[kServerResponseStatistics] && hasObserver('http')) {
215+
stopPerf(this, kServerResponseStatistics, {
216+
detail: {
217+
res: {
218+
statusCode: this.statusCode,
219+
statusMessage: this.statusMessage,
220+
headers: typeof this.getHeaders === 'function' ? this.getHeaders() : {},
221+
},
222+
},
223+
});
224+
}
208225
OutgoingMessage.prototype._finish.call(this);
209226
};
210227

lib/dns.js

+42-27
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const kPerfHooksDnsLookupServiceContext = Symbol('kPerfHooksDnsLookupServiceCont
7070
const kPerfHooksDnsLookupResolveContext = Symbol('kPerfHooksDnsLookupResolveContext');
7171

7272
const {
73+
hasObserver,
7374
startPerf,
7475
stopPerf,
7576
} = require('internal/perf/observe');
@@ -83,7 +84,9 @@ function onlookup(err, addresses) {
8384
return this.callback(dnsException(err, 'getaddrinfo', this.hostname));
8485
}
8586
this.callback(null, addresses[0], this.family || isIP(addresses[0]));
86-
stopPerf(this, kPerfHooksDnsLookupContext);
87+
if (this[kPerfHooksDnsLookupContext] && hasObserver('dns')) {
88+
stopPerf(this, kPerfHooksDnsLookupContext);
89+
}
8790
}
8891

8992

@@ -102,7 +105,9 @@ function onlookupall(err, addresses) {
102105
}
103106

104107
this.callback(null, addresses);
105-
stopPerf(this, kPerfHooksDnsLookupContext);
108+
if (this[kPerfHooksDnsLookupContext] && hasObserver('dns')) {
109+
stopPerf(this, kPerfHooksDnsLookupContext);
110+
}
106111
}
107112

108113

@@ -187,13 +192,15 @@ function lookup(hostname, options, callback) {
187192
process.nextTick(callback, dnsException(err, 'getaddrinfo', hostname));
188193
return {};
189194
}
190-
const detail = {
191-
hostname,
192-
family,
193-
hints,
194-
verbatim,
195-
};
196-
startPerf(req, kPerfHooksDnsLookupContext, { type: 'dns', name: 'lookup', detail });
195+
if (hasObserver('dns')) {
196+
const detail = {
197+
hostname,
198+
family,
199+
hints,
200+
verbatim,
201+
};
202+
startPerf(req, kPerfHooksDnsLookupContext, { type: 'dns', name: 'lookup', detail });
203+
}
197204
return req;
198205
}
199206

@@ -206,7 +213,9 @@ function onlookupservice(err, hostname, service) {
206213
return this.callback(dnsException(err, 'getnameinfo', this.hostname));
207214

208215
this.callback(null, hostname, service);
209-
stopPerf(this, kPerfHooksDnsLookupServiceContext);
216+
if (this[kPerfHooksDnsLookupServiceContext] && hasObserver('dns')) {
217+
stopPerf(this, kPerfHooksDnsLookupServiceContext);
218+
}
210219
}
211220

212221

@@ -231,14 +240,16 @@ function lookupService(address, port, callback) {
231240

232241
const err = cares.getnameinfo(req, address, port);
233242
if (err) throw dnsException(err, 'getnameinfo', address);
234-
startPerf(req, kPerfHooksDnsLookupServiceContext, {
235-
type: 'dns',
236-
name: 'lookupService',
237-
detail: {
238-
host: address,
239-
port
240-
}
241-
});
243+
if (hasObserver('dns')) {
244+
startPerf(req, kPerfHooksDnsLookupServiceContext, {
245+
type: 'dns',
246+
name: 'lookupService',
247+
detail: {
248+
host: address,
249+
port
250+
}
251+
});
252+
}
242253
return req;
243254
}
244255

@@ -255,7 +266,9 @@ function onresolve(err, result, ttls) {
255266
this.callback(dnsException(err, this.bindingName, this.hostname));
256267
else {
257268
this.callback(null, result);
258-
stopPerf(this, kPerfHooksDnsLookupResolveContext);
269+
if (this[kPerfHooksDnsLookupResolveContext] && hasObserver('dns')) {
270+
stopPerf(this, kPerfHooksDnsLookupResolveContext);
271+
}
259272
}
260273
}
261274

@@ -278,14 +291,16 @@ function resolver(bindingName) {
278291
req.ttl = !!(options && options.ttl);
279292
const err = this._handle[bindingName](req, toASCII(name));
280293
if (err) throw dnsException(err, bindingName, name);
281-
startPerf(req, kPerfHooksDnsLookupResolveContext, {
282-
type: 'dns',
283-
name: bindingName,
284-
detail: {
285-
host: name,
286-
ttl: req.ttl
287-
}
288-
});
294+
if (hasObserver('dns')) {
295+
startPerf(req, kPerfHooksDnsLookupResolveContext, {
296+
type: 'dns',
297+
name: bindingName,
298+
detail: {
299+
host: name,
300+
ttl: req.ttl
301+
}
302+
});
303+
}
289304
return req;
290305
}
291306
ObjectDefineProperty(query, 'name', { value: bindingName });

lib/internal/dns/promises.js

+25-13
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const kPerfHooksDnsLookupResolveContext = Symbol('kPerfHooksDnsLookupResolveCont
4848
const {
4949
startPerf,
5050
stopPerf,
51+
hasObserver,
5152
} = require('internal/perf/observe');
5253

5354
function onlookup(err, addresses) {
@@ -58,7 +59,9 @@ function onlookup(err, addresses) {
5859

5960
const family = this.family || isIP(addresses[0]);
6061
this.resolve({ address: addresses[0], family });
61-
stopPerf(this, kPerfHooksDnsLookupContext);
62+
if (this[kPerfHooksDnsLookupContext] && hasObserver('dns')) {
63+
stopPerf(this, kPerfHooksDnsLookupContext);
64+
}
6265
}
6366

6467
function onlookupall(err, addresses) {
@@ -79,7 +82,9 @@ function onlookupall(err, addresses) {
7982
}
8083

8184
this.resolve(addresses);
82-
stopPerf(this, kPerfHooksDnsLookupContext);
85+
if (this[kPerfHooksDnsLookupContext] && hasObserver('dns')) {
86+
stopPerf(this, kPerfHooksDnsLookupContext);
87+
}
8388
}
8489

8590
function createLookupPromise(family, hostname, all, hints, verbatim) {
@@ -111,13 +116,15 @@ function createLookupPromise(family, hostname, all, hints, verbatim) {
111116
if (err) {
112117
reject(dnsException(err, 'getaddrinfo', hostname));
113118
} else {
114-
const detail = {
115-
hostname,
116-
family,
117-
hints,
118-
verbatim,
119-
};
120-
startPerf(req, kPerfHooksDnsLookupContext, { type: 'dns', name: 'lookup', detail });
119+
if (hasObserver('dns')) {
120+
const detail = {
121+
hostname,
122+
family,
123+
hints,
124+
verbatim,
125+
};
126+
startPerf(req, kPerfHooksDnsLookupContext, { type: 'dns', name: 'lookup', detail });
127+
}
121128
}
122129
});
123130
}
@@ -170,7 +177,9 @@ function onlookupservice(err, hostname, service) {
170177
}
171178

172179
this.resolve({ hostname, service });
173-
stopPerf(this, kPerfHooksDnsLookupServiceContext);
180+
if (this[kPerfHooksDnsLookupServiceContext] && hasObserver('dns')) {
181+
stopPerf(this, kPerfHooksDnsLookupServiceContext);
182+
}
174183
}
175184

176185
function createLookupServicePromise(hostname, port) {
@@ -187,7 +196,7 @@ function createLookupServicePromise(hostname, port) {
187196

188197
if (err)
189198
reject(dnsException(err, 'getnameinfo', hostname));
190-
else
199+
else if (hasObserver('dns')) {
191200
startPerf(req, kPerfHooksDnsLookupServiceContext, {
192201
type: 'dns',
193202
name: 'lookupService',
@@ -196,6 +205,7 @@ function createLookupServicePromise(hostname, port) {
196205
port
197206
}
198207
});
208+
}
199209
});
200210
}
201211

@@ -223,7 +233,9 @@ function onresolve(err, result, ttls) {
223233
result, (address, index) => ({ address, ttl: ttls[index] }));
224234

225235
this.resolve(result);
226-
stopPerf(this, kPerfHooksDnsLookupResolveContext);
236+
if (this[kPerfHooksDnsLookupResolveContext] && hasObserver('dns')) {
237+
stopPerf(this, kPerfHooksDnsLookupResolveContext);
238+
}
227239
}
228240

229241
function createResolverPromise(resolver, bindingName, hostname, ttl) {
@@ -241,7 +253,7 @@ function createResolverPromise(resolver, bindingName, hostname, ttl) {
241253

242254
if (err)
243255
reject(dnsException(err, bindingName, hostname));
244-
else {
256+
else if (hasObserver('dns')) {
245257
startPerf(req, kPerfHooksDnsLookupResolveContext, {
246258
type: 'dns',
247259
name: bindingName,

0 commit comments

Comments
 (0)