Support submitting to multiple APIs

This commit is contained in:
durch
2025-05-12 17:51:34 +02:00
parent 9a62581272
commit c7f34d04c0
3 changed files with 52 additions and 43 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "nym-network-monitor"
version = "1.0.2"
version = "1.1.0"
authors.workspace = true
repository.workspace = true
homepage.workspace = true
+43 -38
View File
@@ -17,7 +17,7 @@ use tokio::task::JoinHandle;
use tokio_postgres::{binary_copy::BinaryCopyInWriter, types::Type, Client, NoTls};
use utoipa::ToSchema;
use crate::{NYM_API_URL, PRIVATE_KEY, TOPOLOGY};
use crate::{NYM_API_URLS, PRIVATE_KEY, TOPOLOGY};
struct HydratedRoute {
mix_nodes: Vec<RoutingNode>,
@@ -491,49 +491,54 @@ pub async fn submit_metrics(database_url: Option<&String>) -> anyhow::Result<()>
}
if let Some(private_key) = PRIVATE_KEY.get() {
let node_stats = monitor_mixnode_results().await?;
let gateway_stats = monitor_gateway_results().await?;
if let Some(nym_api_urls) = NYM_API_URLS.get() {
for nym_api_url in nym_api_urls {
let node_stats = monitor_mixnode_results().await?;
let gateway_stats = monitor_gateway_results().await?;
info!("Submitting metrics to {}", *NYM_API_URL);
let client = reqwest::Client::new();
info!("Submitting metrics to {}", nym_api_url);
let client = reqwest::Client::new();
let node_submit_url = format!("{}/{API_VERSION}/{STATUS}/{SUBMIT_NODE}", &*NYM_API_URL);
let gateway_submit_url =
format!("{}/{API_VERSION}/{STATUS}/{SUBMIT_GATEWAY}", &*NYM_API_URL);
let node_submit_url =
format!("{}/{API_VERSION}/{STATUS}/{SUBMIT_NODE}", nym_api_url);
let gateway_submit_url =
format!("{}/{API_VERSION}/{STATUS}/{SUBMIT_GATEWAY}", nym_api_url);
info!("Submitting {} mixnode measurements", node_stats.len());
info!("Submitting {} mixnode measurements", node_stats.len());
node_stats
.chunks(10)
.map(|chunk| {
let monitor_message = MonitorMessage::new(chunk.to_vec(), private_key);
client.post(&node_submit_url).json(&monitor_message).send()
})
.collect::<FuturesUnordered<_>>()
.collect::<Vec<Result<_, _>>>()
.await
.into_iter()
.collect::<Result<Vec<_>, _>>()?;
node_stats
.chunks(10)
.map(|chunk| {
let monitor_message = MonitorMessage::new(chunk.to_vec(), private_key);
client.post(&node_submit_url).json(&monitor_message).send()
})
.collect::<FuturesUnordered<_>>()
.collect::<Vec<Result<_, _>>>()
.await
.into_iter()
.collect::<Result<Vec<_>, _>>()?;
info!("Submitting {} gateway measurements", gateway_stats.len());
info!("Submitting {} gateway measurements", gateway_stats.len());
gateway_stats
.chunks(10)
.map(|chunk| {
let monitor_message = MonitorMessage::new(
chunk.to_vec(),
PRIVATE_KEY.get().expect("We've set this!"),
);
client
.post(&gateway_submit_url)
.json(&monitor_message)
.send()
})
.collect::<FuturesUnordered<_>>()
.collect::<Vec<Result<_, _>>>()
.await
.into_iter()
.collect::<Result<Vec<_>, _>>()?;
gateway_stats
.chunks(10)
.map(|chunk| {
let monitor_message = MonitorMessage::new(
chunk.to_vec(),
PRIVATE_KEY.get().expect("We've set this!"),
);
client
.post(&gateway_submit_url)
.json(&monitor_message)
.send()
})
.collect::<FuturesUnordered<_>>()
.collect::<Vec<Result<_, _>>>()
.await
.into_iter()
.collect::<Result<Vec<_>, _>>()?;
}
}
}
NetworkAccount::empty_buffers();
+8 -4
View File
@@ -13,7 +13,6 @@ use nym_sphinx::chunking::monitoring;
use nym_topology::{HardcodedTopologyProvider, NymTopology};
use std::fs::File;
use std::io::Write;
use std::sync::LazyLock;
use std::time::Duration;
use std::{
collections::VecDeque,
@@ -25,9 +24,7 @@ use tokio::sync::OnceCell;
use tokio::{signal::ctrl_c, sync::RwLock};
use tokio_util::sync::CancellationToken;
static NYM_API_URL: LazyLock<String> = LazyLock::new(|| {
std::env::var(NYM_API).unwrap_or_else(|_| panic!("{} env var not set", NYM_API))
});
static NYM_API_URLS: OnceCell<Vec<String>> = OnceCell::const_new();
static MIXNET_TIMEOUT: OnceCell<u64> = OnceCell::const_new();
static TOPOLOGY: OnceCell<NymTopology> = OnceCell::const_new();
@@ -138,6 +135,9 @@ struct Args {
#[arg(long, env = "DATABASE_URL")]
database_url: Option<String>,
#[arg(long, env = "NYM_APIS")]
nym_apis: Option<Vec<String>>,
}
fn generate_key_pair() -> Result<()> {
@@ -200,6 +200,10 @@ async fn main() -> Result<()> {
PRIVATE_KEY.set(pk).ok();
}
if let Some(nym_apis) = args.nym_apis {
NYM_API_URLS.set(nym_apis).ok();
}
TOPOLOGY
.set(if let Some(topology_file) = args.topology {
NymTopology::new_from_file(topology_file)?