@@ -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:: response_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
@@ -34,53 +34,14 @@ impl Middleware for LogRequests {
34
34
}
35
35
36
36
fn after ( & self , req : & mut dyn RequestExt , res : AfterResult ) -> AfterResult {
37
- let response_time =
38
- if let Ok ( start_ms) = request_header ( req, "x-request-start" ) . parse :: < u128 > ( ) {
39
- let current_ms = SystemTime :: now ( )
40
- . duration_since ( UNIX_EPOCH )
41
- . expect ( "Time went way backwards" )
42
- . as_millis ( ) ;
43
-
44
- if current_ms > start_ms {
45
- // The result cannot be negative
46
- current_ms - start_ms
47
- } else {
48
- // Because our nginx proxy and app run on the same dyno in production, we
49
- // shouldn't have to worry about clock drift. But if something goes wrong,
50
- // calculate the response time based on when the request reached this app.
51
- fallback_response_time ( req)
52
- }
53
- } else {
54
- // X-Request-Start header couldn't be parsed.
55
- // We are probably running locally and not behind nginx.
56
- fallback_response_time ( req)
57
- } ;
58
-
59
- // This will only trucate for requests lasting > 500 million years
60
- let response_time = response_time as u64 ;
61
-
62
- println ! (
63
- "{}" ,
64
- RequestLine {
65
- req,
66
- res: & res,
67
- response_time,
68
- }
69
- ) ;
37
+ println ! ( "{}" , RequestLine { req, res: & res } ) ;
70
38
71
- report_to_sentry ( req, & res, response_time ) ;
39
+ report_to_sentry ( req, & res) ;
72
40
73
41
res
74
42
}
75
43
}
76
44
77
- /// Calculate the response time based on when the request reached the in-app web server.
78
- ///
79
- /// This serves as a fallback in case the `X-Request-Start` header is missing or invalid.
80
- fn fallback_response_time ( req : & mut dyn RequestExt ) -> u128 {
81
- req. elapsed ( ) . as_millis ( )
82
- }
83
-
84
45
struct CustomMetadata {
85
46
entries : Vec < ( & ' static str , String ) > ,
86
47
}
@@ -99,7 +60,7 @@ pub fn add_custom_metadata<V: Display>(req: &mut dyn RequestExt, key: &'static s
99
60
sentry:: configure_scope ( |scope| scope. set_extra ( key, value. to_string ( ) . into ( ) ) ) ;
100
61
}
101
62
102
- fn report_to_sentry ( req : & dyn RequestExt , res : & AfterResult , response_time : u64 ) {
63
+ fn report_to_sentry ( req : & dyn RequestExt , res : & AfterResult ) {
103
64
sentry:: configure_scope ( |scope| {
104
65
{
105
66
let id = req. session ( ) . get ( "user_id" ) . map ( |str| str. to_string ( ) ) ;
@@ -121,7 +82,9 @@ fn report_to_sentry(req: &dyn RequestExt, res: &AfterResult, response_time: u64)
121
82
scope. set_tag ( "response.status" , status. as_str ( ) ) ;
122
83
}
123
84
124
- scope. set_extra ( "Response time [ms]" , response_time. into ( ) ) ;
85
+ if let Some ( response_time) = req. extensions ( ) . find :: < ResponseTime > ( ) {
86
+ scope. set_extra ( "Response time [ms]" , response_time. as_millis ( ) . into ( ) ) ;
87
+ }
125
88
} ) ;
126
89
}
127
90
@@ -139,7 +102,6 @@ pub(crate) fn get_log_message(req: &dyn RequestExt, key: &'static str) -> String
139
102
struct RequestLine < ' r > {
140
103
req : & ' r dyn RequestExt ,
141
104
res : & ' r AfterResult ,
142
- response_time : u64 ,
143
105
}
144
106
145
107
impl Display for RequestLine < ' _ > {
@@ -172,7 +134,11 @@ impl Display for RequestLine<'_> {
172
134
}
173
135
174
136
line. add_quoted_field ( "fwd" , request_header ( self . req , "x-real-ip" ) ) ?;
175
- line. add_field ( "service" , TimeMs ( self . response_time ) ) ?;
137
+
138
+ let response_time = self . req . extensions ( ) . find :: < ResponseTime > ( ) ;
139
+ if let Some ( response_time) = response_time {
140
+ line. add_field ( "service" , response_time) ?;
141
+ }
176
142
line. add_field ( "status" , status. as_str ( ) ) ?;
177
143
line. add_quoted_field ( "user_agent" , request_header ( self . req , header:: USER_AGENT ) ) ?;
178
144
@@ -186,8 +152,10 @@ impl Display for RequestLine<'_> {
186
152
line. add_quoted_field ( "error" , err) ?;
187
153
}
188
154
189
- if self . response_time > SLOW_REQUEST_THRESHOLD_MS {
190
- line. add_marker ( "SLOW REQUEST" ) ?;
155
+ if let Some ( response_time) = response_time {
156
+ if response_time. as_millis ( ) > SLOW_REQUEST_THRESHOLD_MS {
157
+ line. add_marker ( "SLOW REQUEST" ) ?;
158
+ }
191
159
}
192
160
193
161
Ok ( ( ) )
@@ -211,16 +179,6 @@ impl<'a> Display for FullPath<'a> {
211
179
}
212
180
}
213
181
214
- struct TimeMs ( u64 ) ;
215
-
216
- impl Display for TimeMs {
217
- fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
218
- self . 0 . fmt ( f) ?;
219
- f. write_str ( "ms" ) ?;
220
- Ok ( ( ) )
221
- }
222
- }
223
-
224
182
struct LogLine < ' f , ' g > {
225
183
f : & ' f mut Formatter < ' g > ,
226
184
first : bool ,
0 commit comments