pekopt/elastic-apm-php-agent

0.2.5 2018-05-22 14:47 UTC

This package is not auto-updated.

Last update: 2024-09-20 20:51:53 UTC


README

Build Status

这是 Elastic.co APM 产品的 PHP 代理:[Elastic APM](https://elastic.ac.cn/solutions/apm)。请注意,此代理仍然处于 实验性 阶段,尚未准备好用于任何生产环境。

注意: 这是一个分支版本。原始包仓库 - [philkra/elastic-apm-php-agent](https://github.com/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](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