Skip to content

Commit

Permalink
feat: Added more compliant breadcrumb interface as per protocol spec v7
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Mar 22, 2018
1 parent 6da8d69 commit 8846c23
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ serde_derive = "1.0"
serde_json = "1.0"
serde_plain = "0.3.0"
url_serde = "0.2.0"
chrono = { version = "0.4.0", features = ["serde"] }

[patch.crates-io]
serde = { git = "https://github.com/mitsuhiko/serde", branch = "feature/flatten" }
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! Sentry server. It's used by the sentry relay infrastructure as well as the
//! rust Sentry client/.
#![warn(missing_docs)]
extern crate chrono;
extern crate failure;
#[macro_use]
extern crate failure_derive;
Expand Down
71 changes: 66 additions & 5 deletions src/protocol/v7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
use std::collections::HashMap;
use std::net::IpAddr;

use chrono;
use chrono::{DateTime, Utc};
use url_serde;
use url::Url;
use serde::de::{Deserialize, Deserializer, Error as DeError};
Expand Down Expand Up @@ -47,14 +49,72 @@ pub struct SingleException {
pub stacktrace: Option<Stacktrace>,
}

#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[serde(rename_all = "lowercase")]
pub enum Level {
Debug,
Info,
Warning,
Error,
Critical,
}

impl Default for Level {
fn default() -> Level {
Level::Info
}
}

impl Level {
/// A quick way to check if the level is `debug`.
pub fn is_debug(&self) -> bool {
*self == Level::Debug
}

/// A quick way to check if the level is `info`.
pub fn is_info(&self) -> bool {
*self == Level::Info
}

/// A quick way to check if the level is `warning`.
pub fn is_warning(&self) -> bool {
*self == Level::Warning
}

/// A quick way to check if the level is `error`.
pub fn is_error(&self) -> bool {
*self == Level::Error
}

/// A quick way to check if the level is `critical`.
pub fn is_critical(&self) -> bool {
*self == Level::Critical
}
}

/// Represents a single breadcrumb
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(default)]
pub struct Breadcrumb {
pub timestamp: f64,
#[serde(with = "chrono::serde::ts_seconds")] pub timestamp: DateTime<Utc>,
#[serde(rename = "type")] pub ty: String,
pub message: String,
pub category: String,
#[serde(flatten)] pub data: HashMap<String, Value>,
#[serde(skip_serializing_if = "Option::is_none")] pub category: Option<String>,
#[serde(skip_serializing_if = "Level::is_info")] pub level: Level,
#[serde(skip_serializing_if = "Option::is_none")] pub message: Option<String>,
#[serde(skip_serializing_if = "HashMap::is_empty")] pub data: HashMap<String, Value>,
}

impl Default for Breadcrumb {
fn default() -> Breadcrumb {
Breadcrumb {
timestamp: Utc::now(),
ty: "default".into(),
category: None,
level: Default::default(),
message: None,
data: HashMap::new(),
}
}
}

/// Represents user info.
Expand Down Expand Up @@ -89,6 +149,7 @@ pub struct Request {
pub struct Event {
#[serde(skip_serializing_if = "Option::is_none")] pub level: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")] pub fingerprint: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")] pub message: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")] pub logentry: Option<LogEntry>,
#[serde(skip_serializing_if = "Option::is_none")] pub platform: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")] pub timestamp: Option<f64>,
Expand Down

0 comments on commit 8846c23

Please sign in to comment.