Minor refactoring, removal of lazy_static and maud

This commit is contained in:
Gabriel Simmer 2023-10-25 21:12:09 +01:00
parent d5ed1e5cd5
commit 0298508f45
Signed by: arch
SSH key fingerprint: SHA256:m3OEcdtrnBpMX+2BDGh/byv3hrCekCLzDYMdvGEKPPQ
3 changed files with 93 additions and 240 deletions

192
Cargo.lock generated
View file

@ -25,7 +25,7 @@ checksum = "a66537f1bb974b254c98ed142ff995236e81b9d0fe4db0575f46612cb15eb0f9"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.38",
"syn",
]
[[package]]
@ -143,34 +143,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cookie"
version = "0.16.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e859cd57d0710d9e06c381b550c06e76992472a8c6d527aecd2fc673dcc231fb"
dependencies = [
"percent-encoding",
"time",
"version_check",
]
[[package]]
name = "cookie_store"
version = "0.16.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d606d0fba62e13cf04db20536c05cb7f13673c161cb47a47a82b9b9e7d3f1daa"
dependencies = [
"cookie",
"idna 0.2.3",
"log",
"publicsuffix",
"serde",
"serde_derive",
"serde_json",
"time",
"url",
]
[[package]]
name = "core-foundation"
version = "0.9.3"
@ -187,12 +159,6 @@ version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa"
[[package]]
name = "deranged"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2696e8a945f658fd14dc3b87242e6b80cd0f36ff04ea560fa39082368847946"
[[package]]
name = "encoding_rs"
version = "0.8.33"
@ -407,27 +373,6 @@ dependencies = [
"tokio-native-tls",
]
[[package]]
name = "idna"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "418a0a6fab821475f634efe3ccc45c013f742efe03d853e8d3355d5cb850ecf8"
dependencies = [
"matches",
"unicode-bidi",
"unicode-normalization",
]
[[package]]
name = "idna"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e14ddfc70884202db2244c223200c204c2bda1bc6e0998d11b5e024d657209e6"
dependencies = [
"unicode-bidi",
"unicode-normalization",
]
[[package]]
name = "idna"
version = "0.4.0"
@ -513,42 +458,12 @@ version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "matches"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2532096657941c2fea9c289d370a250971c689d4f143798ff67113ec042024a5"
[[package]]
name = "matchit"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94"
[[package]]
name = "maud"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0bab19cef8a7fe1c18a43e881793bfc9d4ea984befec3ae5bd0415abf3ecf00"
dependencies = [
"axum-core",
"http",
"itoa",
"maud_macros",
]
[[package]]
name = "maud_macros"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0be95d66c3024ffce639216058e5bae17a83ecaf266ffc6e4d060ad447c9eed2"
dependencies = [
"proc-macro-error",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "memchr"
version = "2.6.4"
@ -647,7 +562,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.38",
"syn",
]
[[package]]
@ -714,7 +629,7 @@ checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.38",
"syn",
]
[[package]]
@ -735,30 +650,6 @@ version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn 1.0.109",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.69"
@ -789,22 +680,6 @@ version = "2.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "106dd99e98437432fed6519dedecfade6a06a73bb7b2a1e019fdd2bee5778d94"
[[package]]
name = "psl-types"
version = "2.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33cb294fe86a74cbcf50d4445b37da762029549ebeea341421c7c70370f86cac"
[[package]]
name = "publicsuffix"
version = "2.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96a8c1bda5ae1af7f99a2962e49df150414a43d62404644d98dd5c3a93d07457"
dependencies = [
"idna 0.3.0",
"psl-types",
]
[[package]]
name = "quote"
version = "1.0.33"
@ -840,8 +715,6 @@ checksum = "046cd98826c46c2ac8ddecae268eb5c2e58628688a5fc7a2643704a73faba95b"
dependencies = [
"base64",
"bytes",
"cookie",
"cookie_store",
"encoding_rs",
"futures-core",
"futures-util",
@ -958,7 +831,7 @@ checksum = "1e48d1f918009ce3145511378cf68d613e3b3d9137d67272562080d68a2b32d5"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.38",
"syn",
]
[[package]]
@ -1047,17 +920,6 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "syn"
version = "2.0.38"
@ -1126,35 +988,7 @@ checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.38",
]
[[package]]
name = "time"
version = "0.3.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "426f806f4089c493dcac0d24c29c01e2c38baf8e30f1b716ee37e83d200b18fe"
dependencies = [
"deranged",
"itoa",
"serde",
"time-core",
"time-macros",
]
[[package]]
name = "time-core"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3"
[[package]]
name = "time-macros"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ad70d68dba9e1f8aceda7aa6711965dfec1cac869f311a51bd08b3a2ccbce20"
dependencies = [
"time-core",
"syn",
]
[[package]]
@ -1199,7 +1033,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.38",
"syn",
]
[[package]]
@ -1342,7 +1176,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "143b538f18257fac9cad154828a57c6bf5157e1aa604d4816b5995bf6de87ae5"
dependencies = [
"form_urlencoded",
"idna 0.4.0",
"idna",
"percent-encoding",
]
@ -1352,19 +1186,11 @@ version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "vr-event-tracker"
version = "0.1.0"
dependencies = [
"axum",
"lazy_static",
"maud",
"prometheus",
"reqwest",
"serde",
@ -1409,7 +1235,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
"syn 2.0.38",
"syn",
"wasm-bindgen-shared",
]
@ -1443,7 +1269,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.38",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]

View file

@ -8,10 +8,8 @@ edition = "2021"
[dependencies]
axum = "0.6.20"
prometheus = "0.13.3"
maud = { version = "*", features = ["axum"] }
tokio = { version = "1.29.1", features = ["full"] }
lazy_static = "1.4.0"
serde = { version = "1.0.189", features = ["derive"] }
tokio = { version = "1.29.1", features = [ "full" ] }
serde = { version = "1.0.189", features = [ "derive" ] }
toml = "0.8.2"
reqwest = { version = "0.11.22", features = ["json", "cookies"] }
reqwest = { version = "0.11.22", features = [ "json" ] }
url = "2.4.1"

View file

@ -1,7 +1,3 @@
use std::collections::HashMap;
use std::error::Error;
use std::{env, fmt, fs};
use axum::extract::State;
use axum::{
body::Full,
@ -10,48 +6,22 @@ use axum::{
routing::get,
Router,
};
use lazy_static::lazy_static;
use maud::html;
use maud::Markup;
use prometheus::{
register_gauge_vec, register_int_counter_vec, Encoder, GaugeVec, IntCounterVec, TextEncoder,
};
use reqwest::header::USER_AGENT;
use serde::Deserialize;
use std::collections::HashMap;
use std::error::Error;
use std::sync::OnceLock;
use std::{env, fmt, fs};
use url::Url;
lazy_static! {
static ref PLAYER_COUNT: GaugeVec = register_gauge_vec!(
"vrchat_playercount",
"Current number of players in instance.",
&["instance", "world", "name", "group"],
)
.unwrap();
static ref VRCDN_VIEWERS: GaugeVec = register_gauge_vec!(
"vrcdn_viewers",
"Current number viewers according to VRCDN's API.",
&["region", "group"],
)
.unwrap();
static ref WORLD_VISITS: IntCounterVec = register_int_counter_vec!(
"vrchat_world_visits",
"Number of times a world has been visited.",
&["world", "name"],
)
.unwrap();
static ref WORLD_OCCUPANTS: GaugeVec = register_gauge_vec!(
"vrchat_world_occupants",
"Occupants currently in the world",
&["world", "name", "type"]
)
.unwrap();
static ref WORLD_FAVORITES: IntCounterVec = register_int_counter_vec!(
"vrchat_world_favorites",
"Number of times a world has been favorited.",
&["world", "name"],
)
.unwrap();
}
static PLAYER_COUNT: OnceLock<GaugeVec> = OnceLock::new();
static VRCDN_VIEWERS: OnceLock<GaugeVec> = OnceLock::new();
static WORLD_VISITS: OnceLock<IntCounterVec> = OnceLock::new();
static WORLD_OCCUPANTS: OnceLock<GaugeVec> = OnceLock::new();
static WORLD_FAVORITES: OnceLock<IntCounterVec> = OnceLock::new();
#[derive(Debug)]
enum WsError {
@ -155,6 +125,47 @@ async fn main() -> Result<(), ()> {
let mut config: Config = toml::from_str(&content).unwrap();
config.vrchat_token = Some(env::var("VRCHAT_AUTH_TOKEN").unwrap());
let _ = PLAYER_COUNT.set(
register_gauge_vec!(
"vrchat_playercount",
"Current number viewers according to VRCDN's API.",
&["instance", "world", "name", "group"],
)
.unwrap(),
);
let _ = VRCDN_VIEWERS.set(
register_gauge_vec!(
"vrcdn_viewers",
"Current number viewers according to VRCDN's API.",
&["region", "group"],
)
.unwrap(),
);
let _ = WORLD_VISITS.set(
register_int_counter_vec!(
"vrchat_world_visits",
"Number of times a world has been visited.",
&["world", "name"],
)
.unwrap(),
);
let _ = WORLD_OCCUPANTS.set(
register_gauge_vec!(
"vrchat_world_occupants",
"Occupants currently in the world",
&["world", "name", "type"]
)
.unwrap(),
);
let _ = WORLD_FAVORITES.set(
register_int_counter_vec!(
"vrchat_world_favorites",
"Number of times a world has been favorited.",
&["world", "name"],
)
.unwrap(),
);
let app = Router::new()
.route("/", get(homepage))
.route("/metrics", get(metrics_handler))
@ -177,20 +188,14 @@ async fn metrics_handler(State(config): State<Config>) -> Result<impl IntoRespon
}
}
async fn homepage() -> Markup {
html! {
body {
main {
a href = "/metrics" { "metrics" }
}
}
}
async fn homepage() -> &'static str {
r#"<a href="/metrics">/metrics</a>"#
}
async fn metrics(config: Config) -> Result<Vec<u8>, WsError> {
PLAYER_COUNT.reset();
VRCDN_VIEWERS.reset();
WORLD_VISITS.reset();
PLAYER_COUNT.get().unwrap().reset();
VRCDN_VIEWERS.get().unwrap().reset();
WORLD_VISITS.get().unwrap().reset();
let encoder = TextEncoder::new();
let client = reqwest::Client::new();
let auth_cookie = format!("auth={}", &config.vrchat_token.unwrap());
@ -267,6 +272,8 @@ async fn group_metrics(
let data: VrcInstanceData = req.json().await?;
let instance_name = instance.name.unwrap_or(instance.location.unwrap());
PLAYER_COUNT
.get()
.unwrap()
.with_label_values(&[
&instance.world.unwrap(),
&instance.instance.unwrap(),
@ -290,6 +297,8 @@ async fn group_metrics(
let vrcdn_data: VrCdnData = req.json().await.unwrap();
for region in vrcdn_data.viewers {
VRCDN_VIEWERS
.get()
.unwrap()
.with_label_values(&[&region.region, &name])
.set(region.total);
}
@ -319,31 +328,51 @@ async fn world_metrics(
for instance in world_data.instances {
PLAYER_COUNT
.get()
.unwrap()
.with_label_values(&[&instance.0, &id, &name, ""])
.set(instance.1);
}
let current_visits: u64 = match WORLD_VISITS.get_metric_with_label_values(&[&id, &name]) {
let current_visits: u64 = match WORLD_VISITS
.get()
.unwrap()
.get_metric_with_label_values(&[&id, &name])
{
Ok(v) => v.get(),
Err(_) => 0,
};
WORLD_VISITS
.get()
.unwrap()
.with_label_values(&[&id, &name])
.inc_by(world_data.visits - current_visits);
let current_favorites: u64 = match WORLD_FAVORITES.get_metric_with_label_values(&[&id, &name]) {
let current_favorites: u64 = match WORLD_FAVORITES
.get()
.unwrap()
.get_metric_with_label_values(&[&id, &name])
{
Ok(v) => v.get(),
Err(_) => 0,
};
WORLD_FAVORITES
.get()
.unwrap()
.with_label_values(&[&id, &name])
.inc_by(world_data.favorites - current_favorites);
WORLD_OCCUPANTS
.get()
.unwrap()
.with_label_values(&[&id, &name, "private"])
.set(world_data.private_occupants);
WORLD_OCCUPANTS
.get()
.unwrap()
.with_label_values(&[&id, &name, "public"])
.set(world_data.public_occupants);
WORLD_OCCUPANTS
.get()
.unwrap()
.with_label_values(&[&id, &name, "total"])
.set(world_data.total_occupants);