web_server.rs: Added bucket domain to observability.

This commit is contained in:
Jonathan Davies 2023-08-02 14:30:04 +01:00
parent a98855157b
commit 0501d722eb

View file

@ -105,18 +105,34 @@ impl WebServer {
req: Request<Body>, req: Request<Body>,
addr: SocketAddr, addr: SocketAddr,
) -> Result<Response<Body>, Infallible> { ) -> Result<Response<Body>, Infallible> {
let request_host_bucket = req
.headers()
.get(HOST)
.expect("No host header found")
.to_str()
.expect("Error converting host header to string")
.to_string();
if let Ok(forwarded_for_ip_addr) = if let Ok(forwarded_for_ip_addr) =
forwarded_headers::handle_forwarded_for_headers(req.headers()) forwarded_headers::handle_forwarded_for_headers(req.headers())
{ {
// uri() below has a preceding '/', so no space with host
info!( info!(
"{} (via {}) {} {}", "{} (via {}) {} {}{}",
forwarded_for_ip_addr, forwarded_for_ip_addr,
addr, addr,
req.method(), req.method(),
request_host_bucket,
req.uri() req.uri()
); );
} else { } else {
info!("{} {} {}", addr, req.method(), req.uri()); info!(
"{} {} {}{}",
addr,
req.method(),
request_host_bucket,
req.uri()
);
} }
// Lots of instrumentation // Lots of instrumentation
@ -125,12 +141,16 @@ impl WebServer {
.span_builder(format!("Web {} request", req.method())) .span_builder(format!("Web {} request", req.method()))
.with_trace_id(gen_trace_id()) .with_trace_id(gen_trace_id())
.with_attributes(vec![ .with_attributes(vec![
KeyValue::new("domain", format!("{}", request_host_bucket.to_string())),
KeyValue::new("method", format!("{}", req.method())), KeyValue::new("method", format!("{}", req.method())),
KeyValue::new("uri", req.uri().to_string()), KeyValue::new("uri", req.uri().to_string()),
]) ])
.start(&tracer); .start(&tracer);
let metrics_tags = &[KeyValue::new("method", req.method().to_string())]; let metrics_tags = &[
KeyValue::new("domain", request_host_bucket.to_string()),
KeyValue::new("method", req.method().to_string()),
];
// The actual handler // The actual handler
let res = self let res = self
@ -145,21 +165,29 @@ impl WebServer {
// Returning the result // Returning the result
match res { match res {
Ok(res) => { Ok(res) => {
debug!("{} {} {}", req.method(), res.status(), req.uri()); debug!(
"{} {} {}{}",
req.method(),
res.status(),
request_host_bucket,
req.uri()
);
Ok(res) Ok(res)
} }
Err(error) => { Err(error) => {
info!( info!(
"{} {} {} {}", "{} {} {}{} {}",
req.method(), req.method(),
error.http_status_code(), error.http_status_code(),
request_host_bucket,
req.uri(), req.uri(),
error error
); );
self.metrics.error_counter.add( self.metrics.error_counter.add(
1, 1,
&[ &[
metrics_tags[0].clone(), KeyValue::new("domain", request_host_bucket.to_string()),
KeyValue::new("method", req.method().to_string()),
KeyValue::new("status_code", error.http_status_code().to_string()), KeyValue::new("status_code", error.http_status_code().to_string()),
], ],
); );