-
Notifications
You must be signed in to change notification settings - Fork 96
feat: expose stub_status connection-state atomics via event::connecti… #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
u5surf
wants to merge
1
commit into
nginx:main
Choose a base branch
from
u5surf:feat/expose-ngx-stat-stub
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| //! Helpers around nginx's event-loop globals. | ||
|
|
||
| #[cfg(ngx_feature = "stat_stub")] | ||
| use crate::ffi::{ | ||
| ngx_atomic_t, ngx_stat_accepted, ngx_stat_active, ngx_stat_handled, ngx_stat_reading, | ||
| ngx_stat_requests, ngx_stat_waiting, ngx_stat_writing, | ||
| }; | ||
|
|
||
| /// One snapshot of nginx's global connection-state atomics — the | ||
| /// same values that the `ngx_http_stub_status_module` exposes. | ||
| /// | ||
| /// The seven atomics are read independently, so the snapshot is | ||
| /// not consistent across counters; for monitoring use cases the | ||
| /// drift between reads is sub-microsecond and irrelevant. | ||
| #[cfg(ngx_feature = "stat_stub")] | ||
| #[derive(Clone, Copy, Debug, Default)] | ||
| #[non_exhaustive] | ||
| pub struct ConnectionStats { | ||
| /// Connections currently in use. | ||
| pub active: u64, | ||
| /// Connections currently reading a request header. | ||
| pub reading: u64, | ||
| /// Connections currently writing a response. | ||
| pub writing: u64, | ||
| /// Connections currently idle in keep-alive. | ||
| pub waiting: u64, | ||
| /// Total connections accepted since process start. | ||
| pub accepted: u64, | ||
| /// Total connections handled since process start. | ||
| pub handled: u64, | ||
| /// Total requests served since process start. | ||
| pub requests: u64, | ||
| } | ||
|
|
||
| /// Sample the global `ngx_stat_*` connection counters. | ||
| /// | ||
| /// This is the programmatic equivalent of fetching the | ||
| /// `stub_status` response, suitable for plumbing into custom | ||
| /// exporters (Prometheus, statsd, etc.) without having to scrape | ||
| /// HTTP. Available only on nginx builds where | ||
| /// `ngx_http_stub_status_module` is compiled in. | ||
| #[cfg(ngx_feature = "stat_stub")] | ||
| pub fn connection_stats() -> ConnectionStats { | ||
| // SAFETY: the seven atomics are allocated and assigned by | ||
| // nginx core in the master process before workers fork, and | ||
| // are valid for the lifetime of the process. Each pointer is | ||
| // therefore stable to dereference once. | ||
| unsafe { | ||
| snapshot_from_ptrs( | ||
| ngx_stat_active, | ||
| ngx_stat_reading, | ||
| ngx_stat_writing, | ||
| ngx_stat_waiting, | ||
| ngx_stat_accepted, | ||
| ngx_stat_handled, | ||
| ngx_stat_requests, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| /// Read the seven atomics through the supplied pointers. Exists | ||
| /// as its own function so unit tests can exercise the field | ||
| /// mapping with stack-allocated atomics, without depending on the | ||
| /// global `ngx_stat_*` symbols being resolvable in the test | ||
| /// binary. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// Every pointer must be valid for a non-volatile read of | ||
| /// `ngx_atomic_t`. The caller is responsible for upholding that | ||
| /// (in production callers always pass nginx-owned globals, which | ||
| /// satisfy the requirement by construction). | ||
| #[cfg(ngx_feature = "stat_stub")] | ||
| #[allow(clippy::unnecessary_cast)] // `ngx_atomic_t` is `c_ulong`, which is 32-bit on some targets. | ||
| unsafe fn snapshot_from_ptrs( | ||
| active: *const ngx_atomic_t, | ||
| reading: *const ngx_atomic_t, | ||
| writing: *const ngx_atomic_t, | ||
| waiting: *const ngx_atomic_t, | ||
| accepted: *const ngx_atomic_t, | ||
| handled: *const ngx_atomic_t, | ||
| requests: *const ngx_atomic_t, | ||
| ) -> ConnectionStats { | ||
| unsafe { | ||
| ConnectionStats { | ||
| active: *active as u64, | ||
| reading: *reading as u64, | ||
| writing: *writing as u64, | ||
| waiting: *waiting as u64, | ||
| accepted: *accepted as u64, | ||
| handled: *handled as u64, | ||
| requests: *requests as u64, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(all(test, ngx_feature = "stat_stub"))] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn snapshot_from_ptrs_maps_each_field() { | ||
| // Use distinct values so a field-to-field swap is caught. | ||
| let active: ngx_atomic_t = 7; | ||
| let reading: ngx_atomic_t = 1; | ||
| let writing: ngx_atomic_t = 2; | ||
| let waiting: ngx_atomic_t = 4; | ||
| let accepted: ngx_atomic_t = 100; | ||
| let handled: ngx_atomic_t = 99; | ||
| let requests: ngx_atomic_t = 250; | ||
|
|
||
| let s = unsafe { | ||
| snapshot_from_ptrs( | ||
| &raw const active, | ||
| &raw const reading, | ||
| &raw const writing, | ||
| &raw const waiting, | ||
| &raw const accepted, | ||
| &raw const handled, | ||
| &raw const requests, | ||
| ) | ||
| }; | ||
|
|
||
| assert_eq!(s.active, 7); | ||
| assert_eq!(s.reading, 1); | ||
| assert_eq!(s.writing, 2); | ||
| assert_eq!(s.waiting, 4); | ||
| assert_eq!(s.accepted, 100); | ||
| assert_eq!(s.handled, 99); | ||
| assert_eq!(s.requests, 250); | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure this needs a feature gate, but ill defer to @bavshin-f5 or @ensh63 if either of them agree with having it.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@avahahn
Thanks for the review — ②③④ are addressed in the force-pushed commit (rebased onto main, squashed per CONTRIBUTING).
On the gate: it's needed, but not as a Cargo feature —
ngx_featureis the build-time cfgnginx-sysderives from nginx's ownngx_auto_config.h(NGX_STAT_STUB→cargo::rustc-cfg=ngx_feature="stat_stub"). nginx declares these externs under#if (NGX_STAT_STUB)(src/event/ngx_event.h:468), so without stub_status bindgen emits nothing and the module doesn't compile:(
cargo build -p ngx --features vendoredwith the cfg attributes removed.)Worth noting separately:
NGINX_CONFIGURE_BASEhas no--with-http_stub_status_module, so CI never compiles this path — I verified locally withNGX_CONFIGURE_ARGS="--with-http_stub_status_module"(tests + clippy pass with and without, nginx 1.30.4). Happy to add the flag here or leave it for a separate PR.@bavshin-f5 @ensh63 — flagging in case you see it differently.