Add /metrics endpoint for Prometheus (#724)

This commit is contained in:
Jackson Kruger 2023-10-06 17:58:02 -05:00 committed by GitHub
parent 259c5ef3d0
commit dfa43f3c5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 4 deletions

28
Cargo.lock generated
View File

@ -284,6 +284,19 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "actix-web-prom"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f23f332a652836b8f3a6876103c70c9ed436d0e69fa779ab5d7f57b1d5c8d488"
dependencies = [
"actix-web",
"futures-core",
"pin-project-lite",
"prometheus",
"regex",
]
[[package]]
name = "actix-ws"
version = "0.2.5"
@ -2234,6 +2247,7 @@ dependencies = [
"actix-multipart",
"actix-rt",
"actix-web",
"actix-web-prom",
"actix-ws",
"argon2",
"async-trait",
@ -3139,6 +3153,20 @@ dependencies = [
"rustc_version 0.2.3",
]
[[package]]
name = "prometheus"
version = "0.13.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "449811d15fbdf5ceb5c1144416066429cf82316e2ec8ce0c1f6f8a02e7bbcf8c"
dependencies = [
"cfg-if 1.0.0",
"fnv",
"lazy_static",
"memchr",
"parking_lot 0.12.1",
"thiserror",
]
[[package]]
name = "ptr_meta"
version = "0.1.4"

View File

@ -18,6 +18,7 @@ actix-multipart = "0.6.0"
actix-cors = "0.6.4"
actix-ws = "0.2.5"
actix-files = "0.6.2"
actix-web-prom = "0.7.0"
tokio = { version = "1.29.1", features = ["sync"] }
tokio-stream = "0.1.14"
@ -37,7 +38,7 @@ hyper-tls = "0.5.0"
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_with = "3.0.0"
chrono = { version = "0.4.26", features = ["serde"]}
chrono = { version = "0.4.26", features = ["serde"] }
yaserde = "0.8.0"
yaserde_derive = "0.8.0"
xml-rs = "0.8.15"
@ -72,9 +73,21 @@ log = "0.4.19"
env_logger = "0.10.0"
thiserror = "1.0.41"
sqlx = { version = "0.6.3", features = ["offline", "runtime-tokio-rustls", "postgres", "chrono", "macros", "migrate", "decimal", "json"] }
rust_decimal = { version = "1.30.0", features = ["serde-with-float", "serde-with-str"] }
redis = { version = "0.23.0", features = ["tokio-comp", "ahash", "r2d2"]}
sqlx = { version = "0.6.3", features = [
"offline",
"runtime-tokio-rustls",
"postgres",
"chrono",
"macros",
"migrate",
"decimal",
"json",
] }
rust_decimal = { version = "1.30.0", features = [
"serde-with-float",
"serde-with-str",
] }
redis = { version = "0.23.0", features = ["tokio-comp", "ahash", "r2d2"] }
deadpool-redis = "0.12.0"
clickhouse = { version = "0.11.2", features = ["uuid", "time"] }
uuid = { version = "1.2.2", features = ["v4", "fast-rng", "serde"] }

View File

@ -1,4 +1,5 @@
use actix_web::{App, HttpServer};
use actix_web_prom::PrometheusMetricsBuilder;
use env_logger::Env;
use labrinth::database::redis::RedisPool;
use labrinth::file_hosting::S3Host;
@ -88,6 +89,11 @@ async fn main() -> std::io::Result<()> {
let store = MemoryStore::new();
let prometheus = PrometheusMetricsBuilder::new("labrinth")
.endpoint("/metrics")
.build()
.expect("Failed to create prometheus metrics middleware");
info!("Starting Actix HTTP server!");
let labrinth_config = labrinth::app_setup(
@ -101,6 +107,7 @@ async fn main() -> std::io::Result<()> {
// Init App
HttpServer::new(move || {
App::new()
.wrap(prometheus.clone())
.wrap(actix_web::middleware::Compress::default())
.wrap(
RateLimiter::new(MemoryStoreActor::from(store.clone()).start())