定制 inspector-apm/inspector-slim 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

inspector-apm/inspector-slim

最新稳定版本:0.1.2

Composer 安装命令:

composer require inspector-apm/inspector-slim

包简介

Simple Code Execution Monitoring for Slim applications.

README 文档

README

Total Downloads Latest Stable Version License Contributor Covenant

Simple code execution monitoring for Slim framework based applications.

Requirements

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

Install

Install the latest version by:

composer require inspector-apm/inspector-slim

Register On Container

First you have to register the Inspector instance inside the application container in order to make the monitoring agent available within the application.

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

Consider to use environment variables to store your project's INGESTION KEY.

If you are using a Slim 4 skeleton you can add a new container definition in app/dependencies.php file:

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);
        }
        
    ]);
}

You can get an INSPECTOR_INGESTION_KEY creating a new project in your Inspector account.

Attach the Middleware

You can attach the middleware globally:

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

Or in specific routes:

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

Test that everything works

Create a test route and open it in the browser http://localhost:8080:

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

Add Segment

You can add segments to the transaction's timeline from route functions:

$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;
});

If your routes are organized using controllers you need to inject the container in the controller constructor:

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;
    }
}

Official documentation

Check out the official documentation

Contributing

We encourage you to contribute to Inspector! Please check out the Contribution Guidelines about how to proceed. Join us!

LICENSE

This package is licensed under the MIT license.

统计信息

  • 总下载量: 1.81k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-01-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固