File tree 5 files changed +54
-5
lines changed
datadog-instrumentations/src/http
datadog-plugin-http2/test/integration-test
5 files changed +54
-5
lines changed Original file line number Diff line number Diff line change @@ -69,17 +69,29 @@ function patch (http, methodName) {
69
69
try {
70
70
const req = request . call ( this , options , callback )
71
71
const emit = req . emit
72
- const setTimeout = req . setTimeout
72
+
73
+ const requestSetTimeout = req . setTimeout
73
74
74
75
ctx . req = req
75
76
76
77
// tracked to accurately discern custom request socket timeout
77
78
let customRequestTimeout = false
79
+
78
80
req . setTimeout = function ( ) {
79
81
customRequestTimeout = true
80
- return setTimeout . apply ( this , arguments )
82
+ return requestSetTimeout . apply ( this , arguments )
81
83
}
82
84
85
+ req . on ( 'socket' , socket => {
86
+ if ( socket ) {
87
+ const socketSetTimeout = socket . setTimeout
88
+ socket . setTimeout = function ( ) {
89
+ customRequestTimeout = true
90
+ return socketSetTimeout . apply ( this , arguments )
91
+ }
92
+ }
93
+ } )
94
+
83
95
req . emit = function ( eventName , arg ) {
84
96
switch ( eventName ) {
85
97
case 'response' : {
Original file line number Diff line number Diff line change @@ -121,7 +121,7 @@ class HttpClientPlugin extends ClientPlugin {
121
121
} else {
122
122
// conditions for no error:
123
123
// 1. not using a custom agent instance with custom timeout specified
124
- // 2. no invocation of `req.setTimeout`
124
+ // 2. no invocation of `req.setTimeout` or `socket.setTimeout`
125
125
if ( ! args . options . agent ?. options . timeout && ! customRequestTimeout ) return
126
126
127
127
span . setTag ( 'error' , 1 )
Original file line number Diff line number Diff line change @@ -900,6 +900,39 @@ describe('Plugin', () => {
900
900
} )
901
901
} )
902
902
} ) . timeout ( 10000 )
903
+
904
+ it ( 'should record error if req.socket.setTimeout is used with Node 20' , done => {
905
+ const app = express ( )
906
+
907
+ app . get ( '/user' , async ( req , res ) => {
908
+ await new Promise ( resolve => {
909
+ setTimeout ( resolve , 6 * 1000 )
910
+ } )
911
+ res . status ( 200 ) . send ( )
912
+ } )
913
+
914
+ getPort ( ) . then ( port => {
915
+ agent
916
+ . use ( traces => {
917
+ expect ( traces [ 0 ] [ 0 ] ) . to . have . property ( 'error' , 1 )
918
+ } )
919
+ . then ( done )
920
+ . catch ( done )
921
+
922
+ appListener = server ( app , port , async ( ) => {
923
+ const req = http . request ( `${ protocol } ://localhost:${ port } /user` , res => {
924
+ res . on ( 'data' , ( ) => { } )
925
+ } )
926
+
927
+ req . on ( 'error' , ( ) => { } )
928
+ req . on ( 'socket' , socket => {
929
+ socket . setTimeout ( 5000 ) // match default timeout
930
+ } )
931
+
932
+ req . end ( )
933
+ } )
934
+ } )
935
+ } ) . timeout ( 10000 )
903
936
}
904
937
905
938
it ( 'should only record a request once' , done => {
Original file line number Diff line number Diff line change @@ -10,5 +10,7 @@ const server = http.createServer(async (req, res) => {
10
10
}
11
11
} ) . listen ( 0 , ( ) => {
12
12
const port = server . address ( ) . port
13
- process . send ( { port } )
13
+ if ( process . send ) {
14
+ process . send ( { port } )
15
+ }
14
16
} )
Original file line number Diff line number Diff line change @@ -7,5 +7,7 @@ const server = http2.createServer((req, res) => {
7
7
8
8
server . listen ( 0 , ( ) => {
9
9
const port = server . address ( ) . port
10
- process . send ( { port } )
10
+ if ( process . send ) {
11
+ process . send ( { port } )
12
+ }
11
13
} )
You can’t perform that action at this time.
0 commit comments