palzin-apm/palzin-php

监控包

24.8.1 2024-08-06 18:06 UTC

This package is auto-updated.

Last update: 2024-09-06 18:24:35 UTC


README

Latest Release Total Downloads (custom server) Packagist License GitHub last commit GitHub Release Date

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

要求

  • PHP >= 7.2.0

安装

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

composer require palzin-apm/palzin-php

使用

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

use Palzin\Palzin;
use Palzin\Configuration;

$configuration = new Configuration('YOUR_PALZIN_APM_INGESTION_KEY');
$palzin = new Palzin($configuration);

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

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

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

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

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

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

自定义传输

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

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

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

    protected $queue = [];

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

    public function addEntry(\Palzin\Models\Arrayable $entry)
    {
        // Add an \Palzin\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-Palzin-Key: xxxxxxxxxxxx',
            'Content-Type: application/json',
            'Accept: application/json',
        ]);
        curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($this->queue));
        curl_exec($handle);
        curl_close($handle);
    }
}

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

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

许可证

本包采用MIT许可证。