Skip to content

Commit a031cfd

Browse files
ref(send-event): Introduce constant for max breadcrumbs (#2716)
While investigating #2709, I noticed the max breadcrumbs limit was hardcoded in multiple locations. This change extracts the value into a constant, so it is easier to change later, if we ever need to.
1 parent 3728f6f commit a031cfd

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

src/commands/bash_hook.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use crate::config::Config;
1919
use crate::utils::event::{attach_logfile, get_sdk_info};
2020
use crate::utils::releases::detect_release_name;
2121

22+
const MAX_BREADCRUMBS: usize = 100;
23+
2224
const BASH_SCRIPT: &str = include_str!("../bashsupport.sh");
2325
lazy_static! {
2426
static ref FRAME_RE: Regex = Regex::new(r"^(.*?):(.*):(\d+)$").unwrap();
@@ -188,7 +190,7 @@ fn send_event(
188190
}
189191
}
190192

191-
attach_logfile(&mut event, logfile, true)?;
193+
attach_logfile(&mut event, logfile, true, MAX_BREADCRUMBS)?;
192194

193195
event.exception.values.push(Exception {
194196
ty: "BashError".into(),

src/commands/send_event.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ use crate::utils::args::{get_timestamp, validate_distribution};
2020
use crate::utils::event::{attach_logfile, get_sdk_info};
2121
use crate::utils::releases::detect_release_name;
2222

23+
/// The maximum number of breadcrumbs to attach to an event.
24+
const MAX_BREADCRUMBS: usize = 100;
25+
2326
pub fn make_command(command: Command) -> Command {
2427
command.about("Send a manual event to Sentry.")
2528
.long_about(
@@ -143,7 +146,7 @@ pub fn make_command(command: Command) -> Command {
143146
Arg::new("logfile")
144147
.value_name("PATH")
145148
.long("logfile")
146-
.help("Send a logfile as breadcrumbs with the event (last 100 records)"),
149+
.help(format!("Send a logfile as breadcrumbs with the event (last {MAX_BREADCRUMBS} records)")),
147150
)
148151
.arg(
149152
Arg::new("with_categories")
@@ -322,7 +325,12 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
322325
}
323326

324327
if let Some(logfile) = matches.get_one::<String>("logfile") {
325-
attach_logfile(&mut event, logfile, matches.get_flag("with_categories"))?;
328+
attach_logfile(
329+
&mut event,
330+
logfile,
331+
matches.get_flag("with_categories"),
332+
MAX_BREADCRUMBS,
333+
)?;
326334
}
327335

328336
let id = send_raw_event(event)?;

src/utils/event.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ lazy_static! {
1313
}
1414

1515
/// Attaches all logs from a logfile as breadcrumbs to the given event.
16-
pub fn attach_logfile(event: &mut Event<'_>, logfile: &str, with_component: bool) -> Result<()> {
16+
pub fn attach_logfile(
17+
event: &mut Event<'_>,
18+
logfile: &str,
19+
with_component: bool,
20+
max_breadcrumbs: usize,
21+
) -> Result<()> {
1722
let f = fs::File::open(logfile).context("Could not open logfile")?;
1823

1924
// sentry currently requires timestamps for breadcrumbs at all times.
@@ -46,8 +51,8 @@ pub fn attach_logfile(event: &mut Event<'_>, logfile: &str, with_component: bool
4651
})
4752
}
4853

49-
if event.breadcrumbs.len() > 100 {
50-
let skip = event.breadcrumbs.len() - 100;
54+
if event.breadcrumbs.len() > max_breadcrumbs {
55+
let skip = event.breadcrumbs.len() - max_breadcrumbs;
5156
event.breadcrumbs.values.drain(..skip);
5257
}
5358

0 commit comments

Comments
 (0)