Repository navigation
Conversation
de41e8e to
216152e
Compare
216152e to
a67c828
Compare
| /// The value recorded for an HTTP method the conventions do not list as known. | ||
| pub const OTHER_METHOD: &str = "_OTHER"; |
There was a problem hiding this comment.
We could probably just not return the key if it is invalid.
And if invalid, the server shouldn't be handling it anyway, so it does seem useful in telemetry.
I will have to do out an example, but there is a nice way to handle Option, so that the value isn't None or Some("GET"), but rather not present at all, or GET.
There was a problem hiding this comment.
The reason this is here is because http.request.method is required according to the semantic conventions: https://opentelemetry.io/docs/specs/semconv/http/http-metrics/
| pub fn http_request_method(method: &Method) -> &'static str { | ||
| match *method { | ||
| Method::CONNECT => "CONNECT", | ||
| Method::DELETE => "DELETE", | ||
| Method::GET => "GET", | ||
| Method::HEAD => "HEAD", | ||
| Method::OPTIONS => "OPTIONS", | ||
| Method::PATCH => "PATCH", | ||
| Method::POST => "POST", | ||
| Method::PUT => "PUT", | ||
| Method::TRACE => "TRACE", | ||
| _ if method.as_str() == "QUERY" => "QUERY", | ||
| _ => OTHER_METHOD, | ||
| } | ||
| } |
There was a problem hiding this comment.
Is this not already implemented on http::Method (I believe axum re-exports the http crate)? Might be gated behind a feature.
Just curious.
There was a problem hiding this comment.
No. I asked the same question because I couldn't really believe it and probably should have left a comment here (and I have a feeling we had the same discussion before in the company...)
https://docs.rs/http/latest/http/method/struct.Method.html
It only has as_str but that a) borrows from Method and b) doesn't support _OTHER
If you find one that both me and Claude missed I'd be happy. This feels dirty.
| pub fn network_protocol_version(version: Version) -> Option<&'static str> { | ||
| match version { | ||
| Version::HTTP_09 => Some("0.9"), | ||
| Version::HTTP_10 => Some("1.0"), | ||
| Version::HTTP_11 => Some("1.1"), | ||
| Version::HTTP_2 => Some("2"), | ||
| Version::HTTP_3 => Some("3"), | ||
| _ => None, | ||
| } | ||
| } |
There was a problem hiding this comment.
Same here (about this being implemented on http::Version)
There was a problem hiding this comment.
Version has a different problem than Method but also nothing usable.
It only has Debug which I wouldn't rely on even though it's probably stable https://docs.rs/http/latest/http/version/struct.Version.html - that said it'd also return the wrong string: https://docs.rs/http/latest/src/http/version.rs.html#62-75
| /// Returns the `http.route` value: the route template axum matched, never the raw path. | ||
| /// | ||
| /// Axum inserts [`MatchedPath`] while routing, so it is only present for a layer added through | ||
| /// `Router::layer` or `Router::route_layer`, and only for requests a route matched. A layer that | ||
| /// wraps the whole `Router` as a service, and the router's fallback, see no route. | ||
| pub fn http_route(request: &Request) -> Option<&str> { | ||
| request | ||
| .extensions() | ||
| .get::<MatchedPath>() | ||
| .map(MatchedPath::as_str) | ||
| } |
There was a problem hiding this comment.
We might want make some traits for extending these types. So for now, we should maybe make this (and others) pub(crate) so we don't break the API straight away if we do.
There was a problem hiding this comment.
Agreed. I didn't even think about that.
| /// `http.request.method`, see [`http_request_method`]. | ||
| pub method: &'static str, | ||
|
|
||
| /// `url.scheme`. | ||
| pub scheme: &'static str, | ||
|
|
||
| /// `http.route`, see [`http_route`]. | ||
| pub route: Option<String>, | ||
|
|
||
| /// `network.protocol.version`, see [`network_protocol_version`]. | ||
| pub protocol_version: Option<&'static str>, |
There was a problem hiding this comment.
Depending on what guarantees we want to give with this public type, we might want to keep the fields private (to prevent invalid construction/mutation).
Eg: If I had a ServerRequestAttributes, I (or any user of this crate) could change it in a way that's hard to guard against, making the construction checks ineffective.
let request: Request = ...; // some request that I get
let sra = ServerRequestAttributes::from_request(&request);
// At this point, we have a valid SRA.
// But now I can create an invalid state:
let mut sra = sra;
sra.protocol_version = Some("https://www.youtube.com/watch?v=dQw4w9WgXcQ"); // not a valid protocol version.So that then means all usages of it have to then verify (we want encouraging Parse-don't-verify).
There was a problem hiding this comment.
Agreed, done in fe0d2c4 (this PR) by removing all the pubs and creating methods instead.
Total side question: There is no such concept as "properties" in Rust, right? Or being discussed where these kinds of accessor methods are created automatically?
NickLarsenNZ
left a comment
There was a problem hiding this comment.
Another partial review. I will really try and get onto this on Monday.
I'll mainly try to cover the public API of the types, and whether we are allowing invalid states. The rest would be optional nit-picks (we can easily change private internals).
|
This is really and honestly not urgent. But thank you for the review. |

Description
This builds on #1332 to amend the
AxumTraceLayerto also record thehttp.server.request.durationas a metric. It uses the HTTP metric semantic conventions including the bucket sizes. Spans and metrics now reuse the same method, scheme, route and protocol.Breaking, span output and API:
_OTHERwith the original inhttp.request.method_original, and the span name starts withHTTP, as the span conventions require. Before this change, arbitrary client-sent methods became span names (not good).url.schemeis the scheme set withwith_url_schemeinstead of the request URI's scheme, which is empty for the usual origin-form request.network.protocol.versionis recorded.All other span attributes are unchanged.
Definition of Done Checklist
Reviewer
Acceptance