laravel maintained by monitorkit
MonitorKit for Laravel
APM + infrastructure monitoring for Laravel applications. Zero configuration, automatic tracing.
Compatible with: Laravel 6, 7, 8, 9, 10, 11 · PHP 7.4, 8.0, 8.1, 8.2, 8.3
What it traces automatically
| What | How it appears in MonitorKit |
|---|---|
| Every HTTP request | Root trace with method, path, status, duration |
| Eloquent / DB queries | Child spans with SQL, connection, duration |
| Queue jobs | Standalone trace per job with success/failure |
| Exceptions and errors | Trace marked as error with message |
Installation
composer require monitorkit/laravel
Laravel auto-discovers the service provider — no manual registration needed.
Publish the config (optional)
php artisan vendor:publish --tag=monitorkit-config
Add to .env
MONITORKIT_SERVER=https://app.monitorkit.co
MONITORKIT_KEY=mk_your_agent_key_here
MONITORKIT_SERVICE=my-laravel-app
MONITORKIT_ENABLED=true
Get your agent key from MonitorKit dashboard → Settings → Agent Keys → New Key.
That's it. All requests are now traced automatically.
Configuration
All options can be set via environment variables or by publishing config/monitorkit.php.
| Variable | Default | Description |
|---|---|---|
MONITORKIT_SERVER |
— | Your MonitorKit server URL (required) |
MONITORKIT_KEY |
— | Agent API key (required) |
MONITORKIT_SERVICE |
APP_NAME |
Service name shown in traces |
MONITORKIT_ENABLED |
true |
Set to false to disable in local/CI |
MONITORKIT_SAMPLE_RATE |
0.2 |
Fraction of requests to trace (0.1 = 10%). Raise only for low-traffic services or short debug windows. |
MONITORKIT_SLOW_QUERY_MS |
0 |
Only trace queries slower than N ms (0 = all) |
MONITORKIT_MAX_SPANS |
200 |
Max spans per trace (prevents huge payloads) |
MONITORKIT_TIMEOUT |
2 |
HTTP timeout for sending traces (seconds) |
Manual instrumentation
For code sections not traced automatically:
use MonitorKit\Laravel\MonitorKit;
// Start a span, do work, finish it
$span = MonitorKit::startSpan('payment.charge', 'custom');
$result = $stripe->charges->create([...]);
MonitorKit::finishSpan($span);
// Wrap a callable (auto-finishes even on exception)
$result = MonitorKit::trace('redis.get', function () use ($key) {
return Redis::get($key);
}, 'cache');
// Mark as error
try {
// ...
} catch (\Exception $e) {
MonitorKit::finishSpan($span, true, $e->getMessage());
throw $e;
}
Excluding routes from tracing
Add patterns to config/monitorkit.php:
'exclude_paths' => [
'#^/health$#',
'#^/admin/debug#',
'#\.(js|css|png)$#i',
],
Local development tips
Disable in local env:
MONITORKIT_ENABLED=false
Trace only 10% of requests in production (high traffic):
MONITORKIT_SAMPLE_RATE=0.1
Only trace slow queries (> 50ms):
MONITORKIT_SLOW_QUERY_MS=50
Testing with a local MonitorKit server
If you're running MonitorKit locally:
MONITORKIT_SERVER=http://localhost:8000
MONITORKIT_KEY=your-local-key
How it works
The service provider:
- Pushes
MonitorKitMiddlewareas a global middleware - Registers a
QueryExecutedlistener for DB spans - Registers a
MessageLoggedlistener to detect errors - Registers queue job listeners for job tracing
MonitorKitMiddleware::terminate() fires after the response is sent to the client, so sending the trace adds zero latency to your HTTP response time.
Support
- Documentation: https://monitorkit.co/guide
- Issues: https://github.com/gestiondesarrolloinnovacion/monitorkit-laravel/issues
- Email: support@monitorkit.co