Skip to content

Record size of streaming request/response bodies - #4823

Merged
divergentdave merged 3 commits into
mainfrom
david/streaming-body-size-metrics
Aug 25, 2026
Merged

Record size of streaming request/response bodies#4823
divergentdave merged 3 commits into
mainfrom
david/streaming-body-size-metrics

Conversation

@divergentdave

@divergentdave divergentdave commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

Closes #4822. This changes our metrics middleware to wrap request and response bodies, in order to accurately measure request and response sizes when a Content-Length header is not available.

@divergentdave
divergentdave marked this pull request as ready for review August 24, 2026 22:43
@divergentdave
divergentdave requested a review from a team as a code owner August 24, 2026 22:43

@jcjones jcjones left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fun.

Comment thread aggregator_core/src/http_server.rs Outdated
.build(),
request_body_size: meter
.f64_histogram("http.server.request.body_size")
.u64_histogram("http.server.request.body_size")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

............. why was this ever a float? lol

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I just noticed there's a slight inconsistency in the metric name versus https://opentelemetry.io/docs/specs/semconv/http/http-metrics/#metric-httpserverrequestbodysize

///
/// This method keeps track of whether an observation has been recorded, so it is safe to call
/// multiple times.
fn record_metric(&mut self) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of tracking a state in a boolean, would it be possible for this method to take self by value? Perhaps that's impossible given how http_body::Body works?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both call sites only have mutable access to self. I call this from both poll_frame() and from drop() to guard against the possibility that whatever is consuming the stream calls is_end_stream(), gets true, and drops the body without calling poll_frame() a final time to get None. Plus, it's also possible the consumer could call poll_frame() again after getting None.

@divergentdave
divergentdave merged commit 8713b84 into main Aug 25, 2026
8 checks passed
@divergentdave
divergentdave deleted the david/streaming-body-size-metrics branch August 25, 2026 16:23
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.

Fix http.server.request.duration and http.server.response.duration with Transport-Encoding: chunked

3 participants