@@ -8,8 +8,8 @@ use conduit::{header, Host, RequestExt, Scheme, StatusCode};
8
8
use conduit_cookie:: RequestSession ;
9
9
use sentry:: Level ;
10
10
11
+ use crate :: middleware:: request_timing:: ResponseTime ;
11
12
use std:: fmt:: { self , Display , Formatter } ;
12
- use std:: time:: { SystemTime , UNIX_EPOCH } ;
13
13
14
14
const SLOW_REQUEST_THRESHOLD_MS : u64 = 1000 ;
15
15
@@ -28,30 +28,7 @@ impl Middleware for LogRequests {
28
28
}
29
29
30
30
fn after ( & self , req : & mut dyn RequestExt , res : AfterResult ) -> AfterResult {
31
- let response_time =
32
- if let Ok ( start_ms) = request_header ( req, "x-request-start" ) . parse :: < u128 > ( ) {
33
- let current_ms = SystemTime :: now ( )
34
- . duration_since ( UNIX_EPOCH )
35
- . expect ( "Time went way backwards" )
36
- . as_millis ( ) ;
37
-
38
- if current_ms > start_ms {
39
- // The result cannot be negative
40
- current_ms - start_ms
41
- } else {
42
- // Because our nginx proxy and app run on the same dyno in production, we
43
- // shouldn't have to worry about clock drift. But if something goes wrong,
44
- // calculate the response time based on when the request reached this app.
45
- fallback_response_time ( req)
46
- }
47
- } else {
48
- // X-Request-Start header couldn't be parsed.
49
- // We are probably running locally and not behind nginx.
50
- fallback_response_time ( req)
51
- } ;
52
-
53
- // This will only trucate for requests lasting > 500 million years
54
- let response_time = response_time as u64 ;
31
+ let response_time = req. extensions ( ) . find :: < ResponseTime > ( ) . unwrap ( ) ;
55
32
56
33
println ! (
57
34
"{}" ,
@@ -62,19 +39,12 @@ impl Middleware for LogRequests {
62
39
}
63
40
) ;
64
41
65
- report_to_sentry ( req, & res, response_time) ;
42
+ report_to_sentry ( req, & res, response_time. as_millis ( ) ) ;
66
43
67
44
res
68
45
}
69
46
}
70
47
71
- /// Calculate the response time based on when the request reached the in-app web server.
72
- ///
73
- /// This serves as a fallback in case the `X-Request-Start` header is missing or invalid.
74
- fn fallback_response_time ( req : & mut dyn RequestExt ) -> u128 {
75
- req. elapsed ( ) . as_millis ( )
76
- }
77
-
78
48
struct CustomMetadata {
79
49
entries : Vec < ( & ' static str , String ) > ,
80
50
}
@@ -191,7 +161,7 @@ pub(crate) fn get_log_message(req: &dyn RequestExt, key: &'static str) -> String
191
161
struct RequestLine < ' r > {
192
162
req : & ' r dyn RequestExt ,
193
163
res : & ' r AfterResult ,
194
- response_time : u64 ,
164
+ response_time : & ' r ResponseTime ,
195
165
}
196
166
197
167
impl Display for RequestLine < ' _ > {
@@ -224,7 +194,7 @@ impl Display for RequestLine<'_> {
224
194
}
225
195
226
196
line. add_quoted_field ( "fwd" , request_header ( self . req , "x-real-ip" ) ) ?;
227
- line. add_field ( "service" , TimeMs ( self . response_time ) ) ?;
197
+ line. add_field ( "service" , self . response_time ) ?;
228
198
line. add_field ( "status" , status. as_str ( ) ) ?;
229
199
line. add_quoted_field ( "user_agent" , request_header ( self . req , header:: USER_AGENT ) ) ?;
230
200
@@ -238,7 +208,7 @@ impl Display for RequestLine<'_> {
238
208
line. add_quoted_field ( "error" , err) ?;
239
209
}
240
210
241
- if self . response_time > SLOW_REQUEST_THRESHOLD_MS {
211
+ if self . response_time . as_millis ( ) > SLOW_REQUEST_THRESHOLD_MS {
242
212
line. add_marker ( "SLOW REQUEST" ) ?;
243
213
}
244
214
@@ -263,16 +233,6 @@ impl<'a> Display for FullPath<'a> {
263
233
}
264
234
}
265
235
266
- struct TimeMs ( u64 ) ;
267
-
268
- impl Display for TimeMs {
269
- fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
270
- self . 0 . fmt ( f) ?;
271
- f. write_str ( "ms" ) ?;
272
- Ok ( ( ) )
273
- }
274
- }
275
-
276
236
struct LogLine < ' f , ' g > {
277
237
f : & ' f mut Formatter < ' g > ,
278
238
first : bool ,
0 commit comments