Skip to content

Commit

Permalink
chore: better logs
Browse files Browse the repository at this point in the history
  • Loading branch information
lgalabru committed Aug 2, 2023
1 parent 4c88e5d commit 5ff9906
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 20 deletions.
1 change: 1 addition & 0 deletions components/hord-cli/src/config/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct ConfigFile {
#[derive(Deserialize, Debug, Clone)]
pub struct LogConfigFile {
pub ordinals_computation: Option<bool>,
pub chainhook: Option<bool>,
}

#[derive(Deserialize, Debug, Clone)]
Expand Down
4 changes: 4 additions & 0 deletions components/hord-cli/src/config/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ max_number_of_concurrent_bitcoin_scans = 100
max_number_of_processing_threads = 16
max_number_of_networking_threads = 16
max_caching_memory_size_mb = 32000
[logs]
ordinals_computation = true
chainhook = true
"#,
network = network.to_lowercase(),
);
Expand Down
13 changes: 11 additions & 2 deletions components/hord-cli/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct Config {
#[derive(Clone, Debug)]
pub struct LogConfig {
pub ordinals_computation: bool,
pub chainhook: bool,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -233,9 +234,14 @@ impl Config {
},
logs: LogConfig {
ordinals_computation: config_file
.logs
.logs.as_ref()
.and_then(|l| l.ordinals_computation)
.unwrap_or(true),
chainhook: config_file
.logs.as_ref()
.and_then(|l| l.chainhook)
.unwrap_or(true),

},
};
Ok(config)
Expand Down Expand Up @@ -366,6 +372,7 @@ impl Config {
},
logs: LogConfig {
ordinals_computation: true,
chainhook: false,
},
}
}
Expand Down Expand Up @@ -398,6 +405,7 @@ impl Config {
},
logs: LogConfig {
ordinals_computation: true,
chainhook: false,
},
}
}
Expand Down Expand Up @@ -431,7 +439,8 @@ impl Config {
bitcoin_network: BitcoinNetwork::Mainnet,
},
logs: LogConfig {
ordinals_computation: false,
ordinals_computation: true,
chainhook: false,
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions components/hord-cli/src/core/pipeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ pub async fn download_and_pipeline_blocks(
thread_index = (thread_index + 1) % hord_config.ingestion_thread_max;
}

ctx.try_log(|logger| info!(logger, "Gargbage collecting will start"));
ctx.try_log(|logger| info!(logger, "Pipeline successfully fed with sequence of blocks ({} to {})", start_block, end_block));

for tx in tx_thread_pool.iter() {
let _ = tx.send(None);
Expand All @@ -230,7 +230,7 @@ pub async fn download_and_pipeline_blocks(
let _ = storage_thread.join();
let _ = set.shutdown();

ctx.try_log(|logger| info!(logger, "Gargbage collecting did finish"));
ctx.try_log(|logger| info!(logger, "Pipeline successfully processed sequence of blocks ({} to {})", start_block, end_block));

// match guard.report().build() {
// Ok(report) => {
Expand Down
13 changes: 6 additions & 7 deletions components/hord-cli/src/service/http_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use chainhook_sdk::{
observer::ObserverCommand,
utils::Context,
};
use hiro_system_kit::slog;
use redis::{Commands, Connection};
use rocket::config::{self, Config, LogLevel};
use rocket::serde::json::{json, Json, Value as JsonValue};
Expand Down Expand Up @@ -72,7 +71,7 @@ pub async fn start_predicate_api_server(

#[get("/ping")]
fn handle_ping(ctx: &State<Context>) -> Json<JsonValue> {
ctx.try_log(|logger| slog::info!(logger, "Handling HTTP GET /ping"));
ctx.try_log(|logger| info!(logger, "Handling HTTP GET /ping"));
Json(json!({
"status": 200,
"result": "chainhook service up and running",
Expand All @@ -84,13 +83,13 @@ fn handle_get_predicates(
api_config: &State<PredicatesApiConfig>,
ctx: &State<Context>,
) -> Json<JsonValue> {
ctx.try_log(|logger| slog::info!(logger, "Handling HTTP GET /v1/observers"));
ctx.try_log(|logger| info!(logger, "Handling HTTP GET /v1/observers"));
match open_readwrite_predicates_db_conn(api_config) {
Ok(mut predicates_db_conn) => {
let predicates = match get_entries_from_predicates_db(&mut predicates_db_conn, &ctx) {
Ok(predicates) => predicates,
Err(e) => {
ctx.try_log(|logger| slog::warn!(logger, "unable to retrieve predicates: {e}"));
ctx.try_log(|logger| warn!(logger, "unable to retrieve predicates: {e}"));
return Json(json!({
"status": 500,
"message": "unable to retrieve predicates",
Expand Down Expand Up @@ -122,7 +121,7 @@ fn handle_create_predicate(
background_job_tx: &State<Arc<Mutex<Sender<ObserverCommand>>>>,
ctx: &State<Context>,
) -> Json<JsonValue> {
ctx.try_log(|logger| slog::info!(logger, "Handling HTTP POST /v1/observers"));
ctx.try_log(|logger| info!(logger, "Handling HTTP POST /v1/observers"));
let predicate = predicate.into_inner();
if let Err(e) = predicate.validate() {
return Json(json!({
Expand Down Expand Up @@ -169,7 +168,7 @@ fn handle_get_predicate(
api_config: &State<PredicatesApiConfig>,
ctx: &State<Context>,
) -> Json<JsonValue> {
ctx.try_log(|logger| slog::info!(logger, "Handling HTTP GET /v1/observers/{}", predicate_uuid));
ctx.try_log(|logger| info!(logger, "Handling HTTP GET /v1/observers/{}", predicate_uuid));

match open_readwrite_predicates_db_conn(api_config) {
Ok(mut predicates_db_conn) => {
Expand Down Expand Up @@ -219,7 +218,7 @@ fn handle_delete_bitcoin_predicate(
ctx: &State<Context>,
) -> Json<JsonValue> {
ctx.try_log(|logger| {
slog::info!(
info!(
logger,
"Handling HTTP DELETE /v1/observers/{}",
predicate_uuid
Expand Down
39 changes: 30 additions & 9 deletions components/hord-cli/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod runloops;

use crate::cli::fetch_and_standardize_block;
use crate::config::{Config, PredicatesApi, PredicatesApiConfig};
use crate::core::pipeline::download_and_pipeline_blocks;
use crate::core::pipeline::{download_and_pipeline_blocks, PostProcessorCommand};
use crate::core::pipeline::processors::start_inscription_indexing_processor;
use crate::core::protocol::sequencing::{
update_hord_db_and_augment_bitcoin_block_v3,
Expand Down Expand Up @@ -37,7 +37,6 @@ use chainhook_sdk::types::{
OrdinalOperation,
};
use chainhook_sdk::utils::Context;
use hiro_system_kit::slog;
use redis::{Commands, Connection};

use std::collections::{BTreeMap, HashMap};
Expand Down Expand Up @@ -128,7 +127,6 @@ impl Service {

while let Some((start_block, end_block)) = should_sync_hord_db(&self.config, &self.ctx)?
{
// let end_block = end_block.min(start_block + 256);
info!(
self.ctx.expect_logger(),
"Indexing inscriptions from block #{start_block} to block #{end_block}"
Expand All @@ -146,6 +144,8 @@ impl Service {
)
.await?;
}

let _ = blocks_post_processor.commands_tx.send(PostProcessorCommand::Terminate);
}

// Bitcoin scan operation threadpool
Expand Down Expand Up @@ -184,12 +184,23 @@ impl Service {
let (observer_event_tx, observer_event_rx) = crossbeam_channel::unbounded();
let traversals_cache = Arc::new(new_traversals_lazy_cache(hord_config.cache_size));

let inner_ctx = if hord_config.logs.chainhook {
self.ctx.clone()
} else {
Context::empty()
};

info!(
self.ctx.expect_logger(),
"Database successfully updated, service will start streaming blocks"
);

let _ = start_event_observer(
event_observer_config.clone(),
observer_command_tx,
observer_command_rx,
Some(observer_event_tx),
self.ctx.clone(),
inner_ctx,
);

loop {
Expand Down Expand Up @@ -305,7 +316,7 @@ impl Service {
Ok(dbs) => dbs,
Err(e) => {
self.ctx.try_log(|logger| {
slog::error!(logger, "Unable to open readwtite connection: {e}",)
error!(logger, "Unable to open readwtite connection: {e}",)
});
continue;
}
Expand All @@ -315,6 +326,11 @@ impl Service {
match chain_update {
BitcoinChainEvent::ChainUpdatedWithBlocks(ref mut data) => {
for block in data.new_blocks.iter_mut() {
info!(
self.ctx.expect_logger(),
"Block #{} received, starting processing", block.block_identifier.index
);

let mut cache_l1 = HashMap::new();
let mut hint = InscriptionHeigthHint::new();
if let Err(e) = update_hord_db_and_augment_bitcoin_block_v3(
Expand All @@ -328,7 +344,7 @@ impl Service {
&self.ctx,
) {
self.ctx.try_log(|logger| {
slog::error!(
error!(
logger,
"Unable to insert bitcoin block {} in hord_db: {e}",
block.block_identifier.index
Expand All @@ -338,6 +354,11 @@ impl Service {
}
}
BitcoinChainEvent::ChainUpdatedWithReorg(ref mut data) => {
info!(
self.ctx.expect_logger(),
"Re-org detected"
);

for block in data.blocks_to_rollback.iter() {
if let Err(e) = revert_hord_db_with_augmented_bitcoin_block(
block,
Expand All @@ -346,7 +367,7 @@ impl Service {
&self.ctx,
) {
self.ctx.try_log(|logger| {
slog::error!(
error!(
logger,
"Unable to rollback bitcoin block {}: {e}",
block.block_identifier
Expand All @@ -369,7 +390,7 @@ impl Service {
&self.ctx,
) {
self.ctx.try_log(|logger| {
slog::error!(
error!(
logger,
"Unable to apply bitcoin block {} with hord_db: {e}",
block.block_identifier.index
Expand All @@ -380,7 +401,7 @@ impl Service {
}
};
self.ctx.try_log(|logger| {
slog::info!(
info!(
logger,
"Flushing traversals_cache ({} entries)",
traversals_cache.len()
Expand Down

0 comments on commit 5ff9906

Please sign in to comment.