Skip to content

Commit

Permalink
Opentelemetry instrumentation implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusz-reichert authored and m-reichert committed Nov 7, 2024
1 parent 62be80c commit 6931294
Show file tree
Hide file tree
Showing 20 changed files with 387 additions and 78 deletions.
33 changes: 1 addition & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 18 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ edition = "2018"
[features]
liquid = [ "elements" ]
electrum-discovery = [ "electrum-client"]
default = ["no-otlp-tracing"]
otlp-tracing = [
"tracing/max_level_trace",
"tracing-subscriber",
"opentelemetry",
"tracing-opentelemetry",
"opentelemetry-otlp",
"opentelemetry-semantic-conventions"
]
no-otlp-tracing = [
"tracing/max_level_off"
]

[dependencies]
arraydeque = "0.5.1"
Expand Down Expand Up @@ -52,12 +64,12 @@ hyper = "0.14"
hyperlocal = "0.8"
# close to same tokio version as dependent by hyper v0.14 and hyperlocal 0.8 -- things can go awry if they mismatch
tokio = { version = "1", features = ["sync", "macros", "rt-multi-thread", "rt"] }
opentelemetry = { version = "0.20.0", features = ["rt-tokio"] }
tracing-opentelemetry = "0.21.0"
opentelemetry-otlp = { version = "0.13.0", default-features = false, features = ["http-proto", "reqwest-client"] }
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
opentelemetry-semantic-conventions = "0.12.0"
tracing = { version = "0.1.40", features = ["async-await", "log"] }
opentelemetry = { version = "0.20.0", features = ["rt-tokio"], optional = true }
tracing-opentelemetry = { version = "0.21.0", optional = true }
opentelemetry-otlp = { version = "0.13.0", default-features = false, features = ["http-proto", "reqwest-client"], optional = true }
tracing-subscriber = { version = "0.3.17", default-features = false, features = ["env-filter", "fmt"], optional = true }
opentelemetry-semantic-conventions = { version = "0.12.0", optional = true }
tracing = { version = "0.1.40", default-features = false, features = ["attributes"], optional = true }

# optional dependencies for electrum-discovery
electrum-client = { version = "0.8", optional = true }
Expand Down
30 changes: 25 additions & 5 deletions src/bin/electrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ use electrs::{
signal::Waiter,
};

#[cfg(feature = "otlp-tracing")]
use electrs::otlp_trace;

#[cfg(feature = "liquid")]
use electrs::elements::AssetRegistry;
use electrs::metrics::MetricOpts;
Expand Down Expand Up @@ -85,9 +88,12 @@ fn run_server(config: Arc<Config>) -> Result<()> {
match Mempool::update(&mempool, &daemon) {
Ok(_) => break,
Err(e) => {
warn!("Error performing initial mempool update, trying again in 5 seconds: {}", e.display_chain());
warn!(
"Error performing initial mempool update, trying again in 5 seconds: {}",
e.display_chain()
);
signal.wait(Duration::from_secs(5), false)?;
},
}
}
}

Expand Down Expand Up @@ -117,7 +123,6 @@ fn run_server(config: Arc<Config>) -> Result<()> {
));

loop {

main_loop_count.inc();

if let Err(err) = signal.wait(Duration::from_secs(5), true) {
Expand All @@ -137,7 +142,10 @@ fn run_server(config: Arc<Config>) -> Result<()> {
// Update mempool
if let Err(e) = Mempool::update(&mempool, &daemon) {
// Log the error if the result is an Err
warn!("Error updating mempool, skipping mempool update: {}", e.display_chain());
warn!(
"Error updating mempool, skipping mempool update: {}",
e.display_chain()
);
}

// Update subscribed clients
Expand All @@ -147,10 +155,22 @@ fn run_server(config: Arc<Config>) -> Result<()> {
Ok(())
}

fn main() {
fn main_() {
let config = Arc::new(Config::from_args());
if let Err(e) = run_server(config) {
error!("server failed: {}", e.display_chain());
process::exit(1);
}
}

#[cfg(not(feature = "otlp-tracing"))]
fn main() {
main_();
}

#[cfg(feature = "otlp-tracing")]
#[tokio::main]
async fn main() {
let _tracing_guard = otlp_trace::init_tracing("electrs");
main_()
}
16 changes: 9 additions & 7 deletions src/bin/tx-fingerprint-stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,15 @@ fn main() {

//info!("{:?},{:?}", txid, blockid);

let prevouts = chain.lookup_txos(
tx.input
.iter()
.filter(|txin| has_prevout(txin))
.map(|txin| txin.previous_output)
.collect(),
).unwrap();
let prevouts = chain
.lookup_txos(
tx.input
.iter()
.filter(|txin| has_prevout(txin))
.map(|txin| txin.previous_output)
.collect(),
)
.unwrap();

let total_out: u64 = tx.output.iter().map(|out| out.value.to_sat()).sum();
let small_out = tx
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ impl Config {
stderrlog::Timestamp::Off
});
log.init().expect("logging initialization failed");

let config = Config {
log,
network_type,
Expand Down
Loading

0 comments on commit 6931294

Please sign in to comment.