Skip to content

Commit

Permalink
fix(stats): 'no data' for grafana chart errors count (#1039)
Browse files Browse the repository at this point in the history
* initialize counter to 0 to avoid 'no data' in grafana

* reorg integration tests to allow parallel run
  • Loading branch information
bragov4ik authored Aug 29, 2024
1 parent 8289f02 commit 040868d
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 14 deletions.
5 changes: 5 additions & 0 deletions stats/stats-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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());

Expand Down
15 changes: 15 additions & 0 deletions stats/stats-server/tests/it/common.rs
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions stats/stats-server/tests/it/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod common;

mod counters;
mod lines;
mod swagger;
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
7 changes: 7 additions & 0 deletions stats/stats/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ lazy_static! {
)
.unwrap();
}

pub fn initialize_metrics<'a>(enabled_chart_keys: impl IntoIterator<Item = &'a str>) {
for chart_id in enabled_chart_keys {
UPDATE_ERRORS.with_label_values(&[chart_id]).reset();
// making zero observation for histograms doesn't make sense
}
}

0 comments on commit 040868d

Please sign in to comment.