Skip to content

Commit

Permalink
feat: add exe_hash to info
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielc committed Nov 1, 2023
1 parent f8222ea commit c9bd5f0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ minicbor = { version = "0.19.1", features = ["alloc", "std"] }
mockall = "0.11.4"
multiaddr = "0.18"
multibase = "0.9"
multihash = "0.18"
multihash = { version = "0.18", features = ["identity"] }
names = { version = "0.14.0", default-features = false }
nix = "0.26"
num_enum = "0.5.7"
Expand Down Expand Up @@ -199,6 +199,3 @@ authors = [
]
license = "Apache-2.0/MIT"
repository = "https://github.com/3box/rust-ceramic"

#[patch.crates-io]
#libp2p-metrics = { path = "../rust-libp2p/misc/metrics" }
2 changes: 2 additions & 0 deletions one/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ iroh-rpc-types.workspace = true
libipld.workspace = true
libp2p.workspace = true
multiaddr.workspace = true
multibase.workspace = true
multihash.workspace = true
names.workspace = true
prometheus-client.workspace = true
recon.workspace = true
Expand Down
49 changes: 40 additions & 9 deletions one/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod network;
mod pubsub;
mod sql;

use std::{path::PathBuf, str::FromStr, sync::Arc, time::Duration};
use std::{env, path::PathBuf, str::FromStr, sync::Arc, time::Duration};

use anyhow::{anyhow, Result};
use ceramic_core::{EventId, Interest, PeerId};
Expand All @@ -20,11 +20,13 @@ use futures_util::future;
use iroh_metrics::{config::Config as MetricsConfig, MetricsHandle};
use libipld::json::DagJsonCodec;
use libp2p::metrics::Recorder;
use multibase::Base;
use multihash::{Code, Hasher, Multihash, MultihashDigest};
use recon::{FullInterests, Recon, ReconInterestProvider, SQLiteStore, Server, Sha256a};
use signal_hook::consts::signal::*;
use signal_hook_tokio::Signals;
use swagger::{auth::MakeAllowAllAuthenticator, EmptyContext};
use tokio::{sync::oneshot, task, time::timeout};
use tokio::{io::AsyncReadExt, sync::oneshot, task, time::timeout};
use tracing::{debug, info, warn};

use crate::{
Expand Down Expand Up @@ -196,7 +198,7 @@ impl Daemon {
async fn build(opts: DaemonOpts) -> Result<Self> {
let network = opts.network.to_network(&opts.local_network_id)?;

let info = Info::default();
let info = Info::new().await?;

let mut metrics_config = MetricsConfig {
collect: opts.metrics,
Expand Down Expand Up @@ -227,6 +229,7 @@ impl Daemon {
version = info.version,
build = info.build,
instance_id = info.instance_id,
exe_hash = info.exe_hash,
);
debug!(?opts, "using daemon options");

Expand Down Expand Up @@ -525,11 +528,14 @@ pub struct Info {
pub build: String,
/// Unique name generated for this invocation of the process.
pub instance_id: String,
/// Multibase encoded multihash of the current running executable.
pub exe_hash: String,
}

impl Default for Info {
fn default() -> Self {
Self {
impl Info {
async fn new() -> Result<Self> {
let exe_hash = multibase::encode(Base::Base64Url, current_exe_hash().await?.to_bytes());
Ok(Self {
service_name: env!("CARGO_PKG_NAME").to_string(),
build: git_version::git_version!(
prefix = "git:",
Expand All @@ -539,14 +545,39 @@ impl Default for Info {
.to_string(),
version: env!("CARGO_PKG_VERSION").to_string(),
instance_id: names::Generator::default().next().unwrap(),
}
exe_hash,
})
}
}
impl Info {
fn apply_to_metrics_config(&self, cfg: &mut MetricsConfig) {
cfg.service_name = self.service_name.clone();
cfg.version = self.version.clone();
cfg.build = self.build.clone();
cfg.instance_id = self.instance_id.clone();
}
}

async fn current_exe_hash() -> Result<Multihash> {
if cfg!(debug_assertions) {
// Debug builds can be 1GB+, so do we not want to spend the time to hash them.
// Return a fake hash.
let mut hash = multihash::Identity256::default();
// Spells debugg when base64 url encoded with some leading padding.
hash.update(&[00, 117, 230, 238, 130]);
Ok(Code::Identity.wrap(hash.finalize())?)
} else {
let exe_path = env::current_exe()?;
let mut hasher = multihash::Sha2_256::default();
let mut f = tokio::fs::File::open(exe_path).await?;
let mut buffer = vec![0; 4096];

loop {
let bytes_read = f.read(&mut buffer[..]).await?;
if bytes_read == 0 {
break;
}
hasher.update(&buffer[..bytes_read]);
}
let hash = hasher.finalize();
Ok(Code::Sha2_256.wrap(hash)?)
}
}
9 changes: 7 additions & 2 deletions one/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ struct InfoLabels {
version: String,
build: String,
instance_id: String,
exe_hash: String,
}

impl From<crate::Info> for InfoLabels {
Expand All @@ -60,6 +61,7 @@ impl From<crate::Info> for InfoLabels {
version: info.version,
build: info.build,
instance_id: info.instance_id,
exe_hash: info.exe_hash,
}
}
}
Expand Down Expand Up @@ -163,8 +165,11 @@ impl Recorder<TipLoadResult> for Metrics {
async fn handle(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
let data = iroh_metrics::MetricsHandle::encode();
let mut resp = Response::new(Body::from(data));
resp.headers_mut()
.insert("Content-Type", HeaderValue::from_static("text/plain"));
resp.headers_mut().insert(
"Content-Type",
// Use OpenMetrics content type so prometheus knows to parse it accordingly
HeaderValue::from_static("application/openmetrics-text"),
);
Ok(resp)
}

Expand Down

0 comments on commit c9bd5f0

Please sign in to comment.