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
7 changes: 7 additions & 0 deletions sentry-core/src/client.rs
Expand Up @@ -278,6 +278,13 @@ impl Client {
envelope.add_item(session_item);
}
}

if let Some(scope) = scope {
for attachment in scope.attachments.iter().cloned() {
envelope.add_item(attachment);
}
}

transport.send_envelope(envelope);
return event_id;
}
Expand Down
13 changes: 12 additions & 1 deletion sentry-core/src/scope/real.rs
Expand Up @@ -4,7 +4,7 @@ use std::fmt;
use std::sync::{Arc, Mutex, PoisonError, RwLock};

use crate::performance::TransactionOrSpan;
use crate::protocol::{Breadcrumb, Context, Event, Level, User, Value};
use crate::protocol::{Attachment, Breadcrumb, Context, Event, Level, User, Value};
use crate::session::Session;
use crate::Client;

Expand Down Expand Up @@ -46,6 +46,7 @@ pub struct Scope {
pub(crate) event_processors: Arc<Vec<EventProcessor>>,
pub(crate) session: Arc<Mutex<Option<Session>>>,
pub(crate) span: Arc<Option<TransactionOrSpan>>,
pub(crate) attachments: Arc<Vec<Attachment>>,
}

impl fmt::Debug for Scope {
Expand Down Expand Up @@ -218,6 +219,16 @@ impl Scope {
Arc::make_mut(&mut self.event_processors).push(Arc::new(f));
}

/// Adds an attachment to the scope
pub fn add_attachment(&mut self, attachment: Attachment) {
Arc::make_mut(&mut self.attachments).push(attachment);
}

/// Clears attachments from the scope
pub fn clear_attachments(&mut self) {
Arc::make_mut(&mut self.attachments).clear();
}

/// Applies the contained scoped data to fill an event.
pub fn apply_to_event(&self, mut event: Event<'static>) -> Option<Event<'static>> {
// TODO: event really should have an optional level
Expand Down
10 changes: 8 additions & 2 deletions sentry-types/src/protocol/attachment.rs
Expand Up @@ -44,6 +44,8 @@ pub struct Attachment {
pub buffer: Vec<u8>,
/// The filename of the attachment.
pub filename: String,
/// The Content Type of the attachment
pub content_type: Option<String>,
/// The special type of this attachment.
pub ty: Option<AttachmentType>,
}
Expand All @@ -56,10 +58,14 @@ impl Attachment {
{
writeln!(
writer,
r#"{{"type":"attachment","length":{length},"filename":"{filename}","attachment_type":"{at}"}}"#,
r#"{{"type":"attachment","length":{length},"filename":"{filename}","attachment_type":"{at}","content_type":"{ct}"}}"#,
filename = self.filename,
length = self.buffer.len(),
at = self.ty.unwrap_or_default().as_str()
at = self.ty.unwrap_or_default().as_str(),
ct = self
.content_type
.as_ref()
.unwrap_or(&"application/octet-stream".to_string())
)?;

writer.write_all(&self.buffer)?;
Expand Down
6 changes: 6 additions & 0 deletions sentry-types/src/protocol/envelope.rs
Expand Up @@ -65,6 +65,12 @@ impl From<Transaction<'static>> for EnvelopeItem {
}
}

impl From<Attachment> for EnvelopeItem {
fn from(attachment: Attachment) -> Self {
EnvelopeItem::Attachment(attachment)
}
}

/// An Iterator over the items of an Envelope.
#[derive(Clone)]
pub struct EnvelopeItemIter<'s> {
Expand Down