You've already forked AstralRinth
forked from didirus/AstralRinth
Add prom metrics for database, use redis max conn var (#3359)
This commit is contained in:
8
Cargo.lock
generated
8
Cargo.lock
generated
@@ -4288,6 +4288,7 @@ dependencies = [
|
|||||||
"maxminddb",
|
"maxminddb",
|
||||||
"meilisearch-sdk",
|
"meilisearch-sdk",
|
||||||
"murmur2",
|
"murmur2",
|
||||||
|
"prometheus",
|
||||||
"rand 0.8.5",
|
"rand 0.8.5",
|
||||||
"rand_chacha 0.3.1",
|
"rand_chacha 0.3.1",
|
||||||
"redis",
|
"redis",
|
||||||
@@ -6133,9 +6134,16 @@ dependencies = [
|
|||||||
"memchr",
|
"memchr",
|
||||||
"parking_lot 0.12.3",
|
"parking_lot 0.12.3",
|
||||||
"procfs",
|
"procfs",
|
||||||
|
"protobuf",
|
||||||
"thiserror 1.0.64",
|
"thiserror 1.0.64",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "protobuf"
|
||||||
|
version = "2.28.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "106dd99e98437432fed6519dedecfade6a06a73bb7b2a1e019fdd2bee5778d94"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "psm"
|
name = "psm"
|
||||||
version = "0.1.23"
|
version = "0.1.23"
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ actix-multipart = "0.6.1"
|
|||||||
actix-cors = "0.7.0"
|
actix-cors = "0.7.0"
|
||||||
actix-ws = "0.3.0"
|
actix-ws = "0.3.0"
|
||||||
actix-files = "0.6.5"
|
actix-files = "0.6.5"
|
||||||
|
prometheus = "0.13.4"
|
||||||
actix-web-prom = { version = "0.9.0", features = ["process"] }
|
actix-web-prom = { version = "0.9.0", features = ["process"] }
|
||||||
governor = "0.6.3"
|
governor = "0.6.3"
|
||||||
|
|
||||||
|
|||||||
@@ -6,3 +6,4 @@ pub use models::Project;
|
|||||||
pub use models::Version;
|
pub use models::Version;
|
||||||
pub use postgres_database::check_for_migrations;
|
pub use postgres_database::check_for_migrations;
|
||||||
pub use postgres_database::connect;
|
pub use postgres_database::connect;
|
||||||
|
pub use postgres_database::register_and_set_metrics;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use log::info;
|
use log::info;
|
||||||
|
use prometheus::{IntGauge, Registry};
|
||||||
use sqlx::migrate::MigrateDatabase;
|
use sqlx::migrate::MigrateDatabase;
|
||||||
use sqlx::postgres::{PgPool, PgPoolOptions};
|
use sqlx::postgres::{PgPool, PgPoolOptions};
|
||||||
use sqlx::{Connection, PgConnection, Postgres};
|
use sqlx::{Connection, PgConnection, Postgres};
|
||||||
@@ -45,3 +46,30 @@ pub async fn check_for_migrations() -> Result<(), sqlx::Error> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn register_and_set_metrics(
|
||||||
|
pool: &PgPool,
|
||||||
|
registry: &Registry,
|
||||||
|
) -> Result<(), prometheus::Error> {
|
||||||
|
let pg_pool_size =
|
||||||
|
IntGauge::new("labrinth_pg_pool_size", "Size of Postgres pool")?;
|
||||||
|
let pg_pool_idle = IntGauge::new(
|
||||||
|
"labrinth_pg_pool_idle",
|
||||||
|
"Number of idle Postgres connections",
|
||||||
|
)?;
|
||||||
|
|
||||||
|
registry.register(Box::new(pg_pool_size.clone()))?;
|
||||||
|
registry.register(Box::new(pg_pool_idle.clone()))?;
|
||||||
|
|
||||||
|
let pool_ref = pool.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
loop {
|
||||||
|
pg_pool_size.set(pool_ref.size() as i64);
|
||||||
|
pg_pool_idle.set(pool_ref.num_idle() as i64);
|
||||||
|
|
||||||
|
tokio::time::sleep(Duration::from_secs(5)).await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ use ariadne::ids::base62_impl::{parse_base62, to_base62};
|
|||||||
use chrono::{TimeZone, Utc};
|
use chrono::{TimeZone, Utc};
|
||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
use deadpool_redis::{Config, Runtime};
|
use deadpool_redis::{Config, Runtime};
|
||||||
|
use prometheus::{IntGauge, Registry};
|
||||||
use redis::{cmd, Cmd, ExistenceCheck, SetExpiry, SetOptions};
|
use redis::{cmd, Cmd, ExistenceCheck, SetExpiry, SetOptions};
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@@ -38,7 +39,7 @@ impl RedisPool {
|
|||||||
.builder()
|
.builder()
|
||||||
.expect("Error building Redis pool")
|
.expect("Error building Redis pool")
|
||||||
.max_size(
|
.max_size(
|
||||||
dotenvy::var("DATABASE_MAX_CONNECTIONS")
|
dotenvy::var("REDIS_MAX_CONNECTIONS")
|
||||||
.ok()
|
.ok()
|
||||||
.and_then(|x| x.parse().ok())
|
.and_then(|x| x.parse().ok())
|
||||||
.unwrap_or(10000),
|
.unwrap_or(10000),
|
||||||
@@ -53,6 +54,48 @@ impl RedisPool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn register_and_set_metrics(
|
||||||
|
&self,
|
||||||
|
registry: &Registry,
|
||||||
|
) -> Result<(), prometheus::Error> {
|
||||||
|
let redis_max_size = IntGauge::new(
|
||||||
|
"labrinth_redis_pool_max_size",
|
||||||
|
"Maximum size of Redis pool",
|
||||||
|
)?;
|
||||||
|
let redis_size = IntGauge::new(
|
||||||
|
"labrinth_redis_pool_size",
|
||||||
|
"Current size of Redis pool",
|
||||||
|
)?;
|
||||||
|
let redis_available = IntGauge::new(
|
||||||
|
"labrinth_redis_pool_available",
|
||||||
|
"Available connections in Redis pool",
|
||||||
|
)?;
|
||||||
|
let redis_waiting = IntGauge::new(
|
||||||
|
"labrinth_redis_pool_waiting",
|
||||||
|
"Number of futures waiting for a Redis connection",
|
||||||
|
)?;
|
||||||
|
|
||||||
|
registry.register(Box::new(redis_max_size.clone()))?;
|
||||||
|
registry.register(Box::new(redis_size.clone()))?;
|
||||||
|
registry.register(Box::new(redis_available.clone()))?;
|
||||||
|
registry.register(Box::new(redis_waiting.clone()))?;
|
||||||
|
|
||||||
|
let redis_pool_ref = self.pool.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
loop {
|
||||||
|
let status = redis_pool_ref.status();
|
||||||
|
redis_max_size.set(status.max_size as i64);
|
||||||
|
redis_size.set(status.size as i64);
|
||||||
|
redis_available.set(status.available as i64);
|
||||||
|
redis_waiting.set(status.waiting as i64);
|
||||||
|
|
||||||
|
tokio::time::sleep(Duration::from_secs(5)).await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn connect(&self) -> Result<RedisConnection, DatabaseError> {
|
pub async fn connect(&self) -> Result<RedisConnection, DatabaseError> {
|
||||||
Ok(RedisConnection {
|
Ok(RedisConnection {
|
||||||
connection: self.pool.get().await?,
|
connection: self.pool.get().await?,
|
||||||
|
|||||||
@@ -99,6 +99,14 @@ async fn main() -> std::io::Result<()> {
|
|||||||
.build()
|
.build()
|
||||||
.expect("Failed to create prometheus metrics middleware");
|
.expect("Failed to create prometheus metrics middleware");
|
||||||
|
|
||||||
|
database::register_and_set_metrics(&pool, &prometheus.registry)
|
||||||
|
.await
|
||||||
|
.expect("Failed to register database metrics");
|
||||||
|
redis_pool
|
||||||
|
.register_and_set_metrics(&prometheus.registry)
|
||||||
|
.await
|
||||||
|
.expect("Failed to register redis metrics");
|
||||||
|
|
||||||
let search_config = search::SearchConfig::new(None);
|
let search_config = search::SearchConfig::new(None);
|
||||||
|
|
||||||
let labrinth_config = labrinth::app_setup(
|
let labrinth_config = labrinth::app_setup(
|
||||||
|
|||||||
Reference in New Issue
Block a user