tweaks to http handling in gateway

This commit is contained in:
Chris Beck
2025-12-05 01:40:29 -07:00
parent 2b88257682
commit da502583ad
3 changed files with 27 additions and 16 deletions
+20 -12
View File
@@ -6,7 +6,7 @@ use crate::{
use tokio_util::bytes::Buf; use tokio_util::bytes::Buf;
use conf::{Conf, Subcommands}; use conf::{Conf, Subcommands};
use futures_util::FutureExt; use futures_util::FutureExt;
pub use http::{Method, Request, Response, StatusCode}; use http::{Method, Request, Response, StatusCode};
use http_body::Body; use http_body::Body;
use http_body_util::BodyExt; use http_body_util::BodyExt;
use prometheus_http_client::{AlertStatus, ExtractLabels}; use prometheus_http_client::{AlertStatus, ExtractLabels};
@@ -417,10 +417,18 @@ impl Gateway {
resp resp
} }
match (req.method(), req.uri().path()) { match req.uri().path() {
(&Method::GET, "/") => Ok(ok_resp()), "/" | "/health" | "/ready" => {
(&Method::GET, "/health") => Ok(ok_resp()), if !matches!(req.method(), Method::GET | Method::HEAD) {
(&Method::POST, "/alert") => { Ok(err_resp(StatusCode::UNIMPLEMENTED, "Use GET or HEAD with this route"))
} else {
Ok(ok_resp())
}
},
"/alert" => {
if !matches!(req.method(), Method::POST) {
return Ok(err_resp(StatusCode::UNIMPLEMENTED, "Use POST with this route"));
}
let v = req let v = req
.into_body() .into_body()
.collect() .collect()
@@ -434,7 +442,7 @@ impl Gateway {
} else { } else {
Ok(ok_resp()) Ok(ok_resp())
} }
} },
_ => Ok(err_resp( _ => Ok(err_resp(
StatusCode::NOT_FOUND, StatusCode::NOT_FOUND,
format!("Not found '{} {}'", req.method(), req.uri().path()), format!("Not found '{} {}'", req.method(), req.uri().path()),
@@ -473,7 +481,7 @@ impl Gateway {
let prometheus = self let prometheus = self
.prometheus .prometheus
.as_ref() .as_ref()
.ok_or_else(|| (500, "prometheus was not configured".into()))?; .ok_or_else(|| (501, "prometheus was not configured".into()))?;
match prometheus.oneoff_query(query).await { match prometheus.oneoff_query(query).await {
Ok(( Ok((
@@ -509,7 +517,7 @@ impl Gateway {
let prometheus = self let prometheus = self
.prometheus .prometheus
.as_ref() .as_ref()
.ok_or_else(|| (500, "prometheus was not configured".into()))?; .ok_or_else(|| (501, "prometheus was not configured".into()))?;
prometheus.purge_old_plots(); prometheus.purge_old_plots();
match prometheus.create_oneoff_plot(query.clone(), duration).await { match prometheus.create_oneoff_plot(query.clone(), duration).await {
@@ -519,13 +527,13 @@ impl Gateway {
} }
#[cfg(not(feature = "plot"))] #[cfg(not(feature = "plot"))]
GatewayCommand::Plot { .. } => { GatewayCommand::Plot { .. } => {
Err((500, "the plot feature was not enabled at build time".into())) Err((501, "the plot feature was not enabled at build time".into()))
} }
GatewayCommand::Series { matchers } => { GatewayCommand::Series { matchers } => {
let prometheus = self let prometheus = self
.prometheus .prometheus
.as_ref() .as_ref()
.ok_or_else(|| (500, "prometheus was not configured".into()))?; .ok_or_else(|| (501, "prometheus was not configured".into()))?;
let matcher_refs: Vec<&str> = matchers.iter().map(|s| s.as_str()).collect(); let matcher_refs: Vec<&str> = matchers.iter().map(|s| s.as_str()).collect();
match prometheus.series(&matcher_refs).await { match prometheus.series(&matcher_refs).await {
@@ -546,7 +554,7 @@ impl Gateway {
let prometheus = self let prometheus = self
.prometheus .prometheus
.as_ref() .as_ref()
.ok_or_else(|| (500, "prometheus was not configured".into()))?; .ok_or_else(|| (501, "prometheus was not configured".into()))?;
let matcher_refs: Vec<&str> = matchers.iter().map(|s| s.as_str()).collect(); let matcher_refs: Vec<&str> = matchers.iter().map(|s| s.as_str()).collect();
match prometheus.labels(&matcher_refs).await { match prometheus.labels(&matcher_refs).await {
@@ -567,7 +575,7 @@ impl Gateway {
let prometheus = self let prometheus = self
.prometheus .prometheus
.as_ref() .as_ref()
.ok_or_else(|| (500, "prometheus was not configured".into()))?; .ok_or_else(|| (501, "prometheus was not configured".into()))?;
match prometheus.alerts().await { match prometheus.alerts().await {
Ok(data) => { Ok(data) => {
+2
View File
@@ -399,6 +399,7 @@ pub trait Rpc {
fn version(&self) -> Result<Value, ErrorObjectOwned>; fn version(&self) -> Result<Value, ErrorObjectOwned>;
} }
#[allow(unused)]
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct JsonLink { pub struct JsonLink {
@@ -411,6 +412,7 @@ pub struct RecvMessage {
pub envelope: Envelope, pub envelope: Envelope,
} }
#[allow(unused)]
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct Envelope { pub struct Envelope {
+5 -4
View File
@@ -1,8 +1,9 @@
pub mod alertmanager; pub mod alertmanager;
pub mod gateway; pub mod gateway;
pub mod human_duration;
pub mod jsonrpc; pub(crate) mod jsonrpc;
pub mod prometheus; pub(crate) mod human_duration;
pub mod transports; pub(crate) mod prometheus;
pub(crate) mod transports;
pub use gateway::{Gateway, GatewayConfig}; pub use gateway::{Gateway, GatewayConfig};