From e241eef6da45c3b8847e3fb1814d1e9b25a8688f Mon Sep 17 00:00:00 2001 From: Lucas Sunsi Abreu Date: Sun, 21 Jul 2024 10:56:33 -0300 Subject: [PATCH] replace tracing with otel --- Cargo.toml | 2 +- src/lib.rs | 38 ++++++++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a3d165c..36e5b74 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,5 +6,5 @@ edition = "2021" [dependencies] bitcoin = { version= "0.31.0", features = ["std"], default-features = false } bitcoincore-rpc = { version = "0.18.0", default-features = false } +opentelemetry = { version = "0.23.0", features = ["trace"], default-features = false } tokio = { version = "1.12.0", features = ["rt", "macros"], default-features = false } -tracing = { version = "0.1.37", default-features = false } diff --git a/src/lib.rs b/src/lib.rs index 6d528c6..42ecead 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,26 +4,29 @@ use bitcoincore_rpc::{ json::{AddressType, GetBalancesResult, ListTransactionResult}, RpcApi, }; +use opentelemetry::trace::FutureExt; use std::{future::Future, sync::Arc}; -use tracing::Instrument; pub use bitcoincore_rpc as rpc; #[derive(Clone)] pub struct Btc { client: Arc, + tracer: Arc, } pub fn build(rpc_user: &str, rpc_password: &str, rpc_url: &str) -> bitcoincore_rpc::Result { let auth = bitcoincore_rpc::Auth::UserPass(rpc_user.into(), rpc_password.into()); bitcoincore_rpc::Client::new(rpc_url, auth).map(|client| Btc { + tracer: Arc::new(opentelemetry::global::tracer("btcore")), client: Arc::new(client), }) } impl Btc { pub fn get_block_count(&self) -> impl Future> { + start_span(self.tracer.as_ref(), "get_block_count"); let client = self.client.clone(); async move { @@ -31,13 +34,14 @@ impl Btc { .await .unwrap() } - .instrument(span!("get_block_count")) + .with_current_context() } pub fn list_transactions( &self, count: usize, ) -> impl Future>> { + start_span(self.tracer.as_ref(), "listtransactions"); let client = self.client.clone(); async move { @@ -47,7 +51,7 @@ impl Btc { .await .unwrap() } - .instrument(span!("listtransactions")) + .with_current_context() } pub fn list_since_block( @@ -56,6 +60,7 @@ impl Btc { confirmations: usize, ) -> impl Future, BlockHash)>> { + start_span(self.tracer.as_ref(), "listsinceblock"); let client = self.client.clone(); async move { @@ -67,10 +72,11 @@ impl Btc { .await .unwrap() } - .instrument(span!("listsinceblock")) + .with_current_context() } pub fn get_balances(&self) -> impl Future> { + start_span(self.tracer.as_ref(), "getbalances"); let client = self.client.clone(); async move { @@ -78,13 +84,14 @@ impl Btc { .await .unwrap() } - .instrument(span!("getbalances")) + .with_current_context() } pub fn get_balance( &self, number_of_confirmations: Option, ) -> impl Future> { + start_span(self.tracer.as_ref(), "getbalance"); let client = self.client.clone(); async move { @@ -92,13 +99,14 @@ impl Btc { .await .unwrap() } - .instrument(span!("getbalance")) + .with_current_context() } pub fn get_transaction( &self, txid: Txid, ) -> impl Future> { + start_span(self.tracer.as_ref(), "gettransaction"); let client = self.client.clone(); async move { @@ -106,13 +114,14 @@ impl Btc { .await .unwrap() } - .instrument(span!("gettransaction")) + .with_current_context() } pub fn generate_address( &self, address_type: AddressType, ) -> impl Future>> { + start_span(self.tracer.as_ref(), "getnewaddress"); let client = self.client.clone(); async move { @@ -120,10 +129,22 @@ impl Btc { .await .unwrap() } - .instrument(span!("getnewaddress")) + .with_current_context() } } +fn start_span(tracer: &impl opentelemetry::trace::Tracer, method: &'static str) { + opentelemetry::trace::SpanBuilder::from_name(method) + .with_kind(opentelemetry::trace::SpanKind::Client) + .with_attributes([ + opentelemetry::KeyValue::new("service.name", "btcore"), + opentelemetry::KeyValue::new("rpc.service", "bitcoind"), + opentelemetry::KeyValue::new("rpc.system", "jsonrpc"), + opentelemetry::KeyValue::new("rpc.method", method), + ]) + .start(tracer); +} + #[macro_export] macro_rules! span { ($method:literal) => { @@ -153,6 +174,7 @@ mod test { let auth = bitcoincore_rpc::Auth::UserPass(USERNAME.into(), PASSWORD.into()); let client = bitcoincore_rpc::Client::new(RPC_URL, auth).map(|client| Btc { + tracer: Arc::new(opentelemetry::global::tracer("btcore")), client: Arc::new(client), })?;