woody / middleware-correlation-id
符合PSR-15规范的中间件,用于处理关联ID头
dev-master
2019-01-10 22:51 UTC
Requires
- php: ^7.1
- ramsey/uuid: ^3.6
- woody/http-server-middleware: dev-master
This package is auto-updated.
Last update: 2024-09-11 15:34:09 UTC
README
关联ID是一个唯一的标识值,它附加到请求和消息中,允许引用特定的交易或事件链。
展示
某些负载均衡器或WAF提供这样的附加头,因此您只需规范化其名称并将其传播到您的系统中(子请求、日志等)。
关联ID的检测方式为
- CF-RAY
- X-Correlation-ID
- X-Request-ID
如果没有找到任何内容,则使用UUID生成器生成一个新的ID,并将其添加为X-Correlation-ID
并返回到响应中。
实现
只需将中间件添加到您的dispatcher
管道中。
// @todo: generate request $dispatcher = new Dispatcher(); $dispatcher->pipe(new CorrelationIdMiddleware()); // @todo: add other middleware $response = $dispatcher->handle($request);
关联ID也作为名为correlation-id
的attribute
可用。
headers
和response header
都可以被覆盖。
// @todo: generate request $correlationMiddleware = new CorrelationIdMiddleware( [ 'X-Custom-Header', CorrelationIdMiddleware::HEADER_CORRELATION_ID, ], 'X-Custom-Header' ); $dispatcher = new Dispatcher(); $dispatcher->pipe($correlationMiddleware); // @todo: add other middleware $response = $dispatcher->handle($request);
注意:可以通过在构造函数中将第二个参数指定为false
来跳过在响应中添加关联ID头。