@@ -39,7 +39,12 @@ const {
39
39
prepareError,
40
40
} = require ( '_http_common' ) ;
41
41
const { OutgoingMessage } = require ( '_http_outgoing' ) ;
42
- const { outHeadersKey, ondrain, nowDate } = require ( 'internal/http' ) ;
42
+ const {
43
+ outHeadersKey,
44
+ ondrain,
45
+ nowDate,
46
+ emitStatistics
47
+ } = require ( 'internal/http' ) ;
43
48
const {
44
49
defaultTriggerAsyncIdScope,
45
50
getOrSetAsyncId
@@ -57,8 +62,11 @@ const {
57
62
DTRACE_HTTP_SERVER_RESPONSE
58
63
} = require ( 'internal/dtrace' ) ;
59
64
const { getOptionValue } = require ( 'internal/options' ) ;
65
+ const { observerCounts, constants } = internalBinding ( 'performance' ) ;
66
+ const { NODE_PERFORMANCE_ENTRY_TYPE_HTTP } = constants ;
60
67
61
68
const kServerResponse = Symbol ( 'ServerResponse' ) ;
69
+ const kServerResponseStatistics = Symbol ( 'ServerResponseStatistics' ) ;
62
70
const kDefaultHttpServerTimeout =
63
71
getOptionValue ( '--http-server-default-timeout' ) ;
64
72
@@ -150,12 +158,22 @@ function ServerResponse(req) {
150
158
this . useChunkedEncodingByDefault = chunkExpression . test ( req . headers . te ) ;
151
159
this . shouldKeepAlive = false ;
152
160
}
161
+
162
+ const httpObserverCount = observerCounts [ NODE_PERFORMANCE_ENTRY_TYPE_HTTP ] ;
163
+ if ( httpObserverCount > 0 ) {
164
+ this [ kServerResponseStatistics ] = {
165
+ startTime : process . hrtime ( )
166
+ } ;
167
+ }
153
168
}
154
169
Object . setPrototypeOf ( ServerResponse . prototype , OutgoingMessage . prototype ) ;
155
170
Object . setPrototypeOf ( ServerResponse , OutgoingMessage ) ;
156
171
157
172
ServerResponse . prototype . _finish = function _finish ( ) {
158
173
DTRACE_HTTP_SERVER_RESPONSE ( this . connection ) ;
174
+ if ( this [ kServerResponseStatistics ] !== undefined ) {
175
+ emitStatistics ( this [ kServerResponseStatistics ] ) ;
176
+ }
159
177
OutgoingMessage . prototype . _finish . call ( this ) ;
160
178
} ;
161
179
0 commit comments