aeatech/web-snapshot-profiler-newrelic-bundle
Symfony bundle that integrates Web Snapshot Profiler with New Relic for production-grade request profiling. Configure app name/license and enable profiling selectively per route via headers, request params with probabilities, or profile all routes. Requires PHP 8.2+ and ext-newrelic.
Installation:
composer require aeatech/web-snapshot-profiler-newrelic-bundle
Enable the bundle in config/bundles.php:
AEATech\WebSnapshotProfilerNewrelicBundle\AEATechWebSnapshotProfilerNewrelicBundle::class => ['dev' => true, 'prod' => true],
Configure Environment Variables:
Add to .env:
AEA_TECH_WEB_SNAPSHOT_PROFILER_NEWRELIC_APP_NAME="YourAppName"
AEA_TECH_WEB_SNAPSHOT_PROFILER_NEWRELIC_LICENSE="your_newrelic_license_key"
Enable Profiling:
Update config/packages/aea_tech_web_snapshot_profiler_newrelic.yaml:
aea_tech_web_snapshot_profiler_newrelic:
is_profiling_enabled: true
First Use Case: Trigger a snapshot for a specific route or controller method:
use AEATech\WebSnapshotProfilerNewrelicBundle\Attribute\Snapshot;
#[Snapshot]
public function criticalAction(Request $request): Response
{
// Your logic here
}
Route-Level Profiling:
Use the Snapshot attribute on route controllers or methods to profile specific endpoints:
#[Route('/profile-me', name: 'profile_me')]
#[Snapshot]
public function profileMe(): Response
{
return new Response('Profiling this endpoint!');
}
Conditional Profiling: Dynamically enable/disable profiling based on environment or user roles:
# config/packages/aea_tech_web_snapshot_profiler_newrelic.yaml
aea_tech_web_snapshot_profiler_newrelic:
is_profiling_enabled: '%kernel.debug%' # Only in debug mode
Custom Events: Manually trigger snapshots for non-HTTP logic (e.g., CLI commands or background jobs):
use AEATech\WebSnapshotProfilerNewrelicBundle\Service\SnapshotProfiler;
public function __construct(private SnapshotProfiler $profiler) {}
public function processData()
{
$this->profiler->startSnapshot('data_processing');
// Your logic here
$this->profiler->endSnapshot();
}
Integration with Symfony Events: Listen to kernel events to profile specific requests:
use Symfony\Component\HttpKernel\Event\RequestEvent;
use AEATech\WebSnapshotProfilerNewrelicBundle\Service\SnapshotProfiler;
public function onKernelRequest(RequestEvent $event, SnapshotProfiler $profiler)
{
if ($event->getRequest()->getPathInfo() === '/admin') {
$profiler->startSnapshot('admin_dashboard');
}
}
New Relic Custom Attributes: Add custom attributes to enrich profiling data:
$this->profiler->addAttribute('user_id', $user->id);
$this->profiler->addAttribute('request_source', $request->headers->get('X-Request-Source'));
New Relic Extension Dependency:
ext-newrelic (v12.1+) is installed and enabled. Verify with:
php -m | grep newrelic
pecl install newrelic
Profiling Overhead:
prod unless explicitly needed:
aea_tech_web_snapshot_profiler_newrelic:
is_profiling_enabled: false # Default for prod
is_profiling_enabled conditionally (e.g., only for admin users or specific routes).Attribute Conflicts:
#[Snapshot] attribute may conflict with other attributes on the same method. Ensure it’s the last attribute in the list:
#[Route('/example')]
#[ApiPlatform\Operation(...)]
#[Snapshot] // Last attribute
Environment Variable Sensitivity:
license key is exposed in bundles.php if not properly masked. Use %env() in config/packages/ to avoid hardcoding:
newrelic:
license: '%env(string:NEW_RELIC_LICENSE)%'
New Relic App Name Clashes:
app_name, snapshots may merge incorrectly. Append a unique suffix (e.g., app_name: "MyApp-Staging").Check Profiler Status: Verify profiling is enabled via Symfony’s debug toolbar or logs:
bin/console debug:config aea_tech_web_snapshot_profiler_newrelic
New Relic UI:
aea_tech_web_snapshot_profiler custom attribute.Log Errors:
Enable debug logging in config/packages/monolog.yaml:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: ["!event"]
Snapshot Not Appearing?:
#[Snapshot] attribute is on a public method.Custom Snapshot Events: Extend the profiler to support custom events (e.g., database queries, external API calls):
// src/Service/CustomProfiler.php
use AEATech\WebSnapshotProfilerNewrelicBundle\Service\SnapshotProfiler;
class CustomProfiler
{
public function __construct(private SnapshotProfiler $profiler) {}
public function profileQuery(string $query): void
{
$this->profiler->startSnapshot('database_query', ['query' => $query]);
// Execute query
$this->profiler->endSnapshot();
}
}
Override Default Configuration: Create a custom bundle to modify the profiler’s behavior:
// src/DependencyInjection/AEATechWebSnapshotProfilerNewrelicExtension.php
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('aea_tech_web_snapshot_profiler_newrelic.custom_attr', $config['custom_attribute']);
}
New Relic API Integration: Use the New Relic API to programmatically fetch or analyze snapshots:
use NewRelic\Agent;
public function analyzeSnapshots()
{
$snapshots = Agent::getTransactionSnapshots();
// Process snapshots
}
Symfony Flex Recipes: Create a custom recipe to automate installation for your team:
# resources/recipes/AEATechWebSnapshotProfilerNewrelicBundle.yaml
name: "AEATech Web Snapshot Profiler New Relic Bundle"
recipe:
type: "symfony/bundle"
config:
bundles:
AEATech\WebSnapshotProfilerNewrelicBundle\AEATechWebSnapshotProfilerNewrelicBundle: ["dev" => true, "prod" => true]
How can I help you explore Laravel packages today?