use alloy_chains::NamedChain;
use alloy_primitives::{hex, Address, FixedBytes, TxHash, U256};
use tracing::Span;
use url::Url;
#[inline]
pub fn get_message_sent_event(
tx_hash: TxHash,
source_chain: &NamedChain,
destination_chain: &NamedChain,
) -> Span {
tracing::info_span!(
"cctp_rs.get_message_sent_event",
tx_hash = %tx_hash,
source_chain = %source_chain,
destination_chain = %destination_chain,
error.type = tracing::field::Empty,
error.message = tracing::field::Empty,
error.source = tracing::field::Empty,
otel.status_code = "OK",
)
}
#[inline]
pub fn get_attestation_with_retry(
message_hash: &FixedBytes<32>,
source_chain: &NamedChain,
destination_chain: &NamedChain,
max_attempts: u32,
poll_interval_secs: u64,
) -> Span {
tracing::info_span!(
"cctp_rs.get_attestation_with_retry",
message_hash = %hex::encode(message_hash),
source_chain = %source_chain,
destination_chain = %destination_chain,
max_attempts = max_attempts,
poll_interval_secs = poll_interval_secs,
error.type = tracing::field::Empty,
error.message = tracing::field::Empty,
error.source = tracing::field::Empty,
otel.status_code = "OK",
)
}
#[inline]
pub fn get_v2_attestation_with_retry(
tx_hash: TxHash,
source_chain: &NamedChain,
destination_chain: &NamedChain,
max_attempts: u32,
poll_interval_secs: u64,
) -> Span {
tracing::info_span!(
"cctp_rs.get_attestation_with_retry",
tx_hash = %tx_hash,
source_chain = %source_chain,
destination_chain = %destination_chain,
max_attempts = max_attempts,
poll_interval_secs = poll_interval_secs,
version = "v2",
error.type = tracing::field::Empty,
error.message = tracing::field::Empty,
error.source = tracing::field::Empty,
otel.status_code = "OK",
)
}
#[inline]
pub fn get_attestation(url: &Url, attempt: u32) -> Span {
tracing::debug_span!(
"cctp_rs.get_attestation",
url = %url,
attempt = attempt,
)
}
#[inline]
pub fn process_attestation_response(status_code: u16, attempt: u32) -> Span {
tracing::debug_span!(
"cctp_rs.process_attestation_response",
status_code = status_code,
attempt = attempt,
)
}
#[inline]
pub fn deposit_for_burn(
from_address: &Address,
recipient: &Address,
destination_domain: u32,
token_address: &Address,
amount: &U256,
) -> Span {
tracing::info_span!(
"cctp_rs.deposit_for_burn",
from_address = %from_address,
recipient = %recipient,
destination_domain = destination_domain,
token_address = %token_address,
amount = %amount,
error.type = tracing::field::Empty,
error.message = tracing::field::Empty,
error.context = tracing::field::Empty,
otel.status_code = "OK",
)
}
#[inline]
pub fn send_transaction(tx_hash: &str, source_chain: &NamedChain) -> Span {
tracing::debug_span!(
"cctp_rs.send_transaction",
tx_hash = tx_hash,
source_chain = %source_chain,
)
}
#[inline]
pub fn wait_for_confirmation(
tx_hash: TxHash,
chain: &NamedChain,
required_confirmations: u64,
) -> Span {
tracing::debug_span!(
"cctp_rs.wait_for_confirmation",
tx_hash = %tx_hash,
chain = %chain,
required_confirmations = required_confirmations,
)
}
#[inline]
pub fn receive_message(
message_hash: &FixedBytes<32>,
destination_chain: &NamedChain,
attestation_length: usize,
) -> Span {
tracing::info_span!(
"cctp_rs.receive_message",
message_hash = %hex::encode(message_hash),
destination_chain = %destination_chain,
attestation_length_bytes = attestation_length,
)
}
#[inline]
pub fn http_request(method: &str, url: &Url, request_id: Option<&str>) -> Span {
tracing::trace_span!(
"cctp_rs.http_request",
http.method = method,
http.url = %url,
http.request_id = request_id,
)
}
#[inline]
pub fn rpc_call(method: &str, chain: &NamedChain, params_summary: &str) -> Span {
tracing::trace_span!(
"cctp_rs.rpc_call",
rpc.method = method,
rpc.chain = %chain,
rpc.params = params_summary,
)
}
#[inline]
pub fn get_transaction_receipt(tx_hash: TxHash, chain: &NamedChain) -> Span {
tracing::debug_span!(
"cctp_rs.get_transaction_receipt",
tx_hash = %tx_hash,
chain = %chain,
)
}
pub fn record_error<E: std::error::Error>(error: &E) {
let current_span = tracing::Span::current();
current_span.record(
"error.type",
error.to_string().split(':').next().unwrap_or("Unknown"),
);
current_span.record("error.message", error.to_string());
current_span.record("otel.status_code", "ERROR");
if let Some(source) = error.source() {
current_span.record("error.source", source.to_string());
}
}
pub fn record_error_with_context(
error_type: &str,
error_message: &str,
additional_context: Option<&str>,
) {
let current_span = tracing::Span::current();
current_span.record("error.type", error_type);
current_span.record("error.message", error_message);
current_span.record("otel.status_code", "ERROR");
if let Some(context) = additional_context {
current_span.record("error.context", context);
}
}