From 35f47649fb53297a83590e1871d9c9e160f20052 Mon Sep 17 00:00:00 2001 From: lollerfirst Date: Tue, 10 Dec 2024 18:33:14 +0100 Subject: [PATCH 01/12] elasticsearch Layer for tracing --- Cargo.lock | 169 ++++++++++++++++++++++++++++++++- crates/cdk/Cargo.toml | 3 + crates/cdk/src/mint/loggers.rs | 85 +++++++++++++++++ crates/cdk/src/mint/mod.rs | 1 + 4 files changed, 256 insertions(+), 2 deletions(-) create mode 100644 crates/cdk/src/mint/loggers.rs diff --git a/Cargo.lock b/Cargo.lock index 5f020dcc..3123a814 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -161,6 +161,19 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" +[[package]] +name = "async-compression" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df895a515f70646414f4b45c0b79082783b80552b373a68283012928df56f522" +dependencies = [ + "flate2", + "futures-core", + "memchr", + "pin-project-lite", + "tokio", +] + [[package]] name = "async-io" version = "1.13.0" @@ -677,8 +690,10 @@ dependencies = [ "bip39", "bitcoin 0.32.5", "cbor-diag", + "chrono", "ciborium", "criterion", + "elasticsearch", "futures", "getrandom", "instant", @@ -695,6 +710,7 @@ dependencies = [ "tokio", "tokio-tungstenite 0.19.0", "tracing", + "tracing-subscriber", "url", "utoipa", "uuid", @@ -993,14 +1009,16 @@ dependencies = [ [[package]] name = "chrono" -version = "0.4.38" +version = "0.4.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +checksum = "7e36cc9d416881d2e24f9a963be5fb1cd90966419ac844274161d10488b3e825" dependencies = [ "android-tzdata", "iana-time-zone", + "js-sys", "num-traits", "serde", + "wasm-bindgen", "windows-targets 0.52.6", ] @@ -1413,12 +1431,39 @@ version = "0.15.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" +[[package]] +name = "dyn-clone" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d6ef0072f8a535281e4876be788938b528e9a1d43900b82c2569af7da799125" + [[package]] name = "either" version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" +[[package]] +name = "elasticsearch" +version = "8.16.0-alpha.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "774166217d4f9b96e9ab9d6832302e3d47196de507fddddb21de8184a39e2c6d" +dependencies = [ + "base64 0.22.1", + "bytes", + "dyn-clone", + "lazy_static", + "percent-encoding", + "reqwest", + "rustc_version", + "serde", + "serde_json", + "serde_with", + "tokio", + "url", + "void", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -1525,6 +1570,21 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f81ec6369c545a7d40e4589b5597581fa1c441fe1cce96dd1de43159910a36a2" +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + [[package]] name = "form_urlencoded" version = "1.2.1" @@ -1998,6 +2058,22 @@ dependencies = [ "tokio-io-timeout", ] +[[package]] +name = "hyper-tls" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" +dependencies = [ + "bytes", + "http-body-util", + "hyper 1.5.1", + "hyper-util", + "native-tls", + "tokio", + "tokio-native-tls", + "tower-service", +] + [[package]] name = "hyper-util" version = "0.1.10" @@ -2587,6 +2663,23 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "defc4c55412d89136f966bbb339008b474350e5e6e78d2714439c386b3137a03" +[[package]] +name = "native-tls" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8614eb2c83d59d1c8cc974dd3f920198647674a0a035e1af1fa58707e317466" +dependencies = [ + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework 2.11.1", + "security-framework-sys", + "tempfile", +] + [[package]] name = "negentropy" version = "0.3.1" @@ -2779,12 +2872,50 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" +[[package]] +name = "openssl" +version = "0.10.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6174bc48f102d208783c2c84bf931bb75927a617866870de8a4ea85597f871f5" +dependencies = [ + "bitflags 2.6.0", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.90", +] + [[package]] name = "openssl-probe" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" +[[package]] +name = "openssl-sys" +version = "0.9.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45abf306cbf99debc8195b66b7346498d7b10c210de50418b5ccd7ceba08c741" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "option-ext" version = "0.2.0" @@ -3411,6 +3542,7 @@ version = "0.12.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a77c62af46e79de0a562e1a9849205ffcb7fc1238876e9bd743357570e04046f" dependencies = [ + "async-compression", "base64 0.22.1", "bytes", "futures-core", @@ -3420,11 +3552,13 @@ dependencies = [ "http-body-util", "hyper 1.5.1", "hyper-rustls 0.27.3", + "hyper-tls", "hyper-util", "ipnet", "js-sys", "log", "mime", + "native-tls", "once_cell", "percent-encoding", "pin-project-lite", @@ -3438,8 +3572,10 @@ dependencies = [ "serde_urlencoded", "sync_wrapper 1.0.2", "tokio", + "tokio-native-tls", "tokio-rustls 0.26.0", "tokio-socks", + "tokio-util", "tower-service", "url", "wasm-bindgen", @@ -4459,6 +4595,16 @@ dependencies = [ "syn 2.0.90", ] +[[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] + [[package]] name = "tokio-rustls" version = "0.23.4" @@ -4717,6 +4863,16 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-serde" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "704b1aeb7be0d0a84fc9828cae51dab5970fee5088f83d1dd7ee6f6246fc6ff1" +dependencies = [ + "serde", + "tracing-core", +] + [[package]] name = "tracing-subscriber" version = "0.3.19" @@ -4727,12 +4883,15 @@ dependencies = [ "nu-ansi-term", "once_cell", "regex", + "serde", + "serde_json", "sharded-slab", "smallvec", "thread_local", "tracing", "tracing-core", "tracing-log", + "tracing-serde", ] [[package]] @@ -4990,6 +5149,12 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" + [[package]] name = "waker-fn" version = "1.2.0" diff --git a/crates/cdk/Cargo.toml b/crates/cdk/Cargo.toml index 83ea0401..5b1f2382 100644 --- a/crates/cdk/Cargo.toml +++ b/crates/cdk/Cargo.toml @@ -58,6 +58,9 @@ uuid = { version = "1", features = ["v4", "serde"] } # -Z minimal-versions sync_wrapper = "0.1.2" bech32 = "0.9.1" +elasticsearch = "8.16.0-alpha.1" +tracing-subscriber = { version = "0.3.19", features = ["fmt", "std", "json"] } +chrono = "0.4.39" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] tokio = { version = "1.21", features = [ diff --git a/crates/cdk/src/mint/loggers.rs b/crates/cdk/src/mint/loggers.rs new file mode 100644 index 00000000..8fd80439 --- /dev/null +++ b/crates/cdk/src/mint/loggers.rs @@ -0,0 +1,85 @@ +use chrono::Utc; +use elasticsearch::{Elasticsearch, IndexParts}; +use serde::Serialize; +use tokio::sync::mpsc::{self, Sender}; +use tracing::Event; +use tracing::field::{Field, Visit}; +use tracing_subscriber::layer::{Context, Layer}; +use tracing_subscriber::registry::LookupSpan; +use std::fmt::Write; + +pub const BACKLOG: usize = 10_000; + +struct Visitor<'a> { + output: &'a mut String, +} + +impl<'a> Visit for Visitor<'a> { + fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) { + let _ = write!(self.output, "{}={:?} ", field.name(), value); + } +} + +#[derive(Serialize)] +struct LogMessage { + timestamp: String, + level: String, + message: String, + target: String, +} + +pub struct ElasticsearchLayer { + sender: Sender, +} + +impl ElasticsearchLayer { + pub fn new(elasticsearch_url: &str, index: &str) -> Self { + let (sender, mut receiver) = mpsc::channel(BACKLOG); + let client = Elasticsearch::new( + elasticsearch::http::transport::Transport::single_node(elasticsearch_url).unwrap(), + ); + let index = index.to_string(); + + tokio::spawn(async move { + while let Some(log) = receiver.recv().await { + let log_json = serde_json::to_value(log).unwrap(); + if let Err(e) = client + .index(IndexParts::Index(&index)) + .body(log_json) + .send() + .await + { + eprintln!("Failed to send log to Elasticsearch: {:?}", e); + } + } + }); + + Self { sender } + } +} + +impl Layer for ElasticsearchLayer +where + S: tracing::Subscriber + for<'a> LookupSpan<'a>, +{ + fn on_event(&self, event: &Event<'_>, _ctx: Context<'_, S>) { + let timestamp = Utc::now().to_rfc3339(); + let level = event.metadata().level().to_string(); + let target = event.metadata().target().to_string(); + + // Manually process the event fields + let mut message = String::new(); + let mut visitor = Visitor { output: &mut message }; + event.record(&mut visitor); + + let log = LogMessage { + timestamp, + level, + message, + target, + }; + + // Non-blocking send to the channel + let _ = self.sender.try_send(log); + } +} diff --git a/crates/cdk/src/mint/mod.rs b/crates/cdk/src/mint/mod.rs index 844a8d0d..1c48b4a4 100644 --- a/crates/cdk/src/mint/mod.rs +++ b/crates/cdk/src/mint/mod.rs @@ -28,6 +28,7 @@ mod builder; mod check_spendable; mod info; mod keysets; +mod loggers; mod melt; mod mint_nut04; mod start_up_check; From a220ef65ced118cab70a0c592b1b74386463be64 Mon Sep 17 00:00:00 2001 From: lollerfirst Date: Thu, 12 Dec 2024 12:19:04 +0100 Subject: [PATCH 02/12] elasticsearch --- Cargo.lock | 220 ++++++++---------- crates/cdk-mintd/Cargo.toml | 3 + crates/cdk-mintd/src/lib.rs | 1 + .../src/mint => cdk-mintd/src}/loggers.rs | 5 +- crates/cdk-mintd/src/main.rs | 39 ++-- crates/cdk/Cargo.toml | 2 - crates/cdk/src/mint/mod.rs | 1 - 7 files changed, 137 insertions(+), 134 deletions(-) rename crates/{cdk/src/mint => cdk-mintd/src}/loggers.rs (93%) diff --git a/Cargo.lock b/Cargo.lock index 3123a814..b875a30e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -72,9 +72,9 @@ dependencies = [ [[package]] name = "allocator-api2" -version = "0.2.20" +version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45862d1c77f2228b9e10bc609d5bc203d86ebc9b87ad8d5d5167a6c9abf739d9" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" [[package]] name = "android-tzdata" @@ -148,9 +148,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.93" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c95c10ba0b00a02636238b814946408b1322d5ac4760326e6fb8ec956d85775" +checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7" dependencies = [ "backtrace", ] @@ -260,7 +260,7 @@ dependencies = [ "js-sys", "thiserror 1.0.69", "tokio", - "tokio-rustls 0.26.0", + "tokio-rustls 0.26.1", "tokio-socks", "tokio-tungstenite 0.24.0", "url", @@ -673,9 +673,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.2" +version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f34d93e62b03caf570cccc334cbc6c2fceca82f39211051345108adcba3eebdc" +checksum = "27f657647bcff5394bf56c7317665bbf790a137a50eaaa5c6bfbb9e27a518f2d" dependencies = [ "shlex", ] @@ -693,7 +693,6 @@ dependencies = [ "chrono", "ciborium", "criterion", - "elasticsearch", "futures", "getrandom", "instant", @@ -710,7 +709,6 @@ dependencies = [ "tokio", "tokio-tungstenite 0.19.0", "tracing", - "tracing-subscriber", "url", "utoipa", "uuid", @@ -873,13 +871,16 @@ dependencies = [ "cdk-redb", "cdk-sqlite", "cdk-strike", + "chrono", "clap", "config", + "elasticsearch", "futures", "home", "lightning-invoice", "rand", "serde", + "serde_json", "tokio", "tower-http", "tracing", @@ -1062,9 +1063,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.21" +version = "4.5.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb3b4b9e5a7c7514dfa52869339ee98b3156b0bfb4e8a77c4ff4babb64b1604f" +checksum = "3135e7ec2ef7b10c6ed8950f0f792ed96ee093fa088608f1c76e569722700c84" dependencies = [ "clap_builder", "clap_derive", @@ -1072,9 +1073,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.21" +version = "4.5.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b17a95aa67cc7b5ebd32aa5370189aa0d79069ef1c64ce893bd30fb24bff20ec" +checksum = "30582fc632330df2bd26877bde0c1f4470d57c582bbc070376afcd04d8cb4838" dependencies = [ "anstream", "anstyle", @@ -1096,9 +1097,9 @@ dependencies = [ [[package]] name = "clap_lex" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "afb84c814227b90d6895e01398aee0d8033c00e7466aca416fb6a8e0eb19d8a7" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" [[package]] name = "cln-rpc" @@ -1506,9 +1507,9 @@ dependencies = [ [[package]] name = "fastrand" -version = "2.2.0" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "486f806e73c5707928240ddc295403b1b93c96a02038563881c4a2fd84b81ac4" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" [[package]] name = "fedimint-tonic-lnd" @@ -1909,9 +1910,9 @@ dependencies = [ [[package]] name = "http" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" +checksum = "f16ca2af56261c99fba8bac40a10251ce8188205a4c448fbb745a2e4daa76fea" dependencies = [ "bytes", "fnv", @@ -1936,7 +1937,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" dependencies = [ "bytes", - "http 1.1.0", + "http 1.2.0", ] [[package]] @@ -1947,7 +1948,7 @@ checksum = "793429d76616a256bcb62c2a2ec2bed781c8307e797e2598c50010f2bee2544f" dependencies = [ "bytes", "futures-util", - "http 1.1.0", + "http 1.2.0", "http-body 1.0.1", "pin-project-lite", ] @@ -2003,7 +2004,7 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "http 1.1.0", + "http 1.2.0", "http-body 1.0.1", "httparse", "itoa", @@ -2034,14 +2035,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08afdbb5c31130e3034af566421053ab03787c640246a446327f550d11bcb333" dependencies = [ "futures-util", - "http 1.1.0", + "http 1.2.0", "hyper 1.5.1", "hyper-util", - "rustls 0.23.19", + "rustls 0.23.20", "rustls-native-certs 0.8.1", "rustls-pki-types", "tokio", - "tokio-rustls 0.26.0", + "tokio-rustls 0.26.1", "tower-service", "webpki-roots 0.26.7", ] @@ -2083,7 +2084,7 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "http 1.1.0", + "http 1.2.0", "http-body 1.0.1", "hyper 1.5.1", "pin-project-lite", @@ -2379,9 +2380,9 @@ checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" [[package]] name = "js-sys" -version = "0.3.74" +version = "0.3.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a865e038f7f6ed956f788f0d7d60c541fff74c7bd74272c5d4cf15c63743e705" +checksum = "6717b6b5b077764fb5966237269cb3c64edddde4b14ce42647430a78ced9e7b7" dependencies = [ "once_cell", "wasm-bindgen", @@ -2418,9 +2419,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.167" +version = "0.2.168" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc" +checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d" [[package]] name = "libredox" @@ -2612,9 +2613,9 @@ dependencies = [ [[package]] name = "minreq" -version = "2.12.0" +version = "2.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "763d142cdff44aaadd9268bebddb156ef6c65a0e13486bb81673cf2d8739f9b0" +checksum = "36a8e50e917e18a37d500d27d40b7bc7d127e71c0c94fb2d83f43b4afd308390" dependencies = [ "log", "serde", @@ -2987,7 +2988,7 @@ checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" dependencies = [ "cfg-if", "libc", - "redox_syscall 0.5.7", + "redox_syscall 0.5.8", "smallvec", "windows-targets 0.52.6", ] @@ -3033,20 +3034,20 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" [[package]] name = "pest" -version = "2.7.14" +version = "2.7.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "879952a81a83930934cbf1786752d6dedc3b1f29e8f8fb2ad1d0a36f377cf442" +checksum = "8b7cafe60d6cf8e62e1b9b2ea516a089c008945bb5a275416789e7db0bc199dc" dependencies = [ "memchr", - "thiserror 1.0.69", + "thiserror 2.0.6", "ucd-trie", ] [[package]] name = "pest_derive" -version = "2.7.14" +version = "2.7.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d214365f632b123a47fd913301e14c946c61d1c183ee245fa76eb752e59a02dd" +checksum = "816518421cfc6887a0d62bf441b6ffb4536fcc926395a69e1a85852d4363f57e" dependencies = [ "pest", "pest_generator", @@ -3054,9 +3055,9 @@ dependencies = [ [[package]] name = "pest_generator" -version = "2.7.14" +version = "2.7.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb55586734301717aea2ac313f50b2eb8f60d2fc3dc01d190eefa2e625f60c4e" +checksum = "7d1396fd3a870fc7838768d171b4616d5c91f6cc25e377b673d714567d99377b" dependencies = [ "pest", "pest_meta", @@ -3067,9 +3068,9 @@ dependencies = [ [[package]] name = "pest_meta" -version = "2.7.14" +version = "2.7.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b75da2a70cf4d9cb76833c990ac9cd3923c9a8905a8929789ce347c84564d03d" +checksum = "e1e58089ea25d717bfd31fb534e4f3afcc2cc569c70de3e239778991ea3b7dea" dependencies = [ "once_cell", "pest", @@ -3345,9 +3346,9 @@ dependencies = [ "quinn-proto", "quinn-udp", "rustc-hash", - "rustls 0.23.19", + "rustls 0.23.20", "socket2 0.5.8", - "thiserror 2.0.3", + "thiserror 2.0.6", "tokio", "tracing", ] @@ -3363,10 +3364,10 @@ dependencies = [ "rand", "ring 0.17.8", "rustc-hash", - "rustls 0.23.19", + "rustls 0.23.20", "rustls-pki-types", "slab", - "thiserror 2.0.3", + "thiserror 2.0.6", "tinyvec", "tracing", "web-time", @@ -3374,9 +3375,9 @@ dependencies = [ [[package]] name = "quinn-udp" -version = "0.5.7" +version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d5a626c6807713b15cac82a6acaccd6043c9a5408c24baae07611fec3f243da" +checksum = "52cd4b1eff68bf27940dd39811292c49e007f4d0b4c357358dc9b0197be6b527" dependencies = [ "cfg_aliases", "libc", @@ -3456,9 +3457,9 @@ dependencies = [ [[package]] name = "redb" -version = "2.2.0" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84b1de48a7cf7ba193e81e078d17ee2b786236eed1d3f7c60f8a09545efc4925" +checksum = "a7c2a94325f9c5826b17c42af11067230f503747f870117a28180e85696e21ba" dependencies = [ "libc", ] @@ -3474,9 +3475,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.7" +version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" +checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834" dependencies = [ "bitflags 2.6.0", ] @@ -3547,7 +3548,7 @@ dependencies = [ "bytes", "futures-core", "futures-util", - "http 1.1.0", + "http 1.2.0", "http-body 1.0.1", "http-body-util", "hyper 1.5.1", @@ -3563,7 +3564,7 @@ dependencies = [ "percent-encoding", "pin-project-lite", "quinn", - "rustls 0.23.19", + "rustls 0.23.20", "rustls-native-certs 0.8.1", "rustls-pemfile 2.2.0", "rustls-pki-types", @@ -3573,7 +3574,7 @@ dependencies = [ "sync_wrapper 1.0.2", "tokio", "tokio-native-tls", - "tokio-rustls 0.26.0", + "tokio-rustls 0.26.1", "tokio-socks", "tokio-util", "tower-service", @@ -3719,15 +3720,15 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.41" +version = "0.38.42" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7f649912bc1495e167a6edee79151c84b1bad49748cb4f1f1167f459f6224f6" +checksum = "f93dc38ecbab2eb790ff964bb77fa94faf256fd3e73285fd7ba0903b76bedb85" dependencies = [ "bitflags 2.6.0", "errno", "libc", "linux-raw-sys 0.4.14", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -3756,9 +3757,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.19" +version = "0.23.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "934b404430bb06b3fae2cba809eb45a1ab1aecd64491213d7c3301b88393f8d1" +checksum = "5065c3f250cbd332cd894be57c40fa52387247659b14a2d6041d121547903b1b" dependencies = [ "once_cell", "ring 0.17.8", @@ -4010,9 +4011,9 @@ checksum = "f97841a747eef040fcd2e7b3b9a220a7205926e60488e673d9e4926d27772ce5" [[package]] name = "serde" -version = "1.0.215" +version = "1.0.216" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f" +checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e" dependencies = [ "serde_derive", ] @@ -4030,9 +4031,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.215" +version = "1.0.216" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" +checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e" dependencies = [ "proc-macro2", "quote", @@ -4434,9 +4435,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28cce251fcbc87fac86a866eeb0d6c2d536fc16d06f184bb61aeae11aa4cee0c" dependencies = [ "cfg-if", - "fastrand 2.2.0", + "fastrand 2.3.0", "once_cell", - "rustix 0.38.41", + "rustix 0.38.42", "windows-sys 0.59.0", ] @@ -4451,11 +4452,11 @@ dependencies = [ [[package]] name = "thiserror" -version = "2.0.3" +version = "2.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c006c85c7651b3cf2ada4584faa36773bd07bac24acfb39f3c431b36d7e667aa" +checksum = "8fec2a1820ebd077e2b90c4df007bebf344cd394098a13c563957d0afc83ea47" dependencies = [ - "thiserror-impl 2.0.3", + "thiserror-impl 2.0.6", ] [[package]] @@ -4471,9 +4472,9 @@ dependencies = [ [[package]] name = "thiserror-impl" -version = "2.0.3" +version = "2.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f077553d607adc1caf65430528a576c757a71ed73944b66ebb58ef2bbd243568" +checksum = "d65750cab40f4ff1929fb1ba509e9914eb756131cef4210da8d5d700d26f6312" dependencies = [ "proc-macro2", "quote", @@ -4492,9 +4493,9 @@ dependencies = [ [[package]] name = "time" -version = "0.3.36" +version = "0.3.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" +checksum = "35e7868883861bd0e56d9ac6efcaaca0d6d5d82a2a7ec8209ff492c07cf37b21" dependencies = [ "deranged", "itoa", @@ -4513,9 +4514,9 @@ checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" [[package]] name = "time-macros" -version = "0.2.18" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" +checksum = "2834e6017e3e5e4b9834939793b282bc03b37a3336245fa820e35e233e2a85de" dependencies = [ "num-conv", "time-core", @@ -4558,9 +4559,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.41.1" +version = "1.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22cfb5bee7a6a52939ca9224d6ac897bb669134078daa8735560897f69de4d33" +checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551" dependencies = [ "backtrace", "bytes", @@ -4628,12 +4629,11 @@ dependencies = [ [[package]] name = "tokio-rustls" -version = "0.26.0" +version = "0.26.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c7bc40d0e5a97695bb96e27995cd3a08538541b0a846f65bba7a359f36700d4" +checksum = "5f6d0975eaace0cf0fcadee4e4aaa5da15b5c079146f2cffb67c113be122bf37" dependencies = [ - "rustls 0.23.19", - "rustls-pki-types", + "rustls 0.23.20", "tokio", ] @@ -4651,9 +4651,9 @@ dependencies = [ [[package]] name = "tokio-stream" -version = "0.1.16" +version = "0.1.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f4e6ce100d0eb49a2734f8c0812bcd324cf357d21810932c5df6b96ef2b86f1" +checksum = "eca58d7bba4a75707817a2c44174253f9236b2d5fbd055602e9d5c07c139a047" dependencies = [ "futures-core", "pin-project-lite", @@ -4695,19 +4695,19 @@ checksum = "edc5f74e248dc973e0dbb7b74c7e0d6fcc301c694ff50049504004ef4d0cdcd9" dependencies = [ "futures-util", "log", - "rustls 0.23.19", + "rustls 0.23.20", "rustls-pki-types", "tokio", - "tokio-rustls 0.26.0", + "tokio-rustls 0.26.1", "tungstenite 0.24.0", "webpki-roots 0.26.7", ] [[package]] name = "tokio-util" -version = "0.7.12" +version = "0.7.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61e7c3654c13bcd040d4a03abee2c75b1d14a37b423cf5a813ceae1cc903ec6a" +checksum = "d7fcaa8d55a2bdd6b83ace262b016eca0d79ee02818c5c1bcdf0305114081078" dependencies = [ "bytes", "futures-core", @@ -4863,16 +4863,6 @@ dependencies = [ "tracing-core", ] -[[package]] -name = "tracing-serde" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "704b1aeb7be0d0a84fc9828cae51dab5970fee5088f83d1dd7ee6f6246fc6ff1" -dependencies = [ - "serde", - "tracing-core", -] - [[package]] name = "tracing-subscriber" version = "0.3.19" @@ -4883,15 +4873,12 @@ dependencies = [ "nu-ansi-term", "once_cell", "regex", - "serde", - "serde_json", "sharded-slab", "smallvec", "thread_local", "tracing", "tracing-core", "tracing-log", - "tracing-serde", ] [[package]] @@ -4955,11 +4942,11 @@ dependencies = [ "byteorder", "bytes", "data-encoding", - "http 1.1.0", + "http 1.2.0", "httparse", "log", "rand", - "rustls 0.23.19", + "rustls 0.23.20", "rustls-pki-types", "sha1", "thiserror 1.0.69", @@ -5188,9 +5175,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.97" +version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d15e63b4482863c109d70a7b8706c1e364eb6ea449b201a76c5b89cedcec2d5c" +checksum = "a474f6281d1d70c17ae7aa6a613c87fce69a127e2624002df63dcb39d6cf6396" dependencies = [ "cfg-if", "once_cell", @@ -5199,13 +5186,12 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.97" +version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d36ef12e3aaca16ddd3f67922bc63e48e953f126de60bd33ccc0101ef9998cd" +checksum = "5f89bb38646b4f81674e8f5c3fb81b562be1fd936d84320f3264486418519c79" dependencies = [ "bumpalo", "log", - "once_cell", "proc-macro2", "quote", "syn 2.0.90", @@ -5214,9 +5200,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.47" +version = "0.4.49" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9dfaf8f50e5f293737ee323940c7d8b08a66a95a419223d9f41610ca08b0833d" +checksum = "38176d9b44ea84e9184eff0bc34cc167ed044f816accfe5922e54d84cf48eca2" dependencies = [ "cfg-if", "js-sys", @@ -5227,9 +5213,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.97" +version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "705440e08b42d3e4b36de7d66c944be628d579796b8090bfa3471478a2260051" +checksum = "2cc6181fd9a7492eef6fef1f33961e3695e4579b9872a6f7c83aee556666d4fe" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -5237,9 +5223,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.97" +version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98c9ae5a76e46f4deecd0f0255cc223cfa18dc9b261213b8aa0c7b36f61b3f1d" +checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2" dependencies = [ "proc-macro2", "quote", @@ -5250,15 +5236,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.97" +version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ee99da9c5ba11bd675621338ef6fa52296b76b83305e9b6e5c77d4c286d6d49" +checksum = "943aab3fdaaa029a6e0271b35ea10b72b943135afe9bffca82384098ad0e06a6" [[package]] name = "web-sys" -version = "0.3.74" +version = "0.3.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a98bc3c33f0fe7e59ad7cd041b89034fa82a7c2d4365ca538dda6cdaf513863c" +checksum = "04dd7223427d52553d3702c004d3b2fe07c148165faa56313cb00211e31c12bc" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/crates/cdk-mintd/Cargo.toml b/crates/cdk-mintd/Cargo.toml index f3369d6f..8d08a2b5 100644 --- a/crates/cdk-mintd/Cargo.toml +++ b/crates/cdk-mintd/Cargo.toml @@ -37,6 +37,9 @@ url = "2.3" utoipa = { version = "4", optional = true } utoipa-swagger-ui = { version = "4", features = ["axum"], optional = true } rand = "0.8.5" +elasticsearch = "8.16.0-alpha.1" +chrono = "0.4.39" +serde_json = "1.0.133" [features] swagger = ["cdk-axum/swagger", "dep:utoipa", "dep:utoipa-swagger-ui"] diff --git a/crates/cdk-mintd/src/lib.rs b/crates/cdk-mintd/src/lib.rs index f088ba3b..6fdb832e 100644 --- a/crates/cdk-mintd/src/lib.rs +++ b/crates/cdk-mintd/src/lib.rs @@ -6,6 +6,7 @@ pub mod cli; pub mod config; pub mod env_vars; pub mod setup; +pub mod loggers; fn expand_path(path: &str) -> Option { if path.starts_with('~') { diff --git a/crates/cdk/src/mint/loggers.rs b/crates/cdk-mintd/src/loggers.rs similarity index 93% rename from crates/cdk/src/mint/loggers.rs rename to crates/cdk-mintd/src/loggers.rs index 8fd80439..22adf828 100644 --- a/crates/cdk/src/mint/loggers.rs +++ b/crates/cdk-mintd/src/loggers.rs @@ -1,5 +1,7 @@ use chrono::Utc; +use elasticsearch::http::transport::Transport; use elasticsearch::{Elasticsearch, IndexParts}; +use elasticsearch::auth::Credentials; use serde::Serialize; use tokio::sync::mpsc::{self, Sender}; use tracing::Event; @@ -36,7 +38,8 @@ impl ElasticsearchLayer { pub fn new(elasticsearch_url: &str, index: &str) -> Self { let (sender, mut receiver) = mpsc::channel(BACKLOG); let client = Elasticsearch::new( - elasticsearch::http::transport::Transport::single_node(elasticsearch_url).unwrap(), + Transport::single_node(elasticsearch_url) + .unwrap(), ); let index = index.to_string(); diff --git a/crates/cdk-mintd/src/main.rs b/crates/cdk-mintd/src/main.rs index ac61daa6..af9bc018 100644 --- a/crates/cdk-mintd/src/main.rs +++ b/crates/cdk-mintd/src/main.rs @@ -21,13 +21,15 @@ use cdk::nuts::{ContactInfo, CurrencyUnit, MintVersion, PaymentMethod}; use cdk::types::LnKey; use cdk_mintd::cli::CLIArgs; use cdk_mintd::config::{self, DatabaseEngine, LnBackend}; +use cdk_mintd::loggers::ElasticsearchLayer; use cdk_mintd::setup::LnBackendSetup; use cdk_redb::MintRedbDatabase; use cdk_sqlite::MintSqliteDatabase; use clap::Parser; use tokio::sync::Notify; use tower_http::cors::CorsLayer; -use tracing_subscriber::EnvFilter; +use tracing_subscriber::layer::SubscriberExt; +use tracing_subscriber::{EnvFilter, Layer, Registry}; #[cfg(feature = "swagger")] use utoipa::OpenApi; @@ -38,18 +40,6 @@ const DEFAULT_CACHE_TTI_SECS: u64 = 1800; #[tokio::main] async fn main() -> anyhow::Result<()> { - let default_filter = "debug"; - - let sqlx_filter = "sqlx=warn"; - let hyper_filter = "hyper=warn"; - - let env_filter = EnvFilter::new(format!( - "{},{},{}", - default_filter, sqlx_filter, hyper_filter - )); - - tracing_subscriber::fmt().with_env_filter(env_filter).init(); - let args = CLIArgs::parse(); let work_dir = match args.work_dir { @@ -78,6 +68,29 @@ async fn main() -> anyhow::Result<()> { // ENV VARS will take **priority** over those in the config let settings = settings.from_env()?; + let default_filter = "debug"; + + let sqlx_filter = "sqlx=warn"; + let hyper_filter = "hyper=warn"; + + let env_filter = EnvFilter::new(format!( + "{},{},{}", + default_filter, sqlx_filter, hyper_filter + )); + + // Export logs to elastic search + let elasticsearch_layer = ElasticsearchLayer::new("http://localhost:9200", "cdk-mintd-logs"); + let subscriber = Registry::default() + .with(elasticsearch_layer) + .with(tracing_subscriber::fmt::layer().with_filter(env_filter)); + + tracing::subscriber::set_global_default(subscriber).expect("Failed to set subscriber"); + + // Test log messages + tracing::info!(message = "This is an info log", user_id = 123); + tracing::error!(message = "This is an error log"); + + let localstore: Arc + Send + Sync> = match settings.database.engine { DatabaseEngine::Sqlite => { diff --git a/crates/cdk/Cargo.toml b/crates/cdk/Cargo.toml index 5b1f2382..4cc37754 100644 --- a/crates/cdk/Cargo.toml +++ b/crates/cdk/Cargo.toml @@ -58,8 +58,6 @@ uuid = { version = "1", features = ["v4", "serde"] } # -Z minimal-versions sync_wrapper = "0.1.2" bech32 = "0.9.1" -elasticsearch = "8.16.0-alpha.1" -tracing-subscriber = { version = "0.3.19", features = ["fmt", "std", "json"] } chrono = "0.4.39" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] diff --git a/crates/cdk/src/mint/mod.rs b/crates/cdk/src/mint/mod.rs index 1c48b4a4..844a8d0d 100644 --- a/crates/cdk/src/mint/mod.rs +++ b/crates/cdk/src/mint/mod.rs @@ -28,7 +28,6 @@ mod builder; mod check_spendable; mod info; mod keysets; -mod loggers; mod melt; mod mint_nut04; mod start_up_check; From 1c9aba2911bfea98db59637efe3ce2080b367621 Mon Sep 17 00:00:00 2001 From: lollerfirst Date: Thu, 12 Dec 2024 15:55:01 +0100 Subject: [PATCH 03/12] something is working --- Cargo.lock | 103 ++++++++++++++++++-------------- crates/cdk-mintd/Cargo.toml | 2 +- crates/cdk-mintd/src/loggers.rs | 53 ++++++++++------ crates/cdk-mintd/src/main.rs | 8 +-- 4 files changed, 97 insertions(+), 69 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b875a30e..b0420d8b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -161,19 +161,6 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" -[[package]] -name = "async-compression" -version = "0.4.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df895a515f70646414f4b45c0b79082783b80552b373a68283012928df56f522" -dependencies = [ - "flate2", - "futures-core", - "memchr", - "pin-project-lite", - "tokio", -] - [[package]] name = "async-io" version = "1.13.0" @@ -286,6 +273,12 @@ dependencies = [ "tracing", ] +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + [[package]] name = "autocfg" version = "1.4.0" @@ -874,11 +867,11 @@ dependencies = [ "chrono", "clap", "config", - "elasticsearch", "futures", "home", "lightning-invoice", "rand", + "reqwest", "serde", "serde_json", "tokio", @@ -1432,12 +1425,6 @@ version = "0.15.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" -[[package]] -name = "dyn-clone" -version = "1.0.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d6ef0072f8a535281e4876be788938b528e9a1d43900b82c2569af7da799125" - [[package]] name = "either" version = "1.13.0" @@ -1445,24 +1432,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" [[package]] -name = "elasticsearch" -version = "8.16.0-alpha.1" +name = "encoding_rs" +version = "0.8.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "774166217d4f9b96e9ab9d6832302e3d47196de507fddddb21de8184a39e2c6d" +checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59" dependencies = [ - "base64 0.22.1", - "bytes", - "dyn-clone", - "lazy_static", - "percent-encoding", - "reqwest", - "rustc_version", - "serde", - "serde_json", - "serde_with", - "tokio", - "url", - "void", + "cfg-if", ] [[package]] @@ -1776,6 +1751,25 @@ dependencies = [ "tracing", ] +[[package]] +name = "h2" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccae279728d634d083c00f6099cb58f01cc99c145b84b8be2f6c74618d79922e" +dependencies = [ + "atomic-waker", + "bytes", + "fnv", + "futures-core", + "futures-sink", + "http 1.2.0", + "indexmap 2.7.0", + "slab", + "tokio", + "tokio-util", + "tracing", +] + [[package]] name = "half" version = "2.4.1" @@ -1981,7 +1975,7 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", - "h2", + "h2 0.3.26", "http 0.2.12", "http-body 0.4.6", "httparse", @@ -2004,6 +1998,7 @@ dependencies = [ "bytes", "futures-channel", "futures-util", + "h2 0.4.7", "http 1.2.0", "http-body 1.0.1", "httparse", @@ -3543,11 +3538,12 @@ version = "0.12.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a77c62af46e79de0a562e1a9849205ffcb7fc1238876e9bd743357570e04046f" dependencies = [ - "async-compression", "base64 0.22.1", "bytes", + "encoding_rs", "futures-core", "futures-util", + "h2 0.4.7", "http 1.2.0", "http-body 1.0.1", "http-body-util", @@ -3572,11 +3568,11 @@ dependencies = [ "serde_json", "serde_urlencoded", "sync_wrapper 1.0.2", + "system-configuration", "tokio", "tokio-native-tls", "tokio-rustls 0.26.1", "tokio-socks", - "tokio-util", "tower-service", "url", "wasm-bindgen", @@ -4422,6 +4418,27 @@ dependencies = [ "syn 2.0.90", ] +[[package]] +name = "system-configuration" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c879d448e9d986b661742763247d3693ed13609438cf3d006f51f5368a5ba6b" +dependencies = [ + "bitflags 2.6.0", + "core-foundation 0.9.4", + "system-configuration-sys", +] + +[[package]] +name = "system-configuration-sys" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e1d1b10ced5ca923a1fcb8d03e96b8d3268065d724548c0211415ff6ac6bac4" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "tagptr" version = "0.2.0" @@ -4736,7 +4753,7 @@ dependencies = [ "axum", "base64 0.21.7", "bytes", - "h2", + "h2 0.3.26", "http 0.2.12", "http-body 0.4.6", "hyper 0.14.31", @@ -5136,12 +5153,6 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" -[[package]] -name = "void" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" - [[package]] name = "waker-fn" version = "1.2.0" diff --git a/crates/cdk-mintd/Cargo.toml b/crates/cdk-mintd/Cargo.toml index 8d08a2b5..50b2e557 100644 --- a/crates/cdk-mintd/Cargo.toml +++ b/crates/cdk-mintd/Cargo.toml @@ -37,9 +37,9 @@ url = "2.3" utoipa = { version = "4", optional = true } utoipa-swagger-ui = { version = "4", features = ["axum"], optional = true } rand = "0.8.5" -elasticsearch = "8.16.0-alpha.1" chrono = "0.4.39" serde_json = "1.0.133" +reqwest = "0.12.9" [features] swagger = ["cdk-axum/swagger", "dep:utoipa", "dep:utoipa-swagger-ui"] diff --git a/crates/cdk-mintd/src/loggers.rs b/crates/cdk-mintd/src/loggers.rs index 22adf828..46463473 100644 --- a/crates/cdk-mintd/src/loggers.rs +++ b/crates/cdk-mintd/src/loggers.rs @@ -1,7 +1,5 @@ use chrono::Utc; -use elasticsearch::http::transport::Transport; -use elasticsearch::{Elasticsearch, IndexParts}; -use elasticsearch::auth::Credentials; +use reqwest::Client; use serde::Serialize; use tokio::sync::mpsc::{self, Sender}; use tracing::Event; @@ -35,24 +33,44 @@ pub struct ElasticsearchLayer { } impl ElasticsearchLayer { - pub fn new(elasticsearch_url: &str, index: &str) -> Self { + /// Create a new ElasticsearchLayer. + /// + /// # Arguments + /// + /// - `elasticsearch_url`: The base URL of the Elasticsearch instance. + /// - `index`: The name of the index where logs will be sent. + /// - `api_key`: Optional API key for authentication. + pub fn new(elasticsearch_url: &str, index: &str, api_key: Option<&str>) -> Self { let (sender, mut receiver) = mpsc::channel(BACKLOG); - let client = Elasticsearch::new( - Transport::single_node(elasticsearch_url) - .unwrap(), - ); - let index = index.to_string(); + let client = Client::new(); + let base_url = format!("{}/{}/_doc", elasticsearch_url.trim_end_matches('/'), index); + let api_key_header = api_key.map(|key| format!("ApiKey {}", key)); + // Spawn an async task to process logs and send them to Elasticsearch. tokio::spawn(async move { while let Some(log) = receiver.recv().await { - let log_json = serde_json::to_value(log).unwrap(); - if let Err(e) = client - .index(IndexParts::Index(&index)) - .body(log_json) - .send() - .await - { - eprintln!("Failed to send log to Elasticsearch: {:?}", e); + let mut request = client + .post(&base_url) + .json(&log); + + // Add the Authorization header if an API key is provided. + if let Some(ref key) = api_key_header { + request = request.header("Authorization", key); + } + + let response = request.send().await; + + match response { + Ok(res) if !res.status().is_success() => { + eprintln!( + "Failed to send log to Elasticsearch: HTTP {}", + res.status() + ); + } + Ok(_) => {} + Err(e) => { + eprintln!("Failed to send log to Elasticsearch: {:?}", e); + } } } }); @@ -70,7 +88,6 @@ where let level = event.metadata().level().to_string(); let target = event.metadata().target().to_string(); - // Manually process the event fields let mut message = String::new(); let mut visitor = Visitor { output: &mut message }; event.record(&mut visitor); diff --git a/crates/cdk-mintd/src/main.rs b/crates/cdk-mintd/src/main.rs index af9bc018..fc788bc7 100644 --- a/crates/cdk-mintd/src/main.rs +++ b/crates/cdk-mintd/src/main.rs @@ -79,16 +79,16 @@ async fn main() -> anyhow::Result<()> { )); // Export logs to elastic search - let elasticsearch_layer = ElasticsearchLayer::new("http://localhost:9200", "cdk-mintd-logs"); + let elasticsearch_layer = ElasticsearchLayer::new("http://elastic:password@localhost:9200", "cdk-mintd-logs", None); let subscriber = Registry::default() - .with(elasticsearch_layer) - .with(tracing_subscriber::fmt::layer().with_filter(env_filter)); + .with(tracing_subscriber::fmt::layer().with_filter(env_filter)) + .with(elasticsearch_layer); tracing::subscriber::set_global_default(subscriber).expect("Failed to set subscriber"); // Test log messages tracing::info!(message = "This is an info log", user_id = 123); - tracing::error!(message = "This is an error log"); + tracing::error!("This is an error log"); let localstore: Arc + Send + Sync> = From 718842cacffe0fe5dac892183a3c3d2d3517d3a4 Mon Sep 17 00:00:00 2001 From: lollerfirst Date: Thu, 12 Dec 2024 15:56:08 +0100 Subject: [PATCH 04/12] cargo fmt --- crates/cdk-mintd/src/lib.rs | 2 +- crates/cdk-mintd/src/loggers.rs | 19 ++++++++----------- crates/cdk-mintd/src/main.rs | 7 +++++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/crates/cdk-mintd/src/lib.rs b/crates/cdk-mintd/src/lib.rs index 6fdb832e..f6c16bc5 100644 --- a/crates/cdk-mintd/src/lib.rs +++ b/crates/cdk-mintd/src/lib.rs @@ -5,8 +5,8 @@ use std::path::PathBuf; pub mod cli; pub mod config; pub mod env_vars; -pub mod setup; pub mod loggers; +pub mod setup; fn expand_path(path: &str) -> Option { if path.starts_with('~') { diff --git a/crates/cdk-mintd/src/loggers.rs b/crates/cdk-mintd/src/loggers.rs index 46463473..5bf2e106 100644 --- a/crates/cdk-mintd/src/loggers.rs +++ b/crates/cdk-mintd/src/loggers.rs @@ -1,12 +1,12 @@ use chrono::Utc; use reqwest::Client; use serde::Serialize; +use std::fmt::Write; use tokio::sync::mpsc::{self, Sender}; -use tracing::Event; use tracing::field::{Field, Visit}; +use tracing::Event; use tracing_subscriber::layer::{Context, Layer}; use tracing_subscriber::registry::LookupSpan; -use std::fmt::Write; pub const BACKLOG: usize = 10_000; @@ -49,9 +49,7 @@ impl ElasticsearchLayer { // Spawn an async task to process logs and send them to Elasticsearch. tokio::spawn(async move { while let Some(log) = receiver.recv().await { - let mut request = client - .post(&base_url) - .json(&log); + let mut request = client.post(&base_url).json(&log); // Add the Authorization header if an API key is provided. if let Some(ref key) = api_key_header { @@ -62,10 +60,7 @@ impl ElasticsearchLayer { match response { Ok(res) if !res.status().is_success() => { - eprintln!( - "Failed to send log to Elasticsearch: HTTP {}", - res.status() - ); + eprintln!("Failed to send log to Elasticsearch: HTTP {}", res.status()); } Ok(_) => {} Err(e) => { @@ -87,9 +82,11 @@ where let timestamp = Utc::now().to_rfc3339(); let level = event.metadata().level().to_string(); let target = event.metadata().target().to_string(); - + let mut message = String::new(); - let mut visitor = Visitor { output: &mut message }; + let mut visitor = Visitor { + output: &mut message, + }; event.record(&mut visitor); let log = LogMessage { diff --git a/crates/cdk-mintd/src/main.rs b/crates/cdk-mintd/src/main.rs index fc788bc7..8490b88b 100644 --- a/crates/cdk-mintd/src/main.rs +++ b/crates/cdk-mintd/src/main.rs @@ -79,7 +79,11 @@ async fn main() -> anyhow::Result<()> { )); // Export logs to elastic search - let elasticsearch_layer = ElasticsearchLayer::new("http://elastic:password@localhost:9200", "cdk-mintd-logs", None); + let elasticsearch_layer = ElasticsearchLayer::new( + "http://elastic:password@localhost:9200", + "cdk-mintd-logs", + None, + ); let subscriber = Registry::default() .with(tracing_subscriber::fmt::layer().with_filter(env_filter)) .with(elasticsearch_layer); @@ -90,7 +94,6 @@ async fn main() -> anyhow::Result<()> { tracing::info!(message = "This is an info log", user_id = 123); tracing::error!("This is an error log"); - let localstore: Arc + Send + Sync> = match settings.database.engine { DatabaseEngine::Sqlite => { From ff51cb3e726331714952386770e07488b9f52dc1 Mon Sep 17 00:00:00 2001 From: lollerfirst Date: Fri, 13 Dec 2024 15:55:21 +0100 Subject: [PATCH 05/12] settings --- crates/cdk-mintd/src/config.rs | 12 ++++++++++++ crates/cdk-mintd/src/main.rs | 30 +++++++++++++++++------------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/crates/cdk-mintd/src/config.rs b/crates/cdk-mintd/src/config.rs index fc579b17..95cca5fa 100644 --- a/crates/cdk-mintd/src/config.rs +++ b/crates/cdk-mintd/src/config.rs @@ -186,6 +186,18 @@ pub struct Settings { pub lnd: Option, pub fake_wallet: Option, pub database: Database, + pub elasticsearch: Option, +} + +#[derive(Debug, Clone, Serialize, Deserialize, Default)] +pub struct ElasticSearchLogger { + /// authenticated URL endpoint of the ElasticSearch instance + /// (e.g. https://elastic:password@locahost:9200) + pub elasticsearch_url: String, + /// API Key as an alternative, more secure authentication + pub api_key: Option, + /// index under which store the logs + pub index: String } #[derive(Debug, Clone, Serialize, Deserialize, Default)] diff --git a/crates/cdk-mintd/src/main.rs b/crates/cdk-mintd/src/main.rs index 8490b88b..8182ed23 100644 --- a/crates/cdk-mintd/src/main.rs +++ b/crates/cdk-mintd/src/main.rs @@ -69,7 +69,6 @@ async fn main() -> anyhow::Result<()> { let settings = settings.from_env()?; let default_filter = "debug"; - let sqlx_filter = "sqlx=warn"; let hyper_filter = "hyper=warn"; @@ -78,17 +77,24 @@ async fn main() -> anyhow::Result<()> { default_filter, sqlx_filter, hyper_filter )); - // Export logs to elastic search - let elasticsearch_layer = ElasticsearchLayer::new( - "http://elastic:password@localhost:9200", - "cdk-mintd-logs", - None, - ); - let subscriber = Registry::default() - .with(tracing_subscriber::fmt::layer().with_filter(env_filter)) - .with(elasticsearch_layer); + if let Some(extlogger) = settings.elasticsearch.clone() { + // Export logs to elastic search + let elasticsearch_layer = ElasticsearchLayer::new( + &extlogger.elasticsearch_url, + &extlogger.index, + extlogger.api_key.as_deref(), + ); + let subscriber = Registry::default() + .with(tracing_subscriber::fmt::layer().with_filter(env_filter)) + .with(elasticsearch_layer); + + tracing::subscriber::set_global_default(subscriber).expect("Failed to set subscriber"); + } else { + let subscriber = Registry::default() + .with(tracing_subscriber::fmt::layer().with_filter(env_filter)); - tracing::subscriber::set_global_default(subscriber).expect("Failed to set subscriber"); + tracing::subscriber::set_global_default(subscriber).expect("Failed to set subscriber"); + } // Test log messages tracing::info!(message = "This is an info log", user_id = 123); @@ -156,8 +162,6 @@ async fn main() -> anyhow::Result<()> { melt_max: settings.ln.max_melt, }; - println!("{:?}", settings); - match settings.ln.ln_backend { LnBackend::Cln => { let cln_settings = settings From a3e3fa4e9c7e5f5a6281f2e13a60eb10becb6d8e Mon Sep 17 00:00:00 2001 From: lollerfirst Date: Sun, 15 Dec 2024 18:02:25 +0100 Subject: [PATCH 06/12] save the handle and drop when object goes out of scope (shutdown). --- crates/cdk-mintd/src/loggers.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/crates/cdk-mintd/src/loggers.rs b/crates/cdk-mintd/src/loggers.rs index 5bf2e106..4356e66b 100644 --- a/crates/cdk-mintd/src/loggers.rs +++ b/crates/cdk-mintd/src/loggers.rs @@ -1,6 +1,7 @@ use chrono::Utc; use reqwest::Client; use serde::Serialize; +use tokio::task::JoinHandle; use std::fmt::Write; use tokio::sync::mpsc::{self, Sender}; use tracing::field::{Field, Visit}; @@ -30,6 +31,7 @@ struct LogMessage { pub struct ElasticsearchLayer { sender: Sender, + handle: JoinHandle<()>, } impl ElasticsearchLayer { @@ -47,7 +49,7 @@ impl ElasticsearchLayer { let api_key_header = api_key.map(|key| format!("ApiKey {}", key)); // Spawn an async task to process logs and send them to Elasticsearch. - tokio::spawn(async move { + let handle = tokio::spawn(async move { while let Some(log) = receiver.recv().await { let mut request = client.post(&base_url).json(&log); @@ -70,7 +72,7 @@ impl ElasticsearchLayer { } }); - Self { sender } + Self { sender, handle } } } @@ -100,3 +102,9 @@ where let _ = self.sender.try_send(log); } } + +impl Drop for ElasticsearchLayer { + fn drop(&mut self) { + self.handle.abort() + } +} From 01d61a848705eed5206441b6f2774e6c1113a297 Mon Sep 17 00:00:00 2001 From: lollerfirst Date: Sun, 15 Dec 2024 18:07:58 +0100 Subject: [PATCH 07/12] check mpsc write --- crates/cdk-mintd/src/loggers.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/cdk-mintd/src/loggers.rs b/crates/cdk-mintd/src/loggers.rs index 4356e66b..41169b65 100644 --- a/crates/cdk-mintd/src/loggers.rs +++ b/crates/cdk-mintd/src/loggers.rs @@ -1,4 +1,5 @@ use chrono::Utc; +use futures::channel::mpsc::TrySendError; use reqwest::Client; use serde::Serialize; use tokio::task::JoinHandle; @@ -99,7 +100,13 @@ where }; // Non-blocking send to the channel - let _ = self.sender.try_send(log); + let result = self.sender.try_send(log); + match result { + Err(e) => { + eprintln!("Failed to write log message to channel. Likely exceeded backlog capacity: {:?}", e); + } + Ok(()) => {} + } } } From f9109715c4c9255d66f9b5de2cc2ec914b34469d Mon Sep 17 00:00:00 2001 From: lollerfirst Date: Sun, 15 Dec 2024 18:08:44 +0100 Subject: [PATCH 08/12] cargo fmt --- crates/cdk-mintd/src/config.rs | 2 +- crates/cdk-mintd/src/loggers.rs | 3 +-- crates/cdk-mintd/src/main.rs | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/cdk-mintd/src/config.rs b/crates/cdk-mintd/src/config.rs index 95cca5fa..06f50439 100644 --- a/crates/cdk-mintd/src/config.rs +++ b/crates/cdk-mintd/src/config.rs @@ -197,7 +197,7 @@ pub struct ElasticSearchLogger { /// API Key as an alternative, more secure authentication pub api_key: Option, /// index under which store the logs - pub index: String + pub index: String, } #[derive(Debug, Clone, Serialize, Deserialize, Default)] diff --git a/crates/cdk-mintd/src/loggers.rs b/crates/cdk-mintd/src/loggers.rs index 41169b65..b8e4e299 100644 --- a/crates/cdk-mintd/src/loggers.rs +++ b/crates/cdk-mintd/src/loggers.rs @@ -1,10 +1,9 @@ use chrono::Utc; -use futures::channel::mpsc::TrySendError; use reqwest::Client; use serde::Serialize; -use tokio::task::JoinHandle; use std::fmt::Write; use tokio::sync::mpsc::{self, Sender}; +use tokio::task::JoinHandle; use tracing::field::{Field, Visit}; use tracing::Event; use tracing_subscriber::layer::{Context, Layer}; diff --git a/crates/cdk-mintd/src/main.rs b/crates/cdk-mintd/src/main.rs index 8182ed23..d97771f7 100644 --- a/crates/cdk-mintd/src/main.rs +++ b/crates/cdk-mintd/src/main.rs @@ -90,8 +90,8 @@ async fn main() -> anyhow::Result<()> { tracing::subscriber::set_global_default(subscriber).expect("Failed to set subscriber"); } else { - let subscriber = Registry::default() - .with(tracing_subscriber::fmt::layer().with_filter(env_filter)); + let subscriber = + Registry::default().with(tracing_subscriber::fmt::layer().with_filter(env_filter)); tracing::subscriber::set_global_default(subscriber).expect("Failed to set subscriber"); } From d88b3055990a17f460f7913f034756b43de1315a Mon Sep 17 00:00:00 2001 From: lollerfirst Date: Sun, 15 Dec 2024 18:16:00 +0100 Subject: [PATCH 09/12] fix annoying typo --- crates/cdk-mintd/src/loggers.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/cdk-mintd/src/loggers.rs b/crates/cdk-mintd/src/loggers.rs index b8e4e299..b7548407 100644 --- a/crates/cdk-mintd/src/loggers.rs +++ b/crates/cdk-mintd/src/loggers.rs @@ -104,7 +104,7 @@ where Err(e) => { eprintln!("Failed to write log message to channel. Likely exceeded backlog capacity: {:?}", e); } - Ok(()) => {} + Ok(_) => {} } } } From 5efad2cf7171e859ca57a4ce38618627677985b1 Mon Sep 17 00:00:00 2001 From: lollerfirst Date: Sun, 15 Dec 2024 23:15:54 +0100 Subject: [PATCH 10/12] fix infinite logging bug --- crates/cdk-mintd/src/loggers.rs | 5 +++-- crates/cdk-mintd/src/main.rs | 17 +++++------------ 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/crates/cdk-mintd/src/loggers.rs b/crates/cdk-mintd/src/loggers.rs index b7548407..cafe6823 100644 --- a/crates/cdk-mintd/src/loggers.rs +++ b/crates/cdk-mintd/src/loggers.rs @@ -93,8 +93,8 @@ where let log = LogMessage { timestamp, - level, - message, + level: level.clone(), + message: message.clone(), target, }; @@ -103,6 +103,7 @@ where match result { Err(e) => { eprintln!("Failed to write log message to channel. Likely exceeded backlog capacity: {:?}", e); + eprintln!("{}\n{}", level, message) } Ok(_) => {} } diff --git a/crates/cdk-mintd/src/main.rs b/crates/cdk-mintd/src/main.rs index d97771f7..3215dea7 100644 --- a/crates/cdk-mintd/src/main.rs +++ b/crates/cdk-mintd/src/main.rs @@ -72,10 +72,7 @@ async fn main() -> anyhow::Result<()> { let sqlx_filter = "sqlx=warn"; let hyper_filter = "hyper=warn"; - let env_filter = EnvFilter::new(format!( - "{},{},{}", - default_filter, sqlx_filter, hyper_filter - )); + let env_filter = format!("{},{},{}", default_filter, sqlx_filter, hyper_filter,); if let Some(extlogger) = settings.elasticsearch.clone() { // Export logs to elastic search @@ -85,21 +82,17 @@ async fn main() -> anyhow::Result<()> { extlogger.api_key.as_deref(), ); let subscriber = Registry::default() - .with(tracing_subscriber::fmt::layer().with_filter(env_filter)) - .with(elasticsearch_layer); + .with(tracing_subscriber::fmt::layer().with_filter(EnvFilter::new(&env_filter))) + .with(elasticsearch_layer.with_filter(EnvFilter::new(&env_filter))); tracing::subscriber::set_global_default(subscriber).expect("Failed to set subscriber"); } else { - let subscriber = - Registry::default().with(tracing_subscriber::fmt::layer().with_filter(env_filter)); + let subscriber = Registry::default() + .with(tracing_subscriber::fmt::layer().with_filter(EnvFilter::new(&env_filter))); tracing::subscriber::set_global_default(subscriber).expect("Failed to set subscriber"); } - // Test log messages - tracing::info!(message = "This is an info log", user_id = 123); - tracing::error!("This is an error log"); - let localstore: Arc + Send + Sync> = match settings.database.engine { DatabaseEngine::Sqlite => { From ebf55b9af39ea232e564bc088be7413bcbc5b7ee Mon Sep 17 00:00:00 2001 From: lollerfirst Date: Mon, 16 Dec 2024 10:19:10 +0100 Subject: [PATCH 11/12] env_vars settings --- crates/cdk-mintd/src/env_vars.rs | 44 ++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/crates/cdk-mintd/src/env_vars.rs b/crates/cdk-mintd/src/env_vars.rs index eae4de4d..88d9e90e 100644 --- a/crates/cdk-mintd/src/env_vars.rs +++ b/crates/cdk-mintd/src/env_vars.rs @@ -6,8 +6,7 @@ use anyhow::{anyhow, bail, Result}; use cdk::nuts::CurrencyUnit; use crate::config::{ - Cln, Database, DatabaseEngine, FakeWallet, Info, LNbits, Ln, LnBackend, Lnd, MintInfo, - Phoenixd, Settings, Strike, + Cln, Database, DatabaseEngine, ElasticSearchLogger, FakeWallet, Info, LNbits, Ln, LnBackend, Lnd, MintInfo, Phoenixd, Settings, Strike }; pub const DATABASE_ENV_VAR: &str = "CDK_MINTD_DATABASE"; @@ -68,6 +67,10 @@ pub const ENV_FAKE_WALLET_FEE_PERCENT: &str = "CDK_MINTD_FAKE_WALLET_FEE_PERCENT pub const ENV_FAKE_WALLET_RESERVE_FEE_MIN: &str = "CDK_MINTD_FAKE_WALLET_RESERVE_FEE_MIN"; pub const ENV_FAKE_WALLET_MIN_DELAY: &str = "CDK_MINTD_FAKE_WALLET_MIN_DELAY"; pub const ENV_FAKE_WALLET_MAX_DELAY: &str = "CDK_MINTD_FAKE_WALLET_MAX_DELAY"; +// Elastic Search +pub const ENV_ELASTIC_SEARCH_URL: &str = "CDK_MINTD_ELASTIC_SEARCH_URL"; +pub const ENV_ELASTIC_SEARCH_INDEX: &str = "CDK_MINTD_ELASTIC_SEARCH_INDEX"; +pub const ENV_ELASTIC_SEARCH_API_KEY_BASE64: &str = "CDK_MINTD_ELASTIC_SEARCH_API_KEY_BASE64"; impl Settings { pub fn from_env(&mut self) -> Result { @@ -79,6 +82,8 @@ impl Settings { self.info = self.info.clone().from_env(); self.mint_info = self.mint_info.clone().from_env(); self.ln = self.ln.clone().from_env(); + + self.external_loggers_from_env(); match self.ln.ln_backend { LnBackend::Cln => { @@ -104,6 +109,20 @@ impl Settings { Ok(self.clone()) } + + fn external_loggers_from_env(&mut self) { + // ElasticSearch + if env::var(ENV_ELASTIC_SEARCH_URL).is_ok() { + if self.elasticsearch.is_none() { + let extlogger = ElasticSearchLogger { + elasticsearch_url: "".to_string(), api_key: None, index: "".to_string() + }; + self.elasticsearch = Some(extlogger.from_env()); + } else { + self.elasticsearch = Some(self.elasticsearch.clone().expect("Expected ElasticSearchLogger").from_env()); + } + } + } } impl Info { @@ -433,3 +452,24 @@ impl FakeWallet { self } } + + +impl ElasticSearchLogger { + pub fn from_env(mut self) -> Self { + if let Ok(elasticsearch_url) = env::var(ENV_ELASTIC_SEARCH_URL) { + self.elasticsearch_url = elasticsearch_url; + } + + if let Ok(api_key) = env::var(ENV_ELASTIC_SEARCH_API_KEY_BASE64) { + self.api_key = Some(api_key); + } else { + self.api_key = None; + } + + if let Ok(index) = env::var(ENV_ELASTIC_SEARCH_INDEX) { + self.index = index; + } + + self + } +} From 40078c7999189fd08cbae4d3140ff23d98fa3c90 Mon Sep 17 00:00:00 2001 From: lollerfirst Date: Mon, 16 Dec 2024 10:54:13 +0100 Subject: [PATCH 12/12] cargo fmt --- crates/cdk-mintd/src/env_vars.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/crates/cdk-mintd/src/env_vars.rs b/crates/cdk-mintd/src/env_vars.rs index 88d9e90e..78b6ee2d 100644 --- a/crates/cdk-mintd/src/env_vars.rs +++ b/crates/cdk-mintd/src/env_vars.rs @@ -6,7 +6,8 @@ use anyhow::{anyhow, bail, Result}; use cdk::nuts::CurrencyUnit; use crate::config::{ - Cln, Database, DatabaseEngine, ElasticSearchLogger, FakeWallet, Info, LNbits, Ln, LnBackend, Lnd, MintInfo, Phoenixd, Settings, Strike + Cln, Database, DatabaseEngine, ElasticSearchLogger, FakeWallet, Info, LNbits, Ln, LnBackend, + Lnd, MintInfo, Phoenixd, Settings, Strike, }; pub const DATABASE_ENV_VAR: &str = "CDK_MINTD_DATABASE"; @@ -82,7 +83,7 @@ impl Settings { self.info = self.info.clone().from_env(); self.mint_info = self.mint_info.clone().from_env(); self.ln = self.ln.clone().from_env(); - + self.external_loggers_from_env(); match self.ln.ln_backend { @@ -115,11 +116,18 @@ impl Settings { if env::var(ENV_ELASTIC_SEARCH_URL).is_ok() { if self.elasticsearch.is_none() { let extlogger = ElasticSearchLogger { - elasticsearch_url: "".to_string(), api_key: None, index: "".to_string() + elasticsearch_url: "".to_string(), + api_key: None, + index: "".to_string(), }; self.elasticsearch = Some(extlogger.from_env()); } else { - self.elasticsearch = Some(self.elasticsearch.clone().expect("Expected ElasticSearchLogger").from_env()); + self.elasticsearch = Some( + self.elasticsearch + .clone() + .expect("Expected ElasticSearchLogger") + .from_env(), + ); } } } @@ -453,7 +461,6 @@ impl FakeWallet { } } - impl ElasticSearchLogger { pub fn from_env(mut self) -> Self { if let Ok(elasticsearch_url) = env::var(ENV_ELASTIC_SEARCH_URL) {