Skip to content

feat(http_telemetry): add route, header and body fields to server telemetry - #987

Draft
Evgeny Fedorov (evgenyfedorov2) wants to merge 6 commits into
microsoft:mainfrom
evgenyfedorov2:u/efedorov/http-telemetry-pr2
Draft

Evgeny Fedorov (evgenyfedorov2) wants to merge 6 commits into
microsoft:mainfrom
evgenyfedorov2:u/efedorov/http-telemetry-pr2

Conversation

@evgenyfedorov2

@evgenyfedorov2 Evgeny Fedorov (evgenyfedorov2) commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

Summary

Adds route, header and body fields to the server telemetry of http_telemetry. The crate docs describe each field and option.

let layer = Telemetry::server(sink)
    .route_resolver(Arc::new(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"])?;
  • Route: with route_resolver, http.route holds the template of the route that matches the request, also as a metric dimension, and http.route.parameter.<name> holds each route parameter that has a data class.
  • Headers: http.request.header.<name> and http.response.header.<name> hold the configured headers, redacted with their data classes.
  • Bodies: http.request.body and http.response.body hold the start of a body whose Content-Type the configured media types cover, as text, not redacted. body_size_limit sets how many bytes are logged: 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.
  • The hyper_server example logs a route, a request header and JSON bodies.

A follow-up PR adds client telemetry for fetch.

Changes in other crates

  • routerama: adds the RouteTable trait, which lists the routes of a resolver and matches a request to one of them. 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. visit_fields moves from Event and DynEvent to the new VisitFields trait, which both extend, so a hand-written Event or DynEvent must also implement VisitFields.

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.
@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.0%. Comparing base (6230675) to head (37afc41).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #987      +/-   ##
==========================================
  Coverage   100.0%   100.0%              
==========================================
  Files         793      294     -499     
  Lines       67943    20901   -47042     
==========================================
- Hits        67943    20901   -47042     
Flag Coverage Δ
linux 100.0% <100.0%> (ø)
linux-arm 100.0% <100.0%> (ø)
scheduled ?
windows 100.0% <100.0%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant