Skip to content

Commit

Permalink
state: storage: tx: Fix trace nesting in db transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
joeykraut committed Dec 13, 2024
1 parent 3bc2a54 commit d77e73a
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ itertools = "0.10"
serde = { version = "1.0" }
serde_json = "1.0.64"
tracing = "0.1"
tracing-opentelemetry = "0.22"
metrics = "=0.22.3"
lazy_static = "1.4"

Expand Down
1 change: 1 addition & 0 deletions state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ serde_json = "1.0"
slog = { version = "2.2", features = ["max_level_trace"] }
tempfile = { version = "3.8", optional = true }
tracing = { workspace = true, features = ["log"] }
tracing-opentelemetry = { workspace = true }
tracing-slog = "0.2"
tui = "0.19"
tui-logger = "0.8"
Expand Down
20 changes: 13 additions & 7 deletions state/src/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use job_types::{
use libmdbx::{RO, RW};
use system_bus::SystemBus;
use system_clock::SystemClock;
use tracing::{error, info_span, Instrument};
use tracing::{error, info_span};
use util::{err_str, raw_err_str};

use crate::{
Expand Down Expand Up @@ -303,15 +303,18 @@ impl StateInner {
{
let db = self.db.clone();
tokio::task::spawn_blocking(move || {
// Create a new read tx
let _thread_span_guard = info_span!("db_read_thread").entered();
let tx = db.new_read_tx()?;
let _fn_span = tracing::info_span!("db_read_operation").entered();

// Execute the operation
let op_span = info_span!("db_read_operation").entered();
let res = f(&tx)?;
drop(_fn_span); // End the operation span before committing
drop(op_span); // End the operation span before committing

tx.commit()?;
Ok(res)
})
.instrument(info_span!("db_read_thread"))
.await
.map_err(err_str!(StateError::Runtime))?
}
Expand All @@ -328,15 +331,18 @@ impl StateInner {
{
let db = self.db.clone();
tokio::task::spawn_blocking(move || {
// Create a new write tx
let _thread_span_guard = info_span!("db_write_thread").entered();
let tx = db.new_write_tx()?;
let _fn_span = tracing::info_span!("db_write_operation").entered();

// Execute the operation
let op_span = info_span!("db_write_operation").entered();
let res = f(&tx)?;
drop(_fn_span); // End the operation span before committing
drop(op_span); // End the operation span before committing

tx.commit()?;
Ok(res)
})
.instrument(info_span!("db_write_thread"))
.await
.map_err(err_str!(StateError::Runtime))?
}
Expand Down
5 changes: 5 additions & 0 deletions state/src/storage/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{ops::Bound, path::Path};

use libmdbx::{Database, Geometry, WriteMap, RO, RW};
use serde::{Deserialize, Serialize};
use tracing::instrument;
use util::err_str;

use crate::{ciborium_serialize, NUM_TABLES};
Expand Down Expand Up @@ -149,24 +150,28 @@ impl DB {
}

/// Create a new read-only transaction
#[instrument(skip(self))]
pub fn new_read_tx(&self) -> Result<StateTxn<RO>, StorageError> {
let txn = self.new_raw_read_tx()?;
Ok(StateTxn::new(txn))
}

/// Create a new raw read-only transaction
#[instrument(skip(self))]
pub fn new_raw_read_tx(&self) -> Result<DbTxn<RO>, StorageError> {
let txn = self.db.begin_ro_txn().map_err(StorageError::BeginTx)?;
Ok(DbTxn::new(txn))
}

/// Create a new read-write transaction
#[instrument(skip(self))]
pub fn new_write_tx(&self) -> Result<StateTxn<RW>, StorageError> {
let txn = self.new_raw_write_tx()?;
Ok(StateTxn::new(txn))
}

/// Create a new read-write transaction
#[instrument(skip(self))]
pub fn new_raw_write_tx(&self) -> Result<DbTxn<RW>, StorageError> {
self.db.begin_rw_txn().map_err(StorageError::BeginTx).map(DbTxn::new)
}
Expand Down
2 changes: 2 additions & 0 deletions state/src/storage/tx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use std::collections::VecDeque;
use libmdbx::{
Error as MdbxError, Table, TableFlags, Transaction, TransactionKind, WriteFlags, WriteMap, RW,
};
use tracing::instrument;

use crate::ALL_TABLES;

Expand Down Expand Up @@ -59,6 +60,7 @@ impl<'db, T: TransactionKind> StateTxn<'db, T> {
}

/// Commit the transaction
#[instrument(skip(self))]
pub fn commit(self) -> Result<(), StorageError> {
self.inner.commit()
}
Expand Down
2 changes: 1 addition & 1 deletion util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ serde_json = { workspace = true }
tracing = { workspace = true }
tracing-serde = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] }
tracing-opentelemetry = "0.22"
tracing-opentelemetry = { workspace = true }
opentelemetry_sdk = { version = "0.21", features = ["trace", "rt-tokio"] }
opentelemetry-otlp = "0.14"
opentelemetry = { version = "0.21", default-features = false, features = [
Expand Down
2 changes: 1 addition & 1 deletion workers/network-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ util = { path = "../../util" }
itertools = "0.11"
serde_json = { workspace = true }
tracing = { workspace = true }
tracing-opentelemetry = "0.22"
tracing-opentelemetry = { workspace = true }
uuid = "1.1.2"

0 comments on commit d77e73a

Please sign in to comment.