brunosimsenhor / elastic-apm-php-agent
Elastic APM 的 PHP 5.6 代理
7.1.1
2019-07-24 14:04 UTC
Requires
- php: 5.6.*
- ext-curl: *
- guzzlehttp/guzzle: 6.*
- ralouphie/getallheaders: 2.0.5
- ramsey/uuid: ^3.7
Requires (Dev)
- phpunit/phpunit: 6.*
Suggests
- ext-xdebug: Required for processing of request headers
This package is not auto-updated.
Last update: 2024-10-01 04:05:17 UTC
README
这是 Elastic.co 的 APM 产品的 PHP 代理:[Elastic APM](https://elastic.ac.cn/solutions/apm)。
新功能: Laravel & Lumen 包 https://github.com/philkra/elastic-apm-laravel
安装
建议通过 Composer 安装代理。
运行以下 Composer 命令
php composer.phar require philkra/elastic-apm-php-agent
安装后,您需要引入 Composer 的自动加载器
require 'vendor/autoload.php';
使用
使用最小配置初始化代理
$agent = new \PhilKra\Agent( [ 'appName' => 'demo' ] );
创建代理时,您可以直接注入共享上下文,如用户、标签和自定义。
$agent = new \PhilKra\Agent( [ 'appName' => 'with-custom-context' ], [ 'user' => [ 'id' => 12345, 'email' => 'email@acme.com', ], 'tags' => [ // ... more key-values ], 'custom' => [ // ... more key-values ] ] );
捕获错误和异常
代理可以捕获所有从 Throwable
接口实现的错误和异常类型。(https://php.ac.cn/manual/en/class.throwable.php)
$agent->captureThrowable( new Exception() );
添加跨度
添加跨度([事务跨度](https://elastic.ac.cn/guide/en/apm/server/current/transactions.html#transaction-spans))非常简单。请根据您的具体需求查阅文档。以下是一个添加 MySQL 跨度的示例。
// create the agent $agent = new \PhilKra\Agent(['appName' => 'Demo with Spans']); // start a new transaction $transaction = $agent->startTransaction('GET /some/transaction/name'); // create a span $spans = []; $spans[] = [ 'name' => 'Your Span Name. eg: ORM Query', 'type' => 'db.mysql.query', 'start' => 300, // when did tht query start, relative to the transaction start, in milliseconds 'duration' => 23, // duration, in milliseconds 'stacktrace' => [ [ 'function' => "\\YourOrMe\\Library\\Class::methodCall()", 'abs_path' => '/full/path/to/file.php', 'filename' => 'file.php', 'lineno' => 30, 'library_frame' => false, // indicated whether this code is 'owned' by an (external) library or not 'vars' => [ 'arg1' => 'value', 'arg2' => 'value2', ], 'pre_context' => [ // lines of code leading to the context line '<?php', '', '// executing query below', ], 'context_line' => '$result = mysql_query("select * from non_existing_table")', // source code of context line 'post_context' => [// lines of code after to the context line '', '$table = $fakeTableBuilder->buildWithResult($result);', 'return $table;', ], ], ], 'context' => [ 'db' => [ 'instance' => 'my_database', // the database name 'statement' => 'select * from non_existing_table', // the query being executed 'type' => 'sql', 'user' => 'root', // the user executing the query (don't use root!) ], ], ]; // add the array of spans to the transaction $transaction->setSpans($spans); // send our transactions to te apm $agent->send();
没有最小元数据和上下文的事务
$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 )->setUserContext( [ 'id' => 12345, 'email' => "hello@acme.com", ] ); $agent->getTransaction( $trxName )->setCustomContext( [ 'foo' => 'bar', 'bar' => [ 'foo1' => 'bar1', 'foo2' => 'bar2' ] ] ); $agent->getTransaction( $trxName )->setTags( [ 'k1' => 'v1', 'k2' => 'v2' ] );
测试
vendor/bin/phpunit