Awesome-claude-code create-correlation-context
Generates Correlation ID propagation components for PHP 8.4. Creates PSR-15 middleware, Monolog processor, message bus header propagation, and CorrelationContext value object. Includes unit tests.
install
source · Clone the upstream repo
git clone https://github.com/dykyi-roman/awesome-claude-code
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/dykyi-roman/awesome-claude-code "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/create-correlation-context" ~/.claude/skills/dykyi-roman-awesome-claude-code-create-correlation-context && rm -rf "$T"
manifest:
skills/create-correlation-context/SKILL.mdsource content
Correlation Context Generator
Creates Correlation ID propagation infrastructure for distributed PHP applications.
When to Use
- Need to trace requests across multiple services or layers
- Log entries from different components must be linked together
- Async flows (RabbitMQ, Symfony Messenger) lose request context
- Debugging distributed systems requires end-to-end tracing
- Implementing observability and distributed tracing
Generated Components
| Component | Layer | Path | Purpose |
|---|---|---|---|
| Domain/Shared | | UUID-based Value Object |
| Domain/Shared | | Immutable context holder |
| Presentation | | PSR-15 middleware |
| Infrastructure | | Monolog processor |
| Infrastructure | | Message bus stamp |
| Unit tests | Tests | | Tests for all components |
Component Characteristics
CorrelationId Value Object
- Immutable,
class in Domain layerfinal readonly - UUID v4 based with factory method
generate() - Constructor accepts existing string UUID
- Implements
andStringableJsonSerializable - Equality comparison via
methodequals()
CorrelationContext
- Immutable context holder with
,correlationId
, optionalcausationIduserId - Factory method
generates new correlation IDcreate() - Factory method
extracts from PSR-7 request headersfromRequest() - Wither methods:
,withCausationId()withUserId()
CorrelationContextMiddleware (PSR-15)
- Extracts
header from incoming request (or generates new UUID)X-Correlation-ID - Also reads
if presentX-Causation-ID - Stores
as request attributeCorrelationContext - Adds
to response headersX-Correlation-ID - Thread-safe via request attribute passing (no global state)
CorrelationLogProcessor (Monolog)
- Implements Monolog
ProcessorInterface - Adds
,correlation_id
to every log recordcausation_id
fieldextra - Reads context from a
(request-scoped)CorrelationContextHolder
CorrelationMessageStamp
- Implements Symfony Messenger
(or custom stamp interface)StampInterface - Carries
andcorrelationId
through message buscausationId - Used by middleware to propagate context into async handlers
- AMQP header mapping for RabbitMQ interoperability
Generation Process
Step 1: Generate Domain Layer
Path:
src/Domain/Shared/Correlation/
-- UUID-based Value ObjectCorrelationId.php
-- Immutable context holderCorrelationContext.php
Use templates from
references/templates.md (Domain section).
Step 2: Generate Presentation Layer
Path:
src/Presentation/Middleware/
-- PSR-15 middlewareCorrelationContextMiddleware.php
Use templates from
references/templates.md (Presentation section).
Step 3: Generate Infrastructure Layer
Path:
src/Infrastructure/Logging/ and src/Infrastructure/Messaging/
-- Monolog processorCorrelationLogProcessor.php
-- Message bus stampCorrelationMessageStamp.php
Use templates from
references/templates.md (Infrastructure section).
Step 4: Generate Tests
Path:
tests/Unit/Domain/Shared/Correlation/ and tests/Unit/Presentation/Middleware/ and tests/Unit/Infrastructure/
CorrelationIdTest.phpCorrelationContextTest.phpCorrelationContextMiddlewareTest.phpCorrelationLogProcessorTest.php
Use templates from
references/templates.md (Tests section).
File Placement
| Component | Default Path |
|---|---|
| CorrelationId | |
| CorrelationContext | |
| Middleware | |
| Log Processor | |
| Message Stamp | |
| Tests | |
Adapt paths to match existing project structure detected via:
Glob: src/Domain/**/*.php Glob: src/Presentation/**/*.php Glob: src/Infrastructure/**/*.php
Quick Template Reference
CorrelationId (Value Object)
final readonly class CorrelationId implements \Stringable, \JsonSerializable { public function __construct(public string $value) {} public static function generate(): self { /* UUID v4 */ } public function equals(self $other): bool { /* comparison */ } }
CorrelationContext (Context Holder)
final readonly class CorrelationContext { public function __construct( public CorrelationId $correlationId, public ?string $causationId = null, public ?string $userId = null, ) {} public static function create(): self { /* new with generated ID */ } public static function fromRequest(ServerRequestInterface $request): self { /* extract headers */ } }
Middleware (PSR-15)
final readonly class CorrelationContextMiddleware implements MiddlewareInterface { public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $context = CorrelationContext::fromRequest($request); $request = $request->withAttribute(CorrelationContext::class, $context); $response = $handler->handle($request); return $response->withHeader('X-Correlation-ID', $context->correlationId->value); } }
See
references/templates.md for complete implementations and references/examples.md for integration examples.
Usage Example
Middleware Registration (Slim/Mezzio)
$app->add(new CorrelationContextMiddleware());
DI Configuration
return [ CorrelationContextMiddleware::class => autowire(), CorrelationLogProcessor::class => autowire(), ];
Reading Context in Handler
$context = $request->getAttribute(CorrelationContext::class); $this->logger->info('Processing order', ['orderId' => $orderId]); // Log automatically includes correlation_id via processor
See
references/examples.md for Symfony, Laravel, and message bus integration.