From e30dae9a40b68ae0889a4785c108e7eb74961068 Mon Sep 17 00:00:00 2001 From: theanarkh <2923878201@qq.com> Date: Tue, 15 Mar 2022 23:03:43 +0800 Subject: [PATCH] http: trace http client by perf_hooks --- lib/_http_client.js | 15 ++++++++++++++- lib/_http_server.js | 3 ++- lib/internal/http.js | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index 59c4cac89e9d9d..fb5928a5c9e345 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -57,7 +57,7 @@ const Agent = require('_http_agent'); const { Buffer } = require('buffer'); const { defaultTriggerAsyncIdScope } = require('internal/async_hooks'); const { URL, urlToHttpOptions, searchParamsSymbol } = require('internal/url'); -const { kOutHeaders, kNeedDrain } = require('internal/http'); +const { kOutHeaders, kNeedDrain, emitStatistics } = require('internal/http'); const { connResetException, codes } = require('internal/errors'); const { ERR_HTTP_HEADERS_SENT, @@ -75,6 +75,12 @@ const { DTRACE_HTTP_CLIENT_RESPONSE } = require('internal/dtrace'); +const { + hasObserver, +} = require('internal/perf/observe'); + +const kClientRequestStatistics = Symbol('ClientRequestStatistics'); + const { addAbortSignal, finished } = require('stream'); let debug = require('internal/util/debuglog').debuglog('http', (fn) => { @@ -337,6 +343,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() { @@ -604,6 +616,7 @@ function parserOnIncomingClient(res, shouldKeepAlive) { } DTRACE_HTTP_CLIENT_RESPONSE(socket, req); + emitStatistics(req[kClientRequestStatistics]); req.res = res; res.req = req; diff --git a/lib/_http_server.js b/lib/_http_server.js index b3cdc87249c593..63dff3ce1b986c 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -193,7 +193,8 @@ function ServerResponse(req) { if (hasObserver('http')) { this[kServerResponseStatistics] = { - startTime: process.hrtime() + startTime: process.hrtime(), + type: 'HttpRequest' }; } } diff --git a/lib/internal/http.js b/lib/internal/http.js index 56187a2b1cc315..375118da49f59b 100644 --- a/lib/internal/http.js +++ b/lib/internal/http.js @@ -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,