Skip to content

Commit fc0ed45

Browse files
committed
inspector: tracking network activities with diagnostics_channel
1 parent 6a9a7cf commit fc0ed45

File tree

5 files changed

+51
-43
lines changed

5 files changed

+51
-43
lines changed

lib/_http_client.js

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
const {
2525
ArrayIsArray,
2626
Boolean,
27-
DateNow,
2827
Error,
2928
FunctionPrototypeCall,
3029
NumberIsFinite,
@@ -71,7 +70,6 @@ const {
7170
isTraceHTTPEnabled,
7271
traceBegin,
7372
traceEnd,
74-
getNextInspectorEventId,
7573
getNextTraceEventId,
7674
} = require('internal/http');
7775
const {
@@ -95,14 +93,6 @@ const {
9593
stopPerf,
9694
} = require('internal/perf/observe');
9795

98-
const {
99-
isEnabled: isInspectorEnabled,
100-
requestWillBeSent,
101-
responseReceived,
102-
dataReceived,
103-
loadingFinished,
104-
} = internalBinding('inspector');
105-
10696
const kClientRequestStatistics = Symbol('ClientRequestStatistics');
10797

10898
const dc = require('diagnostics_channel');
@@ -385,15 +375,14 @@ ObjectSetPrototypeOf(ClientRequest, OutgoingMessage);
385375

386376
ClientRequest.prototype._finish = function _finish() {
387377
FunctionPrototypeCall(OutgoingMessage.prototype._finish, this);
388-
const url = `${this.protocol}//${this.host}${this.path}`;
389378
if (hasObserver('http')) {
390379
startPerf(this, kClientRequestStatistics, {
391380
type: 'http',
392381
name: 'HttpClient',
393382
detail: {
394383
req: {
395384
method: this.method,
396-
url,
385+
url: `${this.protocol}//${this.host}${this.path}`,
397386
headers: typeof this.getHeaders === 'function' ? this.getHeaders() : {},
398387
},
399388
},
@@ -404,14 +393,6 @@ ClientRequest.prototype._finish = function _finish() {
404393
request: this,
405394
});
406395
}
407-
408-
if (isInspectorEnabled()) {
409-
this._inspectorEventId = getNextInspectorEventId();
410-
const wallTime = DateNow();
411-
const timestamp = wallTime / 1000;
412-
requestWillBeSent(this._inspectorEventId, url, this.method, timestamp, wallTime);
413-
}
414-
415396
if (isTraceHTTPEnabled()) {
416397
this._traceEventId = getNextTraceEventId();
417398
traceBegin(HTTP_CLIENT_TRACE_EVENT_NAME, this._traceEventId);
@@ -699,21 +680,6 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
699680
response: res,
700681
});
701682
}
702-
703-
if (isInspectorEnabled() && typeof req._inspectorEventId === 'string') {
704-
responseReceived(req._inspectorEventId, DateNow() / 1000);
705-
let response = '';
706-
const onData = (chunk) => {
707-
dataReceived(req._inspectorEventId, DateNow() / 1000, chunk.length);
708-
response += chunk.toString();
709-
};
710-
res.on('data', onData);
711-
res.on('end', () => {
712-
loadingFinished(req._inspectorEventId, response, DateNow() / 1000, response.length);
713-
res.removeListener('data', onData);
714-
});
715-
}
716-
717683
if (isTraceHTTPEnabled() && typeof req._traceEventId === 'number') {
718684
traceEnd(HTTP_CLIENT_TRACE_EVENT_NAME, req._traceEventId, {
719685
path: req.path,

lib/internal/http.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@ function resetCache() {
3131
utcCache = undefined;
3232
}
3333

34-
let inspectorEventId = 0;
35-
36-
function getNextInspectorEventId() {
37-
const id = ++inspectorEventId;
38-
return `node-network-inspect-event-${id}`;
39-
}
40-
4134
let traceEventId = 0;
4235

4336
function getNextTraceEventId() {
@@ -64,7 +57,6 @@ module.exports = {
6457
utcDate,
6558
traceBegin,
6659
traceEnd,
67-
getNextInspectorEventId,
6860
getNextTraceEventId,
6961
isTraceHTTPEnabled,
7062
};

lib/internal/process/pre_execution.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const {
44
ArrayPrototypeForEach,
55
Date,
6+
DateNow,
67
DatePrototypeGetDate,
78
DatePrototypeGetFullYear,
89
DatePrototypeGetHours,
@@ -98,6 +99,7 @@ function prepareExecution(options) {
9899
const mainEntry = patchProcessObject(expandArgv1);
99100
setupTraceCategoryState();
100101
setupInspectorHooks();
102+
setupNetworkInspection();
101103
setupNavigator();
102104
setupWarningHandler();
103105
setupWebStorage();
@@ -438,6 +440,51 @@ function setupInspectorHooks() {
438440
}
439441
}
440442

443+
function setupNetworkInspection() {
444+
if (!internalBinding('config').hasInspector) {
445+
return;
446+
}
447+
const dc = require('diagnostics_channel');
448+
const {
449+
dataReceived,
450+
loadingFinished,
451+
requestWillBeSent,
452+
responseReceived,
453+
} = internalBinding('inspector');
454+
455+
let requestId = 0;
456+
const getNextRequestId = () => `node-network-event-${++requestId}`;
457+
458+
dc.subscribe('http.client.request.start', ({ request }) => {
459+
const url = `${request.protocol}//${request.host}${request.path}`;
460+
const wallTime = DateNow();
461+
const timestamp = wallTime / 1000;
462+
request._inspectorRequestId = getNextRequestId();
463+
requestWillBeSent(request._inspectorRequestId, url, request.method, timestamp, wallTime);
464+
});
465+
dc.subscribe('http.client.response.finish', ({ request, response }) => {
466+
responseReceived(request._inspectorRequestId, DateNow() / 1000);
467+
let responseString = '';
468+
const onData = (chunk) => {
469+
dataReceived(request._inspectorRequestId, DateNow() / 1000, chunk.length);
470+
responseString += chunk.toString();
471+
};
472+
response.on('data', onData);
473+
response.on('end', () => {
474+
loadingFinished(request._inspectorRequestId, responseString, DateNow() / 1000, responseString.length);
475+
response.removeListener('data', onData);
476+
});
477+
});
478+
479+
dc.subscribe('undici:request:create', ({ request }) => {
480+
const url = `${request.origin}${request.path}`;
481+
const wallTime = DateNow();
482+
const timestamp = wallTime / 1000;
483+
request._inspectorRequestId = getNextRequestId();
484+
requestWillBeSent(request._inspectorRequestId, url, request.method, timestamp, wallTime);
485+
});
486+
}
487+
441488
// In general deprecations are initialized wherever the APIs are implemented,
442489
// this is used to deprecate APIs implemented in C++ where the deprecation
443490
// utilities are not easily accessible.

src/inspector/network_agent.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ DispatchResponse NetworkAgent::getResponseBody(const String& in_requestId,
2323
auto it = request_id_to_response_.find(in_requestId);
2424
if (it != request_id_to_response_.end()) {
2525
*out_body = it->second;
26+
request_id_to_response_.erase(it);
2627
}
2728
return DispatchResponse::OK();
2829
}

src/inspector_agent.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel,
245245
}
246246
runtime_agent_->disable();
247247
runtime_agent_.reset(); // Dispose before the dispatchers
248+
network_agent_->disable();
249+
network_agent_.reset(); // Dispose before the dispatchers
248250
}
249251

250252
void dispatchProtocolMessage(const StringView& message) {

0 commit comments

Comments
 (0)