inspector-apm/inspector-php

PHP应用程序的检查器。


README

Total Downloads Latest Stable Version License Contributor Covenant Build Status

在继续之前,请考虑给我们一个GitHub星标 ⭐️。谢谢!

代码执行监控,为PHP开发者打造。

需求

  • PHP >= 7.2.0

安装

通过以下方式安装最新版本:

composer require inspector-apm/inspector-php

使用

要开始向Inspector发送数据,您需要一个Ingestion Key来创建Configuration类的实例。您可以在Inspector仪表板上创建新项目以获得INSPECTOR_API_KEY

use Inspector\Inspector;
use Inspector\Configuration;

$configuration = new Configuration('YOUR_INGESTION_KEY');
$inspector = new Inspector($configuration);

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

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

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

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

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

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

自定义传输

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

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

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

    protected $queue = [];

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

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

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

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

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

查看官方文档

贡献

我们鼓励您为Inspector做出贡献!请查看有关如何进行的贡献指南。加入我们吧!

许可证

此包根据MIT许可证授权。