Skip to content

Commit

Permalink
feat: structured logs
Browse files Browse the repository at this point in the history
  • Loading branch information
tsunyoku committed Nov 11, 2023
1 parent 00a91ab commit a82f9ca
Show file tree
Hide file tree
Showing 9 changed files with 265 additions and 99 deletions.
279 changes: 213 additions & 66 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ dotenv = "0.15.0"
clap = { version = "3.0.0-beta.5", features = ["derive", "env"] }
serde = { version = "1.0.130", features = ["derive"] }
serde_json = "1.0.87"
env_logger = "0.9.0"
anyhow = "1.0.48"
log = "0.4.14"
tower = "0.4.11"
Expand All @@ -40,6 +39,7 @@ conceptual-rework = { package = "akatsuki-pp", git = "https://github.com/osuAkat
async-trait = "0.1.62"
skill-rebalance = { package = "akatsuki-pp", git = "https://github.com/osuAkatsuki/akatsuki-pp-rs", branch = "skill-rebalance", features = ["async_tokio"] }
rust-s3 = "0.33.0"
structured-logger = "1.0.3"

[profile.release]
lto = "fat"
Expand Down
5 changes: 4 additions & 1 deletion src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ pub async fn serve(ctx: Context) -> anyhow::Result<()> {
.layer(AddExtensionLayer::new(Arc::new(ctx))),
);

log::info!("serving on {}", server_port);
log::info!(
port = server_port;
"Serving API",
);
axum::Server::bind(&format!("{}:{}", server_host, server_port).parse()?)
.serve(app.into_make_service())
.await?;
Expand Down
13 changes: 7 additions & 6 deletions src/api/routes/calculate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ async fn calculate_play(
Ok(result) => result,
Err(e) => {
log::error!(
"Performance calculation failed for beatmap {}: {}",
request.beatmap_id,
e.to_string()
beatmap_id = request.beatmap_id,
error = e.to_string();
"Performance calculation failed for beatmap",
);

CalculateResponse {
Expand All @@ -170,9 +170,10 @@ async fn calculate_play(
};

log::info!(
"Calculated performance: {}pp for beatmap {}",
result.pp,
request.beatmap_id
performance_points = result.pp,
star_rating = result.stars,
beatmap_id = request.beatmap_id;
"Calculated performance for beatmap.",
);
results.push(result);
}
Expand Down
6 changes: 3 additions & 3 deletions src/api/routes/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ async fn health(Extension(ctx): Extension<Arc<Context>>) -> http::StatusCode {
true => http::StatusCode::OK,
false => {
log::error!(
"Failed health check. Redis healthy: {} | Database healthy: {}",
is_redis_ok,
is_database_ok
redis_healthy = is_redis_ok,
database_healthy = is_database_ok;
"Failed health check.",
);
return http::StatusCode::INTERNAL_SERVER_ERROR;
}
Expand Down
20 changes: 10 additions & 10 deletions src/deploy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,11 @@ async fn recalculate_score(
}

log::info!(
"Recalculated score ID {} (mode: {}) | {} -> {}",
score.id,
score.play_mode,
score.pp,
response.pp,
score_id = score.id,
score_mode = score.play_mode,
old_pp = score.pp,
new_pp = score.pp;
"Recalculated score",
);

Ok(())
Expand Down Expand Up @@ -505,11 +505,11 @@ async fn recalculate_user(
.await?;

log::info!(
"Recalculated user {} in mode {} (rx: {}) | pp: {}",
user_id,
mode,
rx,
new_pp
user_id = user_id,
mode = mode,
relax = rx,
new_pp = new_pp;
"Recalculated user",
);

Ok(())
Expand Down
12 changes: 9 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use performance_service::{
};
use redis::{Client, ConnectionAddr, ConnectionInfo, RedisConnectionInfo};
use s3::{creds::Credentials, Bucket, Region};
use sqlx::mysql::MySqlConnectOptions;
use sqlx::{mysql::MySqlConnectOptions, ConnectOptions};
use structured_logger::{async_json::new_writer, Builder};

fn amqp_dsn(username: &str, password: &str, host: &str, port: u16) -> String {
return format!("amqp://{}:{}@{}:{}", username, password, host, port);
Expand All @@ -15,7 +16,10 @@ fn amqp_dsn(username: &str, password: &str, host: &str, port: u16) -> String {
#[tokio::main]
async fn main() -> anyhow::Result<()> {
dotenv::dotenv().ok();
env_logger::init();

Builder::new()
.with_target_writer("*", new_writer(tokio::io::stdout()))
.init();

let config = Config::parse();

Expand All @@ -24,7 +28,9 @@ async fn main() -> anyhow::Result<()> {
.port(config.database_port)
.username(&config.database_username)
.password(&config.database_password)
.database(&config.database_name);
.database(&config.database_name)
.disable_statement_logging()
.clone();
let database = DbPool::new(database_options, config.database_pool_max_size);

let amqp_url = amqp_dsn(
Expand Down
10 changes: 8 additions & 2 deletions src/mass_recalc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ async fn queue_user(user_id: i32, rework: &Rework, context: &Context) {
.await
.unwrap();

log::info!("Queued user ID {}", user_id);
log::info!(
user_id = user_id;
"Queued user",
);
}

pub async fn serve(context: Context) -> anyhow::Result<()> {
Expand All @@ -100,7 +103,10 @@ pub async fn serve(context: Context) -> anyhow::Result<()> {
print!("\n");
std::io::stdout().flush().unwrap();

log::info!("Mass recalculating on rework ID {}", rework_id);
log::info!(
rework_id = rework_id;
"Mass recalculating on rework",
);

let rework = usecases::reworks::fetch_one(rework_id, Arc::from(context.clone()))
.await?
Expand Down
17 changes: 10 additions & 7 deletions src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ async fn process_scores(
_ => unreachable!(),
};

log::info!("Recalculated PP for score ID {}", score.id);
log::info!(
score_id = score.id;
"Recalculated PP for score",
);

let rework_score = ReworkScore::from_ripple_score(score, rework.rework_id, new_pp);
rework_scores.push(rework_score);
Expand Down Expand Up @@ -275,9 +278,9 @@ async fn handle_queue_request(
.await?;

log::info!(
"Processed recalculation for user ID {} on rework {}",
request.user_id,
rework.rework_name
user_id = request.user_id,
rework_name = rework.rework_name;
"Processed recalculation for user on rework",
);

Ok(())
Expand Down Expand Up @@ -312,9 +315,9 @@ async fn rmq_listen(context: Arc<Context>) -> anyhow::Result<()> {
.unwrap();

log::info!(
"Received recalculation request for user ID {} on rework ID {}",
deserialized_data.user_id,
deserialized_data.rework_id
user_id = deserialized_data.user_id,
rework_id = deserialized_data.rework_id;
"Received recalculation request for user on rework",
);

let context_clone = context.clone();
Expand Down

0 comments on commit a82f9ca

Please sign in to comment.