sourceability / instrumentation-bundle
Instrument commands/workers/custom code with datadog, newrelic, tideways, symfony.
Installs: 3 847
Dependents: 0
Suggesters: 0
Security: 0
Stars: 20
Watchers: 2
Forks: 11
Open Issues: 1
Requires
- php: ^7.4 || ^8.0
Requires (Dev)
- datadog/dd-trace: ^0.82.0
- ekino/newrelic-bundle: ^2.2
- jangregor/phpstan-prophecy: ^1.0
- phpspec/prophecy-phpunit: ^2.0
- phpstan/phpstan: ^1.0
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpstan/phpstan-strict-rules: ^1.0
- phpunit/phpunit: ^9.5
- symfony/config: ^4.4 || ^5.4 || ^6.0 || ^7.0
- symfony/console: ^4.4 || ^5.4 || ^6.0 || ^7.0
- symfony/dependency-injection: ^4.4 || ^5.4 || ^6.0 || ^7.0
- symfony/event-dispatcher: ^4.4 || ^5.4 || ^6.0 || ^7.0
- symfony/http-foundation: ^4.4 || ^5.4 || ^6.0 || ^7.0
- symfony/http-kernel: ^4.4 || ^5.4 || ^6.0 || ^7.0
- symfony/messenger: ^4.4 || ^5.4 || ^6.0 || ^7.0
- symfony/stopwatch: ^4.4 || ^5.4 || ^6.0 || ^7.0
- symplify/easy-coding-standard: ^9.3
Suggests
- symfony/config: ^4.4 || ^5.4 || ^6.0 || ^7.0
- symfony/console: ^4.4 || ^5.4 || ^6.0 || ^7.0
- symfony/dependency-injection: ^4.4 || ^5.4 || ^6.0 || ^7.0
- symfony/event-dispatcher: ^4.4 || ^5.4 || ^6.0 || ^7.0
- symfony/http-foundation: ^4.4 || ^5.4 || ^6.0 || ^7.0
- symfony/http-kernel: ^4.4 || ^5.4 || ^6.0 || ^7.0
- symfony/messenger: ^4.4 || ^5.4 || ^6.0 || ^7.0
- symfony/stopwatch: ^4.4 || ^5.4 || ^6.0 || ^7.0
README
This library provides a simple interface to start and stop instrumenting code with APMs.
Symfony commands and messenger workers have built in symfony event listeners which is convenient because most APMs usually don't support profiling workers out of the box.
Install the library using composer:
$ composer require sourceability/instrumentation
Bundle
This library includes an optional Symfony bundle that you can install by updating config/bundles.php
:
return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
// ...
Sourceability\Instrumentation\Bundle\SourceabilityInstrumentationBundle::class => ['all' => true],
];
Bundle configuration reference:
# Default configuration for extension with alias: "sourceability_instrumentation" sourceability_instrumentation: profilers: # See https://support.tideways.com/documentation/features/application-monitoring/application-performance-overview.html tideways: enabled: false # See https://docs.newrelic.com/docs/agents/php-agent/getting-started/introduction-new-relic-php/ # This requires https://github.com/ekino/EkinoNewRelicBundle newrelic: enabled: false # See https://docs.datadoghq.com/tracing/setup_overview/setup/php/ datadog: enabled: false # This "hacks" the symfony web profiler to create profiles in non web contexts like workers, commands. # This is really useful for development along with https://github.com/sourceability/console-toolbar-bundle symfony: enabled: false # See https://github.com/NoiseByNorthwest/php-spx spx: enabled: false listeners: # Automatically instrument commands command: enabled: false # Automatically instrument messenger workers messenger: enabled: false
Messenger profiling is also available with a middleware.
Please note that you should use either the middleware, or the listener, but not both, as this will distort the statistics sent to your APM/monitoring.
framework: messenger: buses: messenger.bus.default: middleware: - Sourceability\Instrumentation\Messenger\ProfilerMiddleware
Instrumenting a long running command
<?php namespace App\Command; use Sourceability\Instrumentation\Profiler\ProfilerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class IndexCommand extends Command { /** * @var ProfilerInterface */ private $profiler; public function __construct(ProfilerInterface $profiler) { parent::__construct(); $this->profiler = $profiler; } protected function execute(InputInterface $input, OutputInterface $output): int { $this->profiler->stop(); $pager = new Pagerfanta(...); foreach ($pager as $pageResults) { $this->profiler->start('index_batch'); $this->indexer->index($pageResults); $this->profiler->stop(); }; return 0; } }