proemergotech / correlate-php-psr-7
PSR-7 middleware to handle incoming correlation id header for microservices.
Requires
- proemergotech/correlate-php-core: dev-master
- proemergotech/correlate-php-monolog: dev-master
- psr/http-message: ^1.0
Requires (Dev)
- fzaninotto/faker: ~1.4
- mockery/mockery: ~0.9
- phpunit/phpunit: ^5.0
- squizlabs/php_codesniffer: ^2.8
This package is not auto-updated.
Last update: 2024-12-22 04:47:06 UTC
README
Overview
It's very difficult to track a request accross the system when we are working with microservices. We came out a solution for that. We generate a unique version 4 uuid for every request and every service passes this id via request header to other services. We call this correlation ID.
Packages
- proemergotech/correlate-php-laravel
- Middleware for Laravel and Lumen frameworks.
- proemergotech/correlate-php-psr-7
- Middleware for any PSR-7 compatible frameworks like Slim Framework.
- proemergotech/correlate-php-monolog
- Monolog processor for correlate middlewares (you don't have to use this directly).
- proemergotech/correlate-php-guzzle
- Guzzle middleware to add correlation id to every requests.
- proemergotech/correlate-php-core
- Common package for correlate id middlewares to provide consistent header naming accross projects.
Installation
- Install via composer
composer require proemergotech/correlate-php-psr-7
Setup for Slim 3 framework
To use this class as a middleware, you can use ->add( new ExampleMiddleware() );
function chain after the $app
, Route
, or group()
, which in the code below, any one of these, could represent $subject
.
$logger = $app['monolog']; // Must be \Monolog\Logger // This is an optional callback function to set correlation id to a DIC. $callback = function($correlationId) use ($app) { $app->getContainer()['cid'] = $correlationId; } $subject->add(new \ProEmergotech\Correlate\Psr7\Psr7CorrelateMiddleware($logger, $callback));
Passing \Monolog\Logger
is optional.
Usage
This middleware automatically adds correlation id (coming from request header) to every log messages if you provided the optional \Monolog\Logger
instance to middleware's constructor.
You can access the correlation id IN A ROUTE CONTROLLER if you want to work with it.
$cid = $request->getAttribute(\ProEmergotech\Correlate\Correlate::getParamName());
Contributing
See CONTRIBUTING.md
file.
Credits
This package developed by Soma Szélpál at Pro Emergotech Ltd..
License
This project is released under the MIT License.