Skip to content

Commit eef3121

Browse files
[TRUNK-14377] Tags for sentry uploads (#471)
Switches from event_filter to event_mapper so that we can automatically include org, command, and repo in tags. Sample event created with this change: https://trunkio.sentry.io/issues/6387685042/?environment=development&project=4507772986982400&query=is%3Aunresolved%20issue.priority%3A%5Bhigh%2C%20medium%5D&referrer=issue-stream&statsPeriod=24h&stream_index=0
1 parent d3ce794 commit eef3121

File tree

3 files changed

+90
-9
lines changed

3 files changed

+90
-9
lines changed

cli/src/main.rs

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,39 @@ impl Cli {
4141
|token| gather_debug_props(env::args().collect::<Vec<String>>(), token).command_line,
4242
)
4343
}
44+
45+
pub fn command_name(&self) -> &str {
46+
match &self.command {
47+
Commands::Quarantine(..) => "quarantine",
48+
Commands::Test(..) => "test",
49+
Commands::Upload(..) => "upload",
50+
Commands::Validate(..) => "validate",
51+
}
52+
}
53+
54+
pub fn org_url_slug(&self) -> String {
55+
match &self.command {
56+
Commands::Quarantine(args) => args.org_url_slug(),
57+
Commands::Test(args) => args.org_url_slug(),
58+
Commands::Upload(args) => args.org_url_slug.clone(),
59+
Commands::Validate(..) => String::from("not used"),
60+
}
61+
}
62+
63+
pub fn repo_root(&self) -> String {
64+
let explicit_root = match &self.command {
65+
Commands::Quarantine(args) => args.repo_root(),
66+
Commands::Test(args) => args.repo_root(),
67+
Commands::Upload(args) => args.repo_root.clone(),
68+
Commands::Validate(..) => None,
69+
};
70+
explicit_root
71+
.or(std::env::current_dir()
72+
.iter()
73+
.flat_map(|path_buf| path_buf.clone().into_os_string().into_string().into_iter())
74+
.next())
75+
.unwrap_or(String::from("not set"))
76+
}
4477
}
4578

4679
#[derive(Debug, Subcommand)]
@@ -67,7 +100,12 @@ fn main() -> anyhow::Result<()> {
67100
.block_on(async {
68101
let cli = Cli::parse();
69102
let log_level_filter = cli.verbose.log_level_filter();
70-
setup_logger(log_level_filter)?;
103+
setup_logger(
104+
log_level_filter,
105+
cli.command_name(),
106+
cli.org_url_slug(),
107+
cli.repo_root(),
108+
)?;
71109
tracing::info!("{}", TITLE_CARD);
72110
tracing::info!(
73111
command = cli.debug_props(),
@@ -119,19 +157,46 @@ fn to_trace_filter(filter: log::LevelFilter) -> tracing::Level {
119157
}
120158
}
121159

122-
fn setup_logger(log_level_filter: LevelFilter) -> anyhow::Result<()> {
123-
// trunk-ignore(clippy/match_ref_pats)
124-
let sentry_layer = sentry_tracing::layer().event_filter(|md| match md.level() {
125-
&tracing::Level::ERROR => sentry_tracing::EventFilter::Event,
126-
&tracing::Level::WARN => sentry_tracing::EventFilter::Breadcrumb,
127-
&tracing::Level::INFO => sentry_tracing::EventFilter::Breadcrumb,
128-
&tracing::Level::DEBUG => sentry_tracing::EventFilter::Breadcrumb,
129-
_ => sentry_tracing::EventFilter::Ignore,
160+
fn setup_logger(
161+
log_level_filter: LevelFilter,
162+
command_name: &str,
163+
org_url_slug: String,
164+
repo_root: String,
165+
) -> anyhow::Result<()> {
166+
let command_string = String::from(command_name);
167+
let sentry_layer = sentry_tracing::layer().event_mapper(move |event, context| {
168+
// trunk-ignore(clippy/match_ref_pats)
169+
match event.metadata().level() {
170+
&tracing::Level::ERROR => {
171+
let mut event = sentry_tracing::event_from_event(event, context);
172+
event
173+
.tags
174+
.insert(String::from("command_name"), command_string.clone());
175+
event
176+
.tags
177+
.insert(String::from("org_url_slug"), org_url_slug.clone());
178+
event
179+
.tags
180+
.insert(String::from("repo_root"), repo_root.clone());
181+
sentry_tracing::EventMapping::Event(event)
182+
}
183+
&tracing::Level::WARN => sentry_tracing::EventMapping::Breadcrumb(
184+
sentry_tracing::breadcrumb_from_event(event, context),
185+
),
186+
&tracing::Level::INFO => sentry_tracing::EventMapping::Breadcrumb(
187+
sentry_tracing::breadcrumb_from_event(event, context),
188+
),
189+
&tracing::Level::DEBUG => sentry_tracing::EventMapping::Breadcrumb(
190+
sentry_tracing::breadcrumb_from_event(event, context),
191+
),
192+
_ => sentry_tracing::EventMapping::Ignore,
193+
}
130194
});
131195

132196
let console_layer = tracing_subscriber::fmt::Layer::new()
133197
.without_time()
134198
.with_target(false)
199+
.with_level(false)
135200
.with_writer(std::io::stdout.with_max_level(to_trace_filter(log_level_filter)))
136201
.with_filter(FilterFn::new(|metadata| {
137202
!metadata

cli/src/quarantine_command.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ impl QuarantineArgs {
1515
pub fn token(&self) -> String {
1616
self.upload_args.token.clone()
1717
}
18+
19+
pub fn org_url_slug(&self) -> String {
20+
self.upload_args.org_url_slug.clone()
21+
}
22+
23+
pub fn repo_root(&self) -> Option<String> {
24+
self.upload_args.repo_root.clone()
25+
}
1826
}
1927

2028
// This is an alias to `run_upload`, but does not exit on upload failure

cli/src/test_command.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ impl TestArgs {
3030
pub fn token(&self) -> String {
3131
self.upload_args.token.clone()
3232
}
33+
34+
pub fn org_url_slug(&self) -> String {
35+
self.upload_args.org_url_slug.clone()
36+
}
37+
38+
pub fn repo_root(&self) -> Option<String> {
39+
self.upload_args.repo_root.clone()
40+
}
3341
}
3442

3543
#[derive(Debug, Clone)]

0 commit comments

Comments
 (0)