Skip to content

Commit

Permalink
refactor: pull o11y/pretty into its own crate (near#8894)
Browse files Browse the repository at this point in the history
This is a first step toward reducing dependencies in near-primitives.
See near#8888 for context.

`near-o11y` has too many dependencies to be pulled into crates like
`near-primitives` that only need it for pretty printing.

The new crate `near-fmt` is light-weight, it only depends on
`near-primitives-core` and contains only the pretty printing code.
  • Loading branch information
jakmeier authored Apr 6, 2023
1 parent 9a79995 commit bb5b4b5
Show file tree
Hide file tree
Showing 23 changed files with 93 additions and 61 deletions.
14 changes: 13 additions & 1 deletion Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ members = [
"tools/storage-usage-delta-calculator",
"tools/themis",
"utils/config",
"utils/fmt",
"utils/mainnet-res",
"utils/near-cache",
"utils/stdx",
Expand Down Expand Up @@ -177,6 +178,7 @@ near-crypto = { path = "core/crypto" }
near-dyn-configs = { path = "core/dyn-configs" }
near-epoch-manager = { path = "chain/epoch-manager" }
near-flat-storage = { path = "tools/flat-storage" }
near-fmt = { path = "utils/fmt" }
near-indexer = { path = "chain/indexer" }
near-indexer-primitives = { path = "chain/indexer-primitives" }
near-jsonrpc = { path = "chain/jsonrpc" }
Expand Down
1 change: 1 addition & 0 deletions chain/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ time.workspace = true

delay-detector.workspace = true
near-async.workspace = true
near-fmt.workspace = true
near-o11y.workspace = true
near-crypto.workspace = true
near-performance-metrics.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions chain/network/src/peer/peer_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use actix::fut::future::wrap_future;
use actix::{Actor as _, ActorContext as _, ActorFutureExt as _, AsyncContext as _};
use lru::LruCache;
use near_crypto::Signature;
use near_o11y::{handler_debug_span, log_assert, pretty, OpenTelemetrySpanExt, WithSpanContext};
use near_o11y::{handler_debug_span, log_assert, OpenTelemetrySpanExt, WithSpanContext};
use near_performance_metrics_macros::perf;
use near_primitives::hash::CryptoHash;
use near_primitives::network::{AnnounceAccount, PeerId};
Expand Down Expand Up @@ -1559,7 +1559,7 @@ impl actix::Handler<stream::Frame> for PeerActor {
let mut peer_msg = match self.parse_message(&msg) {
Ok(msg) => msg,
Err(err) => {
tracing::debug!(target: "network", "Received invalid data {} from {}: {}", pretty::AbbrBytes(&msg), self.peer_info, err);
tracing::debug!(target: "network", "Received invalid data {} from {}: {}", near_fmt::AbbrBytes(&msg), self.peer_info, err);
return;
}
};
Expand Down
1 change: 0 additions & 1 deletion core/o11y/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ mod io_tracer;
pub mod log_config;
pub mod macros;
pub mod metrics;
pub mod pretty;
pub mod testonly;

/// Produce a tracing-event for target "io_tracer" that will be consumed by the
Expand Down
2 changes: 1 addition & 1 deletion core/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ tokio.workspace = true
tracing.workspace = true

near-crypto.workspace = true
near-o11y.workspace = true
near-fmt.workspace = true
near-primitives-core.workspace = true
near-rpc-error-macro.workspace = true
near-vm-errors.workspace = true
Expand Down
6 changes: 3 additions & 3 deletions core/primitives/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::transaction::{Action, TransferAction};
use crate::types::{AccountId, Balance, ShardId};
use borsh::{BorshDeserialize, BorshSerialize};
use near_crypto::{KeyType, PublicKey};
use near_o11y::pretty;
use near_fmt::AbbrBytes;
use std::borrow::Borrow;
use std::fmt;

Expand Down Expand Up @@ -162,7 +162,7 @@ impl fmt::Debug for DataReceipt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DataReceipt")
.field("data_id", &self.data_id)
.field("data", &format_args!("{}", pretty::AbbrBytes(self.data.as_deref())))
.field("data", &format_args!("{}", AbbrBytes(self.data.as_deref())))
.finish()
}
}
Expand Down Expand Up @@ -197,7 +197,7 @@ pub struct ReceivedData {
impl fmt::Debug for ReceivedData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ReceivedData")
.field("data", &format_args!("{}", pretty::AbbrBytes(self.data.as_deref())))
.field("data", &format_args!("{}", AbbrBytes(self.data.as_deref())))
.finish()
}
}
Expand Down
12 changes: 6 additions & 6 deletions core/primitives/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::serialize::{base64_format, dec_format};
use crate::types::{AccountId, Balance, Gas, Nonce};
use borsh::{BorshDeserialize, BorshSerialize};
use near_crypto::{PublicKey, Signature};
use near_o11y::pretty;
use near_fmt::{AbbrBytes, Slice};
use near_primitives_core::profile::{ProfileDataV2, ProfileDataV3};
use near_primitives_core::types::Compute;
use std::borrow::Borrow;
Expand Down Expand Up @@ -132,7 +132,7 @@ impl From<DeployContractAction> for Action {
impl fmt::Debug for DeployContractAction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DeployContractAction")
.field("code", &format_args!("{}", pretty::AbbrBytes(&self.code)))
.field("code", &format_args!("{}", AbbrBytes(&self.code)))
.finish()
}
}
Expand All @@ -159,7 +159,7 @@ impl fmt::Debug for FunctionCallAction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FunctionCallAction")
.field("method_name", &format_args!("{}", &self.method_name))
.field("args", &format_args!("{}", pretty::AbbrBytes(&self.args)))
.field("args", &format_args!("{}", AbbrBytes(&self.args)))
.field("gas", &format_args!("{}", &self.gas))
.field("deposit", &format_args!("{}", &self.deposit))
.finish()
Expand Down Expand Up @@ -350,7 +350,7 @@ impl fmt::Debug for ExecutionStatus {
ExecutionStatus::Unknown => f.write_str("Unknown"),
ExecutionStatus::Failure(e) => f.write_fmt(format_args!("Failure({})", e)),
ExecutionStatus::SuccessValue(v) => {
f.write_fmt(format_args!("SuccessValue({})", pretty::AbbrBytes(v)))
f.write_fmt(format_args!("SuccessValue({})", AbbrBytes(v)))
}
ExecutionStatus::SuccessReceiptId(receipt_id) => {
f.write_fmt(format_args!("SuccessReceiptId({})", receipt_id))
Expand Down Expand Up @@ -458,8 +458,8 @@ impl Default for ExecutionMetadata {
impl fmt::Debug for ExecutionOutcome {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ExecutionOutcome")
.field("logs", &pretty::Slice(&self.logs))
.field("receipt_ids", &pretty::Slice(&self.receipt_ids))
.field("logs", &Slice(&self.logs))
.field("receipt_ids", &Slice(&self.receipt_ids))
.field("burnt_gas", &self.gas_burnt)
.field("compute_usage", &self.compute_usage.unwrap_or_default())
.field("tokens_burnt", &self.tokens_burnt)
Expand Down
8 changes: 4 additions & 4 deletions core/primitives/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use crate::version::{ProtocolVersion, Version};
use borsh::{BorshDeserialize, BorshSerialize};
use chrono::DateTime;
use near_crypto::{PublicKey, Signature};
use near_o11y::pretty;
use near_fmt::{AbbrBytes, Slice};
use near_primitives_core::config::{ActionCosts, ExtCosts, ParameterCost, VMConfig};
use near_primitives_core::runtime::fees::Fee;
use num_rational::Rational32;
Expand Down Expand Up @@ -1272,7 +1272,7 @@ impl fmt::Debug for FinalExecutionStatus {
FinalExecutionStatus::Started => f.write_str("Started"),
FinalExecutionStatus::Failure(e) => f.write_fmt(format_args!("Failure({:?})", e)),
FinalExecutionStatus::SuccessValue(v) => {
f.write_fmt(format_args!("SuccessValue({})", pretty::AbbrBytes(v)))
f.write_fmt(format_args!("SuccessValue({})", AbbrBytes(v)))
}
}
}
Expand Down Expand Up @@ -1321,7 +1321,7 @@ impl fmt::Debug for ExecutionStatusView {
ExecutionStatusView::Unknown => f.write_str("Unknown"),
ExecutionStatusView::Failure(e) => f.write_fmt(format_args!("Failure({:?})", e)),
ExecutionStatusView::SuccessValue(v) => {
f.write_fmt(format_args!("SuccessValue({})", pretty::AbbrBytes(v)))
f.write_fmt(format_args!("SuccessValue({})", AbbrBytes(v)))
}
ExecutionStatusView::SuccessReceiptId(receipt_id) => {
f.write_fmt(format_args!("SuccessReceiptId({})", receipt_id))
Expand Down Expand Up @@ -1635,7 +1635,7 @@ impl fmt::Debug for FinalExecutionOutcomeView {
.field("status", &self.status)
.field("transaction", &self.transaction)
.field("transaction_outcome", &self.transaction_outcome)
.field("receipts_outcome", &pretty::Slice(&self.receipts_outcome))
.field("receipts_outcome", &Slice(&self.receipts_outcome))
.finish()
}
}
Expand Down
1 change: 1 addition & 0 deletions core/store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ tokio.workspace = true
tracing.workspace = true

near-crypto.workspace = true
near-fmt.workspace = true
near-o11y.workspace = true
near-primitives.workspace = true

Expand Down
26 changes: 11 additions & 15 deletions core/store/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::io;

use near_o11y::pretty;

use crate::DBCol;
use near_fmt::{AbbrBytes, StorageKey};
use std::io;

pub(crate) mod rocksdb;

Expand Down Expand Up @@ -76,26 +74,24 @@ impl std::fmt::Debug for DBOp {
Self::Set { col, key, value } => f
.debug_struct("Set")
.field("col", col)
.field("key", &pretty::StorageKey(key))
.field("value", &pretty::AbbrBytes(value))
.field("key", &StorageKey(key))
.field("value", &AbbrBytes(value))
.finish(),
Self::Insert { col, key, value } => f
.debug_struct("Insert")
.field("col", col)
.field("key", &pretty::StorageKey(key))
.field("value", &pretty::AbbrBytes(value))
.field("key", &StorageKey(key))
.field("value", &AbbrBytes(value))
.finish(),
Self::UpdateRefcount { col, key, value } => f
.debug_struct("UpdateRefcount")
.field("col", col)
.field("key", &pretty::StorageKey(key))
.field("value", &pretty::AbbrBytes(value))
.finish(),
Self::Delete { col, key } => f
.debug_struct("Delete")
.field("col", col)
.field("key", &pretty::StorageKey(key))
.field("key", &StorageKey(key))
.field("value", &AbbrBytes(value))
.finish(),
Self::Delete { col, key } => {
f.debug_struct("Delete").field("col", col).field("key", &StorageKey(key)).finish()
}
Self::DeleteAll { col } => f.debug_struct("DeleteAll").field("col", col).finish(),
Self::DeleteRange { col, from, to } => f
.debug_struct("DeleteRange")
Expand Down
33 changes: 14 additions & 19 deletions core/store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use db::{
LARGEST_TARGET_HEIGHT_KEY, LATEST_KNOWN_KEY, TAIL_KEY,
};
use near_crypto::PublicKey;
use near_o11y::pretty;
use near_fmt::{AbbrBytes, StorageKey};
use near_primitives::account::{AccessKey, Account};
use near_primitives::contract::ContractCode;
pub use near_primitives::errors::StorageError;
Expand Down Expand Up @@ -283,7 +283,7 @@ impl Store {
target: "store",
db_op = "get",
col = %column,
key = %pretty::StorageKey(key),
key = %StorageKey(key),
size = value.as_deref().map(<[u8]>::len)
);
Ok(value)
Expand Down Expand Up @@ -657,22 +657,22 @@ impl StoreUpdate {
for op in &self.transaction.ops {
match op {
DBOp::Insert { col, key, value } => {
tracing::trace!(target: "store", db_op = "insert", col = %col, key = %pretty::StorageKey(key), size = value.len(), value = %pretty::AbbrBytes(value),)
tracing::trace!(target: "store", db_op = "insert", col = %col, key = %StorageKey(key), size = value.len(), value = %AbbrBytes(value),)
}
DBOp::Set { col, key, value } => {
tracing::trace!(target: "store", db_op = "set", col = %col, key = %pretty::StorageKey(key), size = value.len(), value = %pretty::AbbrBytes(value))
tracing::trace!(target: "store", db_op = "set", col = %col, key = %StorageKey(key), size = value.len(), value = %AbbrBytes(value))
}
DBOp::UpdateRefcount { col, key, value } => {
tracing::trace!(target: "store", db_op = "update_rc", col = %col, key = %pretty::StorageKey(key), size = value.len(), value = %pretty::AbbrBytes(value))
tracing::trace!(target: "store", db_op = "update_rc", col = %col, key = %StorageKey(key), size = value.len(), value = %AbbrBytes(value))
}
DBOp::Delete { col, key } => {
tracing::trace!(target: "store", db_op = "delete", col = %col, key = %pretty::StorageKey(key))
tracing::trace!(target: "store", db_op = "delete", col = %col, key = %StorageKey(key))
}
DBOp::DeleteAll { col } => {
tracing::trace!(target: "store", db_op = "delete_all", col = %col)
}
DBOp::DeleteRange { col, from, to } => {
tracing::trace!(target: "store", db_op = "delete_range", col = %col, from = %pretty::StorageKey(from), to = %pretty::StorageKey(to))
tracing::trace!(target: "store", db_op = "delete_range", col = %col, from = %StorageKey(from), to = %StorageKey(to))
}
}
}
Expand All @@ -699,21 +699,16 @@ impl fmt::Debug for StoreUpdate {
writeln!(f, "Store Update {{")?;
for op in self.transaction.ops.iter() {
match op {
DBOp::Insert { col, key, .. } => {
writeln!(f, " + {col} {}", pretty::StorageKey(key))?
}
DBOp::Set { col, key, .. } => writeln!(f, " = {col} {}", pretty::StorageKey(key))?,
DBOp::Insert { col, key, .. } => writeln!(f, " + {col} {}", StorageKey(key))?,
DBOp::Set { col, key, .. } => writeln!(f, " = {col} {}", StorageKey(key))?,
DBOp::UpdateRefcount { col, key, .. } => {
writeln!(f, " ± {col} {}", pretty::StorageKey(key))?
writeln!(f, " ± {col} {}", StorageKey(key))?
}
DBOp::Delete { col, key } => writeln!(f, " - {col} {}", pretty::StorageKey(key))?,
DBOp::Delete { col, key } => writeln!(f, " - {col} {}", StorageKey(key))?,
DBOp::DeleteAll { col } => writeln!(f, " - {col} (all)")?,
DBOp::DeleteRange { col, from, to } => writeln!(
f,
" - {col} [{}, {})",
pretty::StorageKey(from),
pretty::StorageKey(to)
)?,
DBOp::DeleteRange { col, from, to } => {
writeln!(f, " - {col} [{}, {})", StorageKey(from), StorageKey(to))?
}
}
}
writeln!(f, "}}")
Expand Down
1 change: 1 addition & 0 deletions integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ near-client.workspace = true
near-client-primitives.workspace = true
near-crypto.workspace = true
near-epoch-manager.workspace = true
near-fmt.workspace = true
near-jsonrpc.workspace = true
near-jsonrpc-client.workspace = true
near-jsonrpc-primitives.workspace = true
Expand Down
3 changes: 1 addition & 2 deletions integration-tests/src/tests/client/cold_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use near_chain::{ChainGenesis, Provenance};
use near_chain_configs::Genesis;
use near_client::test_utils::TestEnv;
use near_crypto::{InMemorySigner, KeyType};
use near_o11y::pretty;
use near_o11y::testonly::init_test_logger;
use near_primitives::block::Tip;
use near_primitives::sharding::ShardChunk;
Expand All @@ -25,7 +24,7 @@ use std::collections::HashSet;
use strum::IntoEnumIterator;

fn check_key(first_store: &Store, second_store: &Store, col: DBCol, key: &[u8]) {
let pretty_key = pretty::StorageKey(key);
let pretty_key = near_fmt::StorageKey(key);
tracing::debug!("Checking {:?} {:?}", col, pretty_key);

let first_res = first_store.get(col, key);
Expand Down
1 change: 1 addition & 0 deletions runtime/near-vm-logic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ tracing = { workspace = true, optional = true }

near-crypto.workspace = true
near-account-id.workspace = true
near-fmt.workspace = true
near-o11y.workspace = true
near-primitives.workspace = true
near-primitives-core.workspace = true
Expand Down
Loading

0 comments on commit bb5b4b5

Please sign in to comment.