tourze/request-id-bundle
Composer 安装命令:
composer require tourze/request-id-bundle
包简介
Request ID Generator for Symfony
README 文档
README
A Symfony bundle for request ID management, enabling tracking and correlation of requests across your application. Supports HTTP, message queue, and CLI commands for comprehensive request tracing in distributed systems.
Features
- Generate unique request IDs using UUID with Base58 encoding (shorter than standard UUID format)
- Coroutine-safe request ID storage with automatic reset mechanism to prevent memory leaks
- Automatically add request ID to HTTP request/response headers
- Propagate request ID in message queues via Symfony Messenger middleware
- Generate request ID for CLI commands with special "CLI" prefix
- Integrate request ID into logs automatically via Monolog processor
- Support distributed system tracing across different services
Installation
composer require tourze/request-id-bundle
Requirements:
- PHP >= 8.1
- Symfony >= 6.4
- Symfony components: HTTP Kernel, Messenger, Console, Uid
- Monolog >= 3.1
- See composer.json for complete dependencies
Quick Start
1. HTTP Integration
The bundle automatically:
- Checks for request ID in incoming HTTP headers
- Generates new ID if none exists or not trusted
- Sets ID in response headers
- Stores ID in coroutine-safe storage
// In your controllers, you can access the request ID: use RequestIdBundle\Service\RequestIdStorage; class MyController { public function index(RequestIdStorage $requestIdStorage) { $currentRequestId = $requestIdStorage->getRequestId(); // Use the request ID... } }
2. Message Queue Integration
Request IDs are automatically propagated through message queues:
// The message will automatically carry the current request ID $messageBus->dispatch(new MyMessage()); // In message handlers, the original request ID is available: public function handleMessage(MyMessage $message, RequestIdStorage $requestIdStorage) { $originalRequestId = $requestIdStorage->getRequestId(); // Process with original request context... }
3. CLI Support
CLI commands automatically get request IDs with a "CLI" prefix:
$ php bin/console my:command
# A request ID like "CLIxxxxx" will be generated automatically
// In your commands: use RequestIdBundle\Service\RequestIdStorage; class MyCommand extends Command { private RequestIdStorage $requestIdStorage; public function __construct(RequestIdStorage $requestIdStorage) { $this->requestIdStorage = $requestIdStorage; parent::__construct(); } protected function execute(InputInterface $input, OutputInterface $output) { $commandRequestId = $this->requestIdStorage->getRequestId(); // CLI[uuid] // Use request ID... } }
4. Log Integration
Request IDs are automatically added to log records:
$logger->info('Processing user request', ['user_id' => 123]); // Log output will include "request_id": "7fT9P5RoJ3a..."
How It Works
For a detailed workflow diagram, see WORKFLOW.md.
-
HTTP Requests:
RequestIdSubscriberhandles request/response events- Checks if request ID exists in headers
- Uses existing ID if trusted, otherwise generates new one
- Adds ID to response headers
-
Message Queue:
RequestIdMiddlewareintercepts message dispatching/handling- Attaches
RequestIdStampto outgoing messages - Restores original request ID when consuming messages
- Cleans up after message handling
-
CLI Commands:
CommandRequestIdSubscribergenerates "CLI"-prefixed request ID- Sets ID at command start
- Cleans up at command termination
-
Log Integration:
RequestIdProcessoradds request ID to all log records- Makes request ID available in log formatters and outputs
Performance Optimization
- Uses Base58 encoding for shorter IDs than standard UUIDs
- Coroutine support ensures thread safety in async environments
- Automatic cleanup prevents memory leaks in long-running processes
- Middleware approach avoids performance impact on non-request paths
Contributing
Contributions are welcome! Please see our contribution guidelines for details.
License
The MIT License (MIT). Please see License File for more information.
tourze/request-id-bundle 适用场景与选型建议
tourze/request-id-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24.15k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 04 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tourze/request-id-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tourze/request-id-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 24.15k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 18
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-10