From 040868daae8e142440de231e3b5743451b19eba0 Mon Sep 17 00:00:00 2001 From: Kirill Ivanov <8144358+bragov4ik@users.noreply.github.com> Date: Thu, 29 Aug 2024 15:03:52 +0300 Subject: [PATCH] fix(stats): 'no data' for grafana chart errors count (#1039) * initialize counter to 0 to avoid 'no data' in grafana * reorg integration tests to allow parallel run --- stats/stats-server/src/server.rs | 5 +++++ stats/stats-server/tests/it/common.rs | 15 +++++++++++++++ stats/stats-server/tests/{ => it}/counters.rs | 2 ++ stats/stats-server/tests/{ => it}/lines.rs | 0 stats/stats-server/tests/it/main.rs | 5 +++++ stats/stats-server/tests/{ => it}/swagger.rs | 17 +++-------------- stats/stats/src/metrics.rs | 7 +++++++ 7 files changed, 37 insertions(+), 14 deletions(-) create mode 100644 stats/stats-server/tests/it/common.rs rename stats/stats-server/tests/{ => it}/counters.rs (99%) rename stats/stats-server/tests/{ => it}/lines.rs (100%) create mode 100644 stats/stats-server/tests/it/main.rs rename stats/stats-server/tests/{ => it}/swagger.rs (77%) diff --git a/stats/stats-server/src/server.rs b/stats/stats-server/src/server.rs index 4b09df447..20f4af391 100644 --- a/stats/stats-server/src/server.rs +++ b/stats/stats-server/src/server.rs @@ -13,6 +13,7 @@ use anyhow::Context; use blockscout_endpoint_swagger::route_swagger; use blockscout_service_launcher::launcher::{self, LaunchSettings}; use sea_orm::{ConnectOptions, Database}; +use stats::metrics; use stats_proto::blockscout::stats::v1::{ health_actix::route_health, health_server::HealthServer, @@ -111,6 +112,10 @@ pub async fn stats(settings: Settings) -> Result<(), anyhow::Error> { .await; }); + if settings.metrics.enabled { + metrics::initialize_metrics(charts.charts_info.keys().map(|f| f.as_str())); + } + let read_service = Arc::new(ReadService::new(db, charts, settings.limits.into()).await?); let health = Arc::new(HealthService::default()); diff --git a/stats/stats-server/tests/it/common.rs b/stats/stats-server/tests/it/common.rs new file mode 100644 index 000000000..54b879d22 --- /dev/null +++ b/stats/stats-server/tests/it/common.rs @@ -0,0 +1,15 @@ +use reqwest::{RequestBuilder, Response}; + +pub async fn send_arbitrary_request(request: RequestBuilder) -> Response { + let response = request + .send() + .await + .unwrap_or_else(|_| panic!("Failed to send request")); + + if !response.status().is_success() { + let status = response.status(); + let message = response.text().await.expect("Read body as text"); + panic!("Invalid status code (success expected). Status: {status}. Message: {message}") + } + response +} diff --git a/stats/stats-server/tests/counters.rs b/stats/stats-server/tests/it/counters.rs similarity index 99% rename from stats/stats-server/tests/counters.rs rename to stats/stats-server/tests/it/counters.rs index dd127a5f0..035b34fe5 100644 --- a/stats/stats-server/tests/counters.rs +++ b/stats/stats-server/tests/it/counters.rs @@ -3,9 +3,11 @@ use blockscout_service_launcher::{ test_server::{get_test_server_settings, init_server, send_get_request}, }; use chrono::NaiveDate; + use stats::tests::{init_db::init_db_all, mock_blockscout::fill_mock_blockscout_data}; use stats_proto::blockscout::stats::v1::Counters; use stats_server::{stats, Settings}; + use std::{collections::HashSet, path::PathBuf, str::FromStr}; #[tokio::test] diff --git a/stats/stats-server/tests/lines.rs b/stats/stats-server/tests/it/lines.rs similarity index 100% rename from stats/stats-server/tests/lines.rs rename to stats/stats-server/tests/it/lines.rs diff --git a/stats/stats-server/tests/it/main.rs b/stats/stats-server/tests/it/main.rs new file mode 100644 index 000000000..0c6349fec --- /dev/null +++ b/stats/stats-server/tests/it/main.rs @@ -0,0 +1,5 @@ +mod common; + +mod counters; +mod lines; +mod swagger; diff --git a/stats/stats-server/tests/swagger.rs b/stats/stats-server/tests/it/swagger.rs similarity index 77% rename from stats/stats-server/tests/swagger.rs rename to stats/stats-server/tests/it/swagger.rs index c1eaf4e61..2414f4b64 100644 --- a/stats/stats-server/tests/swagger.rs +++ b/stats/stats-server/tests/it/swagger.rs @@ -3,24 +3,13 @@ use blockscout_service_launcher::{ test_server::{get_test_server_settings, init_server}, }; use pretty_assertions::assert_eq; -use reqwest::{RequestBuilder, Response}; + use stats::tests::init_db::init_db_all; use stats_server::{stats, Settings}; + use std::{path::PathBuf, str::FromStr}; -async fn send_arbitrary_request(request: RequestBuilder) -> Response { - let response = request - .send() - .await - .unwrap_or_else(|_| panic!("Failed to send request")); - - if !response.status().is_success() { - let status = response.status(); - let message = response.text().await.expect("Read body as text"); - panic!("Invalid status code (success expected). Status: {status}. Message: {message}") - } - response -} +use crate::common::send_arbitrary_request; #[tokio::test] #[ignore = "needs database"] diff --git a/stats/stats/src/metrics.rs b/stats/stats/src/metrics.rs index 8994a03b6..8372df603 100644 --- a/stats/stats/src/metrics.rs +++ b/stats/stats/src/metrics.rs @@ -23,3 +23,10 @@ lazy_static! { ) .unwrap(); } + +pub fn initialize_metrics<'a>(enabled_chart_keys: impl IntoIterator) { + for chart_id in enabled_chart_keys { + UPDATE_ERRORS.with_label_values(&[chart_id]).reset(); + // making zero observation for histograms doesn't make sense + } +}