use actix_web::Error;
use actix_web::body::MessageBody;
use actix_web::dev::{ServiceRequest, ServiceResponse};
use tracing::Span;
use tracing_actix_web::{DefaultRootSpanBuilder, Level, RootSpanBuilder, root_span};
pub struct AvisoRootSpanBuilder;
impl RootSpanBuilder for AvisoRootSpanBuilder {
fn on_request_start(request: &ServiceRequest) -> Span {
if is_infrastructure_path(request.uri().path()) {
root_span!(level = Level::DEBUG, request)
} else {
root_span!(request)
}
}
fn on_request_end<B: MessageBody>(span: Span, outcome: &Result<ServiceResponse<B>, Error>) {
DefaultRootSpanBuilder::on_request_end(span, outcome);
}
}
fn is_infrastructure_path(path: &str) -> bool {
path == "/health"
|| path == "/metrics"
|| path.starts_with("/static/")
|| path.starts_with("/swagger-ui/")
|| path.starts_with("/api-docs/")
}
#[cfg(test)]
mod tests {
use super::is_infrastructure_path;
#[test]
fn infrastructure_paths_are_quiet() {
assert!(is_infrastructure_path("/health"));
assert!(is_infrastructure_path("/metrics"));
assert!(is_infrastructure_path("/static/logo.png"));
assert!(is_infrastructure_path("/swagger-ui/"));
assert!(is_infrastructure_path("/swagger-ui/index.html"));
assert!(is_infrastructure_path("/api-docs/openapi.json"));
}
#[test]
fn business_paths_remain_loud() {
assert!(!is_infrastructure_path("/"));
assert!(!is_infrastructure_path("/api/v1/notification"));
assert!(!is_infrastructure_path("/api/v1/watch"));
assert!(!is_infrastructure_path("/api/v1/replay"));
assert!(!is_infrastructure_path("/api/v1/schema"));
assert!(!is_infrastructure_path("/api/v1/admin/wipe/all"));
}
#[test]
fn near_misses_remain_loud() {
assert!(!is_infrastructure_path("/healthcheck"));
assert!(!is_infrastructure_path("/healthz"));
assert!(!is_infrastructure_path("/metricsfoo"));
assert!(!is_infrastructure_path("/health/"));
}
}