juliendufresne/inter-app-request-identifier

This package is abandoned and no longer maintained. The author suggests using the https://github.com/ManoManoTech/correlation-ids package instead.

Defines an interface to identify a process running between multiple applications

v0.2.0 2018-03-04 17:31 UTC

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 identifier
  • X-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 identifier
  • root for the root application identifier
  • parent 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'
);