hotrush/elastic-apm-php-agent

0.4.1 2019-11-04 15:18 UTC

This package is auto-updated.

Last update: 2024-09-05 01:53:06 UTC


README

Build Status

这是Elastic.co的APM产品的PHP代理:[https://elastic.ac.cn/solutions/apm](https://elastic.ac.cn/solutions/apm)。请注意,此代理仍处于**试验性**阶段,不适合任何生产使用。

注意: 这是一个分支。原始包仓库 - philkra/elastic-apm-php-agent

安装

推荐通过 Composer 安装代理。

运行以下Composer命令

php composer.phar require hotrush/elastic-apm-php-agent

安装后,您需要引入Composer的自动加载器

require 'vendor/autoload.php';

用法

使用最小配置初始化代理

$agent = new \Hotrush\Agent(['appName' => 'demo']);

在创建代理时,您可以直接注入共享上下文注册表。上下文注册表包含有关用户、请求、标签和自定义的信息。

$contexts = new \Hotrush\Context\ContextsRegistry();
$contexts->getUser()
    ->setId(123)
    ->setUsername('User')
    ->setEmail('email@domain.com');
$contexts->getTags()->addTag('tag_name', 'tag_value');
$contexts->getRequest()->setRequestData(['body' => ['foo' => 'bar']]);
$contexts->getCustom()->addCustom('custom_key', [
    'id' => 1,
    'name' => 'Hello',
]);
$agent = new \Hotrush\Agent([ 'appName' => 'with-custom-context' ], $contexts);

捕获错误和异常

代理可以捕获所有从接口 Throwable (https://php.ac.cn/manual/en/class.throwable.php) 实现的错误和异常类型。

$agent->captureThrowable( new Exception() );

无最小元数据和上下文的交易

$trxName = 'Demo Simple Transaction';
$agent->startTransaction($trxName);
// Do some stuff you want to watch ...
$agent->stopTransaction($trxName);

具有元数据和上下文的交易

$trxName = 'Demo Transaction with more Data';
$agent->startTransaction($trxName);
// Do some stuff you want to watch ...
$agent->stopTransaction($trxName, [
    'result' => '200',
    'type'   => 'demo'
]);
$agent->getTransaction($trxName)->getContextsRegistry()->getUser()->setId(123);
$agent->getTransaction($trxName)->getContextsRegistry()->getTags()->addTag('tag', 'value');
$agent->getTransaction($trxName)->getContextsRegistry()->getTags()->setTags( [ 'k1' => 'v1', 'k2' => 'v2' ] );  

测试

vendor/bin/phpunit