oat-sa / lib-correlation-ids
OAT 关联ID库
0.2.0
2022-01-13 08:59 UTC
Requires
- php: ^7.1.3 || ^8.0
- ramsey/uuid: ^3.8
Requires (Dev)
- phpunit/phpunit: ^8.3
This package is auto-updated.
Last update: 2024-09-17 14:05:10 UTC
README
PHP库,用于关联ID管理。
目录
安装
$ composer require oat-sa/lib-correlation-ids
原则
此库提供了一个接口,允许访问3种类型的关联ID
getCurrentCorrelationId()
:用于当前应用程序进程。getParentCorrelationId()
:用于调用您的应用程序的父应用程序,如果有。getRootCorrelationId()
:用于最初所有调用都源自的根应用程序。
示例
Request
|
v
+---------------+ current: xxx
| Application A | parent: NULL
+---+-----------+ root: NULL
|
v
+---------------+ current: yyy
| Application B | parent: xxx
+---+-----------+ root: xxx
|
v
+---------------+ current: zzz
| Application C | parent: yyy
+---------------+ root: xxx
用法
从HTTP上下文
<?php declare(strict_types=1); use Psr\Http\Message\RequestInterface; use OAT\Library\CorrelationIds\Builder\CorrelationIdsRegistryBuilder; use OAT\Library\CorrelationIds\Generator\CorrelationIdGenerator; $builder = new CorrelationIdsRegistryBuilder(new CorrelationIdGenerator()); /** @var RequestInterface $request */ $registry = $builder->buildFromRequestHeaders($request->getHeaders()); ... $registry->getCurrentCorrelationId(); // current correlation id $registry->getParentCorrelationId(); // parent correlation id (nullable) $registry->getRootCorrelationId(); // root correlation id (nullable)
注意:您可以通过提供自己的CorrelationIdsHeaderNamesProviderInterface实现并传递给CorrelationIdsRegistryBuilder
构造函数来自定义使用的HTTP头名称。
手动
<?php declare(strict_types=1); use OAT\Library\CorrelationIds\Builder\CorrelationIdsRegistryBuilder; use OAT\Library\CorrelationIds\Generator\CorrelationIdGenerator; $builder = new CorrelationIdsRegistryBuilder(new CorrelationIdGenerator()); $registry = $builder->build( 'parentCorrelationId', // optional 'rootCorrelationId' // optional );
测试
运行测试
$ vendor/bin/phpunit
注意:有关可用的测试套件,请参阅phpunit.xml.dist。