Compare commits

...

4 Commits

Author SHA1 Message Date
Dmitry Rodionov
39fbac354f WIP 2023-02-27 14:38:46 +02:00
Shany Pozin
84a8089ae7 cr feedback 2023-02-27 13:00:13 +02:00
Shany Pozin
6ce1638df2 Add tracing for incoming request 2023-02-27 12:38:56 +02:00
Shany Pozin
cf4965f95b Add UUID header to mgmt API 2023-02-27 12:24:15 +02:00
4 changed files with 63 additions and 3 deletions

1
Cargo.lock generated
View File

@@ -4544,6 +4544,7 @@ dependencies = [
"tracing",
"tracing-subscriber",
"url",
"uuid",
"workspace_hack",
]

View File

@@ -39,7 +39,7 @@ pq_proto.workspace = true
workspace_hack.workspace = true
url.workspace = true
uuid = { version = "1.2", features = ["v4", "serde"] }
[dev-dependencies]
byteorder.workspace = true
bytes.workspace = true

View File

@@ -63,8 +63,26 @@ async fn prometheus_metrics_handler(_req: Request<Body>) -> Result<Response<Body
Ok(response)
}
pub fn add_request_id_middleware<B: hyper::body::HttpBody + Send + Sync + 'static>(
) -> Middleware<B, ApiError> {
Middleware::pre(move |mut req| async move {
let headers = req.headers_mut();
let name = HeaderName::from_str("UUID").expect("created header name");
let request_id = uuid::Uuid::new_v4().to_string();
let value = HeaderValue::from_str(&request_id).unwrap();
headers.insert(name, value);
if req.method() == Method::GET {
tracing::debug!("{} {} {}", req.method(), req.uri().path(), request_id);
} else {
tracing::info!("{} {} {}", req.method(), req.uri().path(), request_id);
}
Ok(req)
})
}
pub fn make_router() -> RouterBuilder<hyper::Body, ApiError> {
Router::builder()
.middleware(add_request_id_middleware())
.middleware(Middleware::post_with_info(logger))
.get("/metrics", prometheus_metrics_handler)
.err_handler(error::handler)

View File

@@ -1,9 +1,14 @@
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::Arc;
use anyhow::{anyhow, Context, Result};
use hyper::StatusCode;
use futures::Future;
use hyper::body::HttpBody;
use hyper::header::HeaderName;
use hyper::http::HeaderValue;
use hyper::{Body, Request, Response, Uri};
use hyper::{Method, StatusCode};
use metrics::launch_timestamp::LaunchTimestamp;
use pageserver_api::models::DownloadRemoteLayersTaskSpawnRequest;
use remote_storage::GenericRemoteStorage;
@@ -1157,6 +1162,42 @@ pub fn make_router(
"/v1/tenant/:tenant_id/timeline/:timeline_id/layer/:layer_file_name",
evict_timeline_layer_handler,
)
.get("/v1/panic", always_panic_handler)
.get(
"/v1/panic",
wrap_span("always_panic_handler", always_panic_handler),
)
.any(handler_404))
}
fn wrap_span<H, R, B, E>(
handler_name: &'static str,
handler: H,
) -> impl Fn(Request<hyper::Body>) -> R
where
H: Fn(Request<hyper::Body>) -> R + Send + Sync + 'static,
B: HttpBody + Send + Sync + 'static,
E: Into<Box<dyn std::error::Error + Send + Sync>>,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
{
move |r| -> R {
async {
let headers = r.headers_mut();
let name = HeaderName::from_str("UUID").expect("created header name");
let request_id = "foo";
let value = HeaderValue::from_str(&request_id).unwrap();
headers.insert(name, value);
if r.method() == Method::GET {
tracing::debug!("{} {} {}", r.method(), r.uri().path(), request_id);
} else {
tracing::info!("{} {} {}", r.method(), r.uri().path(), request_id);
}
handler(r)
.instrument(info_span!(
"request",
handler = handler_name,
request_id = request_id
))
.await
}
}
}