-
Notifications
You must be signed in to change notification settings - Fork 0
chore: /metrics endpoint (hash distribution & rehash counters) #47
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,65 @@ | ||
| use crate::AppState; | ||
| use axum::{http::StatusCode, response::IntoResponse}; | ||
| use sqlx::PgPool; | ||
|
|
||
| // Produce Prometheus-style metrics text. | ||
| pub async fn metrics_handler( | ||
| axum::extract::State(state): axum::extract::State<AppState>, | ||
| ) -> impl IntoResponse { | ||
| let pool: &PgPool = &state.pool; | ||
| // Query hash distribution (best-effort) | ||
| let (b2a, b2b, b2y, a2id) = if let Ok(row) = sqlx::query( | ||
| "SELECT \ | ||
| COUNT(*) FILTER (WHERE password_hash LIKE '$2a$%') AS b2a,\ | ||
| COUNT(*) FILTER (WHERE password_hash LIKE '$2b$%') AS b2b,\ | ||
| COUNT(*) FILTER (WHERE password_hash LIKE '$2y$%') AS b2y,\ | ||
| COUNT(*) FILTER (WHERE password_hash LIKE '$argon2id$%') AS a2id\ | ||
| FROM users", | ||
| ) | ||
| .fetch_one(pool) | ||
| .await | ||
| { | ||
| use sqlx::Row; | ||
| ( | ||
| row.try_get::<i64, _>("b2a").unwrap_or(0), | ||
| row.try_get::<i64, _>("b2b").unwrap_or(0), | ||
| row.try_get::<i64, _>("b2y").unwrap_or(0), | ||
| row.try_get::<i64, _>("a2id").unwrap_or(0), | ||
| ) | ||
| } else { | ||
| (0, 0, 0, 0) | ||
| }; | ||
|
|
||
| let rehash_count = state.metrics.get_rehash_count(); | ||
| let mut buf = String::new(); | ||
| buf.push_str("# HELP jive_password_rehash_total Total successful bcrypt to argon2id password rehashes.\n"); | ||
| buf.push_str("# TYPE jive_password_rehash_total counter\n"); | ||
| buf.push_str(&format!("jive_password_rehash_total {}\n", rehash_count)); | ||
| buf.push_str("# HELP jive_password_hash_users Users by password hash algorithm variant.\n"); | ||
| buf.push_str("# TYPE jive_password_hash_users gauge\n"); | ||
| buf.push_str(&format!( | ||
| "jive_password_hash_users{{algo=\"bcrypt_2a\"}} {}\n", | ||
| b2a | ||
| )); | ||
| buf.push_str(&format!( | ||
| "jive_password_hash_users{{algo=\"bcrypt_2b\"}} {}\n", | ||
| b2b | ||
| )); | ||
| buf.push_str(&format!( | ||
| "jive_password_hash_users{{algo=\"bcrypt_2y\"}} {}\n", | ||
| b2y | ||
| )); | ||
| buf.push_str(&format!( | ||
| "jive_password_hash_users{{algo=\"argon2id\"}} {}\n", | ||
| a2id | ||
| )); | ||
|
|
||
| ( | ||
| StatusCode::OK, | ||
| [( | ||
| axum::http::header::CONTENT_TYPE, | ||
| "text/plain; version=0.0.4", | ||
| )], | ||
| buf, | ||
| ) | ||
| } | ||
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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| {"rustc_fingerprint":1863893085117187729,"outputs":{"18122065246313386177":{"success":true,"status":"","code":0,"stdout":"rustc 1.89.0 (29483883e 2025-08-04)\nbinary: rustc\ncommit-hash: 29483883eed69d5fb4db01964cdf2af4d86e9cb2\ncommit-date: 2025-08-04\nhost: aarch64-apple-darwin\nrelease: 1.89.0\nLLVM version: 20.1.7\n","stderr":""},"13007759520587589747":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/huazhou/.rustup/toolchains/stable-aarch64-apple-darwin\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"aarch64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"aes\"\ntarget_feature=\"crc\"\ntarget_feature=\"dit\"\ntarget_feature=\"dotprod\"\ntarget_feature=\"dpb\"\ntarget_feature=\"dpb2\"\ntarget_feature=\"fcma\"\ntarget_feature=\"fhm\"\ntarget_feature=\"flagm\"\ntarget_feature=\"fp16\"\ntarget_feature=\"frintts\"\ntarget_feature=\"jsconv\"\ntarget_feature=\"lor\"\ntarget_feature=\"lse\"\ntarget_feature=\"neon\"\ntarget_feature=\"paca\"\ntarget_feature=\"pacg\"\ntarget_feature=\"pan\"\ntarget_feature=\"pmuv3\"\ntarget_feature=\"ras\"\ntarget_feature=\"rcpc\"\ntarget_feature=\"rcpc2\"\ntarget_feature=\"rdm\"\ntarget_feature=\"sb\"\ntarget_feature=\"sha2\"\ntarget_feature=\"sha3\"\ntarget_feature=\"ssbs\"\ntarget_feature=\"vh\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n","stderr":""}},"successes":{}} | ||
| {"rustc_fingerprint":8876508001675379479,"outputs":{"17747080675513052775":{"success":true,"status":"","code":0,"stdout":"rustc 1.89.0 (29483883e 2025-08-04)\nbinary: rustc\ncommit-hash: 29483883eed69d5fb4db01964cdf2af4d86e9cb2\ncommit-date: 2025-08-04\nhost: aarch64-apple-darwin\nrelease: 1.89.0\nLLVM version: 20.1.7\n","stderr":""},"7971740275564407648":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/huazhou/.rustup/toolchains/stable-aarch64-apple-darwin\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"aarch64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"aes\"\ntarget_feature=\"crc\"\ntarget_feature=\"dit\"\ntarget_feature=\"dotprod\"\ntarget_feature=\"dpb\"\ntarget_feature=\"dpb2\"\ntarget_feature=\"fcma\"\ntarget_feature=\"fhm\"\ntarget_feature=\"flagm\"\ntarget_feature=\"fp16\"\ntarget_feature=\"frintts\"\ntarget_feature=\"jsconv\"\ntarget_feature=\"lor\"\ntarget_feature=\"lse\"\ntarget_feature=\"neon\"\ntarget_feature=\"paca\"\ntarget_feature=\"pacg\"\ntarget_feature=\"pan\"\ntarget_feature=\"pmuv3\"\ntarget_feature=\"ras\"\ntarget_feature=\"rcpc\"\ntarget_feature=\"rcpc2\"\ntarget_feature=\"rdm\"\ntarget_feature=\"sb\"\ntarget_feature=\"sha2\"\ntarget_feature=\"sha3\"\ntarget_feature=\"ssbs\"\ntarget_feature=\"vh\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n","stderr":""}},"successes":{}} |
Binary file not shown.
Oops, something went wrong.
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.
This block of code for querying the password hash distribution (lines 9-24) is identical to the one in the
health_checkfunction inmain.rs. Duplicating code makes maintenance harder and increases the chance of bugs (like an issue I've pointed out separately). Consider extracting this logic into a shared function, for example as a new method onAppStateor in a database utility module, to improve maintainability.