Skip to content

Commit

Permalink
added opentelemetry/prometheus metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Rom3dius committed Mar 25, 2024
1 parent ccec3fb commit 0c08caf
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
23 changes: 22 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,25 @@ services:
}' | jq '.result.available_block_range.high' | grep -q '^1' && echo "Success" && exit 0 || echo "Error" && exit 1
interval: 5s
retries: 5
start_period: 5s
start_period: 5s
# prometheus:
# image: prom/prometheus:latest
# container_name: prometheus
#volumes:
# - ./.config/prometheus.yml:/etc/prometheus/prometheus.yml
# command:
# - '--config.file=/etc/prometheus/prometheus.yml'
# ports:
# - "9090:9090"
# grafana:
# image: grafana/grafana:latest
# container_name: grafana
# ports:
# - "3000:3000"
#volumes:
# - grafana_data:/var/lib/grafana
# environment:
# GF_SECURITY_ADMIN_USER: admin
# GF_SECURITY_ADMIN_PASSWORD: admin
#volumes:
# grafana_data:
4 changes: 3 additions & 1 deletion node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ thiserror = "1.0.34"
tokio = {version = "1.36.0", features = ["macros"]}
toml = "0.8.11"
diesel_migrations = {version = "2.1.0", features=["postgres"]}
axum-otel-metrics = {version = "0.8.0", optional = true}

[dev-dependencies]
cargo-nextest = "0.9.67"

[features]
default = ["delta-tree"]
default = ["delta-tree", "metrics"]
delta-tree = []
metrics = ["axum-otel-metrics"]
5 changes: 3 additions & 2 deletions node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ Short term:
- Move diesel.toml into diesel folder, renaming it to be delta-tree specific
- Add match statements in insert functions so for example Transfer and TransferModel can be passed as arguments
- Add license
- Seperate out database into seperate crate
- Seperate out database into seperate crate (maybe)

Long term:
- As soon as [OnceCell](https://docs.rs/tokio/latest/tokio/sync/struct.OnceCell.html) is a stable feature, replace lazy_static.
- Add metrics, Prometheus compatible if possible

Done:
- ~~Seperate router setup to routes.rs files~~
- ~~Move models from database/entities to domain/models~~
- ~~Implement clean shutdown, stopping TcpListeners and closing database connections (catch CTRL+C signal)~~
- ~~Applying database migrations~~
- ~~Add metrics, Prometheus compatible if possible~~


### Testing
In order to test, make sure you have [cargo-nextest](https://nexte.st) and [docker-compose](https://docs.docker.com/compose/install/#scenario-two-install-the-compose-plugin) installed.
Expand Down
4 changes: 3 additions & 1 deletion node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ async fn main() {
#+# #+# #+# #+# #+# #+# #+# #+# #+# #+# #+#
### ### ### ### ########### ### ### ######## ########
V: ?
"#);
"#);

// Setup logging
fern::Dispatch::new()
Expand Down Expand Up @@ -80,6 +81,7 @@ async fn main() {

// Apply database migrations
run_migrations(&pool).await;
log::debug!("Ran database migrations!");

// Setup application state
let state = AppState { pool: pool.clone() };
Expand Down
23 changes: 19 additions & 4 deletions node/src/routes.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use axum::{
routing::{get, post},
routing::get,
http::StatusCode,
Json, Router,
Router,
};
use axum::response::IntoResponse;

#[cfg(feature="metrics")]
use axum_otel_metrics::HttpMetricsLayerBuilder;

use crate::AppState;

#[cfg(feature="delta-tree")]
Expand All @@ -16,8 +19,11 @@ use crate::handlers::delta_tree::routes::delta_tree_routes;

// Router configuring all accessible API endpoints
pub fn app_router() -> Router<AppState> {
let mut router = Router::new()
.route("/ping", get(ping));
let mut router = Router::new();

// Add default endpoints
router = router.route("/ping", get(ping));


#[cfg(feature="delta-tree")]
{
Expand All @@ -26,6 +32,15 @@ pub fn app_router() -> Router<AppState> {

// add 404 error handler
router = router.fallback(handler_404);

// add metrics
#[cfg(feature="metrics")]
{
let metrics = HttpMetricsLayerBuilder::new().build();
router = router.merge(metrics.routes::<AppState>());
router = router.layer(metrics);
}

router
}

Expand Down

0 comments on commit 0c08caf

Please sign in to comment.