24
24
const {
25
25
ArrayIsArray,
26
26
Boolean,
27
+ DateNow,
27
28
Error,
28
29
FunctionPrototypeCall,
29
30
NumberIsFinite,
@@ -70,6 +71,7 @@ const {
70
71
isTraceHTTPEnabled,
71
72
traceBegin,
72
73
traceEnd,
74
+ getNextInspectorEventId,
73
75
getNextTraceEventId,
74
76
} = require ( 'internal/http' ) ;
75
77
const {
@@ -93,6 +95,14 @@ const {
93
95
stopPerf,
94
96
} = require ( 'internal/perf/observe' ) ;
95
97
98
+ const {
99
+ isEnabled : isInspectorEnabled ,
100
+ requestWillBeSent,
101
+ responseReceived,
102
+ dataReceived,
103
+ loadingFinished,
104
+ } = internalBinding ( 'inspector' ) ;
105
+
96
106
const kClientRequestStatistics = Symbol ( 'ClientRequestStatistics' ) ;
97
107
98
108
const dc = require ( 'diagnostics_channel' ) ;
@@ -375,14 +385,15 @@ ObjectSetPrototypeOf(ClientRequest, OutgoingMessage);
375
385
376
386
ClientRequest . prototype . _finish = function _finish ( ) {
377
387
FunctionPrototypeCall ( OutgoingMessage . prototype . _finish , this ) ;
388
+ const url = `${ this . protocol } //${ this . host } ${ this . path } ` ;
378
389
if ( hasObserver ( 'http' ) ) {
379
390
startPerf ( this , kClientRequestStatistics , {
380
391
type : 'http' ,
381
392
name : 'HttpClient' ,
382
393
detail : {
383
394
req : {
384
395
method : this . method ,
385
- url : ` ${ this . protocol } // ${ this . host } ${ this . path } ` ,
396
+ url,
386
397
headers : typeof this . getHeaders === 'function' ? this . getHeaders ( ) : { } ,
387
398
} ,
388
399
} ,
@@ -393,6 +404,14 @@ ClientRequest.prototype._finish = function _finish() {
393
404
request : this ,
394
405
} ) ;
395
406
}
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
+
396
415
if ( isTraceHTTPEnabled ( ) ) {
397
416
this . _traceEventId = getNextTraceEventId ( ) ;
398
417
traceBegin ( HTTP_CLIENT_TRACE_EVENT_NAME , this . _traceEventId ) ;
@@ -680,6 +699,21 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
680
699
response : res ,
681
700
} ) ;
682
701
}
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
+
683
717
if ( isTraceHTTPEnabled ( ) && typeof req . _traceEventId === 'number' ) {
684
718
traceEnd ( HTTP_CLIENT_TRACE_EVENT_NAME , req . _traceEventId , {
685
719
path : req . path ,
0 commit comments