From e90ed7e20bf3b81950a362a0c742f6922082a52f Mon Sep 17 00:00:00 2001 From: fuxiaohei Date: Thu, 21 Mar 2024 17:53:07 +0800 Subject: [PATCH] feat: add metrics crates to worker --- Cargo.lock | 381 +++++++++++++++++++++---- Cargo.toml | 6 +- crates/worker-server/Cargo.toml | 2 + crates/worker-server/src/lib.rs | 16 ++ crates/worker-server/src/middleware.rs | 30 ++ land-cli/src/cmds/up.rs | 1 + land-worker/src/main.rs | 12 +- 7 files changed, 388 insertions(+), 60 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 69dbc757..3c231098 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -84,9 +84,9 @@ dependencies = [ [[package]] name = "aho-corasick" -version = "1.1.2" +version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" dependencies = [ "memchr", ] @@ -448,9 +448,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.4.2" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" dependencies = [ "serde", ] @@ -1354,6 +1354,21 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[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" @@ -1509,7 +1524,7 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "27d12c0aed7f1e24276a241aadc4cb8ea9f83000f34bc062b7cc2d51e3b0fabd" dependencies = [ - "bitflags 2.4.2", + "bitflags 2.5.0", "debugid", "fxhash", "serde", @@ -1849,6 +1864,7 @@ dependencies = [ "pin-project-lite", "smallvec", "tokio", + "want", ] [[package]] @@ -1860,9 +1876,42 @@ dependencies = [ "futures-util", "http 0.2.12", "hyper 0.14.28", - "rustls", + "rustls 0.21.10", "tokio", - "tokio-rustls", + "tokio-rustls 0.24.1", +] + +[[package]] +name = "hyper-rustls" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0bea761b46ae2b24eb4aef630d8d1c398157b6fc29e6350ecf090a0b70c952c" +dependencies = [ + "futures-util", + "http 1.1.0", + "hyper 1.2.0", + "hyper-util", + "rustls 0.22.2", + "rustls-pki-types", + "tokio", + "tokio-rustls 0.25.0", + "tower-service", +] + +[[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.2.0", + "hyper-util", + "native-tls", + "tokio", + "tokio-native-tls", + "tower-service", ] [[package]] @@ -1872,6 +1921,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca38ef113da30126bbff9cd1705f9273e15d45498615d138b0c20279ac7a76aa" dependencies = [ "bytes", + "futures-channel", "futures-util", "http 1.1.0", "http-body 1.0.0", @@ -1879,6 +1929,9 @@ dependencies = [ "pin-project-lite", "socket2", "tokio", + "tower", + "tower-service", + "tracing", ] [[package]] @@ -1987,11 +2040,11 @@ dependencies = [ [[package]] name = "inquire" -version = "0.7.1" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d98c2ec0b4c8e12455b249aedacfda205df26ee9c6498945b00353a4e682ea6" +checksum = "a3bc24f3f114e409501843dda15ef84dccd13404a364101b5b7ca94f5e756513" dependencies = [ - "bitflags 2.4.2", + "bitflags 2.5.0", "crossterm", "dyn-clone", "fuzzy-matcher", @@ -2128,7 +2181,7 @@ dependencies = [ "lazy_static", "serde", "time", - "toml 0.8.11", + "toml 0.8.12", "tracing", "tracing-subscriber", "vergen", @@ -2169,7 +2222,7 @@ dependencies = [ "land-wit", "md5", "once_cell", - "reqwest", + "reqwest 0.12.0", "serde", "serde_json", "tempdir", @@ -2249,7 +2302,7 @@ dependencies = [ "land-kernel", "land-worker-server", "once_cell", - "reqwest", + "reqwest 0.12.0", "serde", "serde_yaml", "tokio", @@ -2271,7 +2324,7 @@ dependencies = [ "lazy_static", "moka", "once_cell", - "reqwest", + "reqwest 0.12.0", "tokio", "tracing", "wasmtime", @@ -2286,6 +2339,8 @@ dependencies = [ "axum", "hostname", "land-worker-impl", + "metrics", + "metrics-exporter-prometheus", "once_cell", "serde", "tokio", @@ -2347,7 +2402,7 @@ version = "0.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85c833ca1e66078851dba29046874e38f08b2c883700aa29a03ddd3b23814ee8" dependencies = [ - "bitflags 2.4.2", + "bitflags 2.5.0", "libc", "redox_syscall", ] @@ -2461,6 +2516,52 @@ dependencies = [ "autocfg", ] +[[package]] +name = "metrics" +version = "0.22.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2be3cbd384d4e955b231c895ce10685e3d8260c5ccffae898c96c723b0772835" +dependencies = [ + "ahash 0.8.11", + "portable-atomic", +] + +[[package]] +name = "metrics-exporter-prometheus" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d58e362dc7206e9456ddbcdbd53c71ba441020e62104703075a69151e38d85f" +dependencies = [ + "base64 0.22.0", + "http-body-util", + "hyper 1.2.0", + "hyper-tls", + "hyper-util", + "indexmap", + "ipnet", + "metrics", + "metrics-util", + "quanta", + "thiserror", + "tokio", + "tracing", +] + +[[package]] +name = "metrics-util" +version = "0.16.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b07a5eb561b8cbc16be2d216faf7757f9baf3bfb94dbb0fae3df8387a5bb47f" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", + "hashbrown 0.14.3", + "metrics", + "num_cpus", + "quanta", + "sketches-ddsketch", +] + [[package]] name = "mime" version = "0.3.17" @@ -2525,6 +2626,24 @@ dependencies = [ "uuid", ] +[[package]] +name = "native-tls" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07226173c32f2926027b63cce4bcd8076c3552846cbe7925f3aaffeac0a3b92e" +dependencies = [ + "lazy_static", + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + [[package]] name = "newline-converter" version = "0.3.0" @@ -2683,19 +2802,57 @@ dependencies = [ "percent-encoding", "quick-xml", "reqsign", - "reqwest", + "reqwest 0.11.27", "serde", "serde_json", "tokio", "uuid", ] +[[package]] +name = "openssl" +version = "0.10.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95a0481286a310808298130d22dd1fef0fa571e05a8f44ec801801e84b216b1f" +dependencies = [ + "bitflags 2.5.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.53", +] + [[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.101" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dda2b0f344e78efc2facf7d195d098df0dd72151b26ab98da807afc26c198dff" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "ordered-float" version = "3.9.2" @@ -2915,6 +3072,12 @@ dependencies = [ "universal-hash", ] +[[package]] +name = "portable-atomic" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" + [[package]] name = "powerfmt" version = "0.2.0" @@ -3010,7 +3173,7 @@ version = "0.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57206b407293d2bcd3af849ce869d52068623f19e1b5ff8e8778e3309439682b" dependencies = [ - "bitflags 2.4.2", + "bitflags 2.5.0", "memchr", "unicase", ] @@ -3133,7 +3296,7 @@ version = "11.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d86a7c4638d42c44551f4791a20e687dbb4c3de1f33c43dd71e355cd429def1" dependencies = [ - "bitflags 2.4.2", + "bitflags 2.5.0", ] [[package]] @@ -3280,7 +3443,7 @@ dependencies = [ "percent-encoding", "quick-xml", "rand 0.8.5", - "reqwest", + "reqwest 0.11.27", "rust-ini", "serde", "serde_json", @@ -3290,9 +3453,9 @@ dependencies = [ [[package]] name = "reqwest" -version = "0.11.26" +version = "0.11.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78bf93c4af7a8bb7d879d51cebe797356ff10ae8516ace542b5182d9dcac10b2" +checksum = "dd67538700a17451e7cba03ac727fb961abb7607553461627b97de0b89cf4a62" dependencies = [ "base64 0.21.7", "bytes", @@ -3303,16 +3466,15 @@ dependencies = [ "http 0.2.12", "http-body 0.4.6", "hyper 0.14.28", - "hyper-rustls", + "hyper-rustls 0.24.2", "ipnet", "js-sys", "log", "mime", - "mime_guess", "once_cell", "percent-encoding", "pin-project-lite", - "rustls", + "rustls 0.21.10", "rustls-native-certs", "rustls-pemfile", "serde", @@ -3321,7 +3483,51 @@ dependencies = [ "sync_wrapper", "system-configuration", "tokio", - "tokio-rustls", + "tokio-rustls 0.24.1", + "tokio-util", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "wasm-streams", + "web-sys", + "winreg", +] + +[[package]] +name = "reqwest" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58b48d98d932f4ee75e541614d32a7f44c889b72bd9c2e04d95edd135989df88" +dependencies = [ + "base64 0.21.7", + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "http 1.1.0", + "http-body 1.0.0", + "http-body-util", + "hyper 1.2.0", + "hyper-rustls 0.26.0", + "hyper-util", + "ipnet", + "js-sys", + "log", + "mime", + "mime_guess", + "once_cell", + "percent-encoding", + "pin-project-lite", + "rustls 0.22.2", + "rustls-pemfile", + "rustls-pki-types", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tokio-rustls 0.25.0", "tokio-util", "tower-service", "url", @@ -3329,7 +3535,7 @@ dependencies = [ "wasm-bindgen-futures", "wasm-streams", "web-sys", - "webpki-roots", + "webpki-roots 0.26.1", "winreg", ] @@ -3498,11 +3704,11 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.31" +version = "0.38.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949" +checksum = "65e04861e65f21776e67888bfbea442b3642beaa0138fdb1dd7a84a52dffdb89" dependencies = [ - "bitflags 2.4.2", + "bitflags 2.5.0", "errno", "itoa", "libc", @@ -3519,10 +3725,24 @@ checksum = "f9d5a6813c0759e4609cd494e8e725babae6a2ca7b62a5536a13daaec6fcb7ba" dependencies = [ "log", "ring", - "rustls-webpki", + "rustls-webpki 0.101.7", "sct", ] +[[package]] +name = "rustls" +version = "0.22.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e87c9956bd9807afa1f77e0f7594af32566e830e088a5576d27c5b6f30f49d41" +dependencies = [ + "log", + "ring", + "rustls-pki-types", + "rustls-webpki 0.102.2", + "subtle", + "zeroize", +] + [[package]] name = "rustls-native-certs" version = "0.6.3" @@ -3544,6 +3764,12 @@ dependencies = [ "base64 0.21.7", ] +[[package]] +name = "rustls-pki-types" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ede67b28608b4c60685c7d54122d4400d90f62b40caee7700e700380a390fa8" + [[package]] name = "rustls-webpki" version = "0.101.7" @@ -3554,6 +3780,17 @@ dependencies = [ "untrusted", ] +[[package]] +name = "rustls-webpki" +version = "0.102.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "faaa0a62740bedb9b2ef5afa303da42764c012f743917351dc9a237ea1663610" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + [[package]] name = "rustversion" version = "1.0.14" @@ -3985,6 +4222,12 @@ dependencies = [ "walkdir", ] +[[package]] +name = "sketches-ddsketch" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85636c14b73d81f541e525f585c0a2109e6744e1565b5c1668e31c70c10ed65c" + [[package]] name = "slab" version = "0.4.9" @@ -4002,9 +4245,9 @@ checksum = "826167069c09b99d56f31e9ae5c99049e932a98c9dc2dac47645b08dbbf76ba7" [[package]] name = "smallvec" -version = "1.13.1" +version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "socket2" @@ -4110,7 +4353,7 @@ dependencies = [ "paste", "percent-encoding", "rust_decimal", - "rustls", + "rustls 0.21.10", "rustls-pemfile", "serde", "serde_json", @@ -4124,7 +4367,7 @@ dependencies = [ "tracing", "url", "uuid", - "webpki-roots", + "webpki-roots 0.25.4", ] [[package]] @@ -4175,7 +4418,7 @@ dependencies = [ "atoi", "base64 0.21.7", "bigdecimal", - "bitflags 2.4.2", + "bitflags 2.5.0", "byteorder", "bytes", "chrono", @@ -4222,7 +4465,7 @@ dependencies = [ "atoi", "base64 0.21.7", "bigdecimal", - "bitflags 2.4.2", + "bitflags 2.5.0", "byteorder", "chrono", "crc", @@ -4413,7 +4656,7 @@ version = "0.26.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0682e006dd35771e392a6623ac180999a9a854b1d4a6c12fb2e804941c2b1f58" dependencies = [ - "bitflags 2.4.2", + "bitflags 2.5.0", "cap-fs-ext", "cap-std", "fd-lock", @@ -4580,13 +4823,34 @@ dependencies = [ "syn 2.0.53", ] +[[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.24.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c28327cf380ac148141087fbfb9de9d7bd4e84ab5d2c28fbc911d753de8a7081" dependencies = [ - "rustls", + "rustls 0.21.10", + "tokio", +] + +[[package]] +name = "tokio-rustls" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "775e0c0f0adb3a2f22a00c4745d728b479985fc15ee7ca6a2608388c5569860f" +dependencies = [ + "rustls 0.22.2", + "rustls-pki-types", "tokio", ] @@ -4626,14 +4890,14 @@ dependencies = [ [[package]] name = "toml" -version = "0.8.11" +version = "0.8.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af06656561d28735e9c1cd63dfd57132c8155426aa6af24f36a00a351f88c48e" +checksum = "e9dd1545e8208b4a5af1aa9bbd0b4cf7e9ea08fabc5d0a5c67fcaafa17433aa3" dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit 0.22.7", + "toml_edit 0.22.9", ] [[package]] @@ -4658,9 +4922,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.7" +version = "0.22.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18769cd1cec395d70860ceb4d932812a0b4d06b1a4bb336745a4d21b9496e992" +checksum = "8e40bb779c5187258fd7aad0eb68cb8706a0a81fa712fbea808ab43c4b8374c4" dependencies = [ "indexmap", "serde", @@ -4691,7 +4955,7 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e9cd434a998747dd2c4276bc96ee2e0c7a2eadf3cae88e52be55a05fa9053f5" dependencies = [ - "bitflags 2.4.2", + "bitflags 2.5.0", "bytes", "futures-util", "http 1.1.0", @@ -4921,9 +5185,9 @@ checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" [[package]] name = "uuid" -version = "1.7.0" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f00cc9702ca12d3c81455259621e676d0f7251cec66a21e98fe2e9a37db93b2a" +checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0" dependencies = [ "getrandom", "serde", @@ -4994,7 +5258,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95e022c29ad56af4cc0a8a8f0e0191abf9e0a0c4a68d25dfe088c39c9a8e3d2c" dependencies = [ "anyhow", - "bitflags 2.4.2", + "bitflags 2.5.0", "cap-fs-ext", "cap-rand", "cap-std", @@ -5138,7 +5402,7 @@ version = "0.121.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9dbe55c8f9d0dbd25d9447a5a889ff90c0cc3feaa7395310d3d826b2c703eaab" dependencies = [ - "bitflags 2.4.2", + "bitflags 2.5.0", "indexmap", "semver", ] @@ -5149,7 +5413,7 @@ version = "0.201.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84e5df6dba6c0d7fafc63a450f1738451ed7a0b52295d83e868218fa286bf708" dependencies = [ - "bitflags 2.4.2", + "bitflags 2.5.0", "indexmap", "semver", ] @@ -5425,7 +5689,7 @@ checksum = "9ca912bda309188bd25ab7652c6654b34aacdf43047c716ee1cb685a28079078" dependencies = [ "anyhow", "async-trait", - "bitflags 2.4.2", + "bitflags 2.5.0", "bytes", "cap-fs-ext", "cap-net-ext", @@ -5542,6 +5806,15 @@ version = "0.25.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f20c57d8d7db6d3b86154206ae5d8fba62dd39573114de97c2cb0578251f8e1" +[[package]] +name = "webpki-roots" +version = "0.26.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3de34ae270483955a94f4b21bdaaeb83d508bb84a01435f393818edb0012009" +dependencies = [ + "rustls-pki-types", +] + [[package]] name = "which" version = "6.0.0" @@ -5573,7 +5846,7 @@ checksum = "7a7ecd6e1ffba1278cfd24a001a13da11d86801e0ad9342f11a370ce0df50e14" dependencies = [ "anyhow", "async-trait", - "bitflags 2.4.2", + "bitflags 2.5.0", "thiserror", "tracing", "wasmtime", @@ -5829,7 +6102,7 @@ version = "0.36.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9643b83820c0cd246ecabe5fa454dd04ba4fa67996369466d0747472d337346" dependencies = [ - "bitflags 2.4.2", + "bitflags 2.5.0", "windows-sys 0.52.0", ] @@ -5839,7 +6112,7 @@ version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "288f992ea30e6b5c531b52cdd5f3be81c148554b09ea416f058d16556ba92c27" dependencies = [ - "bitflags 2.4.2", + "bitflags 2.5.0", "wit-bindgen-rt", "wit-bindgen-rust-macro", ] @@ -5895,7 +6168,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "421c0c848a0660a8c22e2fd217929a0191f14476b68962afd2af89fd22e39825" dependencies = [ "anyhow", - "bitflags 2.4.2", + "bitflags 2.5.0", "indexmap", "log", "serde", diff --git a/Cargo.toml b/Cargo.toml index ca52c251..a9f39991 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,7 +47,7 @@ opendal = { version = "0.45.0", default-features = false, features = [ "rustls", ] } rand = "0.8.5" -reqwest = { version = "0.11.26", default-features = false, features = [ +reqwest = { version = "0.12.0", default-features = false, features = [ "stream", "rustls-tls", "blocking", @@ -62,7 +62,7 @@ serde = { version = "1.0.197", features = ["derive"] } serde_json = "1.0.114" time = { version = "0.3.34", features = ["local-offset"] } tokio = { version = "1.36.0", features = ["full"] } -toml = "0.8.11" +toml = "0.8.12" tower-http = { version = "0.5.2", features = [ "timeout", "cors", @@ -75,7 +75,7 @@ tracing-subscriber = { version = "0.3.18", features = [ "local-time", "env-filter", ] } -uuid = { version = "1.7.0", features = ["v4"] } +uuid = { version = "1.8.0", features = ["v4"] } wit-bindgen = "0.22.0" wit-bindgen-core = "0.22.0" wit-bindgen-rust = "0.22.0" diff --git a/crates/worker-server/Cargo.toml b/crates/worker-server/Cargo.toml index 1f66af85..60156527 100644 --- a/crates/worker-server/Cargo.toml +++ b/crates/worker-server/Cargo.toml @@ -9,6 +9,8 @@ anyhow = { workspace = true } axum = { workspace = true } hostname = "0.3.1" land-worker-impl = { workspace = true } +metrics = "0.22.3" +metrics-exporter-prometheus = "0.14.0" once_cell = { workspace = true } serde = { workspace = true } tokio = { workspace = true } diff --git a/crates/worker-server/src/lib.rs b/crates/worker-server/src/lib.rs index e9798e89..991899d4 100644 --- a/crates/worker-server/src/lib.rs +++ b/crates/worker-server/src/lib.rs @@ -10,6 +10,7 @@ use axum::{ }; use land_worker_impl::hostcall::Request as WasmRequest; use land_worker_impl::Context; +use metrics_exporter_prometheus::PrometheusBuilder; use once_cell::sync::OnceCell; use std::{net::SocketAddr, time::Duration}; use tokio::time::Instant; @@ -25,6 +26,7 @@ pub struct Opts { pub default_wasm: String, pub endpoint_name: Option, pub wasm_aot: bool, + pub metrics: bool, } impl Default for Opts { @@ -35,12 +37,14 @@ impl Default for Opts { default_wasm: "".to_string(), endpoint_name: Some("localhost".to_string()), wasm_aot: false, + metrics: false, } } } static ENDPOINT_NAME: OnceCell = OnceCell::new(); static AOT_ENABLED: OnceCell = OnceCell::new(); +static METRICS_ENABLED: OnceCell = OnceCell::new(); pub async fn start(opts: Opts) -> Result<()> { let hostname = if let Some(endpoint) = opts.endpoint_name { @@ -56,6 +60,13 @@ pub async fn start(opts: Opts) -> Result<()> { debug!("Wasm dir: {}", opts.dir); debug!("Default wasm: {}", opts.default_wasm); debug!("AOT enabled: {}", opts.wasm_aot); + debug!("Metrics enabled: {}", opts.metrics); + + // enable prometheus metrics api + if opts.metrics { + // use for local visit, :9000 + PrometheusBuilder::new().install()?; + } // create directory std::fs::create_dir_all(&opts.dir).unwrap(); @@ -63,6 +74,7 @@ pub async fn start(opts: Opts) -> Result<()> { DEFAULT_WASM.set(opts.default_wasm).unwrap(); ENDPOINT_NAME.set(hostname).unwrap(); AOT_ENABLED.set(opts.wasm_aot).unwrap(); + METRICS_ENABLED.set(opts.metrics).unwrap(); // set pool's local dir to load module file land_worker_impl::pool::FILE_DIR.set(opts.dir).unwrap(); @@ -98,6 +110,7 @@ pub async fn load_default_wasm() -> Result<()> { async fn default_handler( ConnectInfo(addr): ConnectInfo, Extension(ctx): Extension, + Extension(metrics): Extension, req: Request, ) -> Result { let st = Instant::now(); @@ -108,6 +121,7 @@ async fn default_handler( let span = info_span!("[HTTP]",remote = %addr.to_string(), req_id = %ctx.req_id.clone(), method = %method, uri = %uri, host = %ctx.host); let span_clone = span.clone(); + metrics.req_cnt.increment(1); // if wasm_module is empty, return 404 if ctx.wasm_module.is_empty() { @@ -117,6 +131,7 @@ async fn default_handler( elapsed = %st.elapsed().as_micros(), "Function not found", ); + metrics.req_function_notfound_cnt.increment(1); return Err(ServerError::not_found(ctx, "Function not found")); } @@ -133,6 +148,7 @@ async fn default_handler( "Internal error: {}", err, ); + metrics.req_function_error_cnt.increment(1); let msg = format!("Internal error: {}", err); return Err(ServerError::internal_error(ctx, &msg)); } diff --git a/crates/worker-server/src/middleware.rs b/crates/worker-server/src/middleware.rs index 6842cf41..696a33b8 100644 --- a/crates/worker-server/src/middleware.rs +++ b/crates/worker-server/src/middleware.rs @@ -1,8 +1,11 @@ +use crate::METRICS_ENABLED; + use super::{DEFAULT_WASM, ENDPOINT_NAME}; use axum::extract::Request; use axum::http::StatusCode; use axum::middleware::Next; use axum::response::Response; +use metrics::{counter, Counter}; use serde::Serialize; #[derive(Default, Clone, Serialize, Debug)] @@ -15,6 +18,13 @@ pub struct WorkerContext { pub endpoint: String, } +#[derive(Clone)] +pub struct WorkerMetrics { + pub req_cnt: Counter, + pub req_function_notfound_cnt: Counter, + pub req_function_error_cnt: Counter, +} + /// middleware to add worker context info to request pub async fn middleware(mut request: Request, next: Next) -> Result { let req_id = ulid::Ulid::new().to_string(); @@ -46,6 +56,20 @@ pub async fn middleware(mut request: Request, next: Next) -> Result Result Result<()> { return Ok(()); } land_common::tracing::init(args.output.verbose); - if args.token.is_empty() { + if args.token.is_empty() && !args.local_mode { return Err(anyhow!("LAND_SERVER_TOKEN is required")); } // get local ip data agent::ip::init().await?; - // run worker-agent role - agent::run(args.cloud_server_url, args.token, args.dir.clone()).await?; + // local mode do not get data from center + if !args.local_mode { + // run worker-agent role + agent::run(args.cloud_server_url, args.token, args.dir.clone()).await?; + } let opts = land_worker_server::Opts { addr: args.address.parse()?, @@ -54,6 +59,7 @@ async fn main() -> Result<()> { default_wasm: "".to_string(), endpoint_name: None, wasm_aot: true, + metrics: true, }; land_worker_server::start(opts).await?; Ok(())