Installation
composer require inspector-apm/inspector-php
Add the service provider to config/app.php:
'providers' => [
InspectorApm\InspectorApmServiceProvider::class,
],
Configuration Publish the config file:
php artisan vendor:publish --provider="InspectorApm\InspectorApmServiceProvider" --tag="config"
Update config/inspector-apm.php with your API key and application name.
First Use Case
Enable auto-instrumentation in config/inspector-apm.php:
'auto_instrument' => true,
Restart your Laravel app. The package will now automatically track:
Verify in the Inspector Dashboard under Services > Your App.
Manual Instrumentation Explicitly track custom spans (e.g., third-party APIs, business logic):
use InspectorApm\Facades\Inspector;
Inspector::startSpan('custom_operation', function () {
// Your code here
sleep(1); // Simulate work
});
Middleware Integration Add custom metadata to requests:
use InspectorApm\Facades\Inspector;
public function handle($request, Closure $next)
{
Inspector::setAttribute('user_id', auth()->id());
return $next($request);
}
Database Query Tagging Tag specific queries for debugging:
DB::table('users')->where('active', 1)->get(['id', 'name'])
->each(function ($user) {
Inspector::addSpan('user_processing', function () use ($user) {
// Process user
});
});
Queue Job Monitoring Track job execution time and payload:
public function handle()
{
Inspector::setAttribute('job_payload', $this->payload);
// Job logic
}
Error Tracking Correlate errors with requests:
try {
// Risky operation
} catch (\Exception $e) {
Inspector::recordException($e);
throw $e;
}
Route Group Tagging Use middleware to tag entire route groups:
Route::middleware(['tag:api.v1'])->group(function () {
// Routes here
});
Service Container Binding
Inject Inspector into services:
public function __construct(private Inspector $inspector) {}
Event Listeners Track async events:
public function handle(UserRegistered $event)
{
$this->inspector->startSpan('user_registration', function () use ($event) {
// Sync logic
});
}
Auto-Instrumentation Overhead
'auto_instrument' => env('APP_ENV') !== 'local',
'ignored_routes' => ['/health', '/debug*'],
Sampling Rate
'sampling_rate' => 0.1, // 10% of transactions
Database Query Sampling
'db_sampling_rate' => 0.05,
Middleware Order
InspectorApmMiddleware after auth/session middleware to avoid missing metadata.Queue Workers
php artisan queue:work --env=production
php artisan inspector:status
php artisan config:clear
'log_level' => \Monolog\Logger::DEBUG,
Custom Spans Extend with context:
Inspector::startSpan('payment_processing', function () {
Inspector::setAttribute('amount', $amount);
Inspector::setAttribute('currency', $currency);
});
Span Events Add sub-events to spans:
$span = Inspector::startSpan('order_fulfillment');
$span->addEvent('inventory_check', ['status' => 'success']);
Resource Tracking Monitor external resources (e.g., HTTP clients):
$span = Inspector::startSpan('external_api_call');
try {
$response = Http::withOptions(['trace' => true])->get('...');
} finally {
$span->end();
}
Custom Metrics Track business metrics:
Inspector::incrementMetric('orders.processed');
Inspector::setMetric('revenue', $amount);
Error Grouping Group similar errors by type:
Inspector::recordException($e, ['group' => 'payment_failures']);
How can I help you explore Laravel packages today?