Skip to content

Commit

Permalink
Add ability to send an Event as Raw Envelope (#1582)
Browse files Browse the repository at this point in the history
  • Loading branch information
Swatinem authored Apr 20, 2023
1 parent 4c245f1 commit fc38b84
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 31 deletions.
24 changes: 12 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ regex = "1.7.1"
runas = "1.0.0"
rust-ini = "0.18.0"
semver = "1.0.16"
sentry = { version = "0.29.3", default-features = false, features = [
sentry = { version = "0.31.0", default-features = false, features = [
"anyhow",
"curl",
"contexts",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/send_envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn make_command(command: Command) -> Command {
)
}

fn send_raw_envelope(envelope: Envelope, dsn: Dsn) {
pub fn send_raw_envelope(envelope: Envelope, dsn: Dsn) {
debug!("{:?}", envelope);
with_sentry_client(dsn, |c| c.send_envelope(envelope));
}
Expand Down
44 changes: 36 additions & 8 deletions src/commands/send_event.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::borrow::Cow;
use std::env;
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;
use std::time::SystemTime;

Expand All @@ -13,9 +11,11 @@ use itertools::Itertools;
use log::{debug, warn};
use sentry::protocol::{Event, Level, LogEntry, User};
use sentry::types::{Dsn, Uuid};
use sentry::Envelope;
use serde_json::Value;
use username::get_user_name;

use crate::commands::send_envelope::send_raw_envelope;
use crate::config::Config;
use crate::utils::args::{get_timestamp, validate_distribution};
use crate::utils::event::{attach_logfile, get_sdk_info, with_sentry_client};
Expand All @@ -36,6 +36,12 @@ pub fn make_command(command: Command) -> Command {
.required(false)
.help("The path or glob to the file(s) in JSON format to send as event(s). When provided, all other arguments are ignored."),
)
.arg(
Arg::new("raw")
.long("raw")
.action(ArgAction::SetTrue)
.help("Send events using an envelope without attempting to parse their contents."),
)
.arg(
Arg::new("level")
.value_name("LEVEL")
Expand Down Expand Up @@ -161,6 +167,7 @@ fn send_raw_event(event: Event<'static>, dsn: Dsn) -> Uuid {
pub fn execute(matches: &ArgMatches) -> Result<()> {
let config = Config::current();
let dsn = config.get_dsn()?;
let raw = matches.get_flag("raw");

if let Some(path) = matches.get_one::<String>("path") {
let collected_paths: Vec<PathBuf> = glob_with(path, MatchOptions::new())
Expand All @@ -174,12 +181,33 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
}

for path in collected_paths {
let p = path.as_path();
let file = File::open(p)?;
let reader = BufReader::new(file);
let event: Event = serde_json::from_reader(reader)?;
let id = send_raw_event(event, dsn.clone());
println!("Event from file {} dispatched: {}", p.display(), id);
let raw_event = std::fs::read(&path)?;

let id = if raw {
use std::io::Write;

// Its a bit unfortunate that we still need to parse the whole JSON,
// but envelopes need an `event_id`, which we also want to report.
let json: Value = serde_json::from_slice(&raw_event)?;
let id = json
.as_object()
.and_then(|event| event.get("event_id"))
.and_then(|val| val.as_str())
.and_then(|id| Uuid::parse_str(id).ok())
.unwrap_or_default();
let mut buf = Vec::new();
writeln!(buf, r#"{{"event_id":"{id}"}}"#)?;
writeln!(buf, r#"{{"type":"event","length":{}}}"#, raw_event.len())?;
buf.extend(raw_event);
let envelope = Envelope::from_bytes_raw(buf)?;
send_raw_envelope(envelope, dsn.clone());
id
} else {
let event: Event = serde_json::from_slice(&raw_event)?;
send_raw_event(event, dsn.clone())
};

println!("Event from file {} dispatched: {}", path.display(), id);
}

return Ok(());
Expand Down
21 changes: 12 additions & 9 deletions tests/integration/_cases/send_event/send_event-help.trycmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,45 @@ Arguments:
other arguments are ignored.

Options:
-l, --level <LEVEL>
Optional event severity/log level. (debug|info|warning|error|fatal) [defaults to 'error']
--raw
Send events using an envelope without attempting to parse their contents.

--header <KEY:VALUE>
Custom headers that should be attached to all requests
in key:value format.

--timestamp <TIMESTAMP>
Optional event timestamp in one of supported formats: unix timestamp, RFC2822 or RFC3339.
-l, --level <LEVEL>
Optional event severity/log level. (debug|info|warning|error|fatal) [defaults to 'error']

--auth-token <AUTH_TOKEN>
Use the given Sentry auth token.

--timestamp <TIMESTAMP>
Optional event timestamp in one of supported formats: unix timestamp, RFC2822 or RFC3339.

-r, --release <RELEASE>
Optional identifier of the release.

-d, --dist <DISTRIBUTION>
Set the distribution.

-E, --env <ENVIRONMENT>
Send with a specific environment.

--log-level <LOG_LEVEL>
Set the log output verbosity.

[possible values: trace, debug, info, warn, error]

--no-environ
Do not send environment variables along
-E, --env <ENVIRONMENT>
Send with a specific environment.

--quiet
Do not print any output while preserving correct exit code. This flag is currently
implemented only for selected subcommands.

[aliases: silent]

--no-environ
Do not send environment variables along

-m, --message <MESSAGE>
The event message.

Expand Down

0 comments on commit fc38b84

Please sign in to comment.