Skip to content

Commit b13a645

Browse files
committed
Auto merge of #4115 - Turbo87:sentry-metadata, r=JohnTitor
Sentry: Improve metadata collection The `request.id` tag and the "custom metadata" are currently only assigned to the Sentry scope in the `after()` method of the `LogRequest` middleware. This means that for any manual `sentry::capture_exception()` call these fields will not be set correctly. This PR moves the `configure_scope()` calls into the `before()` method and `add_custom_metadata()` function instead. This allows us to send the fields to Sentry for error reports as soon as they are available.
2 parents 11418ba + a2945a4 commit b13a645

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

src/middleware/log_request.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ impl Middleware for LogRequests {
2121
fn before(&self, req: &mut dyn RequestExt) -> BeforeResult {
2222
let path = OriginalPath(req.path().to_string());
2323
req.mut_extensions().insert(path);
24+
25+
if let Some(request_id) = req
26+
.headers()
27+
.get("x-request-id")
28+
.and_then(|value| value.to_str().ok())
29+
{
30+
sentry::configure_scope(|scope| scope.set_tag("request.id", request_id));
31+
}
32+
2433
Ok(())
2534
}
2635

@@ -86,6 +95,8 @@ pub fn add_custom_metadata<V: Display>(req: &mut dyn RequestExt, key: &'static s
8695
metadata.entries.push((key, value.to_string()));
8796
req.mut_extensions().insert(metadata);
8897
}
98+
99+
sentry::configure_scope(|scope| scope.set_extra(key, value.to_string().into()));
89100
}
90101

91102
fn report_to_sentry(req: &dyn RequestExt, res: &AfterResult, response_time: u64) {
@@ -101,14 +112,6 @@ fn report_to_sentry(req: &dyn RequestExt, res: &AfterResult, response_time: u64)
101112
scope.set_user(Some(user));
102113
}
103114

104-
if let Some(request_id) = req
105-
.headers()
106-
.get("x-request-id")
107-
.and_then(|value| value.to_str().ok())
108-
{
109-
scope.set_tag("request.id", request_id);
110-
}
111-
112115
{
113116
let status = res
114117
.as_ref()
@@ -119,12 +122,6 @@ fn report_to_sentry(req: &dyn RequestExt, res: &AfterResult, response_time: u64)
119122
}
120123

121124
scope.set_extra("Response time [ms]", response_time.into());
122-
123-
if let Some(metadata) = req.extensions().find::<CustomMetadata>() {
124-
for (key, value) in &metadata.entries {
125-
scope.set_extra(key, value.to_string().into());
126-
}
127-
}
128125
});
129126
}
130127

0 commit comments

Comments
 (0)