Skip to content

Commit

Permalink
Make metrics optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilospanck committed Dec 26, 2024
1 parent 2132b54 commit dc0aace
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 26 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ members = [

default-members = [
"florestad",
"metrics",
"crates/floresta",
"crates/floresta-chain",
"crates/floresta-cli",
Expand Down
8 changes: 7 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM debian:11.6-slim@sha256:171530d298096f0697da36b3324182e872db77c66452b85783ea893680cc1b62 AS builder

ARG BUILD_FEATURES=""

RUN apt-get update && apt-get install -y \
build-essential \
cmake \
Expand All @@ -20,7 +22,11 @@ COPY crates/ crates/
COPY fuzz/ fuzz/
COPY metrics/ metrics/
RUN --mount=type=cache,target=/usr/local/cargo/registry \
cargo build --release
if [ -n "$BUILD_FEATURES" ]; then \
cargo build --release --features "$BUILD_FEATURES"; \
else \
cargo build --release; \
fi

FROM debian:11.6-slim@sha256:171530d298096f0697da36b3324182e872db77c66452b85783ea893680cc1b62

Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,24 @@ cargo +nightly fuzz run local_address_str

You can replace `local_address_str` with the name of any other target you want to run.

## Metrics

This project uses [`Prometheus`](https://prometheus.io/) as a monitoring system. To enable it you must build the project with the `metrics` feature enabled:

```sh
cargo build --release --features metrics
```

The easiest way to visualize those metrics is by using some observability graphic tool like [Grafana](https://grafana.com/). To make it easier, you can also straight away use the `docker-compose.yml` file to spin up an infrastructure that will run the project with Prometheus and Grafana.

To run it, first make sure you have [Docker Compose](https://docs.docker.com/compose/) installed and then:

```sh
docker-compose up -d --build
```

Grafana should now be available to you at http://localhost:3000. To log in, please check the credentials defined in the `docker-compose.yml` file.

## Contributing
Contributions are welcome, feel free to open an issue or a pull request.

Expand Down
3 changes: 2 additions & 1 deletion crates/floresta-chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ hashbrown = { version = "0.14.0", optional = true }
secp256k1 = { version = "*", features = ["alloc"], optional = true }
floresta-common = { path = "../floresta-common", default-features = false, features = ["std"] }
bitcoinconsensus = { version = "0.106.0", optional = true, default-features = false }
metrics = { path = "../../metrics" }
metrics = { path = "../../metrics", optional = true }

[dev-dependencies]
criterion = "0.5.1"
Expand All @@ -44,6 +44,7 @@ hex = "0.4.3"
[features]
default = []
bitcoinconsensus = ["bitcoin/bitcoinconsensus", "dep:bitcoinconsensus"]
metrics = ["dep:metrics"]

[[bench]]
name = "chain_state_bench"
Expand Down
3 changes: 3 additions & 0 deletions crates/floresta-chain/src/pruned_utreexo/chain_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use floresta_common::Channel;
use log::info;
use log::trace;
use log::warn;
#[cfg(feature = "metrics")]
use metrics;
use rustreexo::accumulator::node_hash::NodeHash;
use rustreexo::accumulator::proof::Proof;
use rustreexo::accumulator::stump::Stump;
Expand Down Expand Up @@ -1102,6 +1104,7 @@ impl<PersistedState: ChainStore> UpdatableChainstate for ChainState<PersistedSta
block.txdata.len()
);

#[cfg(feature = "metrics")]
metrics::get_metrics().block_height.set(height.into());

if !self.is_in_idb() || height % 10_000 == 0 {
Expand Down
6 changes: 2 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ services:
container_name: floresta
build:
context: .
args:
BUILD_FEATURES: "metrics"
ports:
- 50001:50001
- 8332:8332
- 3333:3333
restart: unless-stopped
deploy:
resources:
reservations:
memory: 16G
prometheus:
image: prom/prometheus
container_name: prometheus
Expand Down
3 changes: 2 additions & 1 deletion florestad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ floresta-electrum = { path = "../crates/floresta-electrum" }
floresta-watch-only = { path = "../crates/floresta-watch-only" }
floresta-wire = { path = "../crates/floresta-wire" }
floresta-compact-filters = { path = "../crates/floresta-compact-filters", optional = true }
metrics = { path = "../metrics", optional = true }

anyhow = "1.0.40"
jsonrpc-http-server = { version = "18.0.0", optional = true }
Expand All @@ -38,7 +39,6 @@ jsonrpc-core-client = { version = "18.0.0", features = [
], optional = true }
zmq = { version = "0.10.0", optional = true }
dns-lookup = "2.0.4"
metrics = { path = "../metrics" }

[target.'cfg(unix)'.dependencies]
daemonize = { version = "0.5.0" }
Expand Down Expand Up @@ -66,6 +66,7 @@ json-rpc = [
"compact-filters"
]
default = ["experimental-p2p", "json-rpc"]
metrics = ["dep:metrics"]

[build-dependencies]
toml = "0.5.10"
41 changes: 23 additions & 18 deletions florestad/src/florestad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt::Arguments;
use std::fs::File;
use std::io;
use std::io::BufReader;
#[cfg(feature = "metrics")]
use std::net::Ipv4Addr;
use std::net::SocketAddr;
use std::path::PathBuf;
Expand Down Expand Up @@ -42,12 +43,13 @@ use log::error;
use log::info;
use log::warn;
use log::Record;
#[cfg(feature = "metrics")]
use metrics;
use tokio::net::TcpListener;
use tokio::sync::RwLock;
use tokio::task;
use tokio::time::Duration;
use tokio::time::{self};
#[cfg(feature = "metrics")]
use tokio::time::{self, Duration};
use tokio_rustls::rustls::internal::pemfile::certs;
use tokio_rustls::rustls::internal::pemfile::pkcs8_private_keys;
use tokio_rustls::rustls::NoClientAuth;
Expand Down Expand Up @@ -531,24 +533,27 @@ impl Florestad {
task::spawn(chain_provider.run(kill_signal, sender));

// Metrics
let metrics_server_address =
SocketAddr::new(std::net::IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 3333);
task::spawn(metrics::metrics_server(metrics_server_address));
info!(
"Started metrics server on: {}",
metrics_server_address.to_string()
);
#[cfg(feature = "metrics")]
{
let metrics_server_address =
SocketAddr::new(std::net::IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 3333);
task::spawn(metrics::metrics_server(metrics_server_address));
info!(
"Started metrics server on: {}",
metrics_server_address.to_string()
);

// Periodically update memory usage
tokio::spawn(async {
let interval = Duration::from_secs(5);
let mut ticker = time::interval(interval);
// Periodically update memory usage
tokio::spawn(async {
let interval = Duration::from_secs(5);
let mut ticker = time::interval(interval);

loop {
ticker.tick().await;
metrics::get_metrics().update_memory_usage();
}
});
loop {
ticker.tick().await;
metrics::get_metrics().update_memory_usage();
}
});
}
}

fn setup_logger(
Expand Down

0 comments on commit dc0aace

Please sign in to comment.