Skip to content

Commit

Permalink
tests tools: fix gauges/counters in prometheus metrics (#67)
Browse files Browse the repository at this point in the history
* Fix gauges/counters

Move values to gauges/counters as it needs.

* add dump interval for prometheus to bgtask

---------

Co-authored-by: Kirill Fomichev <[email protected]>
  • Loading branch information
linuskendall and fanatid authored Jun 1, 2023
1 parent 8722ae1 commit e140dfc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 23 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion tests/bgtask_creator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ sea-orm = { version = "0.10.6", features = ["macros", "runtime-tokio-rustls", "s
sea-query = { version = "0.28.1", features = ["postgres-array"] }
solana-sdk = "1.14.10"
sqlx = { version = "0.6.2", features = ["macros", "runtime-tokio-rustls", "postgres", "uuid", "offline", "json"] }
tokio = { version = "1.23.0", features = ["macros", "rt-multi-thread", "fs"] }
tokio = { version = "1.23.0", features = ["macros", "rt-multi-thread"] }
txn_forwarder = { path = "../txn_forwarder" }
41 changes: 25 additions & 16 deletions tests/bgtask_creator/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use {
anyhow::Context,
clap::{value_parser, Arg, ArgAction, Command},
digital_asset_types::dao::{
asset, asset_authority, asset_creators, asset_data, asset_grouping,
Expand All @@ -14,19 +13,18 @@ use {
metrics::setup_metrics,
tasks::{BgTask, DownloadMetadata, DownloadMetadataTask, IntoTaskData, TaskManager},
},
prometheus::{IntGaugeVec, Opts, Registry, TextEncoder},
prometheus::{IntGaugeVec, Opts, Registry},
sea_orm::{
entity::*, query::*, DbBackend, DeleteResult, EntityTrait, JsonValue, SqlxPostgresConnector,
},
solana_sdk::pubkey::Pubkey,
sqlx::types::chrono::Utc,
std::{collections::HashMap, path::PathBuf, str::FromStr, sync::Arc, time},
tokio::fs,
tokio::time::Duration,
txn_forwarder::save_metrics,
};

lazy_static::lazy_static! {
pub static ref REGISTRY: Registry = Registry::new();

pub static ref BGTASK_SHOW: IntGaugeVec = IntGaugeVec::new(
Opts::new("bgtask_show", "Number of assets in tasks"),
&["type", "kind"]
Expand All @@ -50,9 +48,6 @@ async fn main() -> anyhow::Result<()> {
init_logger();
info!("Starting bgtask creator");

REGISTRY.register(Box::new(BGTASK_SHOW.clone())).unwrap();
REGISTRY.register(Box::new(BGTASK_CREATE.clone())).unwrap();

let matches = Command::new("bgtaskcreator")
.arg(
Arg::new("config")
Expand Down Expand Up @@ -112,6 +107,13 @@ async fn main() -> anyhow::Result<()> {
.required(false)
.action(ArgAction::Set),
)
.arg(
Arg::new("prom_save_interval")
.long("prom-save-interval")
.help("Prometheus metrics file update interval")
.required(false)
.action(ArgAction::Set),
)
.subcommand(
Command::new("show").about("Show tasks").arg(
Arg::new("print")
Expand All @@ -132,6 +134,20 @@ async fn main() -> anyhow::Result<()> {
.subcommand(Command::new("delete").about("Delete ALL pending background tasks"))
.get_matches();

let registry = Registry::new();
registry.register(Box::new(BGTASK_SHOW.clone()))?;
registry.register(Box::new(BGTASK_CREATE.clone()))?;
let metrics_jh = save_metrics(
registry,
matches.get_one::<String>("prom").cloned(),
Duration::from_millis(
matches
.get_one::<u64>("prom_save_interval")
.cloned()
.unwrap_or(1_000),
),
);

let config_path = matches.get_one::<PathBuf>("config");
if let Some(config_path) = config_path {
info!("Loading config from: {}", config_path.display());
Expand Down Expand Up @@ -389,14 +405,7 @@ WHERE
}
}

if let Some(prom) = matches.get_one::<String>("prom") {
let metrics = TextEncoder::new()
.encode_to_string(&REGISTRY.gather())
.context("could not encode custom metrics")?;
fs::write(prom, metrics).await?;
}

Ok(())
metrics_jh.await
}

fn find_by_type<'a>(
Expand Down
8 changes: 4 additions & 4 deletions tests/tree-status/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use {
stream::{self, StreamExt},
},
log::{debug, error, info},
prometheus::{IntCounter, IntGaugeVec, Opts, Registry},
prometheus::{IntGauge, IntGaugeVec, Opts, Registry},
sea_orm::{
sea_query::{Expr, Value},
ColumnTrait, ConnectionTrait, DatabaseConnection, DbBackend, DbErr, EntityTrait,
Expand Down Expand Up @@ -67,11 +67,11 @@ lazy_static::lazy_static! {
&["tree"]
).unwrap();

pub static ref TREE_STATUS_LEAVES_COMPLETED: IntCounter = IntCounter::new(
pub static ref TREE_STATUS_LEAVES_COMPLETED: IntGauge = IntGauge::new(
"tree_status_leaves_completed", "Number of complete trees"
).unwrap();

pub static ref TREE_STATUS_LEAVES_INCOMPLETE: IntCounter = IntCounter::new(
pub static ref TREE_STATUS_LEAVES_INCOMPLETE: IntGauge = IntGauge::new(
"tree_status_leaves_incomplete", "Number of incomplete trees"
).unwrap();

Expand Down Expand Up @@ -246,7 +246,7 @@ async fn main() -> anyhow::Result<()> {
if let Some(group) = args.prom_group.clone() {
labels.insert("group".to_owned(), group);
}
let registry = Registry::new_custom(None, Some(labels)).unwrap();
let registry = Registry::new_custom(None, Some(labels))?;
registry.register(Box::new(TREE_STATUS_MAX_SEQ.clone()))?;
registry.register(Box::new(TREE_STATUS_MISSING_SEQ.clone()))?;
registry.register(Box::new(TREE_STATUS_LEAVES_COMPLETED.clone()))?;
Expand Down
4 changes: 2 additions & 2 deletions tests/txn_forwarder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use {
log::info,
plerkle_messenger::{MessengerConfig, ACCOUNT_STREAM, TRANSACTION_STREAM},
plerkle_serialization::serializer::seralize_encoded_transaction_with_status,
prometheus::{IntGaugeVec, Opts, Registry},
prometheus::{IntCounterVec, Opts, Registry},
solana_client::{
nonblocking::rpc_client::RpcClient, rpc_config::RpcTransactionConfig,
rpc_request::RpcRequest,
Expand All @@ -34,7 +34,7 @@ use {
};

lazy_static::lazy_static! {
pub static ref TXN_FORWARDER_SENT: IntGaugeVec = IntGaugeVec::new(
pub static ref TXN_FORWARDER_SENT: IntCounterVec = IntCounterVec::new(
Opts::new("txn_forwarder_sent", "Number of sent transactions"),
&["tree"]
).unwrap();
Expand Down

0 comments on commit e140dfc

Please sign in to comment.