Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c68b9de

Browse files
committedSep 6, 2021
Use a common prefix for logged errors
The `at=error ` prefix is used for Heroku error messages and using a common prefix aids in metrics collection and is easily searchable.
1 parent c00bc1c commit c68b9de

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed
 

‎src/bin/background-worker.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ use std::time::Duration;
2424
fn main() {
2525
println!("Booting runner");
2626

27+
if let Err(message) = main_impl() {
28+
eprintln!("at=error mod=background_worker error=\"{}\"", message);
29+
std::process::exit(1);
30+
}
31+
}
32+
33+
fn main_impl() -> Result<(), &'static str> {
2734
let db_config = config::DatabasePools::full_from_environment();
2835
let base_config = config::Base::from_environment();
2936
let uploader = base_config.uploader();
@@ -43,13 +50,13 @@ fn main() {
4350
let job_start_timeout = dotenv::var("BACKGROUND_JOB_TIMEOUT")
4451
.unwrap_or_else(|_| "30".into())
4552
.parse()
46-
.expect("Invalid value for `BACKGROUND_JOB_TIMEOUT`");
53+
.map_err(|_| "Invalid value for `BACKGROUND_JOB_TIMEOUT`")?;
4754

4855
println!("Cloning index");
4956

5057
let repository_config = RepositoryConfig::from_environment();
5158
let repository = Arc::new(Mutex::new(
52-
Repository::open(&repository_config).expect("Failed to clone index"),
59+
Repository::open(&repository_config).map_err(|_| "Failed to clone index")?,
5360
));
5461
println!("Index cloned");
5562

@@ -76,12 +83,12 @@ fn main() {
7683
failure_count += 1;
7784
if failure_count < 5 {
7885
eprintln!(
79-
"Error running jobs (n = {}) -- retrying: {:?}",
86+
"at=error mod=background_worker error=\"Error running jobs (n = {}) -- retrying: {:?}\"",
8087
failure_count, e,
8188
);
8289
runner = build_runner();
8390
} else {
84-
panic!("Failed to begin running jobs 5 times. Restarting the process");
91+
return Err("Failed to begin running jobs 5 times. Restarting the process");
8592
}
8693
}
8794
sleep(Duration::from_secs(1));

‎src/bin/monitor.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn check_stalled_update_downloads(conn: &PgConnection) -> Result<()> {
7676

7777
const EVENT_KEY: &str = "update_downloads_stalled";
7878

79-
println!("Checking for stalled background jobs");
79+
println!("Checking for stalled update_downloads job");
8080

8181
// Max job execution time in minutes
8282
let max_job_time = dotenv::var("MONITOR_MAX_UPDATE_DOWNLOADS_TIME")
@@ -153,7 +153,10 @@ fn log_and_trigger_event(event: on_call::Event) -> Result<()> {
153153
match event {
154154
on_call::Event::Trigger {
155155
ref description, ..
156-
} => println!("Paging on-call: {}", description),
156+
} => eprintln!(
157+
"at=error mod=on_call_monitor Paging on-call: {}",
158+
description
159+
),
157160
on_call::Event::Resolve {
158161
description: Some(ref description),
159162
..

‎src/bin/server.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
146146
println!("Persisting remaining downloads counters");
147147
match app.downloads_counter.persist_all_shards(&app) {
148148
Ok(stats) => stats.log(),
149-
Err(err) => println!("downloads_counter error: {}", err),
149+
Err(err) => eprintln!("at=error mod=downloads_counter error=\"{}\"", err),
150150
}
151151

152152
println!("Server has gracefully shutdown!");
@@ -163,7 +163,7 @@ fn downloads_counter_thread(app: Arc<App>) {
163163

164164
match app.downloads_counter.persist_next_shard(&app) {
165165
Ok(stats) => stats.log(),
166-
Err(err) => println!("downloads_counter error: {}", err),
166+
Err(err) => eprintln!("at=error mod=downloads_counter error=\"{}\"", err),
167167
}
168168
});
169169
}
@@ -178,7 +178,7 @@ fn log_instance_metrics_thread(app: Arc<App>) {
178178

179179
std::thread::spawn(move || loop {
180180
if let Err(err) = log_instance_metrics_inner(&app) {
181-
eprintln!("log_instance_metrics error: {}", err);
181+
eprintln!("at=error mod=log_instance_metrics error=\"{}\"", err);
182182
}
183183
std::thread::sleep(interval);
184184
});

‎src/git.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ impl Repository {
252252
self.perform_commit_and_push(message, modified_file)
253253
.map(|_| println!("Commit and push finished for \"{}\"", message))
254254
.map_err(|err| {
255-
eprintln!("Commit and push for \"{}\" errored: {}", message, err);
255+
eprintln!("at=error mod=git error=\"Commit and push for \"{}\" errored: {}\"", message, err);
256256
err
257257
})
258258
}
@@ -272,7 +272,9 @@ impl Repository {
272272
let head = self.head_oid()?;
273273

274274
if head != original_head {
275-
println!("Resetting index from {} to {}", original_head, head);
275+
// This isn't strictly an error, but treating it as one for monitoring as it indicates
276+
// that an error has occurred while processing the previous index operation.
277+
eprintln!("at=error mod=git error=\"Resetting index from {} to {}\"", original_head, head);
276278
}
277279

278280
let obj = self.repository.find_object(head, None)?;

0 commit comments

Comments
 (0)
Please sign in to comment.