phauthentic / correlation-id
Correlation ID and PSR7 middleware to inject it
Installs: 42 311
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: ^8.1
- psr/http-message: ^1.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- phpstan/phpstan: ^1.10.0
- phpunit/phpunit: ^10.0
- squizlabs/php_codesniffer: ^3.6
Suggests
- ramsey/uuid: If you want to use this UUID generator for the correlation ID. It will be automatically used instead of built in UUID4v1 generator if it is available.
README
An implementation of a Correlation ID and a framework agnostic PSR 15-HTTP middlware.
A Correlation ID, also known as a Transit ID, is a unique identifier value that is attached to requests and messages that allow reference to a particular transaction or event chain. The Correlation Pattern, which depends on the use of Correlation ID is a well documented Enterprise Integration Pattern.
Documentation
The Correlation ID Value Object
The correlation ID is a singleton class that will always return the same ID for the current life-cycle of the request.
Calling CorrelationID::toString()
will return a string and will return the same string for the whole live cycle of the application. You can compare a string to the Correlation ID by calling CorrelationID::sameAs('your-string')
.
By default it uses its internal implementation to generate a UUID v4 as value of the Correlation ID. If you are using ramsey/uuid, it will use it automatically.
PSR 15 HTTP Middleware
The middleware will automatically put the correlation ID into your request object as attribute and header value. By default both use the CorrelationID
name.
$middleware = new CorrelationIDMiddleware( CorrelationID::toString() );
Response
Since there is no standard for where this needs to be done, just add the correlation ID to your response where it suits your architecture or framework the best.
$response->withHeader('CorrelationID', CorrelationId::toString());
For Symfony there is a bundle available that will automatically make the Correlation ID available in the request and response object.
Copyright & License
Licensed under the MIT license.
Copyright (c) Phauthentic / Florian Krämer