about summary refs log tree commit diff
path: root/tvix/nar-bridge
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2024-09-29T21·34+0200
committerflokli <flokli@flokli.de>2024-11-23T09·40+0000
commit02903133f4336c203efdf835979c5790c035189f (patch)
treebc22a3c0b10f6c0461cc0a1cd37ad155fc731011 /tvix/nar-bridge
parent5f670a2f67723436f020d96dbe74fe01db51ebf7 (diff)
feat(tvix/nar-bridge): wire up metrics layer r/8952
This provides some global HTTP statistics.

Change-Id: I8bd3e034123154a49d94720b0c8d0c3babde5ae3
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12557
Reviewed-by: Jonas Chevalier <zimbatm@zimbatm.com>
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/nar-bridge')
-rw-r--r--tvix/nar-bridge/Cargo.toml4
-rw-r--r--tvix/nar-bridge/src/lib.rs18
2 files changed, 19 insertions, 3 deletions
diff --git a/tvix/nar-bridge/Cargo.toml b/tvix/nar-bridge/Cargo.toml
index 79d65f77c1d3..e2d50befb68e 100644
--- a/tvix/nar-bridge/Cargo.toml
+++ b/tvix/nar-bridge/Cargo.toml
@@ -16,11 +16,13 @@ futures = { workspace = true }
 itertools = { workspace = true }
 prost = { workspace = true }
 nix-compat = { path = "../nix-compat", features = ["async"] }
+opentelemetry = { workspace = true }
 thiserror = { workspace = true }
 tokio = { workspace = true }
 tokio-listener = { workspace = true, features = ["axum07", "clap", "multi-listener", "sd_listen"] }
 tokio-util = { workspace = true, features = ["io", "io-util", "compat"] }
 tonic = { workspace = true, features = ["tls", "tls-roots"] }
+tower-otel-http-metrics = { workspace = true, optional = true }
 tvix-castore = { path = "../castore" }
 tvix-store = { path = "../store" }
 tvix-tracing = { path = "../tracing", features = ["tonic", "axum"] }
@@ -38,7 +40,7 @@ tonic-build = { workspace = true }
 
 [features]
 default = ["otlp"]
-otlp = ["tvix-tracing/otlp"]
+otlp = ["tvix-tracing/otlp", "tower-otel-http-metrics"]
 xp-store-composition-cli = ["tvix-store/xp-composition-cli"]
 
 [dev-dependencies]
diff --git a/tvix/nar-bridge/src/lib.rs b/tvix/nar-bridge/src/lib.rs
index db926e8cede4..0b1e865d5553 100644
--- a/tvix/nar-bridge/src/lib.rs
+++ b/tvix/nar-bridge/src/lib.rs
@@ -43,7 +43,16 @@ impl AppState {
 }
 
 pub fn gen_router(priority: u64) -> Router<AppState> {
-    Router::new()
+    #[cfg(feature = "otlp")]
+    let metrics_meter = opentelemetry::global::meter("nar-bridge");
+
+    #[cfg(feature = "otlp")]
+    let metrics_layer = tower_otel_http_metrics::HTTPMetricsLayerBuilder::new()
+        .with_meter(metrics_meter)
+        .build()
+        .unwrap();
+
+    let router = Router::new()
         .route("/", get(root))
         .route("/nar/:nar_str", get(four_o_four))
         .route("/nar/:nar_str", head(nar::head_root_nodes))
@@ -53,7 +62,12 @@ pub fn gen_router(priority: u64) -> Router<AppState> {
         .route("/:narinfo_str", get(narinfo::get))
         .route("/:narinfo_str", head(narinfo::head))
         .route("/:narinfo_str", put(narinfo::put))
-        .route("/nix-cache-info", get(move || nix_cache_info(priority)))
+        .route("/nix-cache-info", get(move || nix_cache_info(priority)));
+
+    #[cfg(feature = "otlp")]
+    return router.layer(metrics_layer);
+    #[cfg(not(feature = "otlp"))]
+    return router;
 }
 
 async fn root() -> &'static str {