actix_web_opentelemetry/middleware/route_formatter.rs
1//! # Route Formatter
2//!
3//! Format routes from paths.
4
5/// Interface for formatting routes from paths.
6///
7/// This crate will render the actix web [match pattern] by default. E.g. for
8/// path `/users/123/profile` the route for this span would be
9/// `/users/{id}/profile`.
10///
11/// [match pattern]: actix_web::HttpRequest::match_pattern
12///
13/// # Custom Formatter Examples
14///
15/// ```
16/// use actix_web_opentelemetry::RouteFormatter;
17///
18/// // A formatter to ensure all paths are reported as lowercase.
19/// #[derive(Debug)]
20/// struct MyLowercaseFormatter;
21///
22/// impl RouteFormatter for MyLowercaseFormatter {
23/// fn format(&self, path: &str) -> String {
24/// path.to_lowercase()
25/// }
26/// }
27///
28/// // now a match with pattern `/USERS/{id}` would be recorded as `/users/{id}`
29/// ```
30pub trait RouteFormatter: std::fmt::Debug {
31 /// Function from path to route.
32 /// e.g. /users/123 -> /users/:id
33 fn format(&self, path: &str) -> String;
34}