woody/middleware-correlation-id

Middleware PSR-15 compliant which handle Correlation ID Header

dev-master 2019-01-10 22:51 UTC

This package is auto-updated.

Last update: 2024-04-11 14:31:18 UTC


README

A Correlation ID is a unique identifier value that is attached to requests and messages that allow reference to a particular transaction or event chain.

Presentation

Some loadbalancer or WAF provide such additional headers, so you just need to normalize its name a propagate it into your system (sub-request, logs, ...).

Correlation ID is detected as it :

  • CF-RAY
  • X-Correlation-ID
  • X-Request-ID

If nothing is found, a new one is generated using a UUID generator and added as X-Correlation-ID and returned into the response.

Implementation

Just add the middleware into your dispatcher pipeline.

// @todo: generate request

$dispatcher = new Dispatcher();
$dispatcher->pipe(new CorrelationIdMiddleware());

// @todo: add other middleware

$response = $dispatcher->handle($request);

Correlation ID is also available as attribute under the correlation-id name.

Both headers and response header can be overridden.

// @todo: generate request

$correlationMiddleware = new CorrelationIdMiddleware(
    [
        'X-Custom-Header',
        CorrelationIdMiddleware::HEADER_CORRELATION_ID,
    ],
    'X-Custom-Header'
);

$dispatcher = new Dispatcher();
$dispatcher->pipe($correlationMiddleware);

// @todo: add other middleware

$response = $dispatcher->handle($request);

Note: adding correlation id header in response can be skipped by specifying false as secondary parameter in the constructor.

Documentation

The Value of Correlation IDs

Correlation IDs for microservices architectures