cryptoman3 / elastic-apm-php
将PHP应用程序事务推送到APM服务器的elastic/apm-agent-php包装器
v2.3
2023-02-14 16:56 UTC
Requires
- php: ^7|^8
- ext-elastic_apm: *
- illuminate/support: ^6.0|^7.0|^8.0|^9.0|^10.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-25 00:31:33 UTC
README
⚠️ 警告:该包的依赖项ext-elastic_apm
处于开发模式,不时推出破坏性更改。还建议不要在生产环境中使用。因此,直到依赖项稳定,该项目将不再维护。您可以随意分支并相应地更改。或者使用任何可用的替代方案
需求
- 该包依赖于elastic的apm-agent-php扩展。
- php
^7.2
- 如果想要与Laravel一起使用,则需要Laravel版本 >=
6.x
。
安装
要使用composer安装此包,请运行
composer require cryptoman3/elastic-apm-php
如果您想安装任何特定版本,请使用适当的版本。上面的命令将安装最新版本。
Laravel
- 此包使用Laravel的自动发现功能。但是,如果您仍然想安装,那么
- 在您的
config/app.php
的providers数组中添加Anik\ElasticApm\Providers\ElasticApmServiceProvider::class
。 - 在您的
config/app.php
的facade数组中添加Anik\ElasticApm\Facades\Agent::class
。 - 运行
php artisan vendor:publish
并选择要发布的提供者以在配置目录中发布配置文件。它将在配置目录中复制elastic-apm.php
。
- 在您的
Lumen
- 要使用Lumen安装此包,您不需要启用
Facade
。 - 在您的
bootstrap/app.php
中注册服务提供程序$app->register(Anik\ElasticApm\Providers\ElasticApmServiceProvider::class);
- 如果您想更改配置,请将
elastic-apm.php
从vendor/anik/elastic-apm-php/src/config/elastic-apm.php
复制到您的config/app.php
文件中。 - 在您的
bootstrap/app.php
中注册复制的配置文件$app->configure('elastic-apm')
用法
错误跟踪
如果您想保留错误记录,那么
- 对于Laravel,在您的
bootstrap/app.php
中
// COMMENT THIS SECTION $app->singleton( Illuminate\Contracts\Debug\ExceptionHandler::class, App\Exceptions\Handler::class );
// USE THIS SECTION FOR LARAVEL <= 7 $app->singleton(Illuminate\Contracts\Debug\ExceptionHandler::class, function ($app) { return new Anik\ElasticApm\Exceptions\Handler(new App\Exceptions\Handler($app), [ // NotFoundHttpException::class, // (1) // ConnectException::class, // (2) ]); });
// USE THIS SECTION FOR LARAVEL >= 8 $app->singleton(Illuminate\Contracts\Debug\ExceptionHandler::class, function ($app) { return new Anik\ElasticApm\Exceptions\HandlerThrowable(new App\Exceptions\Handler($app), [ // NotFoundHttpException::class, // (1) // ConnectException::class, // (2) ]); });
- 对于Lumen,在您的
bootstrap/app.php
中
// COMMENT THIS SECTION $app->singleton( Illuminate\Contracts\Debug\ExceptionHandler::class, App\Exceptions\Handler::class );
// USE THIS SECTION FOR LUMEN <= 7 $app->singleton(Illuminate\Contracts\Debug\ExceptionHandler::class, function ($app) { return new Anik\ElasticApm\Exceptions\Handler(new App\Exceptions\Handler(), [ // NotFoundHttpException::class, // (1) // ConnectException::class, // (2) ]); });
// USE THIS SECTION FOR LUMEN >= 8 $app->singleton(Illuminate\Contracts\Debug\ExceptionHandler::class, function ($app) { return new Anik\ElasticApm\Exceptions\HandlerThrowable(new App\Exceptions\Handler(), [ // NotFoundHttpException::class, // (1) // ConnectException::class, // (2) ]); });
请求响应跟踪
如果您想保留应用程序接收和处理的请求记录,那么
- 对于Laravel,在您的
App\Http\Kernel
的中间件中添加Anik\ElasticApm\Middleware\RecordForegroundTransaction
类。
<?php class Kernel extends HttpKernel { protected $middleware = [ // ... \Anik\ElasticApm\Middleware\RecordForegroundTransaction::class, // .. ]; }
- 对于Lumen,在您的
bootstrap/app.php
文件中添加Anik\ElasticApm\Middleware\RecordForegroundTransaction
类。
$app->middleware([ // ... \Anik\ElasticApm\Middleware\RecordForegroundTransaction::class, // ... ]);
后台作业跟踪
对于Laravel和Lumen,您必须在作业的中间件中添加Anik\ElasticApm\Middleware\RecordBackgroundTransaction
类。
HTTP调用跟踪
要跟踪HTTP调用,您需要使用Guzzle。您可以将RecordHttpTransaction
类作为Guzzle的处理器堆栈传递。然后它会记录HTTP调用。
$stack = \GuzzleHttp\HandlerStack::create(); $stack->push(new \Anik\ElasticApm\Middleware\RecordHttpTransaction(), 'whatever-you-wish'); $client = new \GuzzleHttp\Client([ 'base_uri' => 'http://httpbin.org', 'timeout' => 10.0, 'handler' => $stack, ]); $client->request('GET', '/');
文档
请检查项目描述中给出的URL,以获得对其功能的全面了解。以及如何进行自定义。
PRs?
如果您发现任何错误并进行了更新,请确保您发送了PR。PR总是受欢迎的。