From 3e7664e72e36fa675a8117bd00308bd5952a7ba6 Mon Sep 17 00:00:00 2001 From: MasterPtato Date: Sat, 31 May 2025 00:54:19 +0000 Subject: [PATCH] chore: fix guard metrics --- packages/edge/infra/edge-server/Cargo.toml | 3 ++ .../infra/guard/core/src/proxy_service.rs | 20 +++++----- packages/edge/infra/guard/server/Cargo.toml | 40 +++++++++---------- .../usage-metrics-publish/src/lib.rs | 5 +++ 4 files changed, 38 insertions(+), 30 deletions(-) diff --git a/packages/edge/infra/edge-server/Cargo.toml b/packages/edge/infra/edge-server/Cargo.toml index 61265d3234..6442eb6043 100644 --- a/packages/edge/infra/edge-server/Cargo.toml +++ b/packages/edge/infra/edge-server/Cargo.toml @@ -26,6 +26,9 @@ s3-util.workspace = true edge-monolith-workflow-worker.workspace = true pegboard-ws.workspace = true +# Singleton +pegboard-usage-metrics-publish.workspace = true + # API api-edge-monolith-public.workspace = true api-edge-monolith-edge.workspace = true diff --git a/packages/edge/infra/guard/core/src/proxy_service.rs b/packages/edge/infra/guard/core/src/proxy_service.rs index 1b56c184ca..2ce3905cc4 100644 --- a/packages/edge/infra/guard/core/src/proxy_service.rs +++ b/packages/edge/infra/guard/core/src/proxy_service.rs @@ -680,6 +680,11 @@ impl ProxyService { self.handle_http_request(req, target).await }; + let status = match &res { + Ok(resp) => resp.status().as_u16().to_string(), + Err(_) => "error".to_string(), + }; + // Record metrics let duration_secs = start_time.elapsed().as_secs_f64(); metrics::PROXY_REQUEST_DURATION @@ -691,16 +696,11 @@ impl ProxyService { res }; - let status = match &res { - Ok(resp) => resp.status().as_u16().to_string(), - Err(err) => { - metrics::PROXY_REQUEST_ERROR - .with_label_values(&[&err.to_string()]) - .inc(); - - "error".to_string() - } - }; + if let Err(err) = &res { + metrics::PROXY_REQUEST_ERROR + .with_label_values(&[&err.to_string()]) + .inc(); + } res } diff --git a/packages/edge/infra/guard/server/Cargo.toml b/packages/edge/infra/guard/server/Cargo.toml index 917b4769df..776643ae69 100644 --- a/packages/edge/infra/guard/server/Cargo.toml +++ b/packages/edge/infra/guard/server/Cargo.toml @@ -14,35 +14,35 @@ name = "rivet_guard" path = "src/lib.rs" [dependencies] -rivet-guard-core.workspace = true -tokio.workspace = true anyhow.workspace = true -tracing.workspace = true -uuid = { version = "1.3", features = ["v4"] } -serde_json = "1.0" +chirp-client.workspace = true +chirp-workflow.workspace = true clap = { version = "4.3", features = ["derive"] } -rivet-config.workspace = true -rivet-logs.workspace = true -rivet-runtime.workspace = true -rivet-pools.workspace = true -rivet-metrics.workspace = true +cluster.workspace = true +fdb-util.workspace = true +foundationdb.workspace = true futures = "0.3.30" +game-namespace-resolve-name-id = { path = "../../../../core/services/game/ops/namespace-resolve-name-id" } global-error.workspace = true -cluster.workspace = true -chirp-workflow.workspace = true -chirp-client.workspace = true -rivet-connection.workspace = true -regex = "1.10.3" once_cell = "1.19.0" pegboard.workspace = true -fdb-util.workspace = true -foundationdb.workspace = true -rustls-pemfile = "2.0.0" -rustls = { version = "0.23.25" } +regex = "1.10.3" rivet-cache.workspace = true +rivet-config.workspace = true +rivet-connection.workspace = true +rivet-guard-core.workspace = true +rivet-logs.workspace = true +rivet-metrics.workspace = true +rivet-pools.workspace = true +rivet-runtime.workspace = true route = { path = "../../../../core/services/route" } -game-namespace-resolve-name-id = { path = "../../../../core/services/game/ops/namespace-resolve-name-id" } +rustls = { version = "0.23.25" } +rustls-pemfile = "2.0.0" +serde_json = "1.0" +tokio.workspace = true +tracing.workspace = true types-proto = { path = "../../../../common/types-proto/core", package = "types-proto" } +uuid = { version = "1.3", features = ["v4"] } [dev-dependencies] uuid = { version = "1.3", features = ["v4", "fast-rng"] } diff --git a/packages/edge/services/pegboard/standalone/usage-metrics-publish/src/lib.rs b/packages/edge/services/pegboard/standalone/usage-metrics-publish/src/lib.rs index da1be365e1..42fe753f29 100644 --- a/packages/edge/services/pegboard/standalone/usage-metrics-publish/src/lib.rs +++ b/packages/edge/services/pegboard/standalone/usage-metrics-publish/src/lib.rs @@ -118,6 +118,11 @@ pub async fn run_from_env( env_usage.memory += actor.resources.memory_mib as u64; } + // Clear old metrics because they will never be set to 0 (due to no actors being present and thus no + // metrics update) + pegboard::metrics::ENV_CPU_USAGE.reset(); + pegboard::metrics::ENV_MEMORY_USAGE.reset(); + // Insert metrics for ((env_id, client_flavor), usage) in usage_by_env_and_flavor { pegboard::metrics::ENV_CPU_USAGE