Skip to content

Commit 25b453f

Browse files
feat: Enhance logging with requestIds for traceability (#455)
* feat: Enhance logging with requestIds for traceability Signed-off-by: Dylan Kilkenny <[email protected]> * refactor: Add tests * test: Fix Signed-off-by: Dylan Kilkenny <[email protected]> --------- Signed-off-by: Dylan Kilkenny <[email protected]>
1 parent 55ec56a commit 25b453f

18 files changed

+565
-118
lines changed

Cargo.lock

Lines changed: 85 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ aws-config = { version = "1.6.3", features = ["behavior-version-latest"] }
2020
aws-sdk-kms = "1.75.0"
2121
actix-web = "4"
2222
log = "0.4"
23-
simplelog = "0.12"
23+
tracing = "0.1"
24+
tracing-subscriber = { version = "0.3", features = ["env-filter", "json", "tracing-log"] }
25+
tracing-error = { version = "0.2" }
26+
tracing-appender = "0.2"
27+
tracing-actix-web = "0.7"
2428
prometheus = "0.14"
2529
lazy_static = "1.5"
2630
dotenvy = "0.15"

src/constants/logging.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//! Logging configuration constants
2+
3+
/// Default maximum log file size in bytes (1GB)
4+
pub const DEFAULT_MAX_LOG_FILE_SIZE: u64 = 1_073_741_824;
5+
6+
/// Default log level when not specified
7+
pub const DEFAULT_LOG_LEVEL: &str = "info";
8+
9+
/// Default log format when not specified
10+
pub const DEFAULT_LOG_FORMAT: &str = "compact";
11+
12+
/// Default log mode when not specified
13+
pub const DEFAULT_LOG_MODE: &str = "stdout";
14+
15+
/// Default log directory for file logging
16+
pub const DEFAULT_LOG_DIR: &str = "./logs";
17+
18+
/// Default log directory when running in Docker
19+
pub const DOCKER_LOG_DIR: &str = "logs/";
20+
21+
/// Log file name
22+
pub const LOG_FILE_NAME: &str = "relayer.log";

src/constants/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ pub use transactions::*;
3737

3838
mod network_tags;
3939
pub use network_tags::*;
40+
41+
mod logging;
42+
pub use logging::*;

src/jobs/handlers/notification_handler.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! This module implements the notification handling worker that processes
44
//! notification jobs from the queue.
55
6+
use crate::setup_job_tracing;
67
use actix_web::web::ThinData;
78
use apalis::prelude::{Attempt, Data, *};
89
use eyre::Result;
@@ -29,6 +30,8 @@ pub async fn notification_handler(
2930
context: Data<ThinData<DefaultAppState>>,
3031
attempt: Attempt,
3132
) -> Result<(), Error> {
33+
setup_job_tracing!(job, attempt);
34+
3235
info!("handling notification: {:?}", job.data);
3336

3437
let result = handle_request(job.data, context).await;

src/jobs/handlers/solana_swap_request_handler.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! This module implements the solana token swap request handling worker that processes
44
//! notification jobs from the queue.
55
6+
use crate::setup_job_tracing;
67
use actix_web::web::ThinData;
78
use apalis::prelude::{Attempt, Data, *};
89
use eyre::Result;
@@ -29,6 +30,8 @@ pub async fn solana_token_swap_request_handler(
2930
context: Data<ThinData<DefaultAppState>>,
3031
attempt: Attempt,
3132
) -> Result<(), Error> {
33+
setup_job_tracing!(job, attempt);
34+
3235
info!("handling solana token swap request: {:?}", job.data);
3336

3437
let result = handle_request(job.data, context).await;

src/jobs/handlers/transaction_request_handler.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//!
33
//! Handles the validation and preparation of transactions before they are
44
//! submitted to the network
5+
use crate::setup_job_tracing;
56
use actix_web::web::ThinData;
67
use apalis::prelude::{Attempt, Context, Data, TaskId, Worker, *};
78
use apalis_redis::RedisContext;
@@ -23,6 +24,8 @@ pub async fn transaction_request_handler(
2324
task_id: TaskId,
2425
ctx: RedisContext,
2526
) -> Result<(), Error> {
27+
setup_job_tracing!(job, attempt);
28+
2629
info!("Handling transaction request: {:?}", job.data);
2730
info!("Attempt: {:?}", attempt);
2831
info!("Worker: {:?}", worker);

src/jobs/handlers/transaction_status_handler.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use actix_web::web::ThinData;
88
use apalis::prelude::{Attempt, Data, *};
99

10+
use crate::setup_job_tracing;
1011
use eyre::Result;
1112
use log::info;
1213

@@ -22,6 +23,8 @@ pub async fn transaction_status_handler(
2223
state: Data<ThinData<DefaultAppState>>,
2324
attempt: Attempt,
2425
) -> Result<(), Error> {
26+
setup_job_tracing!(job, attempt);
27+
2528
info!("Handling transaction status job: {:?}", job.data);
2629

2730
let result = handle_request(job.data, state).await;

src/jobs/handlers/transaction_submission_handler.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! - Handles different submission commands (Submit, Cancel, Resubmit)
66
//! - Updates transaction status after submission
77
//! - Enqueues status monitoring jobs
8+
use crate::setup_job_tracing;
89
use actix_web::web::ThinData;
910
use apalis::prelude::{Attempt, Data, *};
1011
use eyre::Result;
@@ -22,6 +23,8 @@ pub async fn transaction_submission_handler(
2223
state: Data<ThinData<DefaultAppState>>,
2324
attempt: Attempt,
2425
) -> Result<(), Error> {
26+
setup_job_tracing!(job, attempt);
27+
2528
info!("handling transaction submission: {:?}", job.data);
2629

2730
let result = handle_request(job.data, state).await;

src/jobs/job.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub struct Job<T> {
1919
pub timestamp: String,
2020
pub job_type: JobType,
2121
pub data: T,
22+
#[serde(skip_serializing_if = "Option::is_none")]
23+
pub request_id: Option<String>,
2224
}
2325

2426
impl<T> Job<T> {
@@ -29,8 +31,13 @@ impl<T> Job<T> {
2931
timestamp: Utc::now().timestamp().to_string(),
3032
job_type,
3133
data,
34+
request_id: None,
3235
}
3336
}
37+
pub fn with_request_id(mut self, id: Option<String>) -> Self {
38+
self.request_id = id;
39+
self
40+
}
3441
}
3542

3643
// Enum to represent different message types

0 commit comments

Comments
 (0)