Skip to content

Commit

Permalink
jsonrpc: add instrumentation to read apis (#19738)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill authored Oct 8, 2024
1 parent faa3eb0 commit 15b64b4
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 38 deletions.
8 changes: 8 additions & 0 deletions crates/sui-core/src/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,8 @@ impl AuthorityState {
self.prepare_certificate(&execution_guard, certificate, input_objects, epoch_store)
}

#[instrument(skip_all)]
#[allow(clippy::type_complexity)]
pub async fn dry_exec_transaction(
&self,
transaction: TransactionData,
Expand Down Expand Up @@ -1961,6 +1963,7 @@ impl AuthorityState {

/// The object ID for gas can be any object ID, even for an uncreated object
#[allow(clippy::collapsible_else_if)]
#[instrument(skip_all)]
pub async fn dev_inspect_transaction_block(
&self,
sender: SuiAddress,
Expand Down Expand Up @@ -5169,6 +5172,7 @@ impl RandomnessRoundReceiver {

#[async_trait]
impl TransactionKeyValueStoreTrait for AuthorityState {
#[instrument(skip(self))]
async fn multi_get(
&self,
transactions: &[TransactionDigest],
Expand Down Expand Up @@ -5206,6 +5210,7 @@ impl TransactionKeyValueStoreTrait for AuthorityState {
Ok((txns, fx, evts))
}

#[instrument(skip(self))]
async fn multi_get_checkpoints(
&self,
checkpoint_summaries: &[CheckpointSequenceNumber],
Expand Down Expand Up @@ -5258,6 +5263,7 @@ impl TransactionKeyValueStoreTrait for AuthorityState {
Ok((summaries, contents, summaries_by_digest, contents_by_digest))
}

#[instrument(skip(self))]
async fn deprecated_get_transaction_checkpoint(
&self,
digest: TransactionDigest,
Expand All @@ -5267,6 +5273,7 @@ impl TransactionKeyValueStoreTrait for AuthorityState {
.map(|res| res.map(|(_epoch, checkpoint)| checkpoint))
}

#[instrument(skip(self))]
async fn get_object(
&self,
object_id: ObjectID,
Expand All @@ -5277,6 +5284,7 @@ impl TransactionKeyValueStoreTrait for AuthorityState {
.map_err(Into::into)
}

#[instrument(skip(self))]
async fn multi_get_transaction_checkpoint(
&self,
digests: &[TransactionDigest],
Expand Down
1 change: 1 addition & 0 deletions crates/sui-indexer/src/handlers/tx_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl TxChangesProcessor {
.start_timer();
let object_change: Vec<_> = get_object_changes(
self,
effects,
tx.sender(),
effects.modified_at_versions(),
effects.all_changed_objects(),
Expand Down
4 changes: 4 additions & 0 deletions crates/sui-json-rpc/src/balance_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ use sui_types::gas_coin::GAS;
use sui_types::object::{Object, Owner};
use sui_types::storage::WriteKind;
use sui_types::transaction::InputObjectKind;
use tracing::instrument;

#[instrument(skip_all, fields(transaction_digest = %effects.transaction_digest()))]
pub async fn get_balance_changes_from_effect<P: ObjectProvider<Error = E>, E>(
object_provider: &P,
effects: &TransactionEffects,
Expand Down Expand Up @@ -80,6 +82,7 @@ pub async fn get_balance_changes_from_effect<P: ObjectProvider<Error = E>, E>(
.await
}

#[instrument(skip_all)]
pub async fn get_balance_changes<P: ObjectProvider<Error = E>, E>(
object_provider: &P,
modified_at_version: &[(ObjectID, SequenceNumber, Option<ObjectDigest>)],
Expand Down Expand Up @@ -120,6 +123,7 @@ pub async fn get_balance_changes<P: ObjectProvider<Error = E>, E>(
.collect())
}

#[instrument(skip_all)]
async fn fetch_coins<P: ObjectProvider<Error = E>, E>(
object_provider: &P,
objects: &[(ObjectID, SequenceNumber, Option<ObjectDigest>)],
Expand Down
4 changes: 4 additions & 0 deletions crates/sui-json-rpc/src/object_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ use std::collections::BTreeMap;
use sui_json_rpc_types::ObjectChange;
use sui_types::base_types::{ObjectID, ObjectRef, SequenceNumber, SuiAddress};
use sui_types::effects::ObjectRemoveKind;
use sui_types::effects::{TransactionEffects, TransactionEffectsAPI};
use sui_types::object::Owner;
use sui_types::storage::WriteKind;
use tracing::instrument;

use crate::ObjectProvider;

#[instrument(skip_all, fields(transaction_digest = %effects.transaction_digest()))]
pub async fn get_object_changes<P: ObjectProvider<Error = E>, E>(
object_provider: &P,
effects: &TransactionEffects,
sender: SuiAddress,
modified_at_versions: Vec<(ObjectID, SequenceNumber)>,
all_changed_objects: Vec<(ObjectRef, Owner, WriteKind)>,
Expand Down
7 changes: 7 additions & 0 deletions crates/sui-json-rpc/src/read_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ impl ReadApi {
Ok(checkpoints)
}

#[instrument(skip_all)]
async fn multi_get_transaction_blocks_internal(
&self,
digests: Vec<TransactionDigest>,
Expand Down Expand Up @@ -474,6 +475,7 @@ impl ReadApi {

results.push(get_object_changes(
&object_cache,
effects,
resp.transaction
.as_ref()
.ok_or_else(|| {
Expand Down Expand Up @@ -891,6 +893,7 @@ impl ReadApiServer for ReadApi {
let sender = input.data().intent_message().value.sender();
let object_changes = get_object_changes(
&object_cache,
effects,
sender,
effects.modified_at_versions(),
effects.all_changed_objects(),
Expand Down Expand Up @@ -1106,6 +1109,7 @@ impl SuiRpcModule for ReadApi {
}
}

#[instrument(skip_all)]
fn to_sui_transaction_events(
fullnode_api: &ReadApi,
tx_digest: TransactionDigest,
Expand Down Expand Up @@ -1145,6 +1149,7 @@ pub enum ObjectDisplayError {
StateReadError(#[from] StateReadError),
}

#[instrument(skip(fullnode_api, kv_store))]
async fn get_display_fields(
fullnode_api: &ReadApi,
kv_store: &Arc<TransactionKeyValueStore>,
Expand All @@ -1169,6 +1174,7 @@ async fn get_display_fields(
})
}

#[instrument(skip(kv_store, fullnode_api))]
async fn get_display_object_by_type(
kv_store: &Arc<TransactionKeyValueStore>,
fullnode_api: &ReadApi,
Expand Down Expand Up @@ -1362,6 +1368,7 @@ fn get_value_from_move_struct(
}
}

#[instrument(skip_all)]
fn convert_to_response(
cache: IntermediateTransactionResponse,
opts: &SuiTransactionBlockResponseOptions,
Expand Down
2 changes: 2 additions & 0 deletions crates/sui-json-rpc/src/transaction_execution_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ impl TransactionExecutionApi {
Some(object_cache) if opts.show_object_changes => Some(
get_object_changes(
object_cache,
&response.effects.effects,
sender,
response.effects.effects.modified_at_versions(),
response.effects.effects.all_changed_objects(),
Expand Down Expand Up @@ -302,6 +303,7 @@ impl TransactionExecutionApi {
.await?;
let object_changes = get_object_changes(
&object_cache,
&transaction_effects,
sender,
transaction_effects.modified_at_versions(),
transaction_effects.all_changed_objects(),
Expand Down
86 changes: 59 additions & 27 deletions crates/sui-storage/src/http_key_value_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use sui_types::{
},
transaction::Transaction,
};
use tap::TapFallible;
use tap::{TapFallible, TapOptional};
use tracing::{error, info, instrument, trace, warn};

use crate::key_value_store::{TransactionKeyValueStore, TransactionKeyValueStoreTrait};
Expand Down Expand Up @@ -86,6 +86,45 @@ pub enum Key {
ObjectKey(ObjectID, VersionNumber),
}

impl Key {
/// Return a string representation of the key type
pub fn ty(&self) -> &'static str {
match self {
Key::Tx(_) => "tx",
Key::Fx(_) => "fx",
Key::Events(_) => "ev",
Key::CheckpointContents(_) => "cc",
Key::CheckpointSummary(_) => "cs",
Key::CheckpointContentsByDigest(_) => "cc",
Key::CheckpointSummaryByDigest(_) => "cs",
Key::TxToCheckpoint(_) => "tx2c",
Key::ObjectKey(_, _) => "ob",
}
}

pub fn encode(&self) -> String {
match self {
Key::Tx(digest) => encode_digest(digest),
Key::Fx(digest) => encode_digest(digest),
Key::Events(digest) => encode_digest(digest),
Key::CheckpointContents(seq) => {
encoded_tagged_key(&TaggedKey::CheckpointSequenceNumber(*seq))
}
Key::CheckpointSummary(seq) => {
encoded_tagged_key(&TaggedKey::CheckpointSequenceNumber(*seq))
}
Key::CheckpointContentsByDigest(digest) => encode_digest(digest),
Key::CheckpointSummaryByDigest(digest) => encode_digest(digest),
Key::TxToCheckpoint(digest) => encode_digest(digest),
Key::ObjectKey(object_id, version) => encode_object_key(object_id, version),
}
}

pub fn to_path_elements(&self) -> (String, &'static str) {
(self.encode(), self.ty())
}
}

#[derive(Clone, Debug)]
enum Value {
Tx(Box<Transaction>),
Expand All @@ -96,26 +135,6 @@ enum Value {
TxToCheckpoint(CheckpointSequenceNumber),
}

pub fn key_to_path_elements(key: &Key) -> SuiResult<(String, &'static str)> {
match key {
Key::Tx(digest) => Ok((encode_digest(digest), "tx")),
Key::Fx(digest) => Ok((encode_digest(digest), "fx")),
Key::Events(digest) => Ok((encode_digest(digest), "ev")),
Key::CheckpointContents(seq) => Ok((
encoded_tagged_key(&TaggedKey::CheckpointSequenceNumber(*seq)),
"cc",
)),
Key::CheckpointSummary(seq) => Ok((
encoded_tagged_key(&TaggedKey::CheckpointSequenceNumber(*seq)),
"cs",
)),
Key::CheckpointContentsByDigest(digest) => Ok((encode_digest(digest), "cc")),
Key::CheckpointSummaryByDigest(digest) => Ok((encode_digest(digest), "cs")),
Key::TxToCheckpoint(digest) => Ok((encode_digest(digest), "tx2c")),
Key::ObjectKey(object_id, version) => Ok((encode_object_key(object_id, version), "ob")),
}
}

pub fn path_elements_to_key(digest: &str, type_: &str) -> anyhow::Result<Key> {
let decoded_digest = base64_url::decode(digest)?;

Expand Down Expand Up @@ -202,7 +221,7 @@ impl HttpKVStore {
}

fn get_url(&self, key: &Key) -> SuiResult<Url> {
let (digest, item_type) = key_to_path_elements(key)?;
let (digest, item_type) = key.to_path_elements();
let joined = self
.base_url
.join(&format!("{}/{}", digest, item_type))
Expand All @@ -225,14 +244,14 @@ impl HttpKVStore {
trace!("found cached data for url: {}, len: {:?}", url, res.len());
self.metrics
.key_value_store_num_fetches_success
.with_label_values(&["http_cache", "url"])
.with_label_values(&["http_cache", key.ty()])
.inc();
return Ok(Some(res));
}

self.metrics
.key_value_store_num_fetches_not_found
.with_label_values(&["http_cache", "url"])
.with_label_values(&["http_cache", key.ty()])
.inc();

let resp = self
Expand Down Expand Up @@ -508,9 +527,22 @@ impl TransactionKeyValueStoreTrait for HttpKVStore {
version: SequenceNumber,
) -> SuiResult<Option<Object>> {
let key = Key::ObjectKey(object_id, version);
self.fetch(key)
.await
.map(|maybe| maybe.and_then(|bytes| deser::<_, Object>(&key, bytes.as_ref())))
self.fetch(key).await.map(|maybe| {
maybe
.and_then(|bytes| deser::<_, Object>(&key, bytes.as_ref()))
.tap_some(|_| {
self.metrics
.key_value_store_num_fetches_success
.with_label_values(&["http", key.ty()])
.inc();
})
.tap_none(|| {
self.metrics
.key_value_store_num_fetches_not_found
.with_label_values(&["http", key.ty()])
.inc();
})
})
}

#[instrument(level = "trace", skip_all)]
Expand Down
Loading

0 comments on commit 15b64b4

Please sign in to comment.