speakol-ads / elastic-apm-laravel
一个将 Elastic APM 集成到 Laravel 的包
Requires
- php: >= 7.0
- illuminate/database: 5.5.x|5.6.x|5.7.x|5.8.x
- illuminate/http: 5.5.x|5.6.x|5.7.x|5.8.x
- illuminate/routing: 5.5.x|5.6.x|5.7.x|5.8.x
- illuminate/support: 5.5.x|5.6.x|5.7.x|5.8.x
- ramsey/uuid: ^3.0
- speakol-ads/elastic-apm-php-agent: >=7.1
Requires (Dev)
- phpunit/phpunit: 6.*
This package is auto-updated.
Last update: 2024-09-10 17:47:30 UTC
README
Laravel 包,基于https://github.com/speakol-ads/elastic-apm-php-agent 库,自动处理事务、错误/异常。如果使用 Illuminate\Support\Facades\Auth
,则将用户 ID 添加到上下文中。已测试与 Laravel 5.8.*
及 philkra/elastic-apm-php-agent 版本 7.*
兼容。
安装
composer require speakol-ads/elastic-apm-laravel
中间件
Laravel
注册为(例如)全局中间件,以便在每个请求中调用。 https://laravel.net.cn/docs/5.8/middleware#global-middleware
在 app/Http/Kernel.php
中注册中间件
protected $middleware = [ // ... more middleware \PhilKra\ElasticApmLaravel\Middleware\RecordTransaction::class, ];
Lumen
在 bootstrap/app.php
中注册 PhilKra\ElasticApmLaravel\Middleware\RecordTransaction::class
作为中间件
$app->middleware([ PhilKra\ElasticApmLaravel\Middleware\RecordTransaction::class ]);
跨度
Laravel
通过依赖注入容器提供事务对象,可以在应用程序的任何位置启动新的跨度。当跨度结束时,它将自动将自己添加到事务中。
// Use any normal Laravel method of resolving the dependency $transaction = app(\PhilKra\ElasticApmLaravel\Apm\Transaction::class); $span = $transaction->startNewSpan('My Span', 'app.component_name'); // do some stuff $span->end();
Lumen
待定
错误/异常处理
Laravel
在 app/Exceptions/Handler
中,向 report
方法添加以下内容
\ElasticApm::captureThrowable($exception, [], request()->__apm__());
Lumen
尚未测试。
代理配置
Laravel
默认配置中支持以下环境变量
您还可以发布 elastic-apm.php
配置文件以更改其他设置
php artisan vendor:publish --tag=config
发布后,打开 config/elastic-apm.php
文件并查看各种设置。
Laravel 测试设置
Laravel 提供类以支持使用 PHPUnit 运行单元测试和功能测试。在大多数情况下,您可能希望在测试期间显式禁用 APM,因为它默认启用。有关更多信息,请参阅 Laravel 文档(https://laravel.net.cn/docs/5.8/testing)。
由于 APM 代理使用严格的布尔类型检查其活动状态,您必须确保您的 APM_ACTIVE
值是布尔 false
而不是简单的假值。完成此操作的最佳方法是创建一个 .env.testing
文件,并包含 APM_ACTIVE=false
,以及您测试所需的任何其他环境设置。此文件应安全包含在您的版本控制系统中。