lukaszaleckas / laravel-correlation-id
Installs: 39 898
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 3
Open Issues: 1
pkg:composer/lukaszaleckas/laravel-correlation-id
Requires
- php: >=8.1
- guzzlehttp/guzzle: ^7.5
- laravel/framework: ^9.0|^10.0|^11.0|^12.0
Requires (Dev)
- fakerphp/faker: ^1.15
- larastan/larastan: ^2.8.1
- mockery/mockery: ^1.4
- orchestra/testbench: ^v7.40.1
- phpstan/phpstan-mockery: ^1.1.2
- phpunit/phpunit: ^9.5
- slevomat/coding-standard: ^6.4
- squizlabs/php_codesniffer: ^3.6
README
This package offers a way to correlate all the operations performed on request, asynchronous ones like jobs included.
Correlation id is generated or set from request header, if it has one (header name is specified in config), and appended to every log context.
Installation
- Run
composer require lukaszaleckas/laravel-correlation-id - (optional) Publish
correlation_id.phpconfig:php artisan vendor:publish --tag=laravel-correlation-idif you want to customize the default parameters - (optional) Add
LaravelCorrelationId\Jobs\Traits\RecallsCorrelationIdtrait to your jobs if you want correlation id to be automatically saved on job dispatch and recalled before it is handled / processed.
Usage
Firstly, you should inject LaravelCorrelationId\CorrelationIdService service.
Current correlation id can be accessed by calling service's getCurrentCorrelationId method.
This package extends Laravel's default Dispatcher class so that the correlation ID could be set before the job is dispatched, and recalled before it is processed.
This package adds LaravelCorrelationId\Http\Middleware\CorrelationIdMiddleware middleware to the global middleware stack by default.
If you want to add the middleware to specific routes/groups on your own, you can disable this behaviour by setting CORRELATION_ID_INCLUDE_ROUTE_MIDDLEWARE environment variable to false.
Note on jobs
If you are mocking Laravel's Dispatcher::class in your tests, you should update them
to mock this package's JobDispatcher class:
Mockery::mock(JobDispatcher::class)->shouldReceive('something')->...
Note on Guzzle
If you are using Guzzle and want to pass the correlation ID from one application to another, you will find the \LaravelCorrelationId\Utils\GuzzleUtils\ class useful.
It exposes 2 methods:
getHandlerStackfor use withGuzzleHttp\Client'shandleroptiongetMiddlewarefor use with Laravel's HTTP client facade.
Using these methods will ensure that the client passes the correlation ID header and value to another application.
Upgrading
From 1.x to 2.x
These versions should not have any breaking changes.
However, version 2.x deprecates the \LaravelCorrelationId\Jobs\Middleware\RecallCorrelationIdMiddleware and \LaravelCorrelationId\Jobs\Contracts\AbstractCorrelatableJob classes.
If you still use them, please consider upgrading to use the new \LaravelCorrelationId\Jobs\Traits\RecallsCorrelationId job trait.
Version 2.x also automatically adds the \LaravelCorrelationId\Http\Middleware\CorrelationIdMiddleware as a global middleware, so you no longer have to manually do that.