源可追溯/仪表化

使用 datadog、newrelic、tideways、symfony 仪表命令/工作者/自定义代码。

1.0.4 2024-04-09 11:29 UTC

README

这个库提供了一个简单的接口来启动和停止使用 APMs 仪表化代码。

Symfony 命令和消息传递工作者内置 symfony 事件监听器,这很方便,因为大多数 APMs 通常不支持开箱即用的工作者分析。

使用 composer 安装库

$ composer require sourceability/instrumentation

这个库包含一个可选的 Symfony 包,您可以通过更新 config/bundles.php 来安装。

return [
    Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
    // ...
    Sourceability\Instrumentation\Bundle\SourceabilityInstrumentationBundle::class => ['all' => true],
];

包配置参考

# 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

使用中间件也可以进行消息传递分析。

请注意,您应该使用中间件或监听器之一,而不是两者都使用,因为这会扭曲发送到您的 APM/监控器的统计数据。

framework:
    messenger:
        buses:
            messenger.bus.default:
                middleware:
                    - Sourceability\Instrumentation\Messenger\ProfilerMiddleware

仪表化长时间运行的命令

<?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;
    }
}