You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat(http_telemetry)!: add route, header and body fields to server telemetry - #987
Route: with route_resolver, the middleware logs the template of the route that matches the request, such as /books/{book}, as http.route. http.route is also a dimension of the http.server.request.duration metric. The middleware logs the route parameters that you pass to route_parameter_data_classes as http.route.parameter.<name>, and redacts each value with the data class of its parameter.
Headers: the middleware logs the headers that you pass to request_header_data_classes and response_header_data_classes as http.request.header.<name> and http.response.header.<name>. It redacts each value with the data class of its header.
Bodies: with request_body_media_types and response_body_media_types, the middleware logs the start of request and response bodies as http.request.body.content and http.response.body.content. It logs a body whose Content-Type is one of these media types, any text type, or an application type that ends with +json or +xml. It does not log a body whose Content-Encoding names a coding other than identity. It logs the body as text and does not redact it. body_size_limit sets how many bytes of each body the middleware logs: 32 KiB by default, 1.5 MiB at most.
The middleware never reads a request body itself. It copies a streamed body while the handler reads it.
A follow-up PR adds client telemetry for fetch.
Changes in other crates
routerama: adds the RouteTable trait, which matches a request to a route of a resolver and gives the template of the route and the values of its parameters. Runtime resolvers and the resolvers that #[resolver] generates implement it.
observed (breaking): an #[event] field marked #[flatten] adds fields whose names are known only at run time, and EventDescription::contains_metrics also counts the metrics of those fields. visit_fields moves from Event to the new VisitFields trait, which Event extends, so a hand-written Event must also implement VisitFields, including its CONTAINS_METRICS constant.
Add the `RouteTable` trait and its `RouteHit` match result. A resolver lists
the template and the parameter names of each route, and matches a request to
the number of its route and the raw values of its parameters. `RawResolver`
implements it, so a `DynBuilder` resolver does too.
Every resolver that `#[resolver]` generates implements `RouteTable`. The
templates and the parameter names of its static routes are tables built into
the program. `match_route` uses a second copy of the matching code of
`resolve`, so both pick the same route. A snapshot test shows that the output
of `Generator::generate()` does not change.
An `#[event]` struct can get fields whose names are known only at run
time from a field marked `#[flatten]`. The type of that field implements
the new `VisitFields` trait.
`visit_fields` moves from `Event` and `DynEvent` to `VisitFields`, which
both traits now extend. A hand-written `Event` or `DynEvent` must also
implement `VisitFields`.
With a route resolver, the complete, error and abandoned events of the
server have `http.route`, the template of the matched route, and
`http.route.parameter.<name>` for each route parameter that has a data
class. The layer matches the request again with the resolver, only when
a processor wants the event.
The events also have the configured request and response headers as
`http.request.header.<name>` and `http.response.header.<name>`. Each
value is redacted according to the data class of its header.
With `response_body_media_types`, the complete, error and abandoned
events of the server have `http.response.body`, the start of the
response body as text, when the `Content-Type` of the response matches
one of the configured media types. A media type also covers its
structured suffixes, so `application/json` covers
`application/problem+json`. An entry that is not a media type returns
`MediaTypeError`.
`body_size_limit` sets how many bytes are logged, 32 KiB by default.
Bytes that are not valid UTF-8 become the replacement character, and
the body is logged unredacted.
✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.0%. Comparing base (6542ec1) to head (edde471). ⚠️ Report is 3 commits behind head on main.
With `request_body_media_types`, the complete, error and abandoned
events of the server have `http.request.body`, the start of the
request body as text. The middleware does not read a request body by
itself. It copies the bytes of a streamed body while the handler reads
them, as the HTTP logging middleware of ASP.NET Core does, so a body
that the handler does not read logs an empty text. A body that is
already in memory is copied when the request arrives.
Both lists of media types now cover a `Content-Type` by the rule of
`Microsoft.Extensions.Http.Diagnostics`: a type equal to one of the
entries, any `text` type, and an `application` type whose subtype ends
with `+json` or `+xml`. An empty list logs no body, and an entry whose
type or subtype is `*` returns `MediaTypeError`.
The workspace now needs `http-body-util` 0.1.4, which added
`inspect_frame`.
Evgeny Fedorov (evgenyfedorov2)
changed the title
feat(http_telemetry): add route, header and body fields to server telemetry
feat(http_telemetry)!: add route, header and body fields to server telemetry
Nov 18, 2026
The middleware no longer logs a body whose `Content-Encoding` names a
coding other than `identity`, because the text of compressed bytes
cannot be read. A layer placed before the server layer of
`http_compression`, as the crate docs advise, logged a gzip response
body as broken text. To log such bodies, put the layer after
`http_compression`.
Two new tests make a processor stop the visit of the event fields at a
route parameter and at a header, and check that no later field is
visited.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Events whose measurements come only from flattened fields are incorrectly marked as containing no metrics, causing metrics-only processors to silently discard those measurements.
This PR expands http_telemetry server events with route, header, and body fields. It adds supporting route lookup APIs in routerama and runtime-defined event fields in observed.
Changes:
Adds route dimensions, classified route parameters and headers, and size-limited body logging.
Introduces RouteTable for runtime and macro-generated resolvers.
Adds #[flatten] and moves field visitation into VisitFields, a breaking change for handwritten event implementations.
`RouteTable` now has only `match_route`. A `RouteHit` carries the
template and the parameter names of its route, and
`RouteHit::parameter` returns the value of a parameter by name. Route
numbers, the listing of routes and the generated table of templates
are gone.
`http_telemetry` builds the list of configured route parameters once
and asks each hit for their values, instead of preparing fields for
every route. A route of a `#[resolver]` type lends its template for
`'static`, so `http.route` still needs no copy.
Also fixes findings of a review against the repository rules:
`route_resolver` takes the resolver without an `Arc`, the interner
uses `parking_lot::Mutex` like the rest of the crate, and tests compare
values and panic payloads.
This compares each whole Content-Encoding field line to one coding. A value such as identity, identity contains no non-identity coding, but this check rejects it and omits the body field even when its media type matches. Parse the comma-separated members and trim whitespace before checking each coding, as crates/http_compression/src/compression.rs:999-1007 does. Continue to reject unreadable values and any non-identity coding.
`VisitFields` has a required `CONTAINS_METRICS` constant. For an event
with a `#[flatten]` field, `EventDescription::contains_metrics` now also
counts the constant of the field type. Before, an event whose only
measurements came from a flattened value reported no metrics, so a
processor that selects metric events never received them.
`DynEvent` declares `visit_fields` again, as it does on `main`, because
a trait with a constant cannot be used as `dyn DynEvent`. A foreign
event adaptor therefore needs no `VisitFields` impl.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Unresolved issues make flattened counter-only events advertise metrics without measurements and incorrectly suppress body logging for identity-only Content-Encoding values.
Comparing the entire Content-Encoding value with one token disables body logging for identity, identity or identity\t, even though neither names a non-identity coding. Split comma-separated values, trim whitespace, and ignore empty members, as the decoder does in crates/http_compression/src/compression.rs:994–1007. Continue rejecting any non-identity coding so encoded request and response bodies remain excluded.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Flattening a fieldless counter incorrectly marks an otherwise non-metric parent as containing metrics, causing metric-filtered processors to receive an event with no measurements.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Flattened counter-only events can reach metric processors without measurements, while valid identity-only Content-Encoding lists and whitespace variants incorrectly disable body logging.
The body fields are `http.request.body.content` and
`http.response.body.content`, the names that OpenTelemetry defines.
When the copy of a body reaches the size limit, its text leaves out an
incomplete character at its end instead of turning it into `�`,
because OpenTelemetry says to cut a body on a character boundary.
`http.route` of a route added at run time copies its template once
instead of twice.
The docs of the data class options say "Adds", because each call adds
to the pairs of the earlier calls. The docs of `observed::VisitFields`
say that a field name built at run time must stay in memory until the
process ends.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Flattened counter-only events incorrectly report metrics despite exposing no measurements, and Content-Encoding parsing suppresses body logging for valid identity-only lists or whitespace.
A flattened value adds only its fields to the outer event. The counter
of a flattened event itself is not one of them, yet it made
`CONTAINS_METRICS` true, so a processor that selects metric events got
an event with nothing to record. The constant now comes from the field
metrics of the type.
The docs of `VisitFields::visit_fields` say that it returns `Break` as
soon as the visitor does, and the docs of `#[flatten]` say that the
fields of a flattened `#[event]` reach the log only if it has a
severity attribute.
Each `Content-Encoding` line is split at commas and trimmed, and empty
items are ignored, as `http_compression` reads this header. Before,
`identity, identity` turned body logging off.
New tests check the route, a route parameter and the request and
response headers on the error, panic and abandoned events. The docs say
that the logs can contain personal data from the bodies, and that route
parameters are logged only if the layer has a resolver.
The reason will be displayed to describe this comment to others. Learn more.
🟢 Approval recommended
No active review issues are blocking approval.
No open issues 🎉
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds route, header and body fields to the server telemetry of
http_telemetry.const REQUEST_ID: HeaderName = HeaderName::from_static("x-request-id"); #[routerama::resolver] enum BookRoute<'p> { #[route(GET, "/books/{book}")] GetBook { book: &'p str }, #[route(GET, "/health")] Health, } let layer = Telemetry::server(sink) .clock(clock) .excluded_paths([BasePath::from_static("/health/")]) + .route_resolver(BookRoute::resolver()) + .route_parameter_data_classes([("book", PUBLIC)]) + .request_header_data_classes([(REQUEST_ID, PUBLIC)]) + .request_body_media_types(["application/json"])? + .response_body_media_types(["application/json"])?; let handler = Execute::new(move |_request: HttpRequest| { let response = HttpResponse::new(body_builder.empty()); async move { Ok::<_, HttpError>(response) } }); let service = (layer, handler).into_service();route_resolver, the middleware logs the template of the route that matches the request, such as/books/{book}, ashttp.route.http.routeis also a dimension of thehttp.server.request.durationmetric. The middleware logs the route parameters that you pass toroute_parameter_data_classesashttp.route.parameter.<name>, and redacts each value with the data class of its parameter.request_header_data_classesandresponse_header_data_classesashttp.request.header.<name>andhttp.response.header.<name>. It redacts each value with the data class of its header.request_body_media_typesandresponse_body_media_types, the middleware logs the start of request and response bodies ashttp.request.body.contentandhttp.response.body.content. It logs a body whoseContent-Typeis one of these media types, anytexttype, or anapplicationtype that ends with+jsonor+xml. It does not log a body whoseContent-Encodingnames a coding other thanidentity. It logs the body as text and does not redact it.body_size_limitsets how many bytes of each body the middleware logs: 32 KiB by default, 1.5 MiB at most.A follow-up PR adds client telemetry for
fetch.Changes in other crates
routerama: adds theRouteTabletrait, which matches a request to a route of a resolver and gives the template of the route and the values of its parameters. Runtime resolvers and the resolvers that#[resolver]generates implement it.observed(breaking): an#[event]field marked#[flatten]adds fields whose names are known only at run time, andEventDescription::contains_metricsalso counts the metrics of those fields.visit_fieldsmoves fromEventto the newVisitFieldstrait, whichEventextends, so a hand-writtenEventmust also implementVisitFields, including itsCONTAINS_METRICSconstant.