Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clickhouse replication, exclude bad prom metrics #3344

Merged
merged 3 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apps/labrinth/.env
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ SENDY_API_KEY=none

ANALYTICS_ALLOWED_ORIGINS='["http://127.0.0.1:3000", "http://localhost:3000", "https://modrinth.com", "https://www.modrinth.com", "*"]'

CLICKHOUSE_REPLICATED=false
CLICKHOUSE_URL=http://localhost:8123
CLICKHOUSE_USER=default
CLICKHOUSE_PASSWORD=
Expand Down
2 changes: 1 addition & 1 deletion apps/labrinth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ actix-multipart = "0.6.1"
actix-cors = "0.7.0"
actix-ws = "0.3.0"
actix-files = "0.6.5"
actix-web-prom = { version = "0.8.0", features = ["process"] }
actix-web-prom = { version = "0.9.0", features = ["process"] }
governor = "0.6.3"

tokio = { version = "1.35.1", features = ["sync"] }
Expand Down
29 changes: 23 additions & 6 deletions apps/labrinth/src/clickhouse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,24 @@ pub async fn init_client_with_database(
.execute()
.await?;

let clickhouse_replicated =
dotenvy::var("CLICKHOUSE_REPLICATED").unwrap() == "true";
let cluster_line = if clickhouse_replicated {
"ON cluster '{cluster}'"
} else {
""
};

let engine = if clickhouse_replicated {
"ReplicatedMergeTree('/clickhouse/{installation}/{cluster}/tables/{shard}/{database}/{table}', '{replica}')"
} else {
"MergeTree()"
};

client
.query(&format!(
"
CREATE TABLE IF NOT EXISTS {database}.views
CREATE TABLE IF NOT EXISTS {database}.views {cluster_line}
(
recorded DateTime64(4),
domain String,
Expand All @@ -53,8 +67,9 @@ pub async fn init_client_with_database(
user_agent String,
headers Array(Tuple(String, String))
)
ENGINE = MergeTree()
ENGINE = {engine}
PRIMARY KEY (project_id, recorded, ip)
SETTINGS index_granularity = 8192
"
))
.execute()
Expand All @@ -63,7 +78,7 @@ pub async fn init_client_with_database(
client
.query(&format!(
"
CREATE TABLE IF NOT EXISTS {database}.downloads
CREATE TABLE IF NOT EXISTS {database}.downloads {cluster_line}
(
recorded DateTime64(4),
domain String,
Expand All @@ -78,8 +93,9 @@ pub async fn init_client_with_database(
user_agent String,
headers Array(Tuple(String, String))
)
ENGINE = MergeTree()
ENGINE = {engine}
PRIMARY KEY (project_id, recorded, ip)
SETTINGS index_granularity = 8192
"
))
.execute()
Expand All @@ -88,7 +104,7 @@ pub async fn init_client_with_database(
client
.query(&format!(
"
CREATE TABLE IF NOT EXISTS {database}.playtime
CREATE TABLE IF NOT EXISTS {database}.playtime {cluster_line}
(
recorded DateTime64(4),
seconds UInt64,
Expand All @@ -101,8 +117,9 @@ pub async fn init_client_with_database(
game_version String,
parent UInt64
)
ENGINE = MergeTree()
ENGINE = {engine}
PRIMARY KEY (project_id, recorded, user_id)
SETTINGS index_granularity = 8192
"
))
.execute()
Expand Down
1 change: 1 addition & 0 deletions apps/labrinth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ pub fn check_env_vars() -> bool {
failed |= true;
}

failed |= check_var::<bool>("CLICKHOUSE_REPLICATED");
failed |= check_var::<String>("CLICKHOUSE_URL");
failed |= check_var::<String>("CLICKHOUSE_USER");
failed |= check_var::<String>("CLICKHOUSE_PASSWORD");
Expand Down
3 changes: 3 additions & 0 deletions apps/labrinth/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ async fn main() -> std::io::Result<()> {

let prometheus = PrometheusMetricsBuilder::new("labrinth")
.endpoint("/metrics")
.exclude_regex(r"^/api/v1/.*$")
.exclude_regex(r"^/maven/.*$")
.exclude("/_internal/launcher_socket")
.mask_unmatched_patterns("UNKNOWN")
.build()
.expect("Failed to create prometheus metrics middleware");

Expand Down
Loading