inspector-apm/inspector-slim

为Slim应用程序提供简单代码执行监控。

0.1.2 2023-01-17 20:23 UTC

This package is auto-updated.

Last update: 2024-09-22 14:56:54 UTC


README

Total Downloads Latest Stable Version License Contributor Covenant

为基于Slim框架的应用程序提供简单代码执行监控。

需求

  • PHP >= 7.2.0
  • Slim >= 4.x

安装

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

composer require inspector-apm/inspector-slim

在容器上注册

首先,您需要在应用程序容器内部注册Inspector实例,以便在应用程序中提供监控代理。

$container->set('inspector', function () {
    $configuration = new \Inspector\Slim\Configuration('INSPECTOR_INGESTION_KEY');
    
    return new Inspector($configuration);
});

建议使用环境变量来存储您的项目的INGESTION KEY。

如果您正在使用Slim 4骨架,您可以在app/dependencies.php文件中添加一个新的容器定义

use DI\ContainerBuilder;
use Psr\Container\ContainerInterface;

return function (ContainerBuilder $containerBuilder) {
    $containerBuilder->addDefinitions([
        // Other services definitions...
    
        'inspector' => function (ContainerInterface $container) {
            $configuration = new \Inspector\Slim\Configuration('INSPECTOR_INGESTION_KEY');
            return new Inspector\Inspector($configuration);
        }
        
    ]);
}

您可以通过在您的Inspector账户中创建一个新的项目来获取INSPECTOR_INGESTION_KEY

附加中间件

您可以将中间件全局附加

$app->add(\Inspector\Slim\WebRequestMonitoring::class);

或在特定路由中

$app->get('/', function () {
    
    // your code here...
    
})->add(\Inspector\Slim\WebRequestMonitoring::class);

测试一切是否正常工作

创建一个测试路由并在浏览器中打开它 https://:8080

$app->get('/test', function () {
    
    throw new \Exception('My First Exception.');
    
})->add(\Inspector\Slim\WebRequestMonitoring::class);

添加分段

您可以从路由函数中添加分段到事务的时间线

$app->get('/', function (Request $request, Response $response) {
    /*
     * Retrieve the inspector instance from the container.
     */
    $this->get('inspector')->addSegment(function () {
    
        // Your code here...
        sleep(1);
        
    }, 'sleep');
        
    return $response;
});

如果您的路由是使用控制器组织,您需要在控制器构造函数中注入容器

namespace App\Controllers;


use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

class TestController
{
    protected $container;

    /**
     * Inject the container to retrieve the inspector instance later.
     */
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function __invoke(Request $request, Response $response)
    {
        // Retrieve the inspector instance from the container.
        $this->container->get('inspector')->addSegment(function () {
        
            // Your code here...
            sleep(1);
            
        }, 'sleep');

        $response->getBody()->write('Test route.');

        return $response;
    }
}

官方文档

查看官方文档

贡献

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

许可协议

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