sourceability/instrumentation-bundle

此包已被废弃,不再维护。作者建议使用 sourceability/instrumentation 包代替。

使用 datadog、newrelic、tideways、symfony 等工具对命令/工作者/自定义代码进行监控。

1.0.4 2024-04-09 11:29 UTC

README

此库提供了一个简单的接口,用于使用 APMs 开始和停止对代码进行监控。

Symfony 命令和 messenger 工作者内置了 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

使用中间件也可以进行 messenger 的监控。

请注意,您应该使用中间件或监听器中的一个,而不是两者同时使用,因为这将会扭曲发送到您的 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;
    }
}