ultimate-apm/ultimate-php

23.03.08 2023-03-09 07:49 UTC

This package is auto-updated.

Last update: 2024-09-09 10:57:10 UTC


README

Latest Stable Version Total Downloads License

为PHP开发者提供简单的代码执行监控和错误报告。

要求

  • PHP >= 7.2.0

安装

通过以下方式安装我们的最新版本包:

composer require ultimate-apm/ultimate-php

使用

要开始向Palzin APM发送数据,您需要一个BUGTRAP密钥来创建Configuration类的实例。您可以在Palzin APM仪表板上创建一个新的项目来获取ULTIMATE_BUGTRAP_KEY

use Ultimate\Ultimate;
use Ultimate\Configuration;

$configuration = new Configuration('YOUR_BUGTRAP_KEY');
$ultimate = new Ultimate($configuration);

所有操作都从transaction开始。事务代表一个执行周期,它可以包含一个或多个段落。

// Start an execution cycle with a transaction
$ultimate->startTransaction($_SERVER['PATH_INFO']);

使用addSegment方法来监控事务中的代码块

$result = $ultimate->addSegment(function ($segment) {
    // Do something here...
	return "Hello World!";
}, 'my-process');

echo $result; // this will print "Hello World!"

Palzin APM将实时监控您的代码执行,并在出现问题时持续提醒您。

自定义传输

您还可以设置自定义传输类,以个性化的方式将监控数据从您的服务器传输到Palzin APM。

传输类需要实现\Ultimate\Transports\TransportInterface

class CustomTransport implements \Ultimate\Transports\TransportInterface
{
    protected $configuration;

    protected $queue = [];

    public function __constructor($configuration)
    {
        $this->configuration = $configuration;
    }

    public function addEntry(\Ultimate\Models\Arrayable $entry)
    {
        // Add an \Ultimate\Models\Arrayable entry in the queue.
        $this->queue[] = $entry;
    }

    public function flush()
    {
        // Performs data transfer.
        $handle = curl_init('https://www.palzin.app');
        curl_setopt($handle, CURLOPT_POST, 1);
        curl_setopt($handle, CURLOPT_HTTPHEADER, [
            'X-Ultimate-Key: xxxxxxxxxxxx',
            'Content-Type: application/json',
            'Accept: application/json',
        ]);
        curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($this->queue));
        curl_exec($handle);
        curl_close($handle);
    }
}

然后,您可以通过回调设置新的传输,该回调将接收当前配置状态作为参数,在Ultimate实例中使用。

$ultimate->setTransport(function ($configuration) {
    return new CustomTransport($configuration);
});

许可证

此软件包根据MIT许可证授权。