Skip to content

Commit

Permalink
http: trace http client by perf_hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
theanarkh committed Mar 15, 2022
1 parent a01302b commit 66b9ae9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
17 changes: 17 additions & 0 deletions lib/_http_client.js
Expand Up @@ -75,6 +75,16 @@ const {
DTRACE_HTTP_CLIENT_RESPONSE
} = require('internal/dtrace');

const {
hasObserver,
} = require('internal/perf/observe');

const {
emitStatistics
} = require('internal/http');

const kClientRequestStatistics = Symbol('ClientRequestStatistics');

const { addAbortSignal, finished } = require('stream');

let debug = require('internal/util/debuglog').debuglog('http', (fn) => {
Expand Down Expand Up @@ -337,6 +347,12 @@ ObjectSetPrototypeOf(ClientRequest, OutgoingMessage);
ClientRequest.prototype._finish = function _finish() {
DTRACE_HTTP_CLIENT_REQUEST(this, this.socket);
FunctionPrototypeCall(OutgoingMessage.prototype._finish, this);
if (hasObserver('http')) {
this[kClientRequestStatistics] = {
startTime: process.hrtime(),
type: 'HttpClient'
};
}
};

ClientRequest.prototype._implicitHeader = function _implicitHeader() {
Expand Down Expand Up @@ -604,6 +620,7 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
}

DTRACE_HTTP_CLIENT_RESPONSE(socket, req);
emitStatistics(req[kClientRequestStatistics]);
req.res = res;
res.req = req;

Expand Down
3 changes: 2 additions & 1 deletion lib/_http_server.js
Expand Up @@ -193,7 +193,8 @@ function ServerResponse(req) {

if (hasObserver('http')) {
this[kServerResponseStatistics] = {
startTime: process.hrtime()
startTime: process.hrtime(),
type: 'HttpRequest'
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/http.js
Expand Up @@ -38,7 +38,7 @@ function emitStatistics(statistics) {
const startTime = statistics.startTime;
const diff = process.hrtime(startTime);
const entry = new InternalPerformanceEntry(
'HttpRequest',
statistics.type,
'http',
startTime[0] * 1000 + startTime[1] / 1e6,
diff[0] * 1000 + diff[1] / 1e6,
Expand Down

0 comments on commit 66b9ae9

Please sign in to comment.