diff --git a/.github/workflows/telemetry-server.yml b/.github/workflows/telemetry-server.yml new file mode 100644 index 000000000..e543721f9 --- /dev/null +++ b/.github/workflows/telemetry-server.yml @@ -0,0 +1,34 @@ +name: telemetry-server +# The ingest server is its own workspace (crates/horizon-telemetry-server), so the checks job doesn't build +# it: this one does, when it or the crate it shares with the launcher and the game changes. +on: + push: + branches: [main] + paths: + - 'crates/horizon-telemetry/**' + - 'crates/horizon-telemetry-server/**' + - '.github/workflows/telemetry-server.yml' + pull_request: + paths: + - 'crates/horizon-telemetry/**' + - 'crates/horizon-telemetry-server/**' + - '.github/workflows/telemetry-server.yml' +permissions: + contents: read +jobs: + check: + runs-on: ubuntu-latest + timeout-minutes: 20 + defaults: + run: + working-directory: crates/horizon-telemetry-server + steps: + - uses: actions/checkout@v4 + - run: rustup toolchain install stable --profile minimal --component rustfmt --component clippy + - uses: Swatinem/rust-cache@v2 + with: + workspaces: crates/horizon-telemetry-server + save-if: ${{ github.ref == 'refs/heads/main' }} + - run: cargo +stable fmt --check + - run: cargo +stable clippy --all-targets --locked -- -D warnings + - run: cargo +stable test --locked diff --git a/Cargo.toml b/Cargo.toml index c8e81b0af..6c21e207d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -exclude = ["launcher"] +exclude = ["launcher", "crates/horizon-telemetry-server"] members = ["crates/horizon-core", "crates/horizon-world", "crates/voxel-kernels", "crates/horizon-client", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-prepare", "crates/horizon-telemetry", "xtask"] resolver = "2" diff --git a/crates/horizon-telemetry-server/.gitignore b/crates/horizon-telemetry-server/.gitignore new file mode 100644 index 000000000..b83d22266 --- /dev/null +++ b/crates/horizon-telemetry-server/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/crates/horizon-telemetry-server/Cargo.lock b/crates/horizon-telemetry-server/Cargo.lock new file mode 100644 index 000000000..e4e7d060e --- /dev/null +++ b/crates/horizon-telemetry-server/Cargo.lock @@ -0,0 +1,1106 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "axum" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31b698c5f9a010f6573133b09e0de5408834d0c82f8d7475a89fc1867a71cd90" +dependencies = [ + "axum-core", + "bytes", + "form_urlencoded", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "serde_core", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tower", + "tower-layer", + "tower-service", +] + +[[package]] +name = "axum-core" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c78f31d7b1291f7ee735c1c6780ccde7785daae9a9206026862dab7d8792d1" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "http-body-util", + "mime", + "pin-project-lite", + "sync_wrapper", + "tower-layer", + "tower-service", +] + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "bitflags" +version = "2.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" + +[[package]] +name = "bytes" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" + +[[package]] +name = "cc" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6651c9ed80effdc7db0ff72512157f901af5e3549e341e24b1dd4887d836d838" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "displaydoc" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6232dd377dcc64799954cbd3a9bb882e9cdc1308ccd87b1c098f1fb2eaf82a8" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "fallible-iterator" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" + +[[package]] +name = "fallible-streaming-iterator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" + +[[package]] +name = "find-msvc-tools" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aedcfb3409746eddb02b9e19ebda1c3394f759a152e48ee875a0844d1b955484" + +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures-core" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" + +[[package]] +name = "futures-task" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd" + +[[package]] +name = "futures-util" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "slab", +] + +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasip2", +] + +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "foldhash", +] + +[[package]] +name = "hashlink" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7382cf6263419f2d8df38c55d7da83da5c18aef87fc7a7fc1fb1e344edfe14c1" +dependencies = [ + "hashbrown", +] + +[[package]] +name = "horizon-telemetry" +version = "0.1.0" +dependencies = [ + "getrandom 0.3.4", + "ruzstd", + "serde", + "serde_json", + "ureq", +] + +[[package]] +name = "horizon-telemetry-server" +version = "0.1.0" +dependencies = [ + "axum", + "horizon-telemetry", + "http-body-util", + "rusqlite", + "ruzstd", + "serde", + "serde_json", + "tokio", + "tower", +] + +[[package]] +name = "http" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2a8f2913ee65f60facd6a5905613afaa448497a0230cc41ce022d93290bc2c" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23169fe34a5fbcdd3f3862e78fb9b6fccd5f02a6dc6f732547005d45631ce71c" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "hyper" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c3e324da4c95177d6291d4c8730197c0d1822f8a9766814a4a44fa5ab797c9c" +dependencies = [ + "atomic-waker", + "bytes", + "futures-core", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", +] + +[[package]] +name = "hyper-util" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddc03d96684f9226b8a787cdb71488417b53ab5ea8fdb1dac946cb9431cc8bff" +dependencies = [ + "bytes", + "http", + "http-body", + "hyper", + "pin-project-lite", + "tokio", + "tower-service", +] + +[[package]] +name = "icu_collections" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa68d21081c4a05d5a901a1c62add574c77048b6a1c67be3b50ce0b60d4ca513" +dependencies = [ + "displaydoc", + "potential_utf", + "utf8_iter", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d56e28588da92eee5c3201a6eff33fabdd49b62269c8938d4ff050ce4d900deb" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_normalizer" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12f9cf5f235641ed274641dd81c3f28d870e276763d0797aeeab72317b1c646f" +dependencies = [ + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1563da1ed3e0b3bf3d74c9b85917ac9c56464d2f57242270c09c9e752f8021a0" + +[[package]] +name = "icu_properties" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e7ca276ad3145661a65914e6daf131ca5120cd3dcee8f8f3214b8875184a148" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e590f038c1464a96894fd6d10127e90a8be4509f56ff7ecef851b15cee0b7caa" + +[[package]] +name = "icu_provider" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d27bbb9d3abbefac45d55f647c9de1d44aafcd1186eb91879afef17c396c3e73" +dependencies = [ + "displaydoc", + "icu_locale_core", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb68373c0d6620ef8105e855e7745e18b0d00d3bdb07fb532e434244cdb9a714" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "libc" +version = "0.2.190" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce5d3ddc6d3fa000eb1536d85e147bfe31aacaba692ed6a876f95cb7c855be78" + +[[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 = "litemap" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d9d19d1d6efa0109d2f65ff4c85cddd50bd572e5a00127ab10987290bcefae" + +[[package]] +name = "log" +version = "0.4.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" + +[[package]] +name = "matchit" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3" + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "mio" +version = "1.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1788edb87fdc09c7e26304471e2f5be8cdefb1b6930d6e3985fc02ff53bf86ee" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.61.2", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[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.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" + +[[package]] +name = "potential_utf" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d83eb9bc6d8e5cf568e7a1101d60ee05e81ed50ea106026f3d18deeb046d7661" +dependencies = [ + "zerovec", +] + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom 0.2.17", + "libc", + "untrusted", + "windows-sys 0.52.0", +] + +[[package]] +name = "rusqlite" +version = "0.37.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "165ca6e57b20e1351573e3729b958bc62f0e48025386970b6e4d29e7a7e71f3f" +dependencies = [ + "bitflags", + "fallible-iterator", + "fallible-streaming-iterator", + "hashlink", + "libsqlite3-sys", + "smallvec", +] + +[[package]] +name = "rustls" +version = "0.23.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf" +dependencies = [ + "log", + "once_cell", + "ring", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-pki-types" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f4925028c7eb5d1fcdaf196971378ed9d2c1c4efc7dc5d011256f76c99c0a96" +dependencies = [ + "zeroize", +] + +[[package]] +name = "rustls-webpki" +version = "0.103.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3c3cf1d8b1e7d4927e2d154c3fcb02979afb9939629c62cd9048d4f07b60ac2" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + +[[package]] +name = "ruzstd" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69e4ce4a039f42bf25920b95667e4d789e98749ab11bbb8d2c2936f788421bb5" +dependencies = [ + "twox-hash", +] + +[[package]] +name = "ryu" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.154" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "serde_path_to_error" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a9ff822e371bb5403e391ecd83e182e0e77ba7f6fe0160b795797109d1b457" +dependencies = [ + "itoa", + "serde", + "serde_core", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "smallvec" +version = "1.16.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b3dc8af474f516a851ff4bd12db780f948b9250ad37211e4eec0bccea54e01b" + +[[package]] +name = "socket2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "3.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" + +[[package]] +name = "synstructure" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "901704edd0dfe137f1987838ee4f259e4e063c31371bdb423f7ae38ec6f77f02" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tinystr" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1e27c91459209c2986af3dcf603a5a74a4368754ce37414f59acc971167f643" +dependencies = [ + "displaydoc", + "zerovec", +] + +[[package]] +name = "tokio" +version = "1.53.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce3335fa71841cda333a58d7615b03901380ecf09d59b3296d21f8bbac0dde4e" +dependencies = [ + "libc", + "mio", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys 0.61.2", +] + +[[package]] +name = "tokio-macros" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tower" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper", + "tokio", + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "twox-hash" +version = "2.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86a801b3cea342a06d468c8710662aa29e5e05e4f5c0d62f00bbb7f2ad7941c2" + +[[package]] +name = "unicode-ident" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" + +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "ureq" +version = "2.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02d1a66277ed75f640d608235660df48c8e3c19f3b4edb6a263315626cc3c01d" +dependencies = [ + "base64", + "log", + "once_cell", + "rustls", + "rustls-pki-types", + "url", + "webpki-roots 0.26.11", +] + +[[package]] +name = "url" +version = "2.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasip2" +version = "1.0.4+wasi-0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b67efb37e106e55ce722a510d6b5f9c17f083e5fc79afc2badeb12cc313d9487" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "webpki-roots" +version = "0.26.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "521bc38abb08001b01866da9f51eb7c5d647a19260e00054a8c7fd5f9e57f7a9" +dependencies = [ + "webpki-roots 1.0.9", +] + +[[package]] +name = "webpki-roots" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dcd9d09a39985f5344844e66b0c530a33843579125f23e21e9f0f220850f22a" +dependencies = [ + "rustls-pki-types", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[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 = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "wit-bindgen" +version = "0.57.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + +[[package]] +name = "writeable" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc" + +[[package]] +name = "yoke" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "709fe23a0424b6a435d82152b1bd3fdfb0833487d5fa90d05d42762a9891fef5" +dependencies = [ + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec8ebde2db3681e8c9980cc27822030e68752690ddfa9473e739aeb4dbde6d71" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zerofrom" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ec05a11813ea801ff6d75110ad09cd0824ddba17dfe17128ea0d5f68e6c5272" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f75b4683f6c7f45248d4d64056a24298c6281e0993356d7d1b4a1a962ef10d4a" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zeroize" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13084392c5e4bc371903e2935a5eaeed24905a7511356b883835e18a78f6879" + +[[package]] +name = "zerotrie" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea269c3bd32f0a32c321907a2ae912ba6f4649bb0fc764a15627e99a7095a3f" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", +] + +[[package]] +name = "zerovec" +version = "0.11.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0464e17806c1d976d5cba29399c7f08e516e279e2ba493f63123b5fca67dd8" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.11.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34df6fc39dbd26ddc9c10e6a2984476e13acce22e64e4487636ef494369225da" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" diff --git a/crates/horizon-telemetry-server/Cargo.toml b/crates/horizon-telemetry-server/Cargo.toml new file mode 100644 index 000000000..8dbb0b7fd --- /dev/null +++ b/crates/horizon-telemetry-server/Cargo.toml @@ -0,0 +1,31 @@ +[package] +name = "horizon-telemetry-server" +version = "0.1.0" +edition = "2024" +license = "MIT" +rust-version = "1.90" +publish = false +description = "Receives opt-in data collection batches, keeps them compactly in SQLite and serves anonymous totals" + +# Its own workspace, as the launcher's: the game's workspace never builds a web server. +[workspace] + +[dependencies] +horizon-telemetry = { path = "../horizon-telemetry", default-features = false } +axum = { version = "0.8", default-features = false, features = ["http1", "tokio", "json", "query"] } +tokio = { version = "1", features = ["rt-multi-thread", "macros", "net", "signal", "time"] } +# SQLite built in: one file on a small volume is the whole database. +rusqlite = { version = "0.37", features = ["bundled"] } +serde = { version = "1", features = ["derive"] } +serde_json = "1" +ruzstd = "0.9" + +[dev-dependencies] +# The uploader the launcher uses, against this server, end to end. +horizon-telemetry = { path = "../horizon-telemetry" } +tower = { version = "0.5", features = ["util"] } +http-body-util = "0.1" + +[profile.release] +strip = true +lto = "thin" diff --git a/crates/horizon-telemetry-server/Dockerfile b/crates/horizon-telemetry-server/Dockerfile new file mode 100644 index 000000000..6a4a53c8d --- /dev/null +++ b/crates/horizon-telemetry-server/Dockerfile @@ -0,0 +1,13 @@ +# Built from the repository root: docker build -f crates/horizon-telemetry-server/Dockerfile . +FROM rust:1-slim AS build +WORKDIR /src +# The root manifest only for the shared crate's workspace settings (edition, lints). +COPY Cargo.toml Cargo.toml +COPY crates/horizon-telemetry crates/horizon-telemetry +COPY crates/horizon-telemetry-server crates/horizon-telemetry-server +RUN cargo build --release --locked --manifest-path crates/horizon-telemetry-server/Cargo.toml + +FROM debian:stable-slim +COPY --from=build /src/crates/horizon-telemetry-server/target/release/horizon-telemetry-server /usr/local/bin/ +ENV HORIZON_TELEMETRY_DB=/data/telemetry.sqlite3 HORIZON_TELEMETRY_TRUST_PROXY=1 +CMD ["horizon-telemetry-server"] diff --git a/crates/horizon-telemetry-server/README.md b/crates/horizon-telemetry-server/README.md new file mode 100644 index 000000000..cf2d52393 --- /dev/null +++ b/crates/horizon-telemetry-server/README.md @@ -0,0 +1,58 @@ +# horizon-telemetry-server + +This server receives what players choose to send (performance summaries, crash and bug reports, benchmark uploads: [docs/telemetry.md](../../docs/telemetry.md)) and stores it in one SQLite file. It serves anonymous totals to the website. It is one small Rust process, about 7 MB of memory when idle. + + cargo run --release # in this folder: its own workspace, like the launcher's + PORT=8080 HORIZON_TELEMETRY_DB=/data/telemetry.sqlite3 horizon-telemetry-server + +The launcher sends to it when `HORIZON_TELEMETRY_URL` is set (and to `DEFAULT_ENDPOINT` in `crates/horizon-telemetry` once there is one). + +## Endpoints + +| | | +|---|---| +| `POST /v1/batch` | A zstd batch (`Content-Encoding: zstd`, at most 256 KiB, 1 MiB inflated, 64 items). Returns 204. Returns 400 for a bad batch, 413 for one too large, 415 if it is not zstd, 429 when over a limit. | +| `DELETE /v1/installs/` | Deletes everything that install sent. Its share of the daily totals is rebuilt at once. | +| `GET /v1/aggregate?days=30` | The totals the website reads. Cached 10 minutes, with `Access-Control-Allow-Origin` set to the website. | +| `GET /healthz` | `ok` | + +The totals are grouped per version, OS, GPU vendor and render distance. Each group has its sessions, install-days, hours played, the 14 frame-time buckets summed, and the mean p50 and p99 frame times. Groups with fewer than 5 install-days are left out. The totals also list crash counts per version and the newest 50 benchmark uploads. + +## Storage + +- `systems`: each distinct machine description once. Sessions point at it. +- `perf`: one row per session, in columns, its frame and tick summaries as 80-byte blobs. +- `reports`: crash and bug reports and benchmarks, their data compressed with zstd. +- `perf_daily`: the totals per day, rebuilt from `perf` when a day changes (new sessions, a deletion). + +Measured: **about 190 bytes per play session** on disk, indexes included (`tests/server.rs`, 10 000 sessions). 1 000 players playing twice a day for 180 days is about 70 MB. + +The day stored is the day the server received an item. The sender's IP address is never written down. The per-address rate limit keeps addresses in memory for an hour at most. + +Raw rows are deleted after `HORIZON_TELEMETRY_RETENTION_DAYS` (180). The daily totals stay; they hold no install ID. + +## Settings + +All are environment variables: + +- `PORT` +- `HORIZON_TELEMETRY_DB` +- `HORIZON_TELEMETRY_RETENTION_DAYS` (180) +- `HORIZON_TELEMETRY_MIN_INSTALL_DAYS` (5) +- `HORIZON_TELEMETRY_REQUESTS_PER_HOUR` per address (60, bursts of 6) +- `HORIZON_TELEMETRY_ITEMS_PER_DAY` per install (100) +- `HORIZON_TELEMETRY_TRUST_PROXY=1` behind a proxy (the client's address is then the first of `X-Forwarded-For`) +- `HORIZON_TELEMETRY_ALLOW_ORIGIN` (`https://playhorizon.dev`) + +## Hosting + +`Dockerfile` builds it from the repository root (`docker build -f crates/horizon-telemetry-server/Dockerfile .`). The database goes on a volume mounted at `/data`. + +At Railway's prices (RAM $10/GB-month, CPU $20/vCPU-month, volumes $0.15/GB-month, egress $0.05/GB), one more service with a 1 GB volume uses about $0.35 a month: + +- RAM about 15 MB: $0.15 +- CPU nearly idle: under $0.05 +- the volume: $0.15 +- egress, the cached totals: cents + +That fits inside the Hobby plan's included $5 of usage. Railway Postgres would cost a few dollars a month for its memory alone and buys nothing at this size. A bucket isn't needed: crash reports are a few KB compressed and live in the same file. diff --git a/crates/horizon-telemetry-server/src/lib.rs b/crates/horizon-telemetry-server/src/lib.rs new file mode 100644 index 000000000..b1521a714 --- /dev/null +++ b/crates/horizon-telemetry-server/src/lib.rs @@ -0,0 +1,307 @@ +//! The ingest server for opt-in data collection (docs/telemetry.md). +//! +//! - `POST /v1/batch`: a zstd batch from the launcher, checked by `horizon_telemetry::batch`. +//! - `DELETE /v1/installs/`: everything an install sent goes. +//! - `GET /v1/aggregate?days=30`: the anonymous totals the website reads, cached. +//! - `GET /healthz`. +//! +//! Abuse limits: a token bucket per IP address (kept in memory for an hour, never stored), a +//! daily item cap per install, and the batch size limits. Raw rows expire after +//! `retention_days`; the daily totals stay. One small process and one SQLite file: see +//! `README.md` for running it. +use std::collections::HashMap; +use std::net::{IpAddr, SocketAddr}; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use axum::Router; +use axum::extract::{ConnectInfo, Path, Query, Request, State}; +use axum::http::{HeaderMap, HeaderValue, StatusCode, header}; +use axum::response::{IntoResponse, Response}; +use axum::routing::{delete, get, post}; +use horizon_telemetry::batch; + +pub mod store; + +use store::Store; + +pub struct Config { + /// Days raw rows are kept. + pub retention_days: u32, + /// Groups in the public totals need at least this many install-days. + pub min_install_days: u64, + /// Requests an IP address may make an hour (with a burst of a tenth of it). + pub requests_per_hour: f64, + /// Items one install may send a day. + pub items_per_day: u32, + /// Behind a proxy (Railway, Fly…): the client's address is the first of X-Forwarded-For. + pub trust_proxy: bool, + /// The website allowed to read the totals from a browser. + pub allow_origin: String, +} + +impl Default for Config { + fn default() -> Self { + Self { + retention_days: 180, + min_install_days: 5, + requests_per_hour: 60.0, + items_per_day: 100, + trust_proxy: false, + allow_origin: "https://playhorizon.dev".into(), + } + } +} + +impl Config { + /// The defaults, with `HORIZON_TELEMETRY_RETENTION_DAYS`, `…_MIN_INSTALL_DAYS`, + /// `…_REQUESTS_PER_HOUR`, `…_ITEMS_PER_DAY`, `…_TRUST_PROXY` (1) and `…_ALLOW_ORIGIN`. + pub fn from_env() -> Self { + let var = |k: &str| std::env::var(format!("HORIZON_TELEMETRY_{k}")).ok(); + let d = Self::default(); + Self { + retention_days: var("RETENTION_DAYS") + .and_then(|v| v.parse().ok()) + .unwrap_or(d.retention_days), + min_install_days: var("MIN_INSTALL_DAYS") + .and_then(|v| v.parse().ok()) + .unwrap_or(d.min_install_days), + requests_per_hour: var("REQUESTS_PER_HOUR") + .and_then(|v| v.parse().ok()) + .unwrap_or(d.requests_per_hour), + items_per_day: var("ITEMS_PER_DAY") + .and_then(|v| v.parse().ok()) + .unwrap_or(d.items_per_day), + trust_proxy: var("TRUST_PROXY").is_some_and(|v| v == "1"), + allow_origin: var("ALLOW_ORIGIN").unwrap_or(d.allow_origin), + } + } +} + +pub struct App { + store: Mutex, + config: Config, + /// Tokens left and when they were counted, per client address. + buckets: Mutex>, + /// `/v1/aggregate` answers by `days`, with when they were made. + cache: Mutex>, + /// Today, in days since 1970 (a test moves it). + today: Box u32 + Send + Sync>, +} + +const CACHE_FOR: Duration = Duration::from_secs(600); + +fn status(code: StatusCode, message: &str) -> Response { + (code, message.to_owned()).into_response() +} + +impl App { + pub fn new(store: Store, config: Config) -> Arc { + Self::with_clock(store, config, Box::new(horizon_telemetry::today)) + } + + pub fn with_clock( + store: Store, + config: Config, + today: Box u32 + Send + Sync>, + ) -> Arc { + Arc::new(Self { + store: Mutex::new(store), + config, + buckets: Mutex::default(), + cache: Mutex::default(), + today, + }) + } + + fn store(&self) -> std::sync::MutexGuard<'_, Store> { + self.store.lock().unwrap_or_else(|e| e.into_inner()) + } + + /// Takes a token for `ip`; false when it has none left. + fn allow(&self, ip: IpAddr) -> bool { + let rate = self.config.requests_per_hour / 3600.0; + let burst = (self.config.requests_per_hour / 10.0).max(1.0); + let mut buckets = self.buckets.lock().unwrap_or_else(|e| e.into_inner()); + let now = Instant::now(); + let (tokens, at) = buckets.entry(ip).or_insert((burst, now)); + *tokens = (*tokens + now.duration_since(*at).as_secs_f64() * rate).min(burst); + *at = now; + if *tokens < 1.0 { + return false; + } + *tokens -= 1.0; + true + } + + fn client(&self, headers: &HeaderMap, peer: Option) -> IpAddr { + let forwarded = self + .config + .trust_proxy + .then(|| { + headers + .get("x-forwarded-for")? + .to_str() + .ok()? + .split(',') + .next()? + .trim() + .parse() + .ok() + }) + .flatten(); + forwarded + .or(peer.map(|p| p.ip())) + .unwrap_or(IpAddr::from([0, 0, 0, 0])) + } + + /// The day an install's newest row came in, if it has any left. + pub fn newest_day(&self, install: &[u8]) -> Option { + self.store().newest_day(install).ok().flatten() + } + + /// Daily upkeep: totals rebuilt, expired rows removed, idle rate-limit entries forgotten. + pub fn maintain(&self) -> rusqlite::Result { + let oldest = (self.today)().saturating_sub(self.config.retention_days); + let removed = self.store().expire(oldest)?; + self.buckets + .lock() + .unwrap_or_else(|e| e.into_inner()) + .retain(|_, (_, at)| at.elapsed() < Duration::from_secs(3600)); + if removed > 0 { + self.cache.lock().unwrap_or_else(|e| e.into_inner()).clear(); + } + Ok(removed) + } +} + +fn peer(req: &Request) -> Option { + req.extensions() + .get::>() + .map(|c| c.0) +} + +async fn receive(State(app): State>, req: Request) -> Response { + let ip = app.client(req.headers(), peer(&req)); + if !app.allow(ip) { + return status(StatusCode::TOO_MANY_REQUESTS, "too many requests"); + } + let zstd = req + .headers() + .get(header::CONTENT_ENCODING) + .is_some_and(|v| v.as_bytes().eq_ignore_ascii_case(b"zstd")); + if !zstd { + return status( + StatusCode::UNSUPPORTED_MEDIA_TYPE, + "send zstd (Content-Encoding: zstd)", + ); + } + let Ok(body) = axum::body::to_bytes(req.into_body(), batch::MAX_COMPRESSED).await else { + return status(StatusCode::PAYLOAD_TOO_LARGE, "a batch is at most 256 KiB"); + }; + let batch = match batch::decode(&body) { + Ok(batch) => batch, + Err(e) => return status(StatusCode::BAD_REQUEST, &e), + }; + let today = (app.today)(); + let install = store::install_key(&batch.install_id).unwrap_or_default(); + let mut store = app.store(); + let sent = store.count_on(&install, today).unwrap_or(u32::MAX); + if sent.saturating_add(batch.items.len() as u32) > app.config.items_per_day { + return status( + StatusCode::TOO_MANY_REQUESTS, + "this install's daily limit is reached", + ); + } + match store.insert(&batch, today) { + Ok(()) => StatusCode::NO_CONTENT.into_response(), + Err(e) => { + eprintln!("horizon-telemetry-server: could not store a batch: {e}"); + status(StatusCode::SERVICE_UNAVAILABLE, "try again later") + } + } +} + +async fn forget(State(app): State>, Path(id): Path, req: Request) -> Response { + if !app.allow(app.client(req.headers(), peer(&req))) { + return status(StatusCode::TOO_MANY_REQUESTS, "too many requests"); + } + let Some(install) = store::install_key(&id) else { + return status(StatusCode::BAD_REQUEST, "not an install ID"); + }; + match app.store().delete_install(&install) { + Ok(_) => { + app.cache.lock().unwrap_or_else(|e| e.into_inner()).clear(); + StatusCode::NO_CONTENT.into_response() + } + Err(e) => { + eprintln!("horizon-telemetry-server: could not delete an install: {e}"); + status(StatusCode::SERVICE_UNAVAILABLE, "try again later") + } + } +} + +#[derive(serde::Deserialize)] +struct Window { + days: Option, +} + +async fn aggregate(State(app): State>, Query(window): Query) -> Response { + let days = window.days.unwrap_or(30).clamp(1, 366); + let cached = app + .cache + .lock() + .unwrap_or_else(|e| e.into_inner()) + .get(&days) + .filter(|(at, _)| at.elapsed() < CACHE_FOR) + .map(|(_, json)| json.clone()); + let json = match cached { + Some(json) => json, + None => { + let to = (app.today)(); + let made = + app.store() + .aggregate(to + 1 - days.min(to + 1), to, app.config.min_install_days); + match made { + Ok(value) => { + let json = value.to_string(); + app.cache + .lock() + .unwrap_or_else(|e| e.into_inner()) + .insert(days, (Instant::now(), json.clone())); + json + } + Err(e) => { + eprintln!("horizon-telemetry-server: could not build the totals: {e}"); + return status(StatusCode::SERVICE_UNAVAILABLE, "try again later"); + } + } + } + }; + let origin = + HeaderValue::from_str(&app.config.allow_origin).unwrap_or(HeaderValue::from_static("*")); + ( + [ + ( + header::CONTENT_TYPE, + HeaderValue::from_static("application/json"), + ), + ( + header::CACHE_CONTROL, + HeaderValue::from_static("public, max-age=600"), + ), + (header::ACCESS_CONTROL_ALLOW_ORIGIN, origin), + ], + json, + ) + .into_response() +} + +pub fn router(app: Arc) -> Router { + Router::new() + .route("/v1/batch", post(receive)) + .route("/v1/installs/{id}", delete(forget)) + .route("/v1/aggregate", get(aggregate)) + .route("/healthz", get(|| async { "ok" })) + .with_state(app) +} diff --git a/crates/horizon-telemetry-server/src/main.rs b/crates/horizon-telemetry-server/src/main.rs new file mode 100644 index 000000000..9b9c702c3 --- /dev/null +++ b/crates/horizon-telemetry-server/src/main.rs @@ -0,0 +1,40 @@ +//! `horizon-telemetry-server`: serves on `PORT` (default 8080), keeps its data in +//! `HORIZON_TELEMETRY_DB` (default `telemetry.sqlite3`), and does its upkeep every ten minutes. +use std::net::SocketAddr; +use std::time::Duration; + +use horizon_telemetry_server::{App, Config, router, store::Store}; + +#[tokio::main(flavor = "multi_thread", worker_threads = 2)] +async fn main() { + let path = std::env::var("HORIZON_TELEMETRY_DB").unwrap_or_else(|_| "telemetry.sqlite3".into()); + let port: u16 = std::env::var("PORT") + .ok() + .and_then(|p| p.parse().ok()) + .unwrap_or(8080); + let store = Store::open(&path).unwrap_or_else(|e| panic!("{path}: {e}")); + let app = App::new(store, Config::from_env()); + let upkeep = app.clone(); + tokio::spawn(async move { + let mut every = tokio::time::interval(Duration::from_secs(600)); + loop { + every.tick().await; + if let Err(e) = upkeep.maintain() { + eprintln!("horizon-telemetry-server: upkeep failed: {e}"); + } + } + }); + let listener = tokio::net::TcpListener::bind(SocketAddr::from(([0, 0, 0, 0], port))) + .await + .unwrap_or_else(|e| panic!("port {port}: {e}")); + eprintln!("horizon-telemetry-server: listening on {port}, data in {path}"); + axum::serve( + listener, + router(app).into_make_service_with_connect_info::(), + ) + .with_graceful_shutdown(async { + let _ = tokio::signal::ctrl_c().await; + }) + .await + .unwrap_or_else(|e| panic!("{e}")); +} diff --git a/crates/horizon-telemetry-server/src/store.rs b/crates/horizon-telemetry-server/src/store.rs new file mode 100644 index 000000000..0bac20e50 --- /dev/null +++ b/crates/horizon-telemetry-server/src/store.rs @@ -0,0 +1,428 @@ +//! The database: one SQLite file, kept small. +//! +//! - `systems`: each distinct machine description once. Rows point at it, so a CPU and GPU +//! name isn't stored again with every session. +//! - `perf`: one row per play session, in columns. A frame or tick summary is an 80-byte blob +//! ([`pack`]), not JSON. +//! - `reports`: crash and bug reports and benchmark uploads, their `data` zstd-compressed. +//! - `perf_daily`: the anonymous totals per day, version, OS, GPU vendor and render distance +//! that `/v1/aggregate` reads. Rebuilt from `perf` when a day changes (new sessions, a +//! deletion), and kept after the raw rows expire. +//! +//! Dates are the day the server received an item (days since 1970, UTC). The sender's IP +//! address is never stored. +use std::collections::{HashMap, HashSet}; + +use horizon_telemetry::Kind; +use horizon_telemetry::batch::Batch; +use horizon_telemetry::payload::{BUCKET_EDGES_MS, Payload, Summary, System}; +use rusqlite::{Connection, OptionalExtension, params}; + +const BUCKETS: usize = BUCKET_EDGES_MS.len() + 1; + +const SCHEMA: &str = " +PRAGMA journal_mode = WAL; +PRAGMA synchronous = NORMAL; +CREATE TABLE IF NOT EXISTS systems ( + id INTEGER PRIMARY KEY, + os TEXT NOT NULL, os_version TEXT NOT NULL, arch TEXT NOT NULL, cpu TEXT NOT NULL, + threads INTEGER NOT NULL, ram_gb INTEGER NOT NULL, + gpu TEXT NOT NULL, driver TEXT NOT NULL, backend TEXT NOT NULL, + UNIQUE (os, os_version, arch, cpu, threads, ram_gb, gpu, driver, backend) +); +CREATE TABLE IF NOT EXISTS perf ( + install BLOB NOT NULL, day INTEGER NOT NULL, version TEXT NOT NULL, system INTEGER NOT NULL, + render_distance INTEGER NOT NULL, simulation_distance INTEGER NOT NULL, max_fps INTEGER NOT NULL, + vsync INTEGER NOT NULL, fullscreen INTEGER NOT NULL, graphics TEXT NOT NULL, + seconds INTEGER NOT NULL, frames BLOB NOT NULL, ticks BLOB, + peak_ram_mb INTEGER NOT NULL, to_title_ms INTEGER, to_world_ms INTEGER +); +CREATE INDEX IF NOT EXISTS perf_install ON perf (install); +CREATE INDEX IF NOT EXISTS perf_day ON perf (day); +CREATE TABLE IF NOT EXISTS reports ( + install BLOB NOT NULL, day INTEGER NOT NULL, kind INTEGER NOT NULL, version TEXT NOT NULL, + system INTEGER, body BLOB NOT NULL +); +CREATE INDEX IF NOT EXISTS reports_install ON reports (install); +CREATE INDEX IF NOT EXISTS reports_kind_day ON reports (kind, day); +CREATE TABLE IF NOT EXISTS perf_daily ( + day INTEGER NOT NULL, version TEXT NOT NULL, os TEXT NOT NULL, gpu_vendor TEXT NOT NULL, + render_distance INTEGER NOT NULL, + sessions INTEGER NOT NULL, installs INTEGER NOT NULL, seconds INTEGER NOT NULL, + buckets TEXT NOT NULL, p50_sum REAL NOT NULL, p99_sum REAL NOT NULL, + PRIMARY KEY (day, version, os, gpu_vendor, render_distance) +) WITHOUT ROWID; +CREATE TABLE IF NOT EXISTS dirty (day INTEGER PRIMARY KEY) WITHOUT ROWID; +"; + +fn kind_code(kind: Kind) -> i64 { + match kind { + Kind::Perf => 0, + Kind::Crash => 1, + Kind::Bug => 2, + Kind::Benchmark => 3, + } +} + +/// An install ID as 16 bytes. +pub fn install_key(id: &str) -> Option> { + horizon_telemetry::consent::is_install_id(id).then(|| { + (0..16) + .map(|i| u8::from_str_radix(&id[2 * i..2 * i + 2], 16).unwrap_or(0)) + .collect() + }) +} + +/// A summary as 80 bytes: the count, four percentiles, then the 14 buckets, little-endian. +pub fn pack(s: &Summary) -> Vec { + let mut out = Vec::with_capacity(8 + 16 + 4 * BUCKETS); + out.extend(s.count.to_le_bytes()); + for ms in [s.p50_ms, s.p95_ms, s.p99_ms, s.max_ms] { + out.extend(ms.to_le_bytes()); + } + for i in 0..BUCKETS { + out.extend(s.buckets.get(i).copied().unwrap_or(0).to_le_bytes()); + } + out +} + +pub fn unpack(bytes: &[u8]) -> Option { + if bytes.len() != 8 + 16 + 4 * BUCKETS { + return None; + } + let u32_at = |i: usize| u32::from_le_bytes(bytes[i..i + 4].try_into().unwrap_or_default()); + let f32_at = |i: usize| f32::from_bits(u32_at(i)); + Some(Summary { + count: u64::from_le_bytes(bytes[..8].try_into().ok()?), + p50_ms: f32_at(8), + p95_ms: f32_at(12), + p99_ms: f32_at(16), + max_ms: f32_at(20), + buckets: (0..BUCKETS).map(|i| u32_at(24 + 4 * i)).collect(), + }) +} + +/// The GPU's maker, the one GPU detail the public totals group by. +pub fn gpu_vendor(gpu: &str) -> &'static str { + let gpu = gpu.to_ascii_lowercase(); + [ + ("apple", "apple"), + ("nvidia", "nvidia"), + ("geforce", "nvidia"), + ("radeon", "amd"), + ("amd", "amd"), + ("intel", "intel"), + ("adreno", "qualcomm"), + ("llvmpipe", "software"), + ] + .into_iter() + .find(|(needle, _)| gpu.contains(needle)) + .map_or(if gpu.is_empty() { "unknown" } else { "other" }, |(_, v)| v) +} + +pub struct Store { + db: Connection, +} + +impl Store { + pub fn open(path: &str) -> rusqlite::Result { + let db = Connection::open(path)?; + db.execute_batch(SCHEMA)?; + Ok(Self { db }) + } + + /// How many items an install has sent on `day`. + pub fn count_on(&self, install: &[u8], day: u32) -> rusqlite::Result { + self.db.query_row( + "SELECT (SELECT count(*) FROM perf WHERE install = ?1 AND day = ?2) + + (SELECT count(*) FROM reports WHERE install = ?1 AND day = ?2)", + params![install, day], + |r| r.get(0), + ) + } + + /// Keeps a decoded batch, received on `day`. + pub fn insert(&mut self, batch: &Batch, day: u32) -> rusqlite::Result<()> { + let install = install_key(&batch.install_id).unwrap_or_default(); + let tx = self.db.transaction()?; + for item in &batch.items { + match &item.payload { + Payload::Perf(p) => { + let system = system_id(&tx, &p.system)?; + tx.execute( + "INSERT INTO perf (install, day, version, system, render_distance, simulation_distance, + max_fps, vsync, fullscreen, graphics, seconds, frames, ticks, peak_ram_mb, + to_title_ms, to_world_ms) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16)", + params![ + install, day, item.version, system, + p.settings.render_distance, p.settings.simulation_distance, p.settings.max_fps, + p.settings.vsync, p.settings.fullscreen, p.settings.graphics, + p.seconds, pack(&p.frames), p.ticks.as_ref().map(pack), p.peak_ram_mb, + p.to_title_ms, p.to_world_ms, + ], + )?; + } + payload => { + let system = match payload { + Payload::Crash(c) => Some(system_id(&tx, &c.system)?), + Payload::Bug(b) => Some(system_id(&tx, &b.system)?), + _ => None, + }; + let data = match payload { + Payload::Crash(c) => serde_json::to_vec(c), + Payload::Bug(b) => serde_json::to_vec(b), + Payload::Benchmark(r) => serde_json::to_vec(r), + Payload::Perf(_) => unreachable!("handled above"), + } + .unwrap_or_default(); + let body = ruzstd::encoding::compress_to_vec( + data.as_slice(), + ruzstd::encoding::CompressionLevel::Fastest, + ); + tx.execute( + "INSERT INTO reports (install, day, kind, version, system, body) VALUES (?1, ?2, ?3, ?4, ?5, ?6)", + params![install, day, kind_code(item.kind()), item.version, system, body], + )?; + } + } + } + tx.execute("INSERT OR IGNORE INTO dirty (day) VALUES (?1)", [day])?; + tx.commit() + } + + /// Deletes everything an install sent; the days it touched get their totals rebuilt. + pub fn delete_install(&mut self, install: &[u8]) -> rusqlite::Result { + let tx = self.db.transaction()?; + tx.execute( + "INSERT OR IGNORE INTO dirty (day) SELECT DISTINCT day FROM perf WHERE install = ?1", + [install], + )?; + let mut deleted = tx.execute("DELETE FROM perf WHERE install = ?1", [install])?; + deleted += tx.execute("DELETE FROM reports WHERE install = ?1", [install])?; + tx.commit()?; + self.rebuild_dirty()?; + Ok(deleted) + } + + /// Removes raw rows received before `oldest_day`; the daily totals stay. + pub fn expire(&mut self, oldest_day: u32) -> rusqlite::Result { + // A day's totals are final before its rows go. + self.rebuild_dirty()?; + let mut n = self + .db + .execute("DELETE FROM perf WHERE day < ?1", [oldest_day])?; + n += self + .db + .execute("DELETE FROM reports WHERE day < ?1", [oldest_day])?; + self.db.execute( + "DELETE FROM systems WHERE id NOT IN (SELECT system FROM perf) + AND id NOT IN (SELECT system FROM reports WHERE system IS NOT NULL)", + [], + )?; + Ok(n) + } + + /// Rebuilds the daily totals of every day marked changed. + pub fn rebuild_dirty(&mut self) -> rusqlite::Result<()> { + let days: Vec = { + let mut q = self.db.prepare("SELECT day FROM dirty")?; + q.query_map([], |r| r.get(0))?.collect::>()? + }; + for day in days { + self.rebuild_day(day)?; + } + Ok(()) + } + + fn rebuild_day(&mut self, day: u32) -> rusqlite::Result<()> { + #[derive(Default)] + struct Group { + sessions: u64, + installs: HashSet>, + seconds: u64, + buckets: [u64; BUCKETS], + p50_sum: f64, + p99_sum: f64, + } + let tx = self.db.transaction()?; + let mut groups: HashMap<(String, String, &'static str, u8), Group> = HashMap::new(); + { + let mut q = tx.prepare( + "SELECT perf.install, perf.version, systems.os, systems.gpu, perf.render_distance, + perf.seconds, perf.frames FROM perf JOIN systems ON systems.id = perf.system + WHERE perf.day = ?1", + )?; + let mut rows = q.query([day])?; + while let Some(r) = rows.next()? { + let gpu: String = r.get(3)?; + let key = (r.get(1)?, r.get(2)?, gpu_vendor(&gpu), r.get(4)?); + let Some(frames) = unpack(&r.get::<_, Vec>(6)?) else { + continue; + }; + let g = groups.entry(key).or_default(); + g.sessions += 1; + g.installs.insert(r.get(0)?); + g.seconds += r.get::<_, u64>(5)?; + for (sum, n) in g.buckets.iter_mut().zip(&frames.buckets) { + *sum += u64::from(*n); + } + g.p50_sum += f64::from(frames.p50_ms); + g.p99_sum += f64::from(frames.p99_ms); + } + } + tx.execute("DELETE FROM perf_daily WHERE day = ?1", [day])?; + for ((version, os, vendor, distance), g) in groups { + tx.execute( + "INSERT INTO perf_daily VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)", + params![ + day, + version, + os, + vendor, + distance, + g.sessions, + g.installs.len(), + g.seconds, + serde_json::to_string(&g.buckets.to_vec()).unwrap_or_default(), + g.p50_sum, + g.p99_sum, + ], + )?; + } + tx.execute("DELETE FROM dirty WHERE day = ?1", [day])?; + tx.commit() + } + + /// The anonymous totals for the days `from..=to`, groups with fewer than `k` install-days + /// left out, and the newest benchmark uploads. + pub fn aggregate(&mut self, from: u32, to: u32, k: u64) -> rusqlite::Result { + self.rebuild_dirty()?; + /// One group's sums over the window. + #[derive(Default)] + struct Totals { + sessions: u64, + install_days: u64, + seconds: u64, + buckets: [u64; BUCKETS], + p50_sum: f64, + p99_sum: f64, + } + let mut groups: HashMap<(String, String, String, u8), Totals> = HashMap::new(); + { + let mut q = self.db.prepare( + "SELECT version, os, gpu_vendor, render_distance, sessions, installs, seconds, buckets, + p50_sum, p99_sum FROM perf_daily WHERE day BETWEEN ?1 AND ?2", + )?; + let mut rows = q.query([from, to])?; + while let Some(r) = rows.next()? { + let g = groups + .entry((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?)) + .or_default(); + g.sessions += r.get::<_, u64>(4)?; + g.install_days += r.get::<_, u64>(5)?; + g.seconds += r.get::<_, u64>(6)?; + let buckets: Vec = + serde_json::from_str(&r.get::<_, String>(7)?).unwrap_or_default(); + for (sum, n) in g.buckets.iter_mut().zip(buckets) { + *sum += n; + } + g.p50_sum += r.get::<_, f64>(8)?; + g.p99_sum += r.get::<_, f64>(9)?; + } + } + let (mut sessions, mut hours) = (0, 0.0); + let mut out: Vec = groups + .into_iter() + .filter(|(_, g)| g.install_days >= k) + .map(|((version, os, vendor, distance), g)| { + sessions += g.sessions; + hours += g.seconds as f64 / 3600.0; + let avg = |sum: f64| (sum / g.sessions as f64 * 10.0).round() / 10.0; + serde_json::json!({ + "version": version, "os": os, "gpu_vendor": vendor, "render_distance": distance, + "sessions": g.sessions, "install_days": g.install_days, + "hours": (g.seconds as f64 / 360.0).round() / 10.0, + "frame_buckets": g.buckets.to_vec(), "p50_ms": avg(g.p50_sum), "p99_ms": avg(g.p99_sum), + }) + }) + .collect(); + out.sort_by(|a, b| b["sessions"].as_u64().cmp(&a["sessions"].as_u64())); + let crashes: Vec = { + let mut q = self.db.prepare( + "SELECT version, count(*) FROM reports WHERE kind = ?1 AND day BETWEEN ?2 AND ?3 + GROUP BY version ORDER BY 2 DESC LIMIT 20", + )?; + q.query_map(params![kind_code(Kind::Crash), from, to], |r| { + Ok(serde_json::json!({ "version": r.get::<_, String>(0)?, "crashes": r.get::<_, u64>(1)? })) + })? + .collect::>()? + }; + let benchmarks: Vec = { + let mut q = self.db.prepare( + "SELECT day, version, body FROM reports WHERE kind = ?1 AND day BETWEEN ?2 AND ?3 + ORDER BY rowid DESC LIMIT 50", + )?; + q.query_map(params![kind_code(Kind::Benchmark), from, to], |r| { + let body: Vec = r.get(2)?; + Ok(serde_json::json!({ + "day": r.get::<_, u32>(0)?, "version": r.get::<_, String>(1)?, "report": inflate(&body), + })) + })? + .collect::>()? + }; + Ok(serde_json::json!({ + "from_day": from, "to_day": to, "min_install_days": k, + "bucket_edges_ms": BUCKET_EDGES_MS.map(|e| (f64::from(e) * 10.0).round() / 10.0), + "totals": { "sessions": sessions, "hours": (hours * 10.0).round() / 10.0 }, + "groups": out, "crashes_by_version": crashes, "benchmarks": benchmarks, + })) + } + + /// The day an install's newest row came in, if any (tests, and checking a deletion). + pub fn newest_day(&self, install: &[u8]) -> rusqlite::Result> { + self.db + .query_row( + "SELECT max(day) FROM (SELECT day FROM perf WHERE install = ?1 + UNION ALL SELECT day FROM reports WHERE install = ?1)", + [install], + |r| r.get(0), + ) + .optional() + .map(Option::flatten) + } +} + +fn system_id(tx: &rusqlite::Transaction, s: &System) -> rusqlite::Result { + tx.execute( + "INSERT OR IGNORE INTO systems (os, os_version, arch, cpu, threads, ram_gb, gpu, driver, backend) + VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)", + params![s.os, s.os_version, s.arch, s.cpu, s.threads, s.ram_gb, s.gpu, s.driver, s.backend], + )?; + tx.query_row( + "SELECT id FROM systems WHERE os = ?1 AND os_version = ?2 AND arch = ?3 AND cpu = ?4 + AND threads = ?5 AND ram_gb = ?6 AND gpu = ?7 AND driver = ?8 AND backend = ?9", + params![ + s.os, + s.os_version, + s.arch, + s.cpu, + s.threads, + s.ram_gb, + s.gpu, + s.driver, + s.backend + ], + |r| r.get(0), + ) +} + +/// A stored body back as JSON. +pub fn inflate(body: &[u8]) -> serde_json::Value { + use std::io::Read; + let mut json = Vec::new(); + ruzstd::decoding::StreamingDecoder::new(body) + .ok() + .and_then(|d| d.take(1 << 20).read_to_end(&mut json).ok()) + .and_then(|_| serde_json::from_slice(&json).ok()) + .unwrap_or(serde_json::Value::Null) +} diff --git a/crates/horizon-telemetry-server/tests/server.rs b/crates/horizon-telemetry-server/tests/server.rs new file mode 100644 index 000000000..f8c78f085 --- /dev/null +++ b/crates/horizon-telemetry-server/tests/server.rs @@ -0,0 +1,393 @@ +//! The server through its HTTP interface, and end to end with the launcher's uploader. +use std::sync::Arc; +use std::sync::atomic::{AtomicU32, Ordering}; + +use axum::body::Body; +use axum::http::{Request, StatusCode}; +use horizon_telemetry::batch::{self, Batch}; +use horizon_telemetry::payload::{Crash, Payload, Perf, Settings, Source, System, Timings}; +use horizon_telemetry::{Item, outbox}; +use horizon_telemetry_server::store::{self, Store}; +use horizon_telemetry_server::{App, Config, router}; +use http_body_util::BodyExt; +use tower::ServiceExt; + +const DAY: u32 = 20_741; + +fn install(n: u32) -> String { + format!("{n:032x}") +} + +fn perf(gpu: &str, distance: u8) -> Item { + let mut t = Timings::new(); + (0..30_000).for_each(|i| t.record(8.0 + (i % 50) as f32 / 10.0)); + let mut item = Item::new( + Source::Game, + "v2026.10.15", + Payload::Perf(Perf { + system: System { + os: "linux".into(), + os_version: "Fedora Linux 43".into(), + cpu: "AMD Ryzen 9 7950X".into(), + threads: 32, + ram_gb: 32, + gpu: gpu.into(), + driver: "570.86".into(), + backend: "Vulkan".into(), + ..System::default() + }, + settings: Settings { + render_distance: distance, + simulation_distance: 12, + graphics: "fancy".into(), + ..Settings::default() + }, + seconds: 1800, + frames: t.summary(), + peak_ram_mb: 2300, + ..Perf::default() + }), + ); + item.day = DAY; + item +} + +struct Server { + app: Arc, + day: Arc, +} + +fn server(config: Config) -> Server { + let day = Arc::new(AtomicU32::new(DAY)); + let clock = day.clone(); + let app = App::with_clock( + Store::open(":memory:").unwrap(), + config, + Box::new(move || clock.load(Ordering::SeqCst)), + ); + Server { app, day } +} + +impl Server { + async fn send(&self, req: Request) -> (StatusCode, String) { + let res = router(self.app.clone()).oneshot(req).await.unwrap(); + let code = res.status(); + let body = res.into_body().collect().await.unwrap().to_bytes(); + (code, String::from_utf8_lossy(&body).into_owned()) + } + + async fn post(&self, id: &str, items: Vec) -> StatusCode { + let body = batch::encode(&Batch::new(id, items)); + self.post_raw(body, Some("zstd")).await + } + + async fn post_raw(&self, body: Vec, encoding: Option<&str>) -> StatusCode { + let mut req = Request::post("/v1/batch").header("x-forwarded-for", "203.0.113.9"); + if let Some(e) = encoding { + req = req.header("content-encoding", e); + } + self.send(req.body(Body::from(body)).unwrap()).await.0 + } + + async fn aggregate(&self) -> serde_json::Value { + let (code, body) = self + .send( + Request::get("/v1/aggregate?days=30") + .body(Body::empty()) + .unwrap(), + ) + .await; + assert_eq!(code, StatusCode::OK); + serde_json::from_str(&body).unwrap() + } +} + +fn generous() -> Config { + Config { + requests_per_hour: 100_000.0, + ..Config::default() + } +} + +#[tokio::test] +async fn totals_show_only_groups_with_enough_installs() { + let s = server(generous()); + for n in 0..6 { + assert_eq!( + s.post(&install(n), vec![perf("NVIDIA GeForce RTX 4070", 12); 2]) + .await, + StatusCode::NO_CONTENT + ); + } + for n in 10..12 { + assert_eq!( + s.post(&install(n), vec![perf("Intel(R) Arc A770", 8)]) + .await, + StatusCode::NO_CONTENT + ); + } + let totals = s.aggregate().await; + let groups = totals["groups"].as_array().unwrap(); + assert_eq!( + groups.len(), + 1, + "two installs are too few to show: {totals}" + ); + let g = &groups[0]; + assert_eq!(g["gpu_vendor"], "nvidia"); + assert_eq!( + (g["sessions"].as_u64(), g["install_days"].as_u64()), + (Some(12), Some(6)) + ); + assert_eq!(g["hours"], 6.0); + assert_eq!( + g["frame_buckets"] + .as_array() + .unwrap() + .iter() + .map(|b| b.as_u64().unwrap()) + .sum::(), + 12 * 30_000 + ); + assert_eq!(totals["bucket_edges_ms"].as_array().unwrap().len(), 13); + let text = totals.to_string(); + assert!( + !text.contains(&install(0)) && !text.contains("Ryzen"), + "no install or machine in the totals" + ); +} + +#[tokio::test] +async fn bad_batches_are_refused() { + let s = server(generous()); + let good = batch::encode(&Batch::new(&install(1), vec![perf("x", 8)])); + assert_eq!( + s.post_raw(good.clone(), None).await, + StatusCode::UNSUPPORTED_MEDIA_TYPE + ); + assert_eq!( + s.post_raw(b"garbage".to_vec(), Some("zstd")).await, + StatusCode::BAD_REQUEST + ); + assert_eq!( + s.post_raw(vec![0; batch::MAX_COMPRESSED + 1], Some("zstd")) + .await, + StatusCode::PAYLOAD_TOO_LARGE + ); + assert_eq!( + s.post("not-an-id", vec![perf("x", 8)]).await, + StatusCode::BAD_REQUEST + ); + assert_eq!(s.post_raw(good, Some("zstd")).await, StatusCode::NO_CONTENT); +} + +#[tokio::test] +async fn an_address_is_rate_limited() { + let s = server(Config { + requests_per_hour: 30.0, + trust_proxy: true, + ..Config::default() + }); + for n in 0..3 { + assert_eq!( + s.post(&install(n), vec![perf("x", 8)]).await, + StatusCode::NO_CONTENT + ); + } + assert_eq!( + s.post(&install(9), vec![perf("x", 8)]).await, + StatusCode::TOO_MANY_REQUESTS + ); + // Another address has its own allowance. + let body = batch::encode(&Batch::new(&install(9), vec![perf("x", 8)])); + let req = Request::post("/v1/batch") + .header("x-forwarded-for", "198.51.100.7, 10.0.0.1") + .header("content-encoding", "zstd") + .body(Body::from(body)) + .unwrap(); + assert_eq!(s.send(req).await.0, StatusCode::NO_CONTENT); +} + +#[tokio::test] +async fn an_install_has_a_daily_cap() { + let s = server(Config { + items_per_day: 3, + ..generous() + }); + assert_eq!( + s.post(&install(1), vec![perf("x", 8); 2]).await, + StatusCode::NO_CONTENT + ); + assert_eq!( + s.post(&install(1), vec![perf("x", 8); 2]).await, + StatusCode::TOO_MANY_REQUESTS + ); + assert_eq!( + s.post(&install(1), vec![perf("x", 8)]).await, + StatusCode::NO_CONTENT + ); + s.day.store(DAY + 1, Ordering::SeqCst); + assert_eq!( + s.post(&install(1), vec![perf("x", 8); 3]).await, + StatusCode::NO_CONTENT + ); +} + +#[tokio::test] +async fn deleting_an_install_removes_its_rows_and_its_share_of_the_totals() { + let s = server(Config { + min_install_days: 1, + ..generous() + }); + let crash = Item::new(Source::Game, "v1", Payload::Crash(Crash::default())); + assert_eq!( + s.post(&install(1), vec![perf("Apple M4 Pro", 12), crash]) + .await, + StatusCode::NO_CONTENT + ); + assert_eq!( + s.post(&install(2), vec![perf("Apple M4 Pro", 12)]).await, + StatusCode::NO_CONTENT + ); + assert_eq!(s.aggregate().await["groups"][0]["sessions"], 2); + let (code, _) = s + .send( + Request::delete(format!("/v1/installs/{}", install(1))) + .body(Body::empty()) + .unwrap(), + ) + .await; + assert_eq!(code, StatusCode::NO_CONTENT); + let key = store::install_key(&install(1)).unwrap(); + assert_eq!(s.app.newest_day(&key), None); + let totals = s.aggregate().await; + assert_eq!( + totals["groups"][0]["sessions"], 1, + "the cache was dropped and the day rebuilt" + ); + assert_eq!(totals["crashes_by_version"].as_array().unwrap().len(), 0); + let (code, _) = s + .send( + Request::delete("/v1/installs/nope") + .body(Body::empty()) + .unwrap(), + ) + .await; + assert_eq!(code, StatusCode::BAD_REQUEST); +} + +#[tokio::test] +async fn raw_rows_expire_and_the_totals_stay() { + let s = server(Config { + min_install_days: 1, + retention_days: 180, + ..generous() + }); + assert_eq!( + s.post(&install(1), vec![perf("Apple M4 Pro", 12)]).await, + StatusCode::NO_CONTENT + ); + s.day.store(DAY + 179, Ordering::SeqCst); + assert_eq!(s.app.maintain().unwrap(), 0, "still within retention"); + s.day.store(DAY + 181, Ordering::SeqCst); + assert_eq!(s.app.maintain().unwrap(), 1); + assert_eq!( + s.app.newest_day(&store::install_key(&install(1)).unwrap()), + None + ); + let (_, body) = s + .send( + Request::get("/v1/aggregate?days=366") + .body(Body::empty()) + .unwrap(), + ) + .await; + let totals: serde_json::Value = serde_json::from_str(&body).unwrap(); + assert_eq!(totals["groups"][0]["sessions"], 1); +} + +#[test] +fn a_session_costs_little_storage() { + let dir = std::env::temp_dir().join(format!( + "horizon-telemetry-server-size-{}", + std::process::id() + )); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("t.sqlite3"); + let mut store = Store::open(path.to_str().unwrap()).unwrap(); + let gpus = [ + "Apple M4 Pro", + "NVIDIA GeForce RTX 4070", + "AMD Radeon RX 7800 XT", + "Intel(R) Arc A770", + ]; + let sessions = 10_000; + for n in 0..sessions / 10 { + let items = (0..10) + .map(|i| perf(gpus[(n + i) as usize % 4], 8 + (i % 3) as u8 * 4)) + .collect(); + store + .insert(&Batch::new(&install(n), items), DAY + n % 30) + .unwrap(); + } + store.rebuild_dirty().unwrap(); + drop(store); + let bytes: u64 = std::fs::read_dir(&dir) + .unwrap() + .flatten() + .map(|e| e.metadata().unwrap().len()) + .sum(); + let per = bytes / u64::from(sessions); + eprintln!("{sessions} sessions: {bytes} bytes on disk, {per} bytes a session"); + assert!(per < 250, "{per} bytes a session"); +} + +#[tokio::test] +async fn the_launchers_uploader_reaches_it_end_to_end() { + let s = server(Config { + min_install_days: 1, + ..generous() + }); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("http://{}", listener.local_addr().unwrap()); + let service = + router(s.app.clone()).into_make_service_with_connect_info::(); + tokio::spawn(async move { axum::serve(listener, service).await.unwrap() }); + + let home = std::env::temp_dir().join(format!( + "horizon-telemetry-server-e2e-{}", + std::process::id() + )); + let _ = std::fs::remove_dir_all(&home); + let mut consent = horizon_telemetry::Consent::load(&home); + consent.answer(horizon_telemetry::Choices::ALL); + consent.save(&home).unwrap(); + for _ in 0..3 { + assert!(outbox::push(&home, &perf("Apple M4 Pro", 12)).unwrap()); + } + let (h, u) = (home.clone(), url.clone()); + let sent = tokio::task::spawn_blocking(move || horizon_telemetry::upload::flush(&h, &u)) + .await + .unwrap(); + assert_eq!( + sent, + horizon_telemetry::upload::Flush::Sent { + items: 3, + batches: 1 + } + ); + assert_eq!(s.aggregate().await["groups"][0]["sessions"], 3); + + let id = consent.install_id.clone(); + let deleted = + tokio::task::spawn_blocking(move || horizon_telemetry::upload::delete_remote(&url, &id)) + .await + .unwrap(); + assert_eq!(deleted, Ok(())); + assert_eq!( + s.app + .newest_day(&store::install_key(&consent.install_id).unwrap()), + None + ); +} diff --git a/docs/telemetry.md b/docs/telemetry.md index 6ac1438a5..ca8674588 100644 --- a/docs/telemetry.md +++ b/docs/telemetry.md @@ -52,4 +52,4 @@ Never sent: account names or UUIDs, world names, server or LAN addresses, chat, - `DELETE /v1/installs/`: deletes everything sent under that ID. - `GET /v1/aggregate`: the anonymous totals the website's results page reads. -Retention, rate limits and storage are described with the server. +`crates/horizon-telemetry-server` is the server. Its [README](../crates/horizon-telemetry-server/README.md) covers storage (about 190 bytes per play session), retention (180 days of raw rows, anonymous daily totals kept), rate limits and hosting.