proemergotech / correlate-php-guzzle
Adds correlation ID to Guzzle requests.
Installs: 2 777
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 4
Forks: 1
Open Issues: 0
Requires
- guzzlehttp/guzzle: 6.*
- proemergotech/correlate-php-core: 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 03:31: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-guzzle
Setup for Slim Framework
This example assumes you are already using proemergotech/correlate-php-psr-7 middleware!
use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use GuzzleHttp\Handler\CurlHandler; use ProEmergotech\Correlate\Correlate; use ProEmergotech\Correlate\Guzzle\GuzzleCorrelateMiddleware; use Monolog\Logger; $app = new \Slim\App(); $container = $app->getContainer(); $container['logger'] = function($container) { return new Logger(); }; $container['httpClient'] = function ($container) { $cid = $container['request']->getAttribute( Correlate::getParamName() ); $stack = HandlerStack::create(new CurlHandler()); $stack->push(new GuzzleCorrelateMiddleware($cid)); return new Client(['handler' => $stack]); }; // See "proemergotech/correlate-php-psr-7" project $app->add(new \ProEmergotech\Correlate\Psr7\Psr7CorrelateMiddleware($container['logger'])); /** * Example GET route * * @param \Psr\Http\Message\ServerRequestInterface $req PSR7 request * @param \Psr\Http\Message\ResponseInterface $res PSR7 response * @param array $args Route parameters * * @return \Psr\Http\Message\ResponseInterface */ $app->get('/foo', function ($req, $res, $args) { $httpClient = $this->get('httpClient'); $httpClient->request('GET', 'http://httpbin.org/'); // You can override correlation id here $httpClient->request('GET', 'http://httpbin.org/', [ 'headers' => [ Correlate::getHeaderName() => Correlate::id() ], ]); return $res; });
Setup for Laravel 5
Write a service provider to register a guzzle instance for future usage. Create a handler stack and push the middleware to it.
This example assumes you are already using proemergotech/correlate-php-laravel middleware!
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use ProEmergotech\Correlate\Correlate; use ProEmergotech\Correlate\Guzzle\GuzzleCorrelateMiddleware; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; class GuzzleHttpClientProvider extends ServiceProvider { public function register() { $this->app->bind('guzzle', function () { // Determine correlation id. $cid = $this->app['request']->getCorrelationId(); // If you use proemergotech/correlate-php-laravel middleware // identical but without macros $cid = $this->app['request']->headers->get(Correlate::getHeaderName()); $stack = HandlerStack::create(new CurlHandler()); $stack->push(new GuzzleCorrelateMiddleware($cid)); $config = isset($this->app['config']['guzzle']) ? $this->app['config']['guzzle'] : []; $config['handler'] = $stack; return new Client($config); }); } }
Add serverice provider to config/app.php in your Laravel project.
// config/app.php 'providers' => [ ... \App\Providers\GuzzleHttpClientProvider::class, ],
Setup for Lumen 5
Write a service provider to register a guzzle instance for future usage. Create a handler stack and push the middleware to it.
This example assumes you are already using proemergotech/correlate-php-laravel middleware!
// bootstrap/app.php // ... $app->bind('guzzle', function () use ($app) { // Determine correlation id. $cid = $app['request']->getCorrelationId(); // If you use proemergotech/correlate-php-laravel middleware // identical but without macros $cid = $this->app['request']->headers->get( \ProEmergotech\Correlate\Correlate::getHeaderName() ); $stack = HandlerStack::create(new CurlHandler()); $stack->push(new \ProEmergotech\Correlate\Guzzle\GuzzleCorrelateMiddleware($cid)); return new Client([ 'handler' => $stack ]); }); // ...
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.