@@ -7,8 +7,8 @@ use crate::util::request_header;
7
7
use conduit:: { header, RequestExt , StatusCode } ;
8
8
use conduit_cookie:: RequestSession ;
9
9
10
+ use crate :: middleware:: request_timing:: ResponseTime ;
10
11
use std:: fmt:: { self , Display , Formatter } ;
11
- use std:: time:: { SystemTime , UNIX_EPOCH } ;
12
12
13
13
const SLOW_REQUEST_THRESHOLD_MS : u64 = 1000 ;
14
14
@@ -25,30 +25,7 @@ impl Middleware for LogRequests {
25
25
}
26
26
27
27
fn after ( & self , req : & mut dyn RequestExt , res : AfterResult ) -> AfterResult {
28
- let response_time =
29
- if let Ok ( start_ms) = request_header ( req, "x-request-start" ) . parse :: < u128 > ( ) {
30
- let current_ms = SystemTime :: now ( )
31
- . duration_since ( UNIX_EPOCH )
32
- . expect ( "Time went way backwards" )
33
- . as_millis ( ) ;
34
-
35
- if current_ms > start_ms {
36
- // The result cannot be negative
37
- current_ms - start_ms
38
- } else {
39
- // Because our nginx proxy and app run on the same dyno in production, we
40
- // shouldn't have to worry about clock drift. But if something goes wrong,
41
- // calculate the response time based on when the request reached this app.
42
- fallback_response_time ( req)
43
- }
44
- } else {
45
- // X-Request-Start header couldn't be parsed.
46
- // We are probably running locally and not behind nginx.
47
- fallback_response_time ( req)
48
- } ;
49
-
50
- // This will only trucate for requests lasting > 500 million years
51
- let response_time = response_time as u64 ;
28
+ let response_time = req. extensions ( ) . find :: < ResponseTime > ( ) . unwrap ( ) ;
52
29
53
30
println ! (
54
31
"{}" ,
@@ -59,19 +36,12 @@ impl Middleware for LogRequests {
59
36
}
60
37
) ;
61
38
62
- report_to_sentry ( req, & res, response_time) ;
39
+ report_to_sentry ( req, & res, response_time. as_millis ( ) ) ;
63
40
64
41
res
65
42
}
66
43
}
67
44
68
- /// Calculate the response time based on when the request reached the in-app web server.
69
- ///
70
- /// This serves as a fallback in case the `X-Request-Start` header is missing or invalid.
71
- fn fallback_response_time ( req : & mut dyn RequestExt ) -> u128 {
72
- req. elapsed ( ) . as_millis ( )
73
- }
74
-
75
45
struct CustomMetadata {
76
46
entries : Vec < ( & ' static str , String ) > ,
77
47
}
@@ -142,7 +112,7 @@ pub(crate) fn get_log_message(req: &dyn RequestExt, key: &'static str) -> String
142
112
struct RequestLine < ' r > {
143
113
req : & ' r dyn RequestExt ,
144
114
res : & ' r AfterResult ,
145
- response_time : u64 ,
115
+ response_time : & ' r ResponseTime ,
146
116
}
147
117
148
118
impl Display for RequestLine < ' _ > {
@@ -175,7 +145,7 @@ impl Display for RequestLine<'_> {
175
145
}
176
146
177
147
line. add_quoted_field ( "fwd" , request_header ( self . req , "x-real-ip" ) ) ?;
178
- line. add_field ( "service" , TimeMs ( self . response_time ) ) ?;
148
+ line. add_field ( "service" , self . response_time ) ?;
179
149
line. add_field ( "status" , status. as_str ( ) ) ?;
180
150
line. add_quoted_field ( "user_agent" , request_header ( self . req , header:: USER_AGENT ) ) ?;
181
151
@@ -189,7 +159,7 @@ impl Display for RequestLine<'_> {
189
159
line. add_quoted_field ( "error" , err) ?;
190
160
}
191
161
192
- if self . response_time > SLOW_REQUEST_THRESHOLD_MS {
162
+ if self . response_time . as_millis ( ) > SLOW_REQUEST_THRESHOLD_MS {
193
163
line. add_marker ( "SLOW REQUEST" ) ?;
194
164
}
195
165
@@ -214,16 +184,6 @@ impl<'a> Display for FullPath<'a> {
214
184
}
215
185
}
216
186
217
- struct TimeMs ( u64 ) ;
218
-
219
- impl Display for TimeMs {
220
- fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
221
- self . 0 . fmt ( f) ?;
222
- f. write_str ( "ms" ) ?;
223
- Ok ( ( ) )
224
- }
225
- }
226
-
227
187
struct LogLine < ' f , ' g > {
228
188
f : & ' f mut Formatter < ' g > ,
229
189
first : bool ,
0 commit comments