4
4
use super :: prelude:: * ;
5
5
use crate :: util:: request_header;
6
6
7
- use conduit:: { header, Host , RequestExt , Scheme , StatusCode } ;
8
- use conduit_cookie:: RequestSession ;
9
- use sentry:: Level ;
7
+ use conduit:: { header, RequestExt , StatusCode } ;
10
8
11
9
use crate :: middleware:: normalize_path:: OriginalPath ;
12
10
use crate :: middleware:: request_timing:: ResponseTime ;
13
11
use std:: fmt:: { self , Display , Formatter } ;
14
12
15
13
const SLOW_REQUEST_THRESHOLD_MS : u64 = 1000 ;
16
14
17
- const FILTERED_HEADERS : & [ & str ] = & [ "Authorization" , "Cookie" , "X-Real-Ip" , "X-Forwarded-For" ] ;
18
-
19
15
#[ derive( Default ) ]
20
16
pub ( super ) struct LogRequests ( ) ;
21
17
@@ -32,14 +28,12 @@ impl Middleware for LogRequests {
32
28
}
33
29
) ;
34
30
35
- report_to_sentry ( req, & res, response_time. as_millis ( ) ) ;
36
-
37
31
res
38
32
}
39
33
}
40
34
41
- struct CustomMetadata {
42
- entries : Vec < ( & ' static str , String ) > ,
35
+ pub struct CustomMetadata {
36
+ pub entries : Vec < ( & ' static str , String ) > ,
43
37
}
44
38
45
39
pub fn add_custom_metadata < V : Display > ( req : & mut dyn RequestExt , key : & ' static str , value : V ) {
@@ -54,92 +48,6 @@ pub fn add_custom_metadata<V: Display>(req: &mut dyn RequestExt, key: &'static s
54
48
}
55
49
}
56
50
57
- fn report_to_sentry ( req : & dyn RequestExt , res : & AfterResult , response_time : u64 ) {
58
- let ( message, level) = match res {
59
- Err ( e) => ( e. to_string ( ) , Level :: Error ) ,
60
- Ok ( _) => return ,
61
- } ;
62
-
63
- let config = |scope : & mut sentry:: Scope | {
64
- let method = Some ( req. method ( ) . as_str ( ) . to_owned ( ) ) ;
65
-
66
- let scheme = match req. scheme ( ) {
67
- Scheme :: Http => "http" ,
68
- Scheme :: Https => "https" ,
69
- } ;
70
-
71
- let host = match req. host ( ) {
72
- Host :: Name ( name) => name. to_owned ( ) ,
73
- Host :: Socket ( addr) => addr. to_string ( ) ,
74
- } ;
75
-
76
- let path = & req. extensions ( ) . find :: < OriginalPath > ( ) . unwrap ( ) . 0 ;
77
-
78
- let url = format ! ( "{}://{}{}" , scheme, host, path) . parse ( ) . ok ( ) ;
79
-
80
- {
81
- let id = req. session ( ) . get ( "user_id" ) . map ( |str| str. to_string ( ) ) ;
82
-
83
- let user = sentry:: User {
84
- id,
85
- ..Default :: default ( )
86
- } ;
87
-
88
- scope. set_user ( Some ( user) ) ;
89
- }
90
-
91
- {
92
- let headers = req
93
- . headers ( )
94
- . iter ( )
95
- . filter ( |( k, _v) | !FILTERED_HEADERS . iter ( ) . any ( |name| k == name) )
96
- . map ( |( k, v) | ( k. to_string ( ) , v. to_str ( ) . unwrap_or_default ( ) . to_string ( ) ) )
97
- . collect ( ) ;
98
-
99
- let sentry_req = sentry:: protocol:: Request {
100
- url,
101
- method,
102
- headers,
103
- ..Default :: default ( )
104
- } ;
105
-
106
- scope. add_event_processor ( Box :: new ( move |mut event| {
107
- if event. request . is_none ( ) {
108
- event. request = Some ( sentry_req. clone ( ) ) ;
109
- }
110
- Some ( event)
111
- } ) ) ;
112
- }
113
-
114
- if let Some ( request_id) = req
115
- . headers ( )
116
- . get ( "x-request-id" )
117
- . and_then ( |value| value. to_str ( ) . ok ( ) )
118
- {
119
- scope. set_tag ( "request.id" , request_id) ;
120
- }
121
-
122
- {
123
- let status = res
124
- . as_ref ( )
125
- . map ( |resp| resp. status ( ) )
126
- . unwrap_or ( StatusCode :: INTERNAL_SERVER_ERROR ) ;
127
-
128
- scope. set_tag ( "response.status" , status. as_str ( ) ) ;
129
- }
130
-
131
- scope. set_extra ( "Response time [ms]" , response_time. into ( ) ) ;
132
-
133
- if let Some ( metadata) = req. extensions ( ) . find :: < CustomMetadata > ( ) {
134
- for ( key, value) in & metadata. entries {
135
- scope. set_extra ( key, value. to_string ( ) . into ( ) ) ;
136
- }
137
- }
138
- } ;
139
-
140
- sentry:: with_scope ( config, || sentry:: capture_message ( & message, level) ) ;
141
- }
142
-
143
51
#[ cfg( test) ]
144
52
pub ( crate ) fn get_log_message ( req : & dyn RequestExt , key : & ' static str ) -> String {
145
53
// Unwrap shouldn't panic as no other code has access to the private struct to remove it
0 commit comments