ipx-play: metrics for Prometheus, a dashboard row and a down alert (#458)
ipx-play sent traces and logged JSON but served no /metrics, so nothing showed whether shared links were opening or failing. It now serves them on metrics_bind (9189 in production, off the tunnel's port), with the directory's histogram and /proc reading: requests by method, route and status (by route, so each shared episode is not a row of its own, and an unknown address is route=""), episodes shared, memory, open files, uptime and version. The iPX dashboard gets an ipx-play row, its warnings join the other containers' log panel, and alerts.py fires when Prometheus cannot reach it. The prod-check skill and docs/play.md name it, with the scrape job and the OTEL_RESOURCE_ATTRIBUTES tag the trace panels need. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,10 +9,11 @@
|
||||
//! its file is, nothing more.
|
||||
|
||||
pub mod config;
|
||||
pub mod metrics;
|
||||
mod page;
|
||||
pub mod store;
|
||||
|
||||
use axum::extract::{Path, Query, Request, State};
|
||||
use axum::extract::{MatchedPath, Path, Query, Request, State};
|
||||
use axum::http::{StatusCode, header};
|
||||
use axum::middleware::Next;
|
||||
use axum::response::{IntoResponse, Response};
|
||||
@@ -160,6 +161,7 @@ async fn share(State(st): State<St>, Extension(who): Extension<Who>, Json(mut e)
|
||||
}
|
||||
match st.store.put(&who.0, &e).await {
|
||||
Ok(id) => {
|
||||
metrics::SHARES.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
tracing::info!(server = %who.0, id = %id, "an episode was shared");
|
||||
Json(Shared { url: format!("{}/e/{id}", st.base), id }).into_response()
|
||||
}
|
||||
@@ -198,6 +200,9 @@ async fn healthz(State(st): State<St>) -> Response {
|
||||
/// calls here; and a line, with the server's name and never its token.
|
||||
async fn access_log(req: Request, next: Next) -> Response {
|
||||
let (method, path) = (req.method().clone(), req.uri().path().to_owned());
|
||||
// By route, not path: by path each shared episode would be a row of its own. A path no route
|
||||
// has is "", which someone trying addresses cannot multiply.
|
||||
let route = req.extensions().get::<MatchedPath>().map_or("", |r| r.as_str()).to_owned();
|
||||
let start = Instant::now();
|
||||
let span = if path == "/healthz" {
|
||||
tracing::Span::none()
|
||||
@@ -218,6 +223,14 @@ async fn access_log(req: Request, next: Next) -> Response {
|
||||
let status = resp.status().as_u16();
|
||||
span.record("http.response.status_code", status);
|
||||
if path != "/healthz" {
|
||||
metrics::REQUESTS.observe(
|
||||
&format!(
|
||||
"method=\"{}\",route=\"{}\",status=\"{status}\"",
|
||||
ipx_directory::metrics::method(method.as_str()),
|
||||
ipx_directory::metrics::label(&route)
|
||||
),
|
||||
start.elapsed().as_secs_f64(),
|
||||
);
|
||||
let _in = span.enter();
|
||||
let who = resp.extensions().get::<Who>().map_or("", |w| w.0.as_str()).to_owned();
|
||||
tracing::info!(
|
||||
@@ -301,6 +314,18 @@ mod tests {
|
||||
assert!(!html.contains("javascript:"), "a link that is not a web address is dropped");
|
||||
|
||||
assert_eq!(get(&app, "/e/nothere").await.0, StatusCode::NOT_FOUND);
|
||||
get(&app, "/no/such/page").await;
|
||||
|
||||
let mut out = String::new();
|
||||
metrics::REQUESTS.render(&mut out, "r", "");
|
||||
for row in [
|
||||
r#"r_count{method="GET",route="/e/{id}",status="200"}"#,
|
||||
r#"r_count{method="POST",route="/v1/shares",status="200"}"#,
|
||||
r#"r_count{method="GET",route="",status="404"}"#,
|
||||
] {
|
||||
assert!(out.contains(row), "requests are counted by route, not path: {row} in {out}");
|
||||
}
|
||||
assert!(!out.contains("nothere") && !out.contains("/no/such"), "{out}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
Reference in New Issue
Block a user