Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit 58cca78

Browse files
authored
sanity check metrics configuration (#32799)
1 parent b83cef4 commit 58cca78

File tree

5 files changed

+59
-16
lines changed

5 files changed

+59
-16
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/src/validator.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ use {
6565
use_snapshot_archives_at_startup::UseSnapshotArchivesAtStartup,
6666
},
6767
solana_measure::measure::Measure,
68-
solana_metrics::{datapoint_info, poh_timing_point::PohTimingSender},
68+
solana_metrics::{
69+
datapoint_info, metrics::metrics_config_sanity_check, poh_timing_point::PohTimingSender,
70+
},
6971
solana_poh::{
7072
poh_recorder::PohRecorder,
7173
poh_service::{self, PohService},
@@ -1250,10 +1252,14 @@ impl Validator {
12501252
config.generator_config.clone(),
12511253
);
12521254

1255+
let cluster_type = bank_forks.read().unwrap().root_bank().cluster_type();
1256+
metrics_config_sanity_check(cluster_type)?;
1257+
12531258
datapoint_info!(
12541259
"validator-new",
12551260
("id", id.to_string(), String),
1256-
("version", solana_version::version!(), String)
1261+
("version", solana_version::version!(), String),
1262+
("cluster_type", cluster_type as u32, i64),
12571263
);
12581264

12591265
*start_progress.write().unwrap() = ValidatorStartProgress::Running;

metrics/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ lazy_static = { workspace = true }
1616
log = { workspace = true }
1717
reqwest = { workspace = true, features = ["blocking", "brotli", "deflate", "gzip", "rustls-tls", "json"] }
1818
solana-sdk = { workspace = true }
19+
thiserror = { workspace = true }
1920

2021
[dev-dependencies]
2122
env_logger = { workspace = true }

metrics/src/metrics.rs

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use {
66
gethostname::gethostname,
77
lazy_static::lazy_static,
88
log::*,
9-
solana_sdk::hash::hash,
9+
solana_sdk::{genesis_config::ClusterType, hash::hash},
1010
std::{
1111
cmp,
1212
collections::HashMap,
@@ -17,10 +17,31 @@ use {
1717
thread,
1818
time::{Duration, Instant, UNIX_EPOCH},
1919
},
20+
thiserror::Error,
2021
};
2122

2223
type CounterMap = HashMap<(&'static str, u64), CounterPoint>;
2324

25+
#[derive(Debug, Error)]
26+
pub enum MetricsError {
27+
#[error(transparent)]
28+
VarError(#[from] std::env::VarError),
29+
#[error(transparent)]
30+
ReqwestError(#[from] reqwest::Error),
31+
#[error("SOLANA_METRICS_CONFIG is invalid: '{0}'")]
32+
ConfigInvalid(String),
33+
#[error("SOLANA_METRICS_CONFIG is incomplete")]
34+
ConfigIncomplete,
35+
#[error("SOLANA_METRICS_CONFIG database mismatch: {0}")]
36+
DbMismatch(String),
37+
}
38+
39+
impl From<MetricsError> for String {
40+
fn from(error: MetricsError) -> Self {
41+
error.to_string()
42+
}
43+
}
44+
2445
impl From<&CounterPoint> for DataPoint {
2546
fn from(counter_point: &CounterPoint) -> Self {
2647
let mut point = Self::new(counter_point.name);
@@ -58,7 +79,7 @@ impl InfluxDbMetricsWriter {
5879
}
5980
}
6081

61-
fn build_write_url() -> Result<String, String> {
82+
fn build_write_url() -> Result<String, MetricsError> {
6283
let config = get_metrics_config().map_err(|err| {
6384
info!("metrics disabled: {}", err);
6485
err
@@ -381,44 +402,57 @@ impl MetricsConfig {
381402
}
382403
}
383404

384-
fn get_metrics_config() -> Result<MetricsConfig, String> {
405+
fn get_metrics_config() -> Result<MetricsConfig, MetricsError> {
385406
let mut config = MetricsConfig::default();
386-
387-
let config_var =
388-
env::var("SOLANA_METRICS_CONFIG").map_err(|err| format!("SOLANA_METRICS_CONFIG: {err}"))?;
407+
let config_var = env::var("SOLANA_METRICS_CONFIG")?;
389408

390409
for pair in config_var.split(',') {
391410
let nv: Vec<_> = pair.split('=').collect();
392411
if nv.len() != 2 {
393-
return Err(format!("SOLANA_METRICS_CONFIG is invalid: '{pair}'"));
412+
return Err(MetricsError::ConfigInvalid(pair.to_string()));
394413
}
395414
let v = nv[1].to_string();
396415
match nv[0] {
397416
"host" => config.host = v,
398417
"db" => config.db = v,
399418
"u" => config.username = v,
400419
"p" => config.password = v,
401-
_ => return Err(format!("SOLANA_METRICS_CONFIG is invalid: '{pair}'")),
420+
_ => return Err(MetricsError::ConfigInvalid(pair.to_string())),
402421
}
403422
}
404423

405424
if !config.complete() {
406-
return Err("SOLANA_METRICS_CONFIG is incomplete".to_string());
425+
return Err(MetricsError::ConfigIncomplete);
407426
}
427+
408428
Ok(config)
409429
}
410430

411-
pub fn query(q: &str) -> Result<String, String> {
431+
pub fn metrics_config_sanity_check(cluster_type: ClusterType) -> Result<(), MetricsError> {
432+
let config = match get_metrics_config() {
433+
Ok(config) => config,
434+
Err(MetricsError::VarError(std::env::VarError::NotPresent)) => return Ok(()),
435+
Err(e) => return Err(e),
436+
};
437+
match &config.db[..] {
438+
"mainnet-beta" if cluster_type != ClusterType::MainnetBeta => (),
439+
"tds" if cluster_type != ClusterType::Testnet => (),
440+
"devnet" if cluster_type != ClusterType::Devnet => (),
441+
_ => return Ok(()),
442+
};
443+
let (host, db) = (&config.host, &config.db);
444+
let msg = format!("cluster_type={cluster_type:?} host={host} database={db}");
445+
Err(MetricsError::DbMismatch(msg))
446+
}
447+
448+
pub fn query(q: &str) -> Result<String, MetricsError> {
412449
let config = get_metrics_config()?;
413450
let query_url = format!(
414451
"{}/query?u={}&p={}&q={}",
415452
&config.host, &config.username, &config.password, &q
416453
);
417454

418-
let response = reqwest::blocking::get(query_url.as_str())
419-
.map_err(|err| err.to_string())?
420-
.text()
421-
.map_err(|err| err.to_string())?;
455+
let response = reqwest::blocking::get(query_url.as_str())?.text()?;
422456

423457
Ok(response)
424458
}

programs/sbf/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)