Add tracing span attribute log_reference_id to logs caused by rpc call

This commit is contained in:
binarybaron 2023-08-23 00:37:25 +02:00
parent fb4265b2ea
commit 1ec16c6bc1
3 changed files with 121 additions and 139 deletions

View file

@ -15,17 +15,17 @@ use qrcode::QrCode;
use serde_json::json;
use std::cmp::min;
use std::convert::TryInto;
use std::fmt::Formatter;
use std::future::Future;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use tracing::{debug_span, Instrument, Span};
use tracing::{debug_span, Instrument, Span, field};
use uuid::Uuid;
#[derive(PartialEq, Debug)]
pub struct Request {
pub cmd: Method,
pub log_reference: Option<String>,
}
#[derive(Debug, PartialEq)]
@ -68,52 +68,60 @@ pub enum Method {
}
impl Method {
fn get_tracing_span(&self) -> Span {
match self {
Method::Balance => debug_span!("method", name = "Balance"),
fn get_tracing_span(&self, log_reference_id: Option<String>) -> Span {
let span = match self {
Method::Balance => debug_span!("method", name = "Balance", log_reference_id=field::Empty),
Method::BuyXmr { swap_id, .. } => {
debug_span!("method", name="BuyXmr", swap_id=%swap_id)
debug_span!("method", name="BuyXmr", swap_id=%swap_id, log_reference_id=field::Empty)
}
Method::CancelAndRefund { swap_id } => {
debug_span!("method", name="CancelAndRefund", swap_id=%swap_id)
debug_span!("method", name="CancelAndRefund", swap_id=%swap_id, log_reference_id=field::Empty)
}
Method::Resume { swap_id } => {
debug_span!("method", name="Resume", swap_id=%swap_id)
debug_span!("method", name="Resume", swap_id=%swap_id, log_reference_id=field::Empty)
}
Method::Config => debug_span!("method", name = "Config"),
Method::Config => debug_span!("method", name = "Config", log_reference_id=field::Empty),
Method::ExportBitcoinWallet => {
debug_span!("method", name = "ExportBitcoinWallet")
debug_span!("method", name = "ExportBitcoinWallet", log_reference_id=field::Empty)
}
Method::GetCurrentSwap => {
debug_span!("method", name = "GetCurrentSwap")
debug_span!("method", name = "GetCurrentSwap", log_reference_id=field::Empty)
}
Method::GetSwapInfo { .. } => {
debug_span!("method", name = "GetSwapInfo")
debug_span!("method", name = "GetSwapInfo", log_reference_id=field::Empty)
}
Method::History => debug_span!("method", name = "History"),
Method::History => debug_span!("method", name = "History", log_reference_id=field::Empty),
Method::ListSellers { .. } => {
debug_span!("method", name = "ListSellers")
debug_span!("method", name = "ListSellers", log_reference_id=field::Empty)
}
Method::MoneroRecovery { .. } => {
debug_span!("method", name = "MoneroRecovery")
debug_span!("method", name = "MoneroRecovery", log_reference_id=field::Empty)
}
Method::RawHistory => debug_span!("method", name = "RawHistory"),
Method::RawHistory => debug_span!("method", name = "RawHistory", log_reference_id=field::Empty),
Method::StartDaemon { .. } => {
debug_span!("method", name = "StartDaemon")
debug_span!("method", name = "StartDaemon", log_reference_id=field::Empty)
}
Method::SuspendCurrentSwap => {
debug_span!("method", name = "SuspendCurrentSwap")
debug_span!("method", name = "SuspendCurrentSwap", log_reference_id=field::Empty)
}
Method::WithdrawBtc { .. } => {
debug_span!("method", name = "WithdrawBtc")
debug_span!("method", name = "WithdrawBtc", log_reference_id=field::Empty)
}
};
if let Some(log_reference_id) = log_reference_id {
span.record("log_reference_id", &log_reference_id.as_str());
}
span
}
}
impl Request {
pub fn new(cmd: Method) -> Request {
Request { cmd }
Request { cmd, log_reference: None }
}
pub fn with_id(cmd: Method, id: Option<String>) -> Request {
Request { cmd, log_reference: id }
}
// We pass the outer tracing span down to this function such that it can be passed down to other spawned tokio tasks
@ -125,13 +133,18 @@ impl Request {
) -> Result<serde_json::Value> {
match self.cmd {
Method::SuspendCurrentSwap => {
context.swap_lock.send_suspend_signal().await?;
let swap_id = context.swap_lock.get_current_swap_id().await;
Ok(json!({
"swapId": swap_id,
"success": true
}))
if swap_id.is_some() {
context.swap_lock.send_suspend_signal().await?;
Ok(json!({
"success": true,
"swapId": swap_id.unwrap()
}))
} else {
bail!("No swap is currently running")
}
}
Method::GetSwapInfo { swap_id } => {
let bitcoin_wallet = context
@ -157,7 +170,7 @@ impl Request {
let start_date = context.db.get_swap_start_date(swap_id).await?;
let state_name = format!("{:?}", swap_state);
let state_name = format!("{}", swap_state);
let timelock = match swap_state {
BobState::Started { .. }
@ -181,6 +194,7 @@ impl Request {
};
Ok(json!({
"swapId": swap_id,
"seller": {
"peerId": peerId.to_string(),
"addresses": addresses
@ -189,6 +203,7 @@ impl Request {
"startDate": start_date,
// If none return null, if some unwrap and return as json
"timelock": timelock.map(|tl| tl.map(|tl| json!(tl)).unwrap_or(json!(null))).unwrap_or(json!(null)),
// Use display to get the string representation of the state
"stateName": state_name,
}))
}
@ -660,7 +675,7 @@ impl Request {
}
pub async fn call(self, context: Arc<Context>) -> Result<serde_json::Value> {
let method_span = self.cmd.get_tracing_span();
let method_span = self.cmd.get_tracing_span(self.log_reference.clone());
self.handle_cmd(context, method_span.clone())
.instrument(method_span)
@ -677,6 +692,7 @@ fn qr_code(value: &impl ToString) -> Result<String> {
.build();
Ok(qr_code)
}
pub async fn determine_btc_to_swap<FB, TB, FMG, TMG, FS, TS, FFE, TFE>(
json: bool,
bid_quote: impl Future<Output = Result<BidQuote>>,