定制 oat-sa/lib-correlation-ids-guzzle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

oat-sa/lib-correlation-ids-guzzle

最新稳定版本:v0.4.0

Composer 安装命令:

composer require oat-sa/lib-correlation-ids-guzzle

包简介

OAT Correlation Ids Guzzle Library

README 文档

README

PHP library for correlation ids guzzle middleware based on the correlation ids library.

Table of contents

Installation

$ composer require oat-sa/lib-correlation-ids-guzzle

Principles

This library provides a ready to use guzzle middleware that forwards, as request headers, the correlation ids fetched from the correlation ids registry.

Notes

  • the current process correlation id will be forwarded as the parent one,
  • the root correlation id will be also forwarded.

More details about calls chaining available on the correlation ids library documentation.

Usage

With the provided factory

The GuzzleClientFactory creates for you a guzzle client with the middleware already enabled:

<?php declare(strict_types=1);

use OAT\Library\CorrelationIds\Registry\CorrelationIdsRegistry;
use OAT\Library\CorrelationIds\Registry\CorrelationIdsRegistryInterface;
use OAT\Library\CorrelationIdsGuzzle\Factory\GuzzleClientFactory;
use OAT\Library\CorrelationIdsGuzzle\Middleware\CorrelationIdsGuzzleMiddleware;

/** @var CorrelationIdsRegistryInterface $registry */
$registry = new CorrelationIdsRegistry(...);

$clientFactory = new GuzzleClientFactory(new CorrelationIdsGuzzleMiddleware($registry)));

$client = $clientFactory->create(['some' => 'options']);

...

$client->request('GET', 'http://example.com'); // Will forward correlation ids as request headers automatically.

Manually

You need to push the CorrelationIdsGuzzleMiddleware to your handler stack as follow:

<?php declare(strict_types=1);

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use OAT\Library\CorrelationIds\Registry\CorrelationIdsRegistry;
use OAT\Library\CorrelationIds\Registry\CorrelationIdsRegistryInterface;
use OAT\Library\CorrelationIdsGuzzle\Middleware\CorrelationIdsGuzzleMiddleware;

/** @var CorrelationIdsRegistryInterface $registry */
$registry = new CorrelationIdsRegistry(...);

$handlerStack = HandlerStack::create();
$handlerStack->push(Middleware::mapRequest(new CorrelationIdsGuzzleMiddleware($registry)));

$client = new Client(['handler' => $handlerStack]);

...

$client->request('GET', 'http://example.com'); // Will forward correlation ids as request headers automatically.

Note: you can customize the log context key names by providing you own CorrelationIdsHeaderNamesProviderInterface implementation and pass it to the CorrelationIdsGuzzleMiddleware constructor.

Tests

To run tests:

$ vendor/bin/phpunit

Note: see phpunit.xml.dist for available test suites.

统计信息

  • 总下载量: 8k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 43
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-only
  • 更新时间: 2019-11-22