Skip to content
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

feat: Basic attachment support #466

Merged
merged 3 commits into from May 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion sentry-types/src/protocol/attachment.rs
Expand Up @@ -37,7 +37,7 @@ impl AttachmentType {
}
}

#[derive(Clone, PartialEq)]
#[derive(Clone, PartialEq, Default)]
/// Represents an attachment item.
pub struct Attachment {
/// The actual attachment data.
Expand Down Expand Up @@ -80,6 +80,7 @@ impl fmt::Debug for Attachment {
f.debug_struct("Attachment")
.field("buffer", &self.buffer.len())
.field("filename", &self.filename)
.field("content_type", &self.content_type)
.field("type", &self.ty)
.finish()
}
Expand Down
28 changes: 28 additions & 0 deletions sentry-types/src/protocol/envelope.rs
Expand Up @@ -358,6 +358,34 @@ mod test {
r#"{"event_id":"22d00b3f-d1b1-4b5d-8d20-49d138cd8a9c"}
{"type":"transaction","length":200}
{"event_id":"22d00b3fd1b14b5d8d2049d138cd8a9c","start_timestamp":1595256674.296,"spans":[{"span_id":"d42cee9fc3e74f5c","trace_id":"335e53d614474acc9f89e632b776cc28","start_timestamp":1595256674.296}]}
"#
)
}

#[test]
fn test_event_with_attachment() {
let event_id = Uuid::parse_str("22d00b3f-d1b1-4b5d-8d20-49d138cd8a9c").unwrap();
let timestamp = timestamp("2020-07-20T14:51:14.296Z");
let event = Event {
event_id,
timestamp,
..Default::default()
};
let mut envelope: Envelope = event.into();

envelope.add_item(Attachment {
buffer: "some content".as_bytes().to_vec(),
filename: "file.txt".to_string(),
..Default::default()
});

assert_eq!(
to_str(envelope),
r#"{"event_id":"22d00b3f-d1b1-4b5d-8d20-49d138cd8a9c"}
{"type":"event","length":74}
{"event_id":"22d00b3fd1b14b5d8d2049d138cd8a9c","timestamp":1595256674.296}
{"type":"attachment","length":12,"filename":"file.txt","attachment_type":"event.attachment","content_type":"application/octet-stream"}
some content
"#
)
}
Expand Down
53 changes: 53 additions & 0 deletions sentry/tests/test_basic.rs
Expand Up @@ -3,6 +3,7 @@
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

use sentry::protocol::{Attachment, EnvelopeItem};
use sentry::types::Uuid;

#[test]
Expand Down Expand Up @@ -173,3 +174,55 @@ fn test_attached_stacktrace() {
.flat_map(|ev| ev.threads.into_iter().filter_map(|thrd| thrd.stacktrace));
assert_eq!(stacktraces.count(), 3);
}

#[test]
fn test_attachment_sent_from_scope() {
struct TestTransport(Arc<AtomicUsize>);

impl sentry::Transport for TestTransport {
fn send_envelope(&self, envelope: sentry::Envelope) {
timfish marked this conversation as resolved.
Show resolved Hide resolved
for item in envelope.items() {
if let EnvelopeItem::Attachment(attachment) = item {
assert_eq!(attachment.filename, "test-file.txt");
assert_eq!(attachment.buffer, vec![1, 2, 3, 4, 5, 6, 7, 8, 9]);
self.0.fetch_add(1, Ordering::SeqCst);
}
}
}
}

let events = Arc::new(AtomicUsize::new(0));

let events_for_options = events.clone();
let options = sentry::ClientOptions {
dsn: "http://foo@example.com/42".parse().ok(),
transport: Some(Arc::new(
move |opts: &sentry::ClientOptions| -> Arc<dyn sentry::Transport> {
assert_eq!(opts.dsn.as_ref().unwrap().host(), "example.com");
Arc::new(TestTransport(events_for_options.clone()))
},
)),
..Default::default()
};

sentry::Hub::run(
Arc::new(sentry::Hub::new(
Some(Arc::new(options.into())),
Arc::new(Default::default()),
)),
|| {
sentry::with_scope(
|scope| {
scope.add_attachment(Attachment {
buffer: vec![1, 2, 3, 4, 5, 6, 7, 8, 9],
filename: "test-file.txt".to_string(),
..Default::default()
})
},
|| sentry::capture_message("test", sentry::Level::Error),
)
},
);

assert_eq!(events.load(Ordering::SeqCst), 1);
}