zuams / elastic-apm-laravel
一个将 Elastic APM 集成到 Laravel 的包
Requires
- php: >= 5.0.0
- illuminate/database: 5.*
- illuminate/http: 5.*
- illuminate/routing: 5.*
- illuminate/support: 5.*
- ramsey/uuid: ^3.0
- zuams/elastic-apm-php-agent: v1.0.0
This package is auto-updated.
Last update: 2024-09-23 19:57:14 UTC
README
从Zuams/elastic-apm-laravel分支继承,以添加对 PHP >= 5.6 的支持
安装
composer require zuams/elastic-apm-laravel
中间件
Laravel
注册为(例如)全局中间件,每次请求都会调用。 https://laravel.net.cn/docs/5.6/middleware#global-middleware
在 app/Http/Kernel.php
中注册中间件
protected $middleware = [ // ... more middleware \Zuams\ElasticApmLaravel\Middleware\RecordTransaction::class, ];
Lumen
在 bootstrap/app.php
中注册 Zuams\ElasticApmLaravel\Middleware\RecordTransaction::class
作为中间件
$app->middleware([ Zuams\ElasticApmLaravel\Middleware\RecordTransaction::class ]);
服务提供者
Laravel
无需手动注册服务提供者。它将由 package discovery 自动注册。
Lumen
在 bootstrap/app.php
中注册 \Zuams\ElasticApmLaravel\Providers\ElasticApmServiceProvider::class
作为服务提供者
$app->register(\Zuams\ElasticApmLaravel\Providers\ElasticApmServiceProvider::class);
跨度
Laravel
通过依赖容器提供 Transaction 对象,可以在应用程序的任何位置启动新的跨度。跨度结束时将自动将其添加到事务中。
// Use any normal Laravel method of resolving the dependency $transaction = app(\Zuams\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); ElasticApm::send();
确保在文件顶部导入外观
use ElasticApm;
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.7/testing)。
由于 APM 代理使用严格的布尔类型检查其活动状态,您必须确保您的 APM_ACTIVE
值为布尔 false
,而不仅仅是假值。完成此操作的最佳方法是创建一个 .env.testing
文件,并包括 APM_ACTIVE=false
,以及测试所需的其他任何环境设置。此文件应安全包含在您的版本控制系统中。