Skip to content

Commit e30dae9

Browse files
committed
http: trace http client by perf_hooks
1 parent a01302b commit e30dae9

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

lib/_http_client.js

Lines changed: 14 additions & 1 deletion
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 } = require('internal/http');
60+
const { kOutHeaders, kNeedDrain, emitStatistics } = require('internal/http');
6161
const { connResetException, codes } = require('internal/errors');
6262
const {
6363
ERR_HTTP_HEADERS_SENT,
@@ -75,6 +75,12 @@ const {
7575
DTRACE_HTTP_CLIENT_RESPONSE
7676
} = require('internal/dtrace');
7777

78+
const {
79+
hasObserver,
80+
} = require('internal/perf/observe');
81+
82+
const kClientRequestStatistics = Symbol('ClientRequestStatistics');
83+
7884
const { addAbortSignal, finished } = require('stream');
7985

8086
let debug = require('internal/util/debuglog').debuglog('http', (fn) => {
@@ -337,6 +343,12 @@ ObjectSetPrototypeOf(ClientRequest, OutgoingMessage);
337343
ClientRequest.prototype._finish = function _finish() {
338344
DTRACE_HTTP_CLIENT_REQUEST(this, this.socket);
339345
FunctionPrototypeCall(OutgoingMessage.prototype._finish, this);
346+
if (hasObserver('http')) {
347+
this[kClientRequestStatistics] = {
348+
startTime: process.hrtime(),
349+
type: 'HttpClient'
350+
};
351+
}
340352
};
341353

342354
ClientRequest.prototype._implicitHeader = function _implicitHeader() {
@@ -604,6 +616,7 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
604616
}
605617

606618
DTRACE_HTTP_CLIENT_RESPONSE(socket, req);
619+
emitStatistics(req[kClientRequestStatistics]);
607620
req.res = res;
608621
res.req = req;
609622

lib/_http_server.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ function ServerResponse(req) {
193193

194194
if (hasObserver('http')) {
195195
this[kServerResponseStatistics] = {
196-
startTime: process.hrtime()
196+
startTime: process.hrtime(),
197+
type: 'HttpRequest'
197198
};
198199
}
199200
}

lib/internal/http.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function emitStatistics(statistics) {
3838
const startTime = statistics.startTime;
3939
const diff = process.hrtime(startTime);
4040
const entry = new InternalPerformanceEntry(
41-
'HttpRequest',
41+
statistics.type,
4242
'http',
4343
startTime[0] * 1000 + startTime[1] / 1e6,
4444
diff[0] * 1000 + diff[1] / 1e6,

0 commit comments

Comments
 (0)