aeatech/snapshot-profiler-newrelic
Installation
composer require aeatech/snapshot-profiler-newrelic
Register the Service Provider
Add to config/app.php under providers:
Aeatech\SnapshotProfiler\NewRelic\NewRelicSnapshotProfilerServiceProvider::class,
Publish Configuration
php artisan vendor:publish --provider="Aeatech\SnapshotProfiler\NewRelic\NewRelicSnapshotProfilerServiceProvider"
This generates config/snapshot-profiler-newrelic.php.
Configure New Relic
Ensure your newrelic.ini is properly set up with your license key and app name.
Enable Profiling
Add middleware to app/Http/Kernel.php:
\Aeatech\SnapshotProfiler\NewRelic\Middleware\NewRelicSnapshotProfiler::class,
First Use Case Trigger a profiling snapshot via:
use Aeatech\SnapshotProfiler\NewRelic\Facades\NewRelicSnapshotProfiler;
NewRelicSnapshotProfiler::capture('user_action', ['key' => 'value']);
This will send profiling data to New Relic under the specified transaction name.
Instrument Key Routes Use middleware to auto-capture critical paths:
Route::middleware(['web', 'newrelic.profiler'])->group(function () {
// High-traffic or slow routes
});
Manual Snapshots for Debugging
// In a controller or service
NewRelicSnapshotProfiler::capture('checkout_process', [
'user_id' => auth()->id(),
'cart_size' => Cart::size(),
]);
Conditional Profiling
if (app()->environment('production') && request()->wantsJson()) {
NewRelicSnapshotProfiler::capture('api_endpoint', [
'response_time' => now()->diffInMilliseconds($startTime),
]);
}
Integration with Laravel Events
// In EventServiceProvider
protected $listen = [
'order.created' => [
function ($order) {
NewRelicSnapshotProfiler::capture('order_created', [
'order_id' => $order->id,
'amount' => $order->amount,
]);
},
],
];
Custom Metrics Combine with New Relic’s custom metrics API:
NewRelicSnapshotProfiler::addCustomMetric('Database.QueryTime', 150);
New Relic License Requirements
newrelic.ini license_key and app_name are correct; otherwise, snapshots won’t appear.Performance Overhead
'enabled' => env('APP_ENV') === 'production',
Transaction Naming Collisions
NewRelicSnapshotProfiler::capture('user_profile_load', [...]); // ✅
NewRelicSnapshotProfiler::capture('load_user_profile_for_authenticated_user', [...]); // ❌
Missing Data in Snapshots
user_id) are missing, ensure they’re serializable (no closures, resources, or circular references).dd(NewRelicSnapshotProfiler::getLastSnapshotData());
Middleware Order Matters
\Aeatech\SnapshotProfiler\NewRelic\Middleware\NewRelicSnapshotProfiler after authentication middleware to include user context:
'web' => [
\App\Http\Middleware\Authenticate::class,
\Aeatech\SnapshotProfiler\NewRelic\Middleware\NewRelicSnapshotProfiler::class,
],
Filter Noisy Transactions Exclude internal routes from profiling in config:
'ignore_routes' => [
'health-check',
'admin.*',
],
Leverage New Relic Dashboards Create custom dashboards in New Relic to visualize:
SnapshotProfiler.TransactionCount (total snapshots per minute).SnapshotProfiler.CustomAttribute.* (e.g., user_id).Batch Snapshots for High Volume Reduce API calls by batching:
NewRelicSnapshotProfiler::batch(function () {
NewRelicSnapshotProfiler::capture('bulk_operation', ['items' => 100]);
});
Extend with Custom Profiler
Implement Aeatech\SnapshotProfiler\Contracts\Profiler to add logic:
class CustomNewRelicProfiler implements Profiler {
public function capture(string $name, array $attributes = []): void {
// Add pre-processing (e.g., sanitize $attributes)
NewRelicSnapshotProfiler::send($name, $attributes);
}
}
Register in config/snapshot-profiler-newrelic.php:
'profiler' => \App\Services\CustomNewRelicProfiler::class,
Log Fallbacks If New Relic fails silently, log locally:
try {
NewRelicSnapshotProfiler::capture('fallback_test');
} catch (\Exception $e) {
\Log::error('NewRelic snapshot failed', ['error' => $e]);
}
How can I help you explore Laravel packages today?