Skip to content

Commit

Permalink
use tracing crate instead of log
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyke-bot committed Jan 17, 2024
1 parent 9c104e0 commit b333c0e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 75 deletions.
72 changes: 19 additions & 53 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ anyhow = "1.0.71"
clap = { version = "4.3.4", features = ["derive"] }
dashmap = { version = "5.5.3", features = ["serde"] }
env_logger = "0.10.0"
futures = "0.3.28"
hex = "0.4.3"
log = "0.4.19"
tracing = "0.1.40"
primitive-types = "0.12.1"
r2d2 = "0.8.10"
redis = { version = "0.23.3", features = ["r2d2", "async-std"] }
redis = { version = "0.24.0", features = ["r2d2", "async-std"] }
reqwest = { version = "0.11.18", features = ["rustls", "json", "serde_json"] }
serde = { version = "1.0.164", features = ["derive"] }
serde_json = { version = "1.0.97", features = ["std"] }
Expand Down
45 changes: 26 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,13 @@ fn read_cache(
})
}


#[actix_web::post("/{chain}")]
async fn rpc_call(
path: web::Path<(String, )>,
path: web::Path<(String,)>,
data: web::Data<AppState>,
body: web::Json<Value>,
) -> Result<HttpResponse, Error> {
let (chain, ) = path.into_inner();
let (chain,) = path.into_inner();
let chain_state = data
.chains
.get(&chain.to_uppercase())
Expand All @@ -124,7 +123,7 @@ async fn rpc_call(
let mut ids_in_original_order = vec![];

let mut redis_con = data.redis.get().map_err(|err| {
log::error!("fail to get redis connection because: {}", err);
tracing::error!("fail to get redis connection because: {}", err);
error::ErrorInternalServerError("fail to get redis connection")
})?;

Expand All @@ -151,23 +150,28 @@ async fn rpc_call(
}
};

let result = read_cache(&mut redis_con, cache_entry.handler.as_ref(), &method, &params);
let result = read_cache(
&mut redis_con,
cache_entry.handler.as_ref(),
&method,
&params,
);

match result {
Err(err) => {
log::error!("fail to read cache because: {}", err);
tracing::error!("fail to read cache because: {}", err);
uncached_requests.insert(id, (method, params, None));
}
Ok(CacheStatus::NotAvailable) => {
log::info!("cache not available for method {}", method);
tracing::info!("cache not available for method {}", method);
uncached_requests.insert(id, (method, params, None));
}
Ok(CacheStatus::Cached(cache_key, value)) => {
log::info!("cache hit for method {} with key {}", method, cache_key);
tracing::info!("cache hit for method {} with key {}", method, cache_key);
request_result.insert(id, ResultOrError::Result(value));
}
Ok(CacheStatus::Missed(cache_key)) => {
log::info!("cache missed for method {} with key {}", method, cache_key);
tracing::info!("cache missed for method {} with key {}", method, cache_key);
uncached_requests.insert(id, (method, params, Some(cache_key)));
}
}
Expand All @@ -193,16 +197,19 @@ async fn rpc_call(
chain_state.rpc_url.clone(),
&request_body,
)
.await
.map_err(|err| {
log::error!("fail to make rpc request because: {}", err);
error::ErrorInternalServerError(format!("fail to make rpc request because: {}", err))
})?;
.await
.map_err(|err| {
tracing::error!("fail to make rpc request because: {}", err);
error::ErrorInternalServerError(format!("fail to make rpc request because: {}", err))
})?;

let result_values = match rpc_result {
Value::Array(v) => v,
_ => {
log::error!("array is expected but we got invalid rpc response: {},", rpc_result.to_string());
tracing::error!(
"array is expected but we got invalid rpc response: {},",
rpc_result.to_string()
);
return Err(error::ErrorInternalServerError("invalid rpc response"));
}
};
Expand Down Expand Up @@ -284,10 +291,10 @@ async fn main() -> std::io::Result<()> {

let handler_factories = rpc_cache_handler::all_factories();

log::info!("Provisioning cache tables");
tracing::info!("Provisioning cache tables");

for (name, rpc_url) in arg.endpoints.iter() {
log::info!("Adding endpoint {} linked to {}", name, rpc_url);
tracing::info!("Adding endpoint {} linked to {}", name, rpc_url);

let mut chain_state = ChainState::new(rpc_url.clone());

Expand All @@ -303,7 +310,7 @@ async fn main() -> std::io::Result<()> {

let app_state = web::Data::new(app_state);

log::info!("Server listening on {}:{}", arg.bind, arg.port);
tracing::info!("Server listening on {}:{}", arg.bind, arg.port);

{
let app_state = app_state.clone();
Expand All @@ -314,7 +321,7 @@ async fn main() -> std::io::Result<()> {
.await?;
}

log::info!("Server stopped");
tracing::info!("Server stopped");

Ok(())
}

0 comments on commit b333c0e

Please sign in to comment.