Skip to content

Middleware PSR-15 compliant which handle Correlation ID Header

License

Notifications You must be signed in to change notification settings

woody-labs/middleware-correlation-id

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Middleware Correlation ID

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

About

Middleware PSR-15 compliant which handle Correlation ID Header

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages