Skip to content

feat(types): add Response context #874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# Changelog

## Unreleased

## Features

- feat(core): add Response context ([#874](https://github.com/getsentry/sentry-rust/pull/874)) by @lcian
- The `Response` context can now be attached to events, to include information about HTTP responses such as headers, cookies and status code.
- Example:
```rust
let mut event = Event::new();
let response = ResponseContext {
cookies: Some(r#""csrftoken": "1234567""#.to_owned()),
headers: Some(headers_map),
status_code: Some(500),
body_size: Some(15),
data: Some("Invalid request"),
};
event
.contexts
.insert("response".to_owned(), response.into());
```

## 0.42.0

### Features
Expand Down
29 changes: 28 additions & 1 deletion sentry-types/src/protocol/v7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,8 @@ pub enum Context {
Gpu(Box<GpuContext>),
/// OpenTelemetry data.
Otel(Box<OtelContext>),
/// HTTP response data.
Response(Box<ResponseContext>),
/// Generic other context data.
#[serde(rename = "unknown")]
Other(Map<String, Value>),
Expand All @@ -1117,6 +1119,7 @@ impl Context {
Context::Trace(..) => "trace",
Context::Gpu(..) => "gpu",
Context::Otel(..) => "otel",
Context::Response(..) => "response",
Context::Other(..) => "unknown",
}
}
Expand Down Expand Up @@ -1351,6 +1354,29 @@ pub struct OtelContext {
pub other: Map<String, Value>,
}

/// Holds information about an HTTP response.
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
pub struct ResponseContext {
/// The unparsed cookie values.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cookies: Option<String>,
/// A map of submitted headers.
///
/// If a header appears multiple times, it needs to be merged according to the HTTP standard
/// for header merging. Header names are treated case-insensitively by Sentry.
#[serde(default, skip_serializing_if = "Map::is_empty")]
pub headers: Map<String, String>,
/// The HTTP response status code.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub status_code: Option<u64>,
/// The response body size in bytes.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub body_size: Option<u64>,
/// Response data in any format that makes sense.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
}

/// Holds the identifier for a Span
#[derive(Serialize, Deserialize, Copy, Clone, Eq, PartialEq, Hash)]
#[serde(try_from = "String", into = "String")]
Expand Down Expand Up @@ -1501,9 +1527,10 @@ into_context!(Browser, BrowserContext);
into_context!(Trace, TraceContext);
into_context!(Gpu, GpuContext);
into_context!(Otel, OtelContext);
into_context!(Response, ResponseContext);

const INFERABLE_CONTEXTS: &[&str] = &[
"device", "os", "runtime", "app", "browser", "trace", "gpu", "otel",
"device", "os", "runtime", "app", "browser", "trace", "gpu", "otel", "response",
];

struct ContextsVisitor;
Expand Down
37 changes: 37 additions & 0 deletions sentry-types/tests/test_protocol_v7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,43 @@ mod test_contexts {
);
}

#[test]
fn test_response_context() {
let event = v7::Event {
event_id: event_id(),
timestamp: event_time(),
contexts: {
let mut m = v7::Map::new();
m.insert(
"response".into(),
v7::ResponseContext {
status_code: Some(400),
cookies: Some("sessionId=abc123; Path=/; HttpOnly,authToken=xyz789; Secure; SameSite=Strict".into()),
headers: {
let mut hm = v7::Map::new();
hm.insert("Content-Type".into(), "text/plain".into());
hm
},
body_size: Some(1000),
data: Some("lol".into()),
}
.into(),
);
m
},
..Default::default()
};

assert_roundtrip(&event);
assert_eq!(
serde_json::to_string(&event).unwrap(),
"{\"event_id\":\"d43e86c96e424a93a4fbda156dd17341\",\"timestamp\":1514103120,\
\"contexts\":{\"response\":{\"type\":\"response\",\
\"cookies\":\"sessionId=abc123; Path=/; HttpOnly,authToken=xyz789; Secure; SameSite=Strict\",\
\"headers\":{\"Content-Type\":\"text/plain\"},\"status_code\":400,\"body_size\":1000,\"data\":\"lol\"}}}"
);
}

#[test]
fn test_renamed_contexts() {
let event = v7::Event {
Expand Down
Loading