-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
730995e
commit 2132b54
Showing
12 changed files
with
524 additions
and
10 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
services: | ||
floresta: | ||
container_name: floresta | ||
build: | ||
context: . | ||
ports: | ||
- 50001:50001 | ||
- 8332:8332 | ||
- 3333:3333 | ||
restart: unless-stopped | ||
deploy: | ||
resources: | ||
reservations: | ||
memory: 16G | ||
prometheus: | ||
image: prom/prometheus | ||
container_name: prometheus | ||
command: | ||
- "--config.file=/etc/prometheus/prometheus.yml" | ||
ports: | ||
- 9090:9090 | ||
restart: unless-stopped | ||
volumes: | ||
- ./metrics/prometheus:/etc/prometheus | ||
grafana: | ||
image: grafana/grafana | ||
container_name: grafana | ||
ports: | ||
- 3000:3000 | ||
restart: unless-stopped | ||
environment: | ||
- GF_SECURITY_ADMIN_USER=admin | ||
- GF_SECURITY_ADMIN_PASSWORD=grafana | ||
volumes: | ||
- ./metrics/grafana:/etc/grafana/provisioning/datasources |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "metrics" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
sysinfo = "0.33" | ||
axum = "0.7" | ||
prometheus-client = "0.22.3" | ||
tokio = { version = "1", features = ["full"] } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
apiVersion: 1 | ||
|
||
datasources: | ||
- name: Prometheus | ||
type: prometheus | ||
url: http://prometheus:9090 | ||
isDefault: true | ||
access: proxy | ||
editable: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
global: | ||
scrape_interval: 15s | ||
scrape_timeout: 10s | ||
evaluation_interval: 15s | ||
scrape_configs: | ||
- job_name: prometheus | ||
honor_timestamps: true | ||
scrape_interval: 15s | ||
scrape_timeout: 10s | ||
metrics_path: / | ||
scheme: http | ||
static_configs: | ||
- targets: | ||
- floresta:3333 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use std::net::SocketAddr; | ||
use std::sync::atomic::AtomicU64; | ||
use std::sync::OnceLock; | ||
|
||
use axum::routing::get; | ||
use axum::Router; | ||
use prometheus_client::encoding::text::encode; | ||
use prometheus_client::metrics::gauge::Gauge; | ||
use prometheus_client::registry::Registry; | ||
use sysinfo::System; | ||
|
||
pub struct AppMetrics { | ||
registry: Registry, | ||
pub memory_usage: Gauge<f64, AtomicU64>, | ||
pub block_height: Gauge, | ||
} | ||
|
||
impl AppMetrics { | ||
pub fn new() -> Self { | ||
let mut registry = <Registry>::default(); | ||
let memory_usage = Gauge::<f64, AtomicU64>::default(); | ||
let block_height = Gauge::default(); | ||
|
||
registry.register("block_height", "Current block height", block_height.clone()); | ||
registry.register( | ||
"memory_usage_gigabytes", | ||
"System memory usage in GB", | ||
memory_usage.clone(), | ||
); | ||
|
||
Self { | ||
registry, | ||
block_height, | ||
memory_usage, | ||
} | ||
} | ||
|
||
/// Gets how much memory is being used by the system in which Floresta is | ||
/// running on, not how much memory Floresta itself it's using. | ||
pub fn update_memory_usage(&self) { | ||
let mut system = System::new_all(); | ||
system.refresh_memory(); | ||
|
||
// get used memory in gigabytes / KB / MB / GB | ||
let used_memory = system.used_memory() as f64 / 1024. / 1024. / 1024.; | ||
self.memory_usage.set(used_memory); | ||
} | ||
} | ||
|
||
impl Default for AppMetrics { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
// Singleton to share metrics across crates | ||
static METRICS: OnceLock<AppMetrics> = OnceLock::new(); | ||
pub fn get_metrics() -> &'static AppMetrics { | ||
METRICS.get_or_init(AppMetrics::new) | ||
} | ||
|
||
async fn metrics_handler() -> String { | ||
let mut buffer = String::new(); | ||
encode(&mut buffer, &get_metrics().registry).unwrap(); | ||
|
||
buffer | ||
} | ||
|
||
pub async fn metrics_server(metrics_server_address: SocketAddr) { | ||
let app = Router::new().route("/", get(metrics_handler)); | ||
let listener = tokio::net::TcpListener::bind(metrics_server_address) | ||
.await | ||
.unwrap(); | ||
axum::serve(listener, app).await.unwrap(); | ||
} |