Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Xray Laravel

napp/xray-laravel image

Xray Laravel stats

Downloads
109.7K
Stars
53
Open Issues
6
Forks
31

View on GitHub →

AWS X-Ray for Laravel applications.

Aws X-Ray for Laravel

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

  1. Install the package via composer:
composer require napp/xray-laravel
  1. 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,
// ...
];
  1. 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,
],
  1. 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": [
"*"
]
}
]
}
  1. 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:

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

  • [x] Composer autoload
  • [x] Framework boot
  • [x] Route matching
  • [x] HTTP requests
  • [x] Database queries
  • [x] Queue jobs
  • [x] Blade view render
  • [x] Cache requests

LICENSE

The MIT License (MIT). Please see License File for more information.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Napp Xray Laravel Related Articles

Marketing as Code: Laravel Screenshot Automation image

Marketing as Code: Laravel Screenshot Automation

Read article
Eloquent API Calls image

Eloquent API Calls

Read article
Artisan Tinker in Vim with Tinkeray image

Artisan Tinker in Vim with Tinkeray

Read article
PEST PHP now includes higher-order expectations, Ray debugging integration, and more! image

PEST PHP now includes higher-order expectations, Ray debugging integration, and more!

Read article
Soap - A Laravel SOAP client that provides a clean interface for handling requests and responses image

Soap - A Laravel SOAP client that provides a clean interface for handling requests and responses

Read article
Ray desktop debugging for Laravel image

Ray desktop debugging for Laravel

Read article
DreamzTech logo

DreamzTech

Hire 6-10+ Yrs. experienced skilled Laravel Developers from DreamzTech. We ensure NDA protected, 100% quality delivery. Contact Us & Discuss Your Need.

DreamzTech
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
Tighten logo

Tighten

We help companies turn great ideas into amazing apps, products, and services.

Tighten