ferasalhallak/inspector-php

PHP应用程序的Inspector。

dev-main 2023-09-28 21:33 UTC

This package is not auto-updated.

Last update: 2024-09-27 21:04:08 UTC


README

Total Downloads Latest Stable Version License Contributor Covenant Build Status

简单代码执行监控,专为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->setTransport(function ($configuration) {
    return new CustomTransport($configuration);
});

查看官方文档

贡献

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

许可协议

本软件包采用MIT许可协议。