Skip to content

Commit

Permalink
inspector: tracking network activities with diagnostics_channel
Browse files Browse the repository at this point in the history
  • Loading branch information
cola119 committed Jun 27, 2024
1 parent 6a9a7cf commit fc0ed45
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 43 deletions.
36 changes: 1 addition & 35 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
const {
ArrayIsArray,
Boolean,
DateNow,
Error,
FunctionPrototypeCall,
NumberIsFinite,
Expand Down Expand Up @@ -71,7 +70,6 @@ const {
isTraceHTTPEnabled,
traceBegin,
traceEnd,
getNextInspectorEventId,
getNextTraceEventId,
} = require('internal/http');
const {
Expand All @@ -95,14 +93,6 @@ const {
stopPerf,
} = require('internal/perf/observe');

const {
isEnabled: isInspectorEnabled,
requestWillBeSent,
responseReceived,
dataReceived,
loadingFinished,
} = internalBinding('inspector');

const kClientRequestStatistics = Symbol('ClientRequestStatistics');

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

ClientRequest.prototype._finish = function _finish() {
FunctionPrototypeCall(OutgoingMessage.prototype._finish, this);
const url = `${this.protocol}//${this.host}${this.path}`;
if (hasObserver('http')) {
startPerf(this, kClientRequestStatistics, {
type: 'http',
name: 'HttpClient',
detail: {
req: {
method: this.method,
url,
url: `${this.protocol}//${this.host}${this.path}`,
headers: typeof this.getHeaders === 'function' ? this.getHeaders() : {},
},
},
Expand All @@ -404,14 +393,6 @@ ClientRequest.prototype._finish = function _finish() {
request: this,
});
}

if (isInspectorEnabled()) {
this._inspectorEventId = getNextInspectorEventId();
const wallTime = DateNow();
const timestamp = wallTime / 1000;
requestWillBeSent(this._inspectorEventId, url, this.method, timestamp, wallTime);
}

if (isTraceHTTPEnabled()) {
this._traceEventId = getNextTraceEventId();
traceBegin(HTTP_CLIENT_TRACE_EVENT_NAME, this._traceEventId);
Expand Down Expand Up @@ -699,21 +680,6 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
response: res,
});
}

if (isInspectorEnabled() && typeof req._inspectorEventId === 'string') {
responseReceived(req._inspectorEventId, DateNow() / 1000);
let response = '';
const onData = (chunk) => {
dataReceived(req._inspectorEventId, DateNow() / 1000, chunk.length);
response += chunk.toString();
};
res.on('data', onData);
res.on('end', () => {
loadingFinished(req._inspectorEventId, response, DateNow() / 1000, response.length);
res.removeListener('data', onData);
});
}

if (isTraceHTTPEnabled() && typeof req._traceEventId === 'number') {
traceEnd(HTTP_CLIENT_TRACE_EVENT_NAME, req._traceEventId, {
path: req.path,
Expand Down
8 changes: 0 additions & 8 deletions lib/internal/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ function resetCache() {
utcCache = undefined;
}

let inspectorEventId = 0;

function getNextInspectorEventId() {
const id = ++inspectorEventId;
return `node-network-inspect-event-${id}`;
}

let traceEventId = 0;

function getNextTraceEventId() {
Expand All @@ -64,7 +57,6 @@ module.exports = {
utcDate,
traceBegin,
traceEnd,
getNextInspectorEventId,
getNextTraceEventId,
isTraceHTTPEnabled,
};
47 changes: 47 additions & 0 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const {
ArrayPrototypeForEach,
Date,
DateNow,
DatePrototypeGetDate,
DatePrototypeGetFullYear,
DatePrototypeGetHours,
Expand Down Expand Up @@ -98,6 +99,7 @@ function prepareExecution(options) {
const mainEntry = patchProcessObject(expandArgv1);
setupTraceCategoryState();
setupInspectorHooks();
setupNetworkInspection();
setupNavigator();
setupWarningHandler();
setupWebStorage();
Expand Down Expand Up @@ -438,6 +440,51 @@ function setupInspectorHooks() {
}
}

function setupNetworkInspection() {
if (!internalBinding('config').hasInspector) {
return;
}
const dc = require('diagnostics_channel');
const {
dataReceived,
loadingFinished,
requestWillBeSent,
responseReceived,
} = internalBinding('inspector');

let requestId = 0;
const getNextRequestId = () => `node-network-event-${++requestId}`;

dc.subscribe('http.client.request.start', ({ request }) => {
const url = `${request.protocol}//${request.host}${request.path}`;
const wallTime = DateNow();
const timestamp = wallTime / 1000;
request._inspectorRequestId = getNextRequestId();
requestWillBeSent(request._inspectorRequestId, url, request.method, timestamp, wallTime);
});
dc.subscribe('http.client.response.finish', ({ request, response }) => {
responseReceived(request._inspectorRequestId, DateNow() / 1000);
let responseString = '';
const onData = (chunk) => {
dataReceived(request._inspectorRequestId, DateNow() / 1000, chunk.length);
responseString += chunk.toString();
};
response.on('data', onData);
response.on('end', () => {
loadingFinished(request._inspectorRequestId, responseString, DateNow() / 1000, responseString.length);
response.removeListener('data', onData);
});
});

dc.subscribe('undici:request:create', ({ request }) => {
const url = `${request.origin}${request.path}`;
const wallTime = DateNow();
const timestamp = wallTime / 1000;
request._inspectorRequestId = getNextRequestId();
requestWillBeSent(request._inspectorRequestId, url, request.method, timestamp, wallTime);
});
}

// In general deprecations are initialized wherever the APIs are implemented,
// this is used to deprecate APIs implemented in C++ where the deprecation
// utilities are not easily accessible.
Expand Down
1 change: 1 addition & 0 deletions src/inspector/network_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ DispatchResponse NetworkAgent::getResponseBody(const String& in_requestId,
auto it = request_id_to_response_.find(in_requestId);
if (it != request_id_to_response_.end()) {
*out_body = it->second;
request_id_to_response_.erase(it);
}
return DispatchResponse::OK();
}
Expand Down
2 changes: 2 additions & 0 deletions src/inspector_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel,
}
runtime_agent_->disable();
runtime_agent_.reset(); // Dispose before the dispatchers
network_agent_->disable();
network_agent_.reset(); // Dispose before the dispatchers
}

void dispatchProtocolMessage(const StringView& message) {
Expand Down

0 comments on commit fc0ed45

Please sign in to comment.