juliendufresne / inter-app-request-identifier
Defines an interface to identify a process running between multiple applications
Installs: 5 269
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^7.1.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.10
- guzzlehttp/guzzle: ^6.3
- jakub-onderka/php-console-highlighter: ^0.3.2
- jakub-onderka/php-parallel-lint: ^1.0
- monolog/monolog: ^1.23
- phpmetrics/phpmetrics: ^2.3
- phpstan/phpstan: ^0.9.2
- phpstan/phpstan-phpunit: ^0.9.4
- phpunit/phpunit: ^7.0
- ramsey/uuid: ^3.7
- sensiolabs/security-checker: ^4.1
This package is not auto-updated.
Last update: 2022-02-01 13:12:28 UTC
README
Defines an interface to identify a process running between multiple applications.
When you work with multiple application calling each other, it is hard to follow which application calls which other application.
Furthermore, when you have to see the logs of an application, you can not see where the request come from.
This library provides a solution:
- when an application calls another one, it adds some identifiers in the request header to keep track of who is the caller
- when an application receives a call from another one, it checks if the headers are set and will use them for further request
- it adds an extra section in your monolog logs containing:
- an identification of the current application process
- an identification of the caller who initiate the call to this application
- an identification of the root caller who initiate the global call.
Installation
composer require juliendufresne/inter-app-request-identifier
Example
use JulienDufresne\RequestId\Factory\Generator\RamseyUuidGenerator; use JulienDufresne\RequestId\Factory\RequestIdFromConsoleFactory; use JulienDufresne\RequestId\Factory\RequestIdFromRequestFactory; $generator = new RamseyUuidGenerator(); $factory = RequestIdFromConsoleFactory($generator); $requestIdentifier = $factory->create(); // or, if the current process is coming from the web $factory = new RequestIdFromRequestFactory($generator, 'X-Root-Request-Id', 'X-Parent-Request-Id'); // will search for 'X-Root-Request-Id' and 'X-Parent-Request-Id' in $_SERVER array. // Be careful that $_SERVEr prefix headers with HTTP_ // You might want to set headers to HTTP_X-Root-Request-Id $requestIdentifier = $factory->create($_SERVER);
Generator
Generator is used to generate unique request id for the current running application.
This library provides one default generator: the RamseyUuidGenerator.
If you want to use it, you must install the ramsey/uuid
package.
You can define your own generator by implementing the UniqueIdGeneratorInterface
Guzzle
If you are using guzzle (package guzzlehttp/guzzle) to perform http requests, you can either add the RequestIdMiddleware to your handler stack:
use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use GuzzleHttp\Middleware; use JulienDufresne\RequestId\Guzzle\RequestIdMiddleware; $requestIdMiddleware = new RequestIdMiddleware(/* $requestIdentifier */); $stack = HandlerStack::create(); $stack->push(Middleware::mapRequest($requestIdMiddleware)); $client = new Client(['handler' => $stack]);
or use our factory to create a guzzle client:
use JulienDufresne\RequestId\Guzzle\ClientFactory; use JulienDufresne\RequestId\Guzzle\RequestIdMiddleware; $requestIdMiddleware = new RequestIdMiddleware(/* $requestIdentifier */); $factory = new ClientFactory(); $client = $factory->create();
Changing the headers sent
By default, sent headers are:
X-Root-Request-Id
for the root application identifierX-Parent-Request-Id
for the current application identifier (that will become the parent application of the http request)
You can change this in the middleware:
use JulienDufresne\RequestId\Guzzle\RequestIdMiddleware; $requestIdMiddleware = new RequestIdMiddleware( /* $requestIdentifier */, 'X-Root-Request-Id', 'X-Parent-Request-Id' );
Keep in mind that if you change this, you might want to change this in every applications
Monolog
If you are using monolog to manage your application logs, you can use the RequestIdentifierProcessor:
use JulienDufresne\RequestId\Monolog\RequestIdentifierProcessor; use Monolog\Logger; $processor = new RequestIdentifierProcessor(/* $requestIdentifier */); $logger = new Logger('channel-name'); $logger->pushProcessor([$processor]); $logger->addInfo('message');
Changing the extra keys
By default, the processor will add a request_id
array entry in the extra
section with the following keys:
current
for the current application identifierroot
for the root application identifierparent
for the parent application identifier
You can change this in the processor instantiation:
use JulienDufresne\RequestId\Monolog\RequestIdentifierProcessor; $processor = new RequestIdentifierProcessor( /* $requestIdentifier */, 'request_id', 'current', 'root', 'parent' );