diff --git a/.gitignore b/.gitignore index 408a3f6..7728d03 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ /target -Cargo.lock metrics.db* diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..ccfc6be --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,1355 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "anstream" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" + +[[package]] +name = "anstyle-parse" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys", +] + +[[package]] +name = "anyhow" +version = "1.0.102" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c" + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "bitflags" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3" + +[[package]] +name = "bumpalo" +version = "3.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d20789868f4b01b2f2caec9f5c4e0213b41e3e5702a50157d699ae31ced2fcb" + +[[package]] +name = "cc" +version = "1.2.62" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1dce859f0832a7d088c4f1119888ab94ef4b5d6795d1ce05afb7fe159d79f98" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "chrono" +version = "0.4.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c673075a2e0e5f4a1dde27ce9dee1ea4558c7ffe648f576438a20ca1d2acc4b0" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-link", +] + +[[package]] +name = "clap" +version = "4.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2ce8604710f6733aa641a2b3731eaa1e8b3d9973d5e3565da11800813f997a9" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9" + +[[package]] +name = "colorchoice" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "csv" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52cd9d68cf7efc6ddfaaee42e7288d3a99d613d4b50f76ce9827ae0c6e14f938" +dependencies = [ + "csv-core", + "itoa", + "ryu", + "serde_core", +] + +[[package]] +name = "csv-core" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "704a3c26996a80471189265814dbc2c257598b96b8a7feae2d31ace646bb9782" +dependencies = [ + "memchr", +] + +[[package]] +name = "darling" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cdf337090841a411e2a7f3deb9187445851f91b309c0c0a29e05f74a00a48c0" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1247195ecd7e3c85f83c8d2a366e4210d588e802133e1e355180a9870b517ea4" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d38308df82d1080de0afee5d069fa14b0326a88c14f15c5ccda35b4a6c414c81" +dependencies = [ + "darling_core", + "quote", + "syn", +] + +[[package]] +name = "deranged" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cd812cc2bc1d69d4764bd80df88b4317eaef9e773c75226407d9bc0876b211c" +dependencies = [ + "powerfmt", +] + +[[package]] +name = "diesel" +version = "2.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9940fb8467a0a06312218ed384185cb8536aa10d8ec017d0ce7fad2c1bd882d5" +dependencies = [ + "diesel_derives", + "downcast-rs", + "libsqlite3-sys", + "sqlite-wasm-rs", + "time", +] + +[[package]] +name = "diesel_derives" +version = "2.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1817b7f4279b947fc4cafddec12b0e5f8727141706561ce3ac94a60bddd1cf5" +dependencies = [ + "diesel_table_macro_syntax", + "dsl_auto_type", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "diesel_migrations" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d0f4a98124ba6d4ca75da535f65984badec16a003b6e2f94a01e31a79490b8" +dependencies = [ + "diesel", + "migrations_internals", + "migrations_macros", +] + +[[package]] +name = "diesel_table_macro_syntax" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe2444076b48641147115697648dc743c2c00b61adade0f01ce67133c7babe8c" +dependencies = [ + "syn", +] + +[[package]] +name = "downcast-rs" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "117240f60069e65410b3ae1bb213295bd828f707b5bec6596a1afc8793ce0cbc" + +[[package]] +name = "dsl_auto_type" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd122633e4bef06db27737f21d3738fb89c8f6d5360d6d9d7635dda142a7757e" +dependencies = [ + "darling", + "either", + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "either" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "fastrand" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f1f227452a390804cdb637b74a86990f2a7d7ba4b7d5693aac9b4dd6defd8d6" + +[[package]] +name = "find-msvc-tools" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + +[[package]] +name = "foldhash" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" + +[[package]] +name = "futures-core" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d" + +[[package]] +name = "futures-task" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393" + +[[package]] +name = "futures-util" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "slab", +] + +[[package]] +name = "getrandom" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de51e6874e94e7bf76d726fc5d13ba782deca734ff60d5bb2fb2607c7406555" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasip2", + "wasip3", +] + +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "foldhash 0.1.5", +] + +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" +dependencies = [ + "foldhash 0.2.0", +] + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "iana-time-zone" +version = "0.1.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "id-arena" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "indexmap" +version = "2.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9" +dependencies = [ + "equivalent", + "hashbrown 0.17.1", + "serde", + "serde_core", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "js-sys" +version = "0.3.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67df7112613f8bfd9150013a0314e196f4800d3201ae742489d999db2f979f08" +dependencies = [ + "cfg-if", + "futures-util", + "once_cell", + "wasm-bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "leb128fmt" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" + +[[package]] +name = "libc" +version = "0.2.186" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" + +[[package]] +name = "libsqlite3-sys" +version = "0.35.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "133c182a6a2c87864fe97778797e46c7e999672690dc9fa3ee8e241aa4a9c13f" +dependencies = [ + "cc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "linux-raw-sys" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + +[[package]] +name = "log" +version = "0.4.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" + +[[package]] +name = "matchers" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" +dependencies = [ + "regex-automata", +] + +[[package]] +name = "memchr" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" + +[[package]] +name = "metrics" +version = "0.24.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89550ee9f79e88fef3119de263694973a8adb26c21d75322164fb8c493039fe2" +dependencies = [ + "portable-atomic", + "rapidhash", +] + +[[package]] +name = "metrics-sqlite" +version = "0.6.1" +dependencies = [ + "anyhow", + "chrono", + "clap", + "csv", + "diesel", + "diesel_migrations", + "libsqlite3-sys", + "metrics", + "serde", + "tempfile", + "thiserror", + "tokio", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "migrations_internals" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36c791ecdf977c99f45f23280405d7723727470f6689a5e6dbf513ac547ae10d" +dependencies = [ + "serde", + "toml", +] + +[[package]] +name = "migrations_macros" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36fc5ac76be324cfd2d3f2cf0fdf5d5d3c4f14ed8aaebadb09e304ba42282703" +dependencies = [ + "migrations_internals", + "proc-macro2", + "quote", +] + +[[package]] +name = "nu-ansi-term" +version = "0.50.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "pkg-config" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19f132c84eca552bf34cab8ec81f1c1dcc229b811638f9d283dceabe58c5569e" + +[[package]] +name = "portable-atomic" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49" + +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + +[[package]] +name = "prettyplease" +version = "0.2.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" +dependencies = [ + "proc-macro2", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "rapidhash" +version = "4.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e48930979c155e2f33aa36ab3119b5ee81332beb6482199a8ecd6029b80b59" +dependencies = [ + "rustversion", +] + +[[package]] +name = "regex-automata" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a" + +[[package]] +name = "rsqlite-vfs" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c51c9ae4df8a7fba42103df5c621fa3c37eccf3a3c650879e90fc48b11cc192c" +dependencies = [ + "hashbrown 0.16.1", + "thiserror", +] + +[[package]] +name = "rustix" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[package]] +name = "ryu" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" + +[[package]] +name = "semver" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.149" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "serde_spanned" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6662b5879511e06e8999a8a235d848113e942c9124f211511b16466ee2995f26" +dependencies = [ + "serde_core", +] + +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "sqlite-wasm-rs" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdd578e94101503d97e2b286bbf8db2135035ca24b2ce4cbf3f9e2fb2bbf1eee" +dependencies = [ + "cc", + "js-sys", + "rsqlite-vfs", + "wasm-bindgen", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tempfile" +version = "3.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" +dependencies = [ + "fastrand", + "getrandom", + "once_cell", + "rustix", + "windows-sys", +] + +[[package]] +name = "thiserror" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "thread_local" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "time" +version = "0.3.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9e442fc33d7fdb45aa9bfeb312c095964abdf596f7567261062b2a7107aaabd" +dependencies = [ + "deranged", + "itoa", + "num-conv", + "powerfmt", + "serde_core", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b36ee98fd31ec7426d599183e8fe26932a8dc1fb76ddb6214d05493377d34ca" + +[[package]] +name = "time-macros" +version = "0.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71e552d1249bf61ac2a52db88179fd0673def1e1ad8243a00d9ec9ed71fee3dd" +dependencies = [ + "num-conv", + "time-core", +] + +[[package]] +name = "tokio" +version = "1.52.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc7f01b389ac15039e4dc9531aa973a135d7a4135281b12d7c1bc79fd57fffe" +dependencies = [ + "pin-project-lite", +] + +[[package]] +name = "toml" +version = "0.9.12+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf92845e79fc2e2def6a5d828f0801e29a2f8acc037becc5ab08595c7d5e9863" +dependencies = [ + "serde_core", + "serde_spanned", + "toml_datetime", + "toml_parser", + "winnow 0.7.15", +] + +[[package]] +name = "toml_datetime" +version = "0.7.5+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92e1cfed4a3038bc5a127e35a2d360f145e1f4b971b551a2ba5fd7aedf7e1347" +dependencies = [ + "serde_core", +] + +[[package]] +name = "toml_parser" +version = "1.1.2+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2abe9b86193656635d2411dc43050282ca48aa31c2451210f4202550afb7526" +dependencies = [ + "winnow 1.0.3", +] + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex-automata", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", +] + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "wasip2" +version = "1.0.1+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0562428422c63773dad2c345a1882263bbf4d65cf3f42e90921f787ef5ad58e7" +dependencies = [ + "wit-bindgen 0.46.0", +] + +[[package]] +name = "wasip3" +version = "0.4.0+wasi-0.3.0-rc-2026-01-06" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5428f8bf88ea5ddc08faddef2ac4a67e390b88186c703ce6dbd955e1c145aca5" +dependencies = [ + "wit-bindgen 0.51.0", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.121" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49ace1d07c165b0864824eee619580c4689389afa9dc9ed3a4c75040d82e6790" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.121" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e68e6f4afd367a562002c05637acb8578ff2dea1943df76afb9e83d177c8578" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.121" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d95a9ec35c64b2a7cb35d3fead40c4238d0940c86d107136999567a4703259f2" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.121" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4e0100b01e9f0d03189a92b96772a1fb998639d981193d7dbab487302513441" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "wasm-encoder" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "990065f2fe63003fe337b932cfb5e3b80e0b4d0f5ff650e6985b1048f62c8319" +dependencies = [ + "leb128fmt", + "wasmparser", +] + +[[package]] +name = "wasm-metadata" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0e353e6a2fbdc176932bbaab493762eb1255a7900fe0fea1a2f96c296cc909" +dependencies = [ + "anyhow", + "indexmap", + "wasm-encoder", + "wasmparser", +] + +[[package]] +name = "wasmparser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe" +dependencies = [ + "bitflags", + "hashbrown 0.15.5", + "indexmap", + "semver", +] + +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "winnow" +version = "0.7.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df79d97927682d2fd8adb29682d1140b343be4ac0f08fd68b7765d9c059d3945" + +[[package]] +name = "winnow" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0592e1c9d151f854e6fd382574c3a0855250e1d9b2f99d9281c6e6391af352f1" + +[[package]] +name = "wit-bindgen" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f17a85883d4e6d00e8a97c586de764dabcc06133f7f1d55dce5cdc070ad7fe59" + +[[package]] +name = "wit-bindgen" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5" +dependencies = [ + "wit-bindgen-rust-macro", +] + +[[package]] +name = "wit-bindgen-core" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea61de684c3ea68cb082b7a88508a8b27fcc8b797d738bfc99a82facf1d752dc" +dependencies = [ + "anyhow", + "heck", + "wit-parser", +] + +[[package]] +name = "wit-bindgen-rust" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7c566e0f4b284dd6561c786d9cb0142da491f46a9fbed79ea69cdad5db17f21" +dependencies = [ + "anyhow", + "heck", + "indexmap", + "prettyplease", + "syn", + "wasm-metadata", + "wit-bindgen-core", + "wit-component", +] + +[[package]] +name = "wit-bindgen-rust-macro" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c0f9bfd77e6a48eccf51359e3ae77140a7f50b1e2ebfe62422d8afdaffab17a" +dependencies = [ + "anyhow", + "prettyplease", + "proc-macro2", + "quote", + "syn", + "wit-bindgen-core", + "wit-bindgen-rust", +] + +[[package]] +name = "wit-component" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2" +dependencies = [ + "anyhow", + "bitflags", + "indexmap", + "log", + "serde", + "serde_derive", + "serde_json", + "wasm-encoder", + "wasm-metadata", + "wasmparser", + "wit-parser", +] + +[[package]] +name = "wit-parser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecc8ac4bc1dc3381b7f59c34f00b67e18f910c2c0f50015669dde7def656a736" +dependencies = [ + "anyhow", + "id-arena", + "indexmap", + "log", + "semver", + "serde", + "serde_derive", + "serde_json", + "unicode-xid", + "wasmparser", +] + +[[package]] +name = "zmij" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" diff --git a/Cargo.toml b/Cargo.toml index 3be39c8..0770618 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "metrics-sqlite" -version = "0.6.0" +version = "0.6.1" authors = ["Jeremy Knope "] description = "Library for providing SQLite backend for metrics" keywords = ["metrics", "sqlite"] diff --git a/examples/chaos.rs b/examples/chaos.rs new file mode 100644 index 0000000..d73bad2 --- /dev/null +++ b/examples/chaos.rs @@ -0,0 +1,104 @@ +//! Local load harness for stress-testing the metrics-sqlite worker. +//! +//! Run with: +//! +//! RUST_LOG=info cargo run --example chaos +//! +//! It writes to `chaos.db` in the working directory and emits metrics from +//! several threads as fast as it can. By itself it just generates load — +//! the database stays healthy and the logs stay quiet. +//! +//! To exercise the failure / recovery paths you induce errors from outside +//! the process. The crate runs SQLite in WAL mode with `busy_timeout=5000`, +//! so the realistic external attacks are: +//! +//! ### 1) Hold a write lock for longer than the 5s busy timeout +//! +//! In another terminal while chaos is running: +//! +//! ( echo "BEGIN IMMEDIATE; SELECT 1;"; sleep 90 ) | sqlite3 chaos.db +//! +//! Each commit attempt in the worker waits up to 5s, then errors with +//! "database is locked". You should see at most one such ERROR line in the +//! log per minute, the first one accompanied by a throttle suppression +//! count after the second minute. After three consecutive failures the +//! worker logs a "connection is broken, reconnecting" WARN; reconnect +//! attempts are gated at one per 30s while the lock is held. When the +//! lock releases, the next flush succeeds silently. +//! +//! ### 2) Make commits fail by running out of disk +//! +//! Point `chaos.db` at a tiny ramdisk so the worker hits SQLITE_FULL / +//! disk-I/O errors during real commits. On macOS: +//! +//! DEV=$(hdiutil attach -nomount ram://2048) # ~1 MiB ramdisk +//! diskutil eraseVolume APFS small "$DEV" +//! # then run chaos with the working directory set to /Volumes/small +//! +//! ### What healthy behavior looks like +//! +//! - At most ~1 "Error flushing metrics: ..." line per minute, with a +//! "(N similar errors suppressed)" suffix once suppression has work to do. +//! - At most ~1 "Error sending metric ..." line per minute if the worker +//! channel ever fills. +//! - "metrics-sqlite database connection is broken, reconnecting" at most +//! once per 30s while the problem persists. +//! - "metrics-sqlite queue exceeded 100000 items" if a failure mode keeps +//! the worker from draining for that long. +//! +//! When the external problem is fixed, the next reconnect restores the +//! pipeline and the log goes quiet. + +use metrics::{counter, gauge, histogram}; +use metrics_sqlite::SqliteExporter; +use std::time::{Duration, Instant}; +use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt}; + +const DB_PATH: &str = "chaos.db"; +const WORKER_THREADS: usize = 4; +const PER_THREAD_RATE_HZ: u64 = 2_000; + +fn main() { + tracing_subscriber::registry() + .with( + EnvFilter::try_from_default_env() + .or_else(|_| EnvFilter::try_new("info,metrics_sqlite=info")) + .unwrap(), + ) + .with(fmt::layer()) + .init(); + + let exporter = SqliteExporter::new(Duration::from_millis(250), None, DB_PATH) + .expect("Failed to create SqliteExporter"); + exporter + .install() + .expect("Failed to install SqliteExporter"); + + tracing::info!( + "chaos running: writing to {DB_PATH}, {WORKER_THREADS} threads at ~{PER_THREAD_RATE_HZ} Hz each. Break the DB from another terminal to repro; Ctrl-C to stop." + ); + + let started = Instant::now(); + let mut handles = Vec::new(); + for worker_id in 0..WORKER_THREADS { + handles.push(std::thread::spawn(move || { + let period = Duration::from_micros(1_000_000 / PER_THREAD_RATE_HZ); + let mut next = Instant::now(); + loop { + counter!("chaos.events", "worker" => worker_id.to_string()).increment(1); + gauge!("chaos.elapsed_secs").set(started.elapsed().as_secs_f64()); + histogram!("chaos.iteration_us").record(period.as_micros() as f64); + next += period; + if let Some(remaining) = next.checked_duration_since(Instant::now()) { + std::thread::sleep(remaining); + } else { + // Falling behind; reset so we don't busy-spin catching up. + next = Instant::now(); + } + } + })); + } + for h in handles { + let _ = h.join(); + } +} diff --git a/mise.toml b/mise.toml new file mode 100644 index 0000000..0c0bb97 --- /dev/null +++ b/mise.toml @@ -0,0 +1,9 @@ +[tasks.precommit] +run = [ + "cargo clippy --all-targets -- -D clippy::uninlined_format_args", + "cargo fmt", + "cargo test --all" +] + +[tasks.clippy-fix] +run = "cargo clippy --all-targets --fix --allow-dirty -- -D clippy::uninlined_format_args" diff --git a/src/lib.rs b/src/lib.rs index ceb9cc3..9cb2620 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,7 @@ use diesel::{insert_into, sql_query}; use metrics::{GaugeValue, Key, KeyName, SetRecorderError, SharedString, Unit}; use diesel_migrations::{EmbeddedMigrations, MigrationHarness}; -use std::sync::Arc; +use std::sync::Mutex; use std::{ collections::{HashMap, VecDeque}, path::{Path, PathBuf}, @@ -29,6 +29,59 @@ const BACKGROUND_CHANNEL_LIMIT: usize = 8000; const SQLITE_DEFAULT_MAX_VARIABLES: usize = 999; const METRIC_FIELDS_PER_ROW: usize = 3; const INSERT_BATCH_SIZE: usize = SQLITE_DEFAULT_MAX_VARIABLES / METRIC_FIELDS_PER_ROW; +/// Hard cap on metrics buffered in memory by the worker. If flushing to SQLite +/// keeps failing, the oldest metrics beyond this limit are dropped so a broken +/// database can never grow the queue without bound. +const QUEUE_HARD_LIMIT: usize = 100_000; +/// Number of consecutive failed flushes after which the worker rebuilds its +/// database connection, even if the error didn't look connection-fatal. +const RECONNECT_AFTER_FAILURES: u64 = 3; +/// Minimum delay between worker database reconnection attempts. +const RECONNECT_BACKOFF: Duration = Duration::from_secs(30); +/// Minimum delay between repeated error log lines of the same kind. +const ERROR_LOG_INTERVAL: Duration = Duration::from_secs(60); + +/// Rate limiter for repetitive error logs. Emits at most one log per interval +/// and reports how many were suppressed in between. +struct LogThrottle { + interval: Duration, + last_logged: Option, + suppressed: u64, +} +impl LogThrottle { + const fn new(interval: Duration) -> Self { + LogThrottle { + interval, + last_logged: None, + suppressed: 0, + } + } + /// Returns `Some(suppressed_since_last_log)` when a log line should be + /// emitted now, or `None` when it should be suppressed. + fn allow(&mut self) -> Option { + let now = Instant::now(); + let due = match self.last_logged { + Some(last) => now.duration_since(last) >= self.interval, + None => true, + }; + if due { + self.last_logged = Some(now); + Some(std::mem::take(&mut self.suppressed)) + } else { + self.suppressed += 1; + None + } + } + + /// Invokes `emit` with the count of previously-suppressed lines, but only + /// if enough time has passed since the last emission. Otherwise the call + /// is silently dropped and the suppression counter is incremented. + fn log_if_due(&mut self, emit: impl FnOnce(u64)) { + if let Some(suppressed) = self.allow() { + emit(suppressed); + } + } +} /// Error type for any db/vitals related errors #[derive(Debug, Error)] @@ -86,7 +139,6 @@ mod recorder; mod schema; use crate::metrics_db::query; -use crate::recorder::Handle; pub use metrics_db::{MetricsDb, Session}; pub use models::{Metric, MetricKey, NewMetric}; @@ -148,18 +200,21 @@ fn setup_db>(path: P) -> Result { Ok(db) } -/// Like `setup_db`, but if the database is malformed, removes it and retries once. -fn setup_db_or_reset>(path: P) -> Result { +/// Like `setup_db`, but if the database is malformed, removes it and retries +/// once. The boolean in the success result is `true` when a reset actually +/// happened, so callers can drop any cached state that referenced the old +/// database (notably `metric_keys` row ids). +fn setup_db_or_reset>(path: P) -> Result<(SqliteConnection, bool)> { let path = path.as_ref(); match setup_db(path) { - Ok(db) => Ok(db), + Ok(db) => Ok((db, false)), Err(err) if err.is_malformed_db() => { warn!( "Database is malformed, removing and recreating: {}", path.display() ); remove_db_files(path); - setup_db(path) + setup_db(path).map(|db| (db, true)) } Err(err) => Err(err), } @@ -173,7 +228,6 @@ enum RegisterType { enum Event { Stop, DescribeKey(RegisterType, KeyName, Option, SharedString), - RegisterKey(RegisterType, Key, Arc), IncrementCounter(Duration, Key, u64), AbsoluteCounter(Duration, Key, u64), UpdateGauge(Duration, Key, GaugeValue), @@ -220,9 +274,11 @@ impl SqliteExporterHandle { pub struct SqliteExporter { thread: Option>, sender: SyncSender, + send_error_throttle: Mutex, } struct InnerState { db: SqliteConnection, + db_path: PathBuf, last_housekeeping: Instant, housekeeping: Option, retention: Option, @@ -233,11 +289,14 @@ struct InnerState { counters: HashMap, key_ids: HashMap, queue: VecDeque, + consecutive_flush_failures: u64, + last_reconnect: Option, } impl InnerState { - fn new(flush_duration: Duration, db: SqliteConnection) -> Self { + fn new(flush_duration: Duration, db: SqliteConnection, db_path: PathBuf) -> Self { InnerState { db, + db_path, last_housekeeping: Instant::now(), housekeeping: None, retention: None, @@ -248,6 +307,8 @@ impl InnerState { counters: HashMap::new(), key_ids: HashMap::new(), queue: VecDeque::with_capacity(FLUSH_QUEUE_LIMIT), + consecutive_flush_failures: 0, + last_reconnect: None, } } fn set_housekeeping( @@ -283,31 +344,120 @@ impl InnerState { } } fn flush(&mut self) -> Result<(), diesel::result::Error> { - use crate::schema::metrics::dsl::metrics; if self.queue.is_empty() { self.last_flush = Instant::now(); return Ok(()); } - let drain_buffer: Vec = self.queue.drain(..).collect(); - let db = &mut self.db; - let transaction_result = db.transaction::<_, diesel::result::Error, _>(|db| { - let chunk_size = INSERT_BATCH_SIZE.max(1); - for chunk in drain_buffer.chunks(chunk_size) { - insert_into(metrics).values(chunk).execute(db)?; - } - Ok(()) - }); - match transaction_result { + // Operate on the queue in-place: pass the deque's two backing slices + // directly to the insert. On failure we leave the queue alone, so a + // broken database stays at zero memcpy cost per attempt — the cascade + // that filled the channel in the original incident is what made each + // failed flush O(queue_size) by draining and re-extending. + let (front, back) = self.queue.as_slices(); + match Self::insert_metrics(&mut self.db, [front, back]) { Ok(()) => { + self.queue.clear(); self.last_flush = Instant::now(); + self.consecutive_flush_failures = 0; Ok(()) } Err(e) => { - self.queue.extend(drain_buffer); + self.consecutive_flush_failures += 1; + // Queue is intact; just cap memory. + self.enforce_queue_cap(); + // A broken transaction manager never heals on its own, so the + // connection has to be rebuilt; also rebuild after repeated + // failures of any kind as a backstop. + if Self::is_connection_fatal(&e) + || self.consecutive_flush_failures >= RECONNECT_AFTER_FAILURES + { + self.reconnect(); + } Err(e) } } } + + /// Inserts every metric in a single transaction, batched to stay under + /// SQLite's bound-variable limit. Accepts multiple slices so a `VecDeque` + /// can be inserted in-place without copying into a contiguous buffer. + fn insert_metrics<'a, S>( + db: &mut SqliteConnection, + slabs: S, + ) -> Result<(), diesel::result::Error> + where + S: IntoIterator, + { + use crate::schema::metrics::dsl::metrics; + db.transaction::<_, diesel::result::Error, _>(|db| { + let chunk_size = INSERT_BATCH_SIZE.max(1); + for slab in slabs { + for chunk in slab.chunks(chunk_size) { + insert_into(metrics).values(chunk).execute(db)?; + } + } + Ok(()) + }) + } + + /// Drops the oldest queued metrics if the queue has grown past its hard + /// limit, so a database that stays unreachable can't exhaust memory. + fn enforce_queue_cap(&mut self) { + if self.queue.len() > QUEUE_HARD_LIMIT { + let overflow = self.queue.len() - QUEUE_HARD_LIMIT; + self.queue.drain(..overflow); + warn!( + "metrics-sqlite queue exceeded {} items while flushing kept failing, dropped {} oldest metrics", + QUEUE_HARD_LIMIT, overflow + ); + } + } + + /// True for errors that leave the connection permanently unusable, where + /// the only recovery is to rebuild it. + fn is_connection_fatal(e: &diesel::result::Error) -> bool { + matches!(e, diesel::result::Error::BrokenTransactionManager) + } + + /// Rebuilds the SQLite connection after a fatal error, subject to a backoff + /// so a permanently broken database can't cause a reconnect storm. + fn reconnect(&mut self) { + if let Some(last) = self.last_reconnect { + if last.elapsed() < RECONNECT_BACKOFF { + return; + } + } + self.last_reconnect = Some(Instant::now()); + warn!( + "metrics-sqlite database connection is broken, reconnecting to {}", + self.db_path.display() + ); + match setup_db_or_reset(&self.db_path) { + Ok((db, was_reset)) => { + self.db = db; + // Cached key ids may be stale if the database was recreated. + self.key_ids.clear(); + self.consecutive_flush_failures = 0; + if was_reset { + // The database was malformed and recreated, so any rows we + // had queued reference `metric_key_id` values from the old + // `metric_keys` table. Inserting them now would orphan or + // misattribute them in the join, so drop the queue. + let dropped = self.queue.len(); + if dropped > 0 { + warn!( + "metrics-sqlite database was recreated; dropping {dropped} queued metrics with stale key ids" + ); + self.queue.clear(); + } + } + info!("metrics-sqlite database connection re-established"); + } + Err(e) => { + error!("metrics-sqlite failed to reconnect to database: {:?}", e); + } + } + } fn queue_metric(&mut self, timestamp: Duration, key: &str, value: f64) -> Result<()> { let metric_key_id = match self.key_ids.get(key) { Some(key) => *key, @@ -340,13 +490,16 @@ impl InnerState { fn run_worker( db: SqliteConnection, + db_path: PathBuf, receiver: Receiver, flush_duration: Duration, ) -> JoinHandle<()> { thread::Builder::new() .name("metrics-sqlite: worker".to_string()) .spawn(move || { - let mut state = InnerState::new(flush_duration, db); + let mut state = InnerState::new(flush_duration, db, db_path); + let mut flush_error_throttle = LogThrottle::new(ERROR_LOG_INTERVAL); + let mut queue_error_throttle = LogThrottle::new(ERROR_LOG_INTERVAL); info!("SQLite worker started"); loop { // Check if we need to flush based on elapsed time @@ -378,9 +531,6 @@ fn run_worker( error!("Failed to create key entry: {:?}", e); } } - Ok(Event::RegisterKey(_key_type, _key, _handle)) => { - // we currently don't do anything with register... - } Ok(Event::IncrementCounter(timestamp, key, value)) => { let key_name = key.name(); let entry = state.counters.entry(key.clone()).or_insert(0); @@ -389,7 +539,18 @@ fn run_worker( *entry }; if let Err(e) = state.queue_metric(timestamp, key_name, value as _) { - error!("Error queueing metric: {:?}", e); + queue_error_throttle.log_if_due(|suppressed| { + if suppressed > 0 { + error!( + "Error queueing metric: {:?} ({} similar errors suppressed in the last {}s)", + e, + suppressed, + ERROR_LOG_INTERVAL.as_secs() + ); + } else { + error!("Error queueing metric: {:?}", e); + } + }); } should_flush = state.should_flush(); } @@ -397,7 +558,18 @@ fn run_worker( let key_name = key.name(); state.counters.insert(key.clone(), value); if let Err(e) = state.queue_metric(timestamp, key_name, value as _) { - error!("Error queueing metric: {:?}", e); + queue_error_throttle.log_if_due(|suppressed| { + if suppressed > 0 { + error!( + "Error queueing metric: {:?} ({} similar errors suppressed in the last {}s)", + e, + suppressed, + ERROR_LOG_INTERVAL.as_secs() + ); + } else { + error!("Error queueing metric: {:?}", e); + } + }); } should_flush = state.should_flush(); } @@ -419,14 +591,36 @@ fn run_worker( } }; if let Err(e) = state.queue_metric(timestamp, key_name, value) { - error!("Error queueing metric: {:?}", e); + queue_error_throttle.log_if_due(|suppressed| { + if suppressed > 0 { + error!( + "Error queueing metric: {:?} ({} similar errors suppressed in the last {}s)", + e, + suppressed, + ERROR_LOG_INTERVAL.as_secs() + ); + } else { + error!("Error queueing metric: {:?}", e); + } + }); } should_flush = state.should_flush(); } Ok(Event::UpdateHistogram(timestamp, key, value)) => { let key_name = key.name(); if let Err(e) = state.queue_metric(timestamp, key_name, value) { - error!("Error queueing metric: {:?}", e); + queue_error_throttle.log_if_due(|suppressed| { + if suppressed > 0 { + error!( + "Error queueing metric: {:?} ({} similar errors suppressed in the last {}s)", + e, + suppressed, + ERROR_LOG_INTERVAL.as_secs() + ); + } else { + error!("Error queueing metric: {:?}", e); + } + }); } should_flush = state.should_flush(); } @@ -483,7 +677,18 @@ fn run_worker( debug!("Flushing due to elapsed time ({}s)", flush_duration.as_secs()); } if let Err(e) = state.flush() { - error!("Error flushing metrics: {}", e); + if let Some(suppressed) = flush_error_throttle.allow() { + if suppressed > 0 { + error!( + "Error flushing metrics: {} ({} similar errors suppressed in the last {}s)", + e, + suppressed, + ERROR_LOG_INTERVAL.as_secs() + ); + } else { + error!("Error flushing metrics: {}", e); + } + } } } if state.should_housekeep() { @@ -510,13 +715,15 @@ impl SqliteExporter { keep_duration: Option, path: P, ) -> Result { - let mut db = setup_db_or_reset(path)?; + let path = path.as_ref().to_path_buf(); + let (mut db, _was_reset) = setup_db_or_reset(&path)?; Self::housekeeping(&mut db, keep_duration, None, true); let (sender, receiver) = std::sync::mpsc::sync_channel(BACKGROUND_CHANNEL_LIMIT); - let thread = run_worker(db, receiver, flush_interval); + let thread = run_worker(db, path, receiver, flush_interval); let exporter = SqliteExporter { thread: Some(thread), sender, + send_error_throttle: Mutex::new(LogThrottle::new(ERROR_LOG_INTERVAL)), }; Ok(exporter) } @@ -602,6 +809,29 @@ impl SqliteExporter { } } + /// Logs a failure to hand an event to the worker, rate-limited so that a + /// full channel (sustained backpressure) can't flood the logs. + fn log_send_failure(&self, context: &str, err: &dyn std::fmt::Debug) { + if let Ok(mut throttle) = self.send_error_throttle.lock() { + if let Some(suppressed) = throttle.allow() { + if suppressed > 0 { + error!( + "Error sending metric {} to SQLite worker: {:?} ({} similar errors suppressed in the last {}s)", + context, + err, + suppressed, + ERROR_LOG_INTERVAL.as_secs() + ); + } else { + error!( + "Error sending metric {} to SQLite worker: {:?}", + context, err + ); + } + } + } + } + /// Install recorder as `metrics` crate's Recorder pub fn install(self) -> Result> { let handle = SqliteExporterHandle { @@ -620,9 +850,160 @@ impl Drop for SqliteExporter { #[cfg(test)] mod tests { - use crate::SqliteExporter; + use crate::{ + InnerState, LogThrottle, NewMetric, QUEUE_HARD_LIMIT, SqliteExporter, setup_db_or_reset, + }; use std::time::{Duration, Instant}; + fn test_state() -> (InnerState, tempfile::TempDir) { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("metrics.db"); + let (db, _was_reset) = setup_db_or_reset(&path).unwrap(); + (InnerState::new(Duration::from_secs(5), db, path), dir) + } + + #[test] + fn enforce_queue_cap_drops_oldest_when_over_limit() { + let (mut state, _dir) = test_state(); + // Queue more than the hard limit; values are tagged 0..N so we can tell + // which survived. + let total = QUEUE_HARD_LIMIT + 250; + for i in 0..total { + state.queue.push_back(NewMetric { + timestamp: 0.0, + metric_key_id: 1, + value: i as f64, + }); + } + state.enforce_queue_cap(); + // Queue is clamped to the limit and it's the oldest that were dropped. + assert_eq!(state.queue.len(), QUEUE_HARD_LIMIT); + assert_eq!(state.queue.front().unwrap().value, 250.0); + assert_eq!(state.queue.back().unwrap().value, (total - 1) as f64); + // A queue at or under the limit is left untouched. + state.enforce_queue_cap(); + assert_eq!(state.queue.len(), QUEUE_HARD_LIMIT); + } + + #[test] + fn failed_flush_leaves_queue_intact() { + use diesel::connection::SimpleConnection; + let (mut state, _dir) = test_state(); + // Drop the metrics table so every insert in the next flush fails. We + // do this from the test, not via a production API, to keep the test + // hook out of the public crate surface. + state + .db + .batch_execute("DROP TABLE metrics") + .expect("setup: drop metrics table"); + for i in 0..5_000 { + state.queue.push_back(NewMetric { + timestamp: 0.0, + metric_key_id: 1, + value: i as f64, + }); + } + let before = state.queue.len(); + assert!(state.flush().is_err(), "flush should fail"); + // The whole point of the in-place flush: the queue is intact. + assert_eq!(state.queue.len(), before); + assert_eq!(state.queue.front().unwrap().value, 0.0); + assert_eq!(state.queue.back().unwrap().value, 4_999.0); + assert_eq!(state.consecutive_flush_failures, 1); + } + + #[test] + fn reconnect_drops_queue_when_database_is_recreated() { + use crate::setup_db; + use diesel::connection::SimpleConnection; + use std::io::{Seek, SeekFrom, Write}; + let (mut state, _dir) = test_state(); + for i in 0..100 { + state.queue.push_back(NewMetric { + timestamp: 0.0, + metric_key_id: 1, + value: i as f64, + }); + } + // Force pending writes into the main DB file and truncate the WAL so + // the corruption below isn't masked by WAL contents. + state + .db + .batch_execute("PRAGMA wal_checkpoint(TRUNCATE)") + .expect("setup: wal checkpoint"); + // Release the real DB's file handle before we corrupt and reset. On + // Windows the malformed-reset path can't `DeleteFile` a file that + // still has an open handle, so without this swap the reset silently + // fails and the queue never gets cleared (unlike POSIX, where unlink + // works through open handles). The in-memory placeholder just keeps + // `InnerState` in a valid shape until `reconnect` replaces it. + state.db = setup_db(":memory:").expect("setup: in-memory placeholder"); + // Keep a valid SQLite header (first 100 bytes) but overwrite a later + // page so the next `PRAGMA quick_check` trips the malformed path. We + // overwrite a long enough region to clobber whichever page holds the + // schema, regardless of page size. + let mut f = std::fs::OpenOptions::new() + .write(true) + .open(&state.db_path) + .expect("setup: open db file"); + f.seek(SeekFrom::Start(100)) + .expect("setup: seek past header"); + f.write_all(&[0xffu8; 16 * 1024]) + .expect("setup: write garbage pages"); + f.sync_all().expect("setup: sync garbage to disk"); + drop(f); + + state.reconnect(); + + // Reset happened: the rebuilt DB has fresh `metric_keys` ids, so the + // queued rows must be dropped to avoid orphaning them. + assert!( + state.queue.is_empty(), + "queue should be cleared after reset" + ); + assert_eq!(state.consecutive_flush_failures, 0); + // And the new connection is usable. + assert!(state.flush().is_ok()); + } + + #[test] + fn reconnect_rebuilds_connection_with_backoff() { + let (mut state, _dir) = test_state(); + state.consecutive_flush_failures = 5; + state.reconnect(); + // A successful reconnect clears the failure counter and records when it + // happened. + assert_eq!(state.consecutive_flush_failures, 0); + let first_attempt = state.last_reconnect.expect("reconnect should run"); + // The rebuilt connection is usable. + assert!(state.flush().is_ok()); + + // A second reconnect right away is suppressed by the backoff, so it + // neither re-attempts nor touches state. + state.consecutive_flush_failures = 5; + state.reconnect(); + assert_eq!(state.consecutive_flush_failures, 5); + assert_eq!(state.last_reconnect, Some(first_attempt)); + } + + #[test] + fn log_throttle_suppresses_and_reports_count() { + let mut throttle = LogThrottle::new(Duration::from_millis(50)); + // First call is always allowed, with nothing suppressed yet. + assert_eq!(throttle.allow(), Some(0)); + // Rapid follow-ups are suppressed. + for _ in 0..7 { + assert_eq!(throttle.allow(), None); + } + // Once the interval elapses, the next call is allowed and reports how + // many were suppressed in between. + std::thread::sleep(Duration::from_millis(60)); + assert_eq!(throttle.allow(), Some(7)); + // Counter resets after reporting. + std::thread::sleep(Duration::from_millis(60)); + assert_eq!(throttle.allow(), Some(0)); + } + #[test] fn test_threading() { use std::thread; diff --git a/src/metrics_db.rs b/src/metrics_db.rs index 3854a94..84b4830 100644 --- a/src/metrics_db.rs +++ b/src/metrics_db.rs @@ -179,9 +179,10 @@ impl MetricsDb { pub fn import_from_csv, D: AsRef>(path: S, destination: D) -> Result<()> { use crate::InnerState; use csv::ReaderBuilder; - let db = setup_db(destination)?; + let destination = destination.as_ref().to_path_buf(); + let db = setup_db(&destination)?; let mut reader = ReaderBuilder::new().from_path(path)?; - let mut inner = InnerState::new(Duration::from_secs(5), db); + let mut inner = InnerState::new(Duration::from_secs(5), db, destination); let header = reader.headers()?.to_owned(); let mut flush_counter = 0u64; for record in reader.records() { diff --git a/src/recorder.rs b/src/recorder.rs index 38255f6..164df4e 100644 --- a/src/recorder.rs +++ b/src/recorder.rs @@ -7,6 +7,7 @@ use std::{ sync::{Arc, mpsc::SyncSender}, time::SystemTime, }; +#[cfg(feature = "log_dropped_metrics")] use tracing::error; pub(crate) struct Handle { @@ -154,7 +155,7 @@ impl Recorder for SqliteExporter { unit, description, )) { - error!("Error sending metric description: {:?}", e); + self.log_send_failure("description", &e); } } @@ -165,7 +166,7 @@ impl Recorder for SqliteExporter { unit, description, )) { - error!("Error sending metric description: {:?}", e); + self.log_send_failure("description", &e); } } @@ -176,56 +177,31 @@ impl Recorder for SqliteExporter { unit, description, )) { - error!("Error sending metric description: {:?}", e); + self.log_send_failure("description", &e); } } - // in future we could record these to the SQLite database for informational/metadata usage + // Registration doesn't touch the worker channel: the previous implementation + // sent a `RegisterKey` event that the worker ignored, which under load + // produced the dominant share of "TrySendError::Full" spam. fn register_counter(&self, key: &Key, _metadata: &Metadata) -> Counter { - let sender = self.sender.clone(); - let handle = Arc::new(Handle { - sender, + Counter::from_arc(Arc::new(Handle { + sender: self.sender.clone(), key: key.clone(), - }); - if let Err(e) = self.sender.try_send(Event::RegisterKey( - RegisterType::Counter, - key.clone(), - handle.clone(), - )) { - error!("Error sending metric registration: {:?}", e); - } - Counter::from_arc(handle) + })) } fn register_gauge(&self, key: &Key, _metadata: &Metadata) -> Gauge { - let sender = self.sender.clone(); - let handle = Arc::new(Handle { - sender, + Gauge::from_arc(Arc::new(Handle { + sender: self.sender.clone(), key: key.clone(), - }); - if let Err(e) = self.sender.try_send(Event::RegisterKey( - RegisterType::Gauge, - key.clone(), - handle.clone(), - )) { - error!("Error sending metric registration: {:?}", e); - } - Gauge::from_arc(handle) + })) } fn register_histogram(&self, key: &Key, _metadata: &Metadata) -> Histogram { - let sender = self.sender.clone(); - let handle = Arc::new(Handle { - sender, + Histogram::from_arc(Arc::new(Handle { + sender: self.sender.clone(), key: key.clone(), - }); - if let Err(e) = self.sender.try_send(Event::RegisterKey( - RegisterType::Histogram, - key.clone(), - handle.clone(), - )) { - error!("Error sending metric registration: {:?}", e); - } - Histogram::from_arc(handle) + })) } }