Skip to content

Commit

Permalink
feat(cli): Add span with swap_id to all logs caused by swap (#96)
Browse files Browse the repository at this point in the history
* feat(cli): Add span with swap_id to all logs caused by swap
  • Loading branch information
binarybaron authored Sep 22, 2024
1 parent 1dd35b3 commit f91255f
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions swap/src/cli/api/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ use std::net::SocketAddr;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use tracing::debug_span;
use tracing::Instrument;
use tracing::Span;
use typeshare::typeshare;
use uuid::Uuid;

Expand All @@ -38,6 +40,11 @@ pub trait Request {
async fn request(self, ctx: Arc<Context>) -> Result<Self::Response>;
}

/// This generates a tracing span which is attached to all logs caused by a swap
fn get_swap_tracing_span(swap_id: Uuid) -> Span {
debug_span!("swap", swap_id = %swap_id)
}

// BuyXmr
#[typeshare]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
Expand All @@ -62,7 +69,10 @@ impl Request for BuyXmrArgs {
type Response = BuyXmrResponse;

async fn request(self, ctx: Arc<Context>) -> Result<Self::Response> {
buy_xmr(self, ctx).await
let swap_id = Uuid::new_v4();
let swap_span = get_swap_tracing_span(swap_id);

buy_xmr(self, swap_id, ctx).instrument(swap_span).await
}
}

Expand All @@ -84,7 +94,9 @@ impl Request for ResumeSwapArgs {
type Response = ResumeSwapResponse;

async fn request(self, ctx: Arc<Context>) -> Result<Self::Response> {
resume_swap(self, ctx).await
let swap_span = get_swap_tracing_span(self.swap_id);

resume_swap(self, ctx).instrument(swap_span).await
}
}

Expand All @@ -100,7 +112,9 @@ impl Request for CancelAndRefundArgs {
type Response = serde_json::Value;

async fn request(self, ctx: Arc<Context>) -> Result<Self::Response> {
cancel_and_refund(self, ctx).await
let swap_span = get_swap_tracing_span(self.swap_id);

cancel_and_refund(self, ctx).instrument(swap_span).await
}
}

Expand Down Expand Up @@ -536,6 +550,7 @@ pub async fn get_swap_info(
#[tracing::instrument(fields(method = "buy_xmr"), skip(context))]
pub async fn buy_xmr(
buy_xmr: BuyXmrArgs,
swap_id: Uuid,
context: Arc<Context>,
) -> Result<BuyXmrResponse, anyhow::Error> {
let BuyXmrArgs {
Expand All @@ -544,8 +559,6 @@ pub async fn buy_xmr(
monero_receive_address,
} = buy_xmr;

let swap_id = Uuid::new_v4();

let bitcoin_wallet = Arc::clone(
context
.bitcoin_wallet
Expand Down

0 comments on commit f91255f

Please sign in to comment.