jonsutherland/xray-laravel
Composer 安装命令:
composer require jonsutherland/xray-laravel
包简介
AWS X-Ray for Laravel applications.
README 文档
README
The package automatically trace your laravel application and sends to AWS X-Ray.
What is X-Ray?
X-Ray is a distributed tracing system for production apps. AWS X-Ray traces user requests as they travel through your entire application. It aggregates the data generated by the individual services and resources that make up your application, providing you an end-to-end view of how your application is performing.
X-Ray for Laravel
This package enables automatic tracing of important parts of your application, such as http request, database queries, views and queue jobs. Those parts are being traced and sent to AWS X-Ray for you to improve performance.
Below is a simple example of a http request with a database query. This query is quite slow and could maybe be optimized or cached.
Each element has extra information, such as the database query stack trace.
Installation
- Install the package via composer:
composer require napp/xray-laravel
- Add middleware to the top of the global middleware in
App\Http\Kernel.php.
protected $middleware = [ \Napp\Xray\Middleware\RequestTracing::class, // here \App\Http\Middleware\TrustProxies::class, \App\Http\Middleware\CheckForMaintenanceMode::class, // ... ];
- Add XrayServiceProvider to the very top of providers in
config/app.php.
'providers' => [ /* * Laravel Framework Service Providers... */ Napp\Xray\XrayServiceProvider::class, // here Illuminate\Auth\AuthServiceProvider::class, Illuminate\Broadcasting\BroadcastServiceProvider::class, // ... ];
Optionally, you can add the facade in config/app.php.
'aliases' => [ // ... 'Xray' => \Napp\Xray\Facades\Xray::class, ],
- Edit the AWS Execution role to include X-Ray permissions.
Either add the preexisting policy from AWS AWSXrayWriteOnlyAccess, or create your own:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "xray:PutTraceSegments", "xray:PutTelemetryRecords", "xray:GetSamplingRules", "xray:GetSamplingTargets", "xray:GetSamplingStatisticSummaries" ], "Resource": [ "*" ] } ] } - Head over to AWS Console, to Lambda and find your function. Activate X-Ray Tracing.
Manually use the Tracer
Lets say you want to trace a specific piece of your code to deeply understand the impact on performance.
Xray::addSegment('MyCustomLogic'); // run your code Xray::endSegment('MyCustomLogic');
Another use case is to inspect some heavy php side parsing of data.
use Napp\Xray\Facades\Xray; class XMLParser { public function handle($file) { // adding some metadata to the segment Xray::addSegment('XMLParser', null, [ 'file' => $file->name() ]); $this->parse($file); Xray::endSegment('XMLParser'); } private function parse($xml): array { Xray::addSegment('XMLParser parse'); $output = $this->getAttributeList(); // some more code Xray::endSegment('XMLParser parse'); return $output; } private function getAttributeList(): array { Xray::addSegment('XMLParser getAttributeList'); // your code Xray::endSegment('XMLParser getAttributeList'); return []; } }
The above results in:
Request filtering
There might be some requests you wish not to track in Amazon X-Ray. In order to filter out those requests, you can call the addRequestFilterCallback function, on the Xray facade. This function takes a callback as a parameter. The callback takes a Symfony\Component\HttpFoundation\Request as a parameter and is expected to return a boolean.
Please note that this function call needs to be done in the register function of your AppServiceProvider. You should, also, keep the checks relatively simple, since most of the application service providers won't be booted when calling the filtering callbacks.
When LaravelXray is booting, the request is checked against each registered callbacks. If none returns false, the request is captured. Otherwise, it is not.
use Symfony\Component\HttpFoundation\Request; use Napp\Xray\Facades\Xray; class AppServiceProvider extends ServiceProvider { /** * Register any application services. * * @return void */ public function register() { Xray::addRequestFilterCallback(function(Request $request) { /* some conditions */ return true; }); } }
Daemon support
The X-Ray daemon is automatically run in a Lambda environment. Use this over the default Napp\Xray\Submission\APISegmentSubmitter to relay requests to Amazon X-Ray.
Firstly, publish the X-Ray config and then update the submitter in config/xray.php to \Napp\Xray\Submission\DaemonSegmentSubmitter::class
php artisan vendor:publish --tag=xray-config # config/xray.php ... 'submitter' => \Napp\Xray\Submission\DaemonSegmentSubmitter::class, ...
The daemon submitter will pick up the _AWS_XRAY_DAEMON_ADDRESS _AWS_XRAY_DAEMON_PORT. These environment variables are injected for you if using a service like Laravel Vapor
Disable Tracer
If you want to disable the Tracer, just add to the .env file.
XRAY_ENABLED=false
What Tracers are supported
- Composer autoload
- Framework boot
- Route matching
- HTTP requests
- Database queries
- Queue jobs
- Blade view render
- Cache requests
- Commands
LICENSE
The MIT License (MIT). Please see License File for more information.
jonsutherland/xray-laravel 适用场景与选型建议
jonsutherland/xray-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.99k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「laravel」 「tracing」 「xray」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jonsutherland/xray-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jonsutherland/xray-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jonsutherland/xray-laravel 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Monolog JSON Formatter for Google StackDriver integration
Observability component.
OpenTelemetry auto-instrumentation for PHP Session functions
OpenTelemetry SDK rule-based sampler
The Azure package for opentelemetry-php
Alfabank REST API integration
统计信息
- 总下载量: 6.99k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04



