Skip to content

Commit

Permalink
Live metrics (#435)
Browse files Browse the repository at this point in the history
* Switch to in-memory metrics.

* Metrics fixes.

* Also write metrics to disk as a backup.
  • Loading branch information
cryptoquick authored Dec 12, 2023
1 parent 8401a3e commit 1f9cffe
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 138 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bitmask-core"
version = "0.7.0-beta.9"
version = "0.7.0-beta.10"
authors = [
"Jose Diego Robles <[email protected]>",
"Hunter Trujillo <[email protected]>",
Expand Down
55 changes: 20 additions & 35 deletions src/bin/bitmaskd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
fs::OpenOptions,
io::ErrorKind,
net::SocketAddr,
path,
str::FromStr,
time::{Duration, Instant},
};
Expand All @@ -24,11 +25,7 @@ use axum::{
use bitcoin_30::secp256k1::{ecdh::SharedSecret, PublicKey, SecretKey};
use bitmask_core::{
bitcoin::{save_mnemonic, sign_and_publish_psbt_file},
carbonado::{
handle_file,
metrics::{metrics, metrics_csv},
server_retrieve, server_store, store,
},
carbonado::{handle_file, metrics, server_retrieve, server_store, store},
constants::{
get_marketplace_nostr_key, get_marketplace_seed, get_network, get_udas_utxo, switch_network,
},
Expand Down Expand Up @@ -432,7 +429,13 @@ async fn co_store(
Path((pk, name)): Path<(String, String)>,
body: Bytes,
) -> Result<impl IntoResponse, AppError> {
let cc = CacheControl::new().with_no_cache();

let incoming_header = carbonado::file::Header::try_from(&body)?;
if incoming_header.pubkey.to_string() != pk {
return Ok((StatusCode::UNAUTHORIZED, TypedHeader(cc), "Unauthorized"));
}

let body_len = incoming_header.encoded_len - incoming_header.padding_len;
info!("POST /carbonado/{pk}/{name}, {body_len} bytes");

Expand Down Expand Up @@ -471,9 +474,9 @@ async fn co_store(
},
}

let cc = CacheControl::new().with_no_cache();
metrics::update(&filepath).await?;

Ok((StatusCode::OK, TypedHeader(cc)))
Ok((StatusCode::OK, TypedHeader(cc), "Success"))
}

async fn co_force_store(
Expand Down Expand Up @@ -721,40 +724,29 @@ async fn send_coins(
}

async fn json_metrics() -> Result<impl IntoResponse, AppError> {
use bitmask_core::carbonado::metrics::metrics;
let path = std::env::var("CARBONADO_DIR").unwrap_or("/tmp/bitmaskd/carbonado".to_owned());
let contents = fs::read_to_string(&format!("{path}/metrics.json")).await?;
let metrics_json = metrics::json().await?;

Ok((
StatusCode::OK,
[("content-type", "application/json")],
contents,
metrics_json,
))
}

async fn csv_metrics() -> Result<impl IntoResponse, AppError> {
let path = std::env::var("CARBONADO_DIR").unwrap_or("/tmp/bitmaskd/carbonado".to_owned());
let contents = fs::read_to_string(&format!("{path}/metrics.csv")).await?;
let metrics_csv = metrics::csv().await;

Ok((
StatusCode::OK,
[("content-type", "application/json")],
contents,
))
Ok((StatusCode::OK, [("content-type", "text/csv")], metrics_csv))
}

async fn periodic_metrics() -> Result<()> {
let path = std::env::var("CARBONADO_DIR").unwrap_or("/tmp/bitmaskd/carbonado".to_owned());
let dir = std::path::Path::new(&path);
fs::create_dir_all(dir).await?;
async fn init_metrics() -> Result<()> {
let path = env::var("CARBONADO_DIR").unwrap_or("/tmp/bitmaskd/carbonado".to_owned());
let dir = path::Path::new(&path);

info!("Starting metrics collection...");

let duration = Instant::now();

let metrics = metrics(dir).await?;
let metrics_json = serde_json::to_string_pretty(&metrics)?;
let metrics_csv = metrics_csv(metrics);
metrics::init(dir).await?;

let duration = Instant::now() - duration;

Expand All @@ -763,11 +755,6 @@ async fn periodic_metrics() -> Result<()> {
duration.as_secs_f32()
);

fs::write(&format!("{path}/metrics.json"), &metrics_json).await?;
fs::write(&format!("{path}/metrics.csv"), &metrics_csv).await?;

sleep(Duration::from_secs(4 * 60 * 60)).await;

Ok(())
}

Expand Down Expand Up @@ -834,10 +821,8 @@ async fn main() -> Result<()> {
.route("/regtest/send/:address/:amount", get(send_coins));
} else {
tokio::spawn(async {
loop {
if let Err(e) = periodic_metrics().await {
error!("Error in periodic metrics: {e}");
}
if let Err(e) = init_metrics().await {
error!("Error in periodic metrics: {e}");
}
});
}
Expand Down
6 changes: 4 additions & 2 deletions src/carbonado.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ mod server {

let (body, _encode_info) = carbonado::file::encode(&sk, Some(&pk), input, level, meta)?;
let filepath = handle_file(&pk_hex, name, body.len()).await?;
fs::write(filepath, body).await?;
fs::write(&filepath, body).await?;
metrics::update(&filepath).await?;
Ok(())
}

Expand All @@ -74,7 +75,8 @@ mod server {

let (body, _encode_info) = carbonado::file::encode(&sk, Some(&pk), input, level, meta)?;
let filepath = handle_file(&pk_hex, name, body.len()).await?;
fs::write(filepath.clone(), body.clone()).await?;
fs::write(&filepath, body.clone()).await?;
metrics::update(&filepath).await?;
Ok((filepath, body))
}

Expand Down
2 changes: 2 additions & 0 deletions src/carbonado/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ pub enum CarbonadoError {
WrongNostrPrivateKey,
/// Debug: {0}
Debug(String),
/// Error: {0}
AnyhowError(#[from] anyhow::Error),
}
Loading

0 comments on commit 1f9cffe

Please sign in to comment.