This project shows how to build lightweight query performance monitoring for a Laravel app using MongoDB.
It captures MongoDB command-level metrics for each request and stores them in a performance_logs collection so you can analyze slow operations and route-level database behavior.
- Tracks MongoDB query duration per operation
- Captures operation type (
find,insert,update, etc.) - Captures target collection
- Captures request duration and route
- Flags slow queries using a configurable threshold in code
- Stores all metrics in MongoDB (
performance_logs)
- PHP 8.2+
- Laravel 12
mongodb/laravel-mongodb- MongoDB Atlas or local MongoDB
git clone <your-repo-url>
cd devrel-tutorial-customMiddlewareForQueryPerformance-laravelnews
composer install
npm installCreate .env if needed:
cp .env.example .env
php artisan key:generateSet MongoDB values in .env:
DB_CONNECTION=mongodb
MONGODB_URI="your-mongodb-uri"
MONGODB_DATABASE="your-database-name"php artisan db:seedphp artisan serveVisit:
http://127.0.0.1:8000/posts
This triggers MongoDB queries and writes monitoring documents into performance_logs.
Use Laravel Tinker:
php artisan tinkerThen run:
App\Models\PerformanceLog::latest('created_at')->take(5)->get();You should see fields like:
routecollectionoperationduration_msrequest_durationis_slowcreated_at
For the full step-by-step implementation and architecture walkthrough, read the article:
If this helped you, please give the repository a star. It helps others discover the project and supports future improvements.
This project is open-source and available under the MIT License.