Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow builds without metrics #193

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,24 @@ jobs:
command: cargo test << parameters.cargo-args >> -j 4
- save-sccache-cache

cargo_check:
executor: docker-executor
steps:
- set-env-path
- *restore-workspace
- *restore-cache
- setup-protoc:
arch: "x86_64"
- setup-sccache
- restore-sccache-cache
- run:
name: Run cargo check default features
command: cargo check --workspace --all-targets
- run:
name: Run cargo check no default features
command: cargo check --workspace --all-targets --no-default-features
- save-sccache-cache

rustfmt:
executor: docker-executor
steps:
Expand Down Expand Up @@ -439,6 +457,9 @@ workflows:
iroh-ci-cd:
jobs:
- cargo_fetch
- cargo_check:
requires:
- cargo_fetch
- rustfmt:
requires:
- cargo_fetch
Expand All @@ -450,11 +471,13 @@ workflows:
cargo-args: "--workspace"
requires:
- cargo_fetch
- cargo_check
- test:
name: "test_linux_x86_64--no-default-features"
cargo-args: "--workspace --no-default-features"
requires:
- cargo_fetch
- cargo_check
- build_release_x86_64:
name: "build linux release x86_64"
context: aws_s3
Expand All @@ -476,9 +499,13 @@ workflows:
- test_darwin:
name: "test_darwin_x86_64"
cargo-args: "--workspace"
requires:
- cargo_check
- test_darwin:
name: "test_darwin_x86_64--no-default-features"
cargo-args: "--workspace --no-default-features"
requires:
- cargo_check
- build_release_osx:
name: "build mac release"
context: aws_s3
Expand Down
7 changes: 4 additions & 3 deletions iroh-bitswap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ unsigned-varint = "0.7.0"
ahash = "0.7.6"
tracing = "0.1.34"
num_enum = "0.5.7"
prometheus-client = "0.17.0"
iroh-metrics = { path = "../iroh-metrics", default-features = false }
prometheus-client = { version = "0.17.0", optional=true}
iroh-metrics = { path = "../iroh-metrics", default-features = false, optional=true }
names = { version = "0.14.0", default-features = false }
git-version = "0.3.5"

Expand All @@ -40,4 +40,5 @@ name = "message"
harness = false

[features]
default = []
default = []
metrics = ["iroh-metrics", "prometheus-client"]
13 changes: 10 additions & 3 deletions iroh-bitswap/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ use std::task::{Context, Poll};

use bytes::Bytes;
use cid::Cid;
#[cfg(feature = "metrics")]
use iroh_metrics::bitswap::Metrics;
use libp2p::core::connection::ConnectionId;
use libp2p::core::{ConnectedPoint, Multiaddr, PeerId};
use libp2p::swarm::handler::OneShotHandler;
use libp2p::swarm::{
DialError, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
};
#[cfg(feature = "metrics")]
use prometheus_client::registry::Registry;
use tracing::{debug, instrument, trace, warn};

Expand Down Expand Up @@ -89,6 +91,7 @@ pub struct Bitswap {
sessions: SessionManager,
#[allow(dead_code)]
config: BitswapConfig,
#[cfg(feature = "metrics")]
metrics: Metrics,
}

Expand All @@ -99,11 +102,12 @@ pub struct BitswapConfig {

impl Bitswap {
/// Create a new `Bitswap`.
pub fn new(config: BitswapConfig, registry: &mut Registry) -> Self {
pub fn new(config: BitswapConfig, #[cfg(feature = "metrics")] registry: &mut Registry) -> Self {
let sessions = SessionManager::new(config.session.clone());
Bitswap {
config,
sessions,
#[cfg(feature = "metrics")]
metrics: Metrics::new(registry),
..Default::default()
}
Expand All @@ -121,7 +125,7 @@ impl Bitswap {
for provider in providers.iter() {
self.sessions.create_session(provider);
}

#[cfg(feature = "metrics")]
self.metrics.providers_total.inc_by(providers.len() as u64);
self.queries
.want(cid, priority, providers.into_iter().collect())
Expand All @@ -130,7 +134,7 @@ impl Bitswap {
#[instrument(skip(self, data))]
pub fn send_block(&mut self, peer_id: &PeerId, cid: Cid, data: Bytes) -> QueryId {
debug!("send_block: {}", cid);

#[cfg(feature = "metrics")]
self.metrics.sent_block_bytes.inc_by(data.len() as u64);
self.sessions.create_session(peer_id);
self.queries.send(*peer_id, cid, data)
Expand Down Expand Up @@ -227,10 +231,12 @@ impl NetworkBehaviour for Bitswap {
// outbound upgrade
}
HandlerEvent::Bitswap(mut message) => {
#[cfg(feature = "metrics")]
self.metrics.requests_total.inc();

// Process incoming message.
while let Some(block) = message.pop_block() {
#[cfg(feature = "metrics")]
self.metrics
.received_block_bytes
.inc_by(block.data().len() as u64);
Expand Down Expand Up @@ -272,6 +278,7 @@ impl NetworkBehaviour for Bitswap {

// Propagate Cancel Events
for cid in message.wantlist().cancels() {
#[cfg(feature = "metrics")]
self.metrics.canceled_total.inc();
let event = BitswapEvent::InboundRequest {
request: InboundRequest::Cancel {
Expand Down
2 changes: 2 additions & 0 deletions iroh-bitswap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod behaviour;
mod block;
mod error;
mod message;
#[cfg(feature = "metrics")]
mod metrics;
mod prefix;
mod protocol;
Expand All @@ -18,5 +19,6 @@ pub use crate::block::tests::create_block as create_test_block;
pub use crate::block::Block;
pub use crate::error::BitswapError;
pub use crate::message::{BitswapMessage, Priority};
#[cfg(feature = "metrics")]
pub use crate::metrics::*;
pub use crate::query::QueryId;
8 changes: 6 additions & 2 deletions iroh-ctl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ config = "0.13.1"
iroh-util = { path = "../iroh-util" }
serde = { version = "1.0", features = ["derive"] }
git-version = "0.3.5"
prometheus-client = "0.17.0"
iroh-metrics = { path = "../iroh-metrics", default-features = false, features = ["rpc-grpc"] }
prometheus-client = { version = "0.17.0", optional=true}
iroh-metrics = { path = "../iroh-metrics", default-features = false, features = ["rpc-grpc"], optional=true }
libp2p = { version = "0.47", default-features = false }
cid = "0.8.5"
multiaddr = "0.14.0"
bytes = "1.1.0"

[features]
default = []
metircs = ["iroh-metrics", "prometheus-client"]
5 changes: 5 additions & 0 deletions iroh-ctl/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use config::{ConfigError, Map, Source, Value};
#[cfg(feature = "metrics")]
use iroh_metrics::config::Config as MetricsConfig;
use iroh_rpc_client::Config as RpcClientConfig;
use iroh_util::insert_into_config_map;
Expand All @@ -13,13 +14,15 @@ pub const ENV_PREFIX: &str = "IROH_CTL";
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Config {
pub rpc_client: RpcClientConfig,
#[cfg(feature = "metrics")]
pub metrics: MetricsConfig,
}

impl Default for Config {
fn default() -> Self {
Self {
rpc_client: RpcClientConfig::default_grpc(),
#[cfg(feature = "metrics")]
metrics: Default::default(),
}
}
Expand All @@ -32,6 +35,7 @@ impl Source for Config {
fn collect(&self) -> Result<Map<String, Value>, ConfigError> {
let mut map: Map<String, Value> = Map::new();
insert_into_config_map(&mut map, "rpc_client", self.rpc_client.collect()?);
#[cfg(feature = "metrics")]
insert_into_config_map(&mut map, "metrics", self.metrics.collect()?);
Ok(map)
}
Expand All @@ -50,6 +54,7 @@ mod tests {
"rpc_client".to_string(),
Value::new(None, default.rpc_client.collect().unwrap()),
);
#[cfg(feature = "metrics")]
expect.insert(
"metrics".to_string(),
Value::new(None, default.metrics.collect().unwrap()),
Expand Down
1 change: 1 addition & 0 deletions iroh-ctl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod config;
pub mod gateway;
#[cfg(feature = "metrics")]
pub mod metrics;
pub mod p2p;
pub mod status;
Expand Down
8 changes: 7 additions & 1 deletion iroh-ctl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ use std::collections::HashMap;
use std::path::PathBuf;

use clap::{Parser, Subcommand};
#[cfg(feature = "metrics")]
use iroh_ctl::metrics;
use iroh_ctl::{
gateway::{run_command as run_gateway_command, Gateway},
metrics,
p2p::{run_command as run_p2p_command, P2p},
store::{run_command as run_store_command, Store},
};
use iroh_rpc_client::Client;
use iroh_util::{iroh_home_path, make_config};
#[cfg(feature = "metrics")]
use prometheus_client::registry::Registry;

use iroh_ctl::{
Expand Down Expand Up @@ -68,11 +70,14 @@ async fn main() -> anyhow::Result<()> {
)
.unwrap();

#[cfg(feature = "metrics")]
let metrics_config = config.metrics.clone();

// stubbing in metrics
#[cfg(feature = "metrics")]
let prom_registry = Registry::default();
// TODO: need to register prometheus metrics
#[cfg(feature = "metrics")]
let metrics_handle = iroh_metrics::MetricsHandle::from_registry_with_tracer(
metrics::metrics_config_with_compile_time_info(metrics_config),
prom_registry,
Expand All @@ -94,6 +99,7 @@ async fn main() -> anyhow::Result<()> {
Commands::Gateway(gateway) => run_gateway_command(client, gateway).await?,
};

#[cfg(feature = "metrics")]
metrics_handle.shutdown();
Ok(())
}
7 changes: 4 additions & 3 deletions iroh-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ serde_json = "1.0.78"
serde_qs = "0.10.1"
tower = { version = "0.4", features = ["util", "timeout", "load-shed", "limit"] }
mime_guess = "2.0.4"
iroh-metrics = { path = "../iroh-metrics", default-features = false }
iroh-metrics = { path = "../iroh-metrics", default-features = false, optional=true }
tracing = "0.1.33"
names = { version = "0.14.0", default-features = false }
git-version = "0.3.5"
Expand All @@ -37,7 +37,7 @@ anyhow = "1"
futures = "0.3.21"
tracing-subscriber = { version = "0.3.11", features = ["env-filter"] }
iroh-resolver = { path = "../iroh-resolver" }
prometheus-client = "0.17.0"
prometheus-client = { version = "0.17.0", optional=true}
tokio-util = { version = "0.7", features = ["io"] }
bytes = "1.1.0"
tower-layer = { version = "0.3" }
Expand All @@ -62,6 +62,7 @@ axum-macros = "0.2.0" # use #[axum_macros::debug_handler] for better error messa


[features]
default = ["rpc-grpc", "rpc-mem"]
default = ["rpc-grpc", "rpc-mem", "metrics"]
metrics = ["iroh-metrics", "iroh-resolver/metrics", "prometheus-client"]
rpc-grpc = ["iroh-rpc-types/grpc", "iroh-rpc-client/grpc", "iroh-metrics/rpc-grpc"]
rpc-mem = ["iroh-rpc-types/mem", "iroh-rpc-client/mem"]
6 changes: 6 additions & 0 deletions iroh-gateway/src/bad_bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ mod tests {
use super::*;
use hex_literal::hex;
use http::StatusCode;
#[cfg(feature = "metrics")]
use iroh_metrics::gateway::Metrics;
use iroh_rpc_client::Config as RpcClientConfig;
#[cfg(feature = "metrics")]
use prometheus_client::registry::Registry;

#[tokio::test]
Expand Down Expand Up @@ -190,13 +192,17 @@ mod tests {
);
config.set_default_headers();

#[cfg(feature = "metrics")]
let mut prom_registry = Registry::default();
#[cfg(feature = "metrics")]
let gw_metrics = Metrics::new(&mut prom_registry);
let rpc_addr = "grpc://0.0.0.0:0".parse().unwrap();
let handler = crate::core::Core::new(
config,
rpc_addr,
#[cfg(feature = "metrics")]
gw_metrics,
#[cfg(feature = "metrics")]
&mut prom_registry,
Arc::new(Some(RwLock::new(bbits))),
)
Expand Down
Loading