Skip to content

Repository files navigation

sourceability/instrumentation

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.htmltideways:
enabled: false# See https://docs.newrelic.com/docs/agents/php-agent/getting-started/introduction-new-relic-php/# This requires https://github.com/ekino/EkinoNewRelicBundlenewrelic:
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-bundlesymfony:
enabled: false# See https://github.com/NoiseByNorthwest/php-spxspx:
enabled: falselisteners:
# Automatically instrument commandscommand:
enabled: false# Automatically instrument messenger workersmessenger:
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

<?phpnamespaceApp\Command;
useSourceability\Instrumentation\Profiler\ProfilerInterface;
useSymfony\Component\Console\Command\Command;
useSymfony\Component\Console\Input\InputInterface;
useSymfony\Component\Console\Output\OutputInterface;
class IndexCommand extends Command
{
/** * @var ProfilerInterface */private$profiler;
publicfunction__construct(ProfilerInterface$profiler)
{
parent::__construct();
$this->profiler = $profiler;
}
protectedfunctionexecute(InputInterface$input, OutputInterface$output): int
{
$this->profiler->stop();
$pager = newPagerfanta(...);
foreach ($pageras$pageResults) {
$this->profiler->start('index_batch');
$this->indexer->index($pageResults);
$this->profiler->stop();
};
return0;
}
}

About

Instrument commands/workers/custom code with datadog, newrelic, tideways, symfony, spx.

Topics

Resources

Stars

19 stars

Watchers

2 watching

Forks

Releases

Used by

Contributors

Languages