Skip to content

feat: add per-user write byte statistics#27547

Merged
davidby-influx merged 1 commit into
master-1.xfrom
DSB/write_stats
Jul 20, 2026
Merged

feat: add per-user write byte statistics#27547
davidby-influx merged 1 commit into
master-1.xfrom
DSB/write_stats

Conversation

@davidby-influx

Copy link
Copy Markdown
Contributor

Add a userwritebytes statistic, the write-path mirror of the shipped userquerybytes measurement, gated behind a new opt-in [http] user-write-bytes-enabled flag. When enabled, every v1, v2, and Prometheus remote write adds its request body bytes to a per-user counter, reported as one userwritebytes statistic per user, tagged with the user name, and visible through SHOW STATS and /debug/vars (admin-gated only when pprof-auth-enabled is set; otherwise /debug/vars is unauthenticated). Unauthenticated writes are attributed to "(anonymous)". Bytes are counted at the same sites that feed the aggregate writeReqBytes field of the httpd statistic, so the per-user values sum exactly to the global counter. Behavior is unchanged by default.

The counters inherit the existing writeReqBytes units: /write counts post-gzip decompressed bytes while the Prometheus endpoint counts compressed wire bytes, and tests pin both so changing that inconsistency becomes a conscious decision. New tests cover the sum invariant, anonymous attribution, concurrent writes under the race detector, early-error paths that must count nothing, and /debug/vars visibility under both pprof-auth modes (admin-gated and the unauthenticated default). The test harness gains a WriteAuthorizer mock, previously declared but never wired, making authenticated write paths testable for the first time. The per-user statistic emission in Handler.Statistics is extracted into a helper shared with userquerybytes, and both flags are now documented in the sample config.

This also fixes a pre-existing gap where gzip request bodies bypassed the MaxBodySize limit: truncateReader now wraps the decompressed stream, so an oversized gzip body is rejected with 413 rather than buffered in full.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR adds opt-in per-user write request byte accounting to the httpd service (mirroring existing per-user query response byte tracking), and fixes MaxBodySize enforcement for gzip-encoded write bodies by applying truncation after decompression.

Changes:

  • Add per-user userwritebytes statistics (tagged by user, with unauthenticated attributed to (anonymous)), gated by user-write-bytes-enabled.
  • Ensure per-user write counters are incremented at the same points as the global writeReqBytes counter, preserving the “sum of per-user equals global” invariant.
  • Fix MaxBodySize for gzip requests by wrapping the decoded stream with truncateReader, preventing decompression bombs from bypassing the limit; add extensive tests covering behavior and /debug/vars visibility.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
services/httpd/service.go Adds the userWriteReqBytes value field constant for per-user write stats.
services/httpd/handler.go Emits per-user userwritebytes stats and accounts write bytes per user; applies MaxBodySize after gzip decode.
services/httpd/handler_test.go Adds comprehensive tests for per-user write byte accounting, gzip sizing behavior, concurrency, and /debug/vars exposure.
services/httpd/config.go Introduces the user-write-bytes-enabled config flag.
etc/config.sample.toml Documents the new user-write-bytes-enabled flag alongside the existing query counterpart.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread services/httpd/handler_test.go
@davidby-influx
davidby-influx marked this pull request as ready for review July 17, 2026 16:16
@davidby-influx

Copy link
Copy Markdown
Contributor Author

devanbenz
devanbenz previously approved these changes Jul 20, 2026

@devanbenz devanbenz left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM

Add a userwritebytes statistic, the write-path mirror of the shipped
userquerybytes measurement, gated behind a new opt-in [http]
user-write-bytes-enabled flag. When enabled, every v1, v2, and Prometheus
remote write adds its request body bytes to a per-user counter, reported as
one userwritebytes statistic per user, tagged with the user name, and visible
through SHOW STATS and /debug/vars (admin-gated only when pprof-auth-enabled
is set; otherwise /debug/vars is unauthenticated). Unauthenticated writes are
attributed to "(anonymous)". Bytes are counted at the same sites that feed
the aggregate writeReqBytes field of the httpd statistic, so the per-user
values sum exactly to the global counter. Behavior is unchanged by default.

The counters inherit the existing writeReqBytes units: /write counts post-gzip
decompressed bytes while the Prometheus endpoint counts compressed wire bytes,
and tests pin both so changing that inconsistency becomes a conscious
decision. New tests cover the sum invariant, anonymous attribution, concurrent
writes under the race detector, early-error paths that must count nothing, and
/debug/vars visibility under both pprof-auth modes (admin-gated and the
unauthenticated default). The test harness gains a WriteAuthorizer
mock, previously declared but never wired, making authenticated write paths
testable for the first time. The per-user statistic emission in
Handler.Statistics is extracted into a helper shared with userquerybytes, and
both flags are now documented in the sample config.

This also fixes a pre-existing gap where gzip request bodies bypassed the
MaxBodySize limit: truncateReader now wraps the decompressed stream, so an
oversized gzip body is rejected with 413 rather than buffered in full.

@devanbenz devanbenz left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM

@davidby-influx

Copy link
Copy Markdown
Contributor Author

rebased

@davidby-influx
davidby-influx merged commit 798e8bf into master-1.x Jul 20, 2026
9 checks passed
@davidby-influx
davidby-influx deleted the DSB/write_stats branch July 20, 2026 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants