cryptoman0 / elastic-apm-php
将PHP应用程序事务推送到APM服务器的elastic/apm-agent-php包装器
v2.1.2
2022-06-24 18:39 UTC
Requires
- php: ^7.2|^8
- ext-elastic_apm: *
- illuminate/support: ^6.0|^7.0|^8.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-29 05:29:20 UTC
README
⚠️ 警告:包依赖 ext-elastic_apm
处于开发模式,会不定时推送破坏性更改。不建议在生产环境中使用。因此,在依赖项稳定性得到解决之前,该项目将不再维护。请随意分支并相应地修改。或者使用任何可用的替代方案。
要求
- 该包依赖于elastic的 apm-agent-php 扩展。
- php
^7.2
- 如果想要与Laravel一起使用,Laravel版本应大于等于
6.x
。
安装
要使用composer安装该包,请运行
composer require anik/elastic-apm-php
如果您想安装特定版本,请在安装时使用适当的版本。上述命令将安装最新版本。
Laravel
- 此包使用Laravel的自动发现功能。但是,如果您仍然想要安装,那么
- 在您的
config/app.php
的提供者数组中添加Anik\ElasticApm\Providers\ElasticApmServiceProvider::class
。 - 在您的
config/app.php
的外观数组中添加Anik\ElasticApm\Facades\Agent::class
。 - 运行
php artisan vendor:publish
并选择要发布的提供者,以在您的配置目录中发布配置文件。它将在您的配置目录中复制elastic-apm.php
。
- 在您的
Lumen
- 要使用您的Lumen安装此包,您不需要启用 外观。
- 在您的
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总是受到欢迎。