moniof โ Monitor Over Fetch

moniof (Monitor Over Fetch) is an Actix Web middleware + instrumentation crate to:
- detect N+1 / over-fetch patterns
- track per-request DB latency
- expose Prometheus metrics
- send Slack alerts
- work with MongoDB and SQLx (Postgres / MySQL / SQLite)
Inspired by Ruby's bullet gem โ but built for Rust + Actix.
โจ Features
- ๐งฑ Actix middleware (
MoniOF) - ๐ต๏ธ N+1 & Over-Fetch detection
- ๐ก MongoDB instrumentation (command events)
- ๐งฎ SQLx instrumentation (via tracing spans)
- ๐ Prometheus metrics
- ๐ Slack alerts for slow DB calls
- ๐งพ Auto response headers:
x-moniof-totalx-moniof-db-total-msx-moniof-elapsed-msx-moniof-slowest-keyx-moniof-n-plus-one-key
๐ Installation
Add to your app's Cargo.toml:
[]
= { = "0.1.0", = ["mongodb", "sqlx"] }
= "4"
= "0.1"
= { = "0.3", = ["env-filter", "fmt", "registry"] }
= "2"
= { = "0.8", = ["postgres", "runtime-tokio-rustls"] }
๐งฉ Public API
pub use ;
pub use MoniOFConfig;
pub use MoniOF;
pub use prom;
pub use MOFMongoEvents;
pub use MOFSqlEvents;
๐ Step 1 โ Initialize moniof globally
Call once in main():
use ;
This installs:
- tracing subscriber
- SQLx instrumentation (if feature enabled)
- log filter for moniof
๐ Step 2 โ Add Actix Middleware
use MoniOF;
new
Now each request produces:
- DB stats
- detection of N+1
- enriched response headers
๐ MongoDB Integration
Attach moniof's MongoDB handler:
use MOFMongoEvents;
use Arc;
use ;
let mut opts = parse.await?;
opts.command_event_handler = Some;
let client = with_options?;
let db = client.database;
Every find, insert, update is tracked.
๐งฎ SQLx Integration
If your crate enables:
= ["sqlx"]
Then SQLx logs are automatically hooked.
Use SQLx normally:
let rows = query!
.fetch_all
.await?;
moniof will record:
- query count
- query latency
- over-fetch patterns
and log:
moniof::sql: SQL completed key="select from users" latency_ms=2
๐ Prometheus Metrics
Expose /metrics:
use prom;
new
Example metrics:
moniof_http_requests_total
moniof_http_request_duration_seconds
moniof_db_total_latency_seconds
moniof_mongo_command_duration_seconds
๐ Slack Alerts
Enabled when slack_webhook is set:
slack_webhook: Some
Alert types:
- Slow DB commands
- Mongo/SQLx failures
- N+1 detection
๐งช Example Response Headers
x-moniof-total: 5
x-moniof-db-total-ms: 12
x-moniof-elapsed-ms: 18
x-moniof-slowest-key: users/find
x-moniof-n-plus-one-key: users/find
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- Built with Actix Web - Fast, powerful web framework
Made with โค๏ธ by the [Srotas Space] (https://srotas.space/open-source)
Support
- Documentation: docs.rs/moniof
- Issues: GitHub Issues
- Discussions: GitHub Discussions