Create correlation context

From dykyi-roman/awesome-claude-code · View interactive version

Create correlation context is an AI agent skill from the GitHub repository dykyi-roman/awesome-claude-code. 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.

How to use this skill

Skill file

Correlation Context Generator

Creates Correlation ID propagation infrastructure for distributed PHP applications.

When to Use

Generated Components

| Component | Layer | Path | Purpose |

|-----------|-------|------|---------|

| CorrelationId | Domain/Shared | src/Domain/Shared/Correlation/CorrelationId.php | UUID-based Value Object |

| CorrelationContext | Domain/Shared | src/Domain/Shared/Correlation/CorrelationContext.php | Immutable context holder |

| CorrelationContextMiddleware | Presentation | src/Presentation/Middleware/CorrelationContextMiddleware.php | PSR-15 middleware |

| CorrelationLogProcessor | Infrastructure | src/Infrastructure/Logging/CorrelationLogProcessor.php | Monolog processor |

| CorrelationMessageStamp | Infrastructure | src/Infrastructure/Messaging/CorrelationMessageStamp.php | Message bus stamp |

| Unit tests | Tests | tests/Unit/... | Tests for all components |

Component Characteristics

CorrelationId Value Object

CorrelationContext

CorrelationContextMiddleware (PSR-15)

CorrelationLogProcessor (Monolog)

CorrelationMessageStamp


Generation Process

Step 1: Generate Domain Layer

Path: src/Domain/Shared/Correlation/

  1. CorrelationId.php -- UUID-based Value Object
  2. CorrelationContext.php -- Immutable context holder

Use templates from references/templates.md (Domain section).

Step 2: Generate Presentation Layer

Path: src/Presentation/Middleware/

  1. CorrelationContextMiddleware.php -- PSR-15 middleware

Use templates from references/templates.md (Presentation section).

Step 3: Generate Infrastructure Layer

Path: src/Infrastructure/Logging/ and src/Infrastructure/Messaging/

  1. CorrelationLogProcessor.php -- Monolog processor
  2. CorrelationMessageStamp.php -- Message bus stamp

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/

  1. CorrelationIdTest.php
  2. CorrelationContextTest.php
  3. CorrelationContextMiddlewareTest.php
  4. CorrelationLogProcessorTest.php

Use templates from references/templates.md (Tests section).


File Placement

| Component | Default Path |

|-----------|--------------|

| CorrelationId | src/Domain/Shared/Correlation/CorrelationId.php |

| CorrelationContext | src/Domain/Shared/Correlation/CorrelationContext.php |

| Middleware | src/Presentation/Middleware/CorrelationContextMiddleware.php |

| Log Processor | src/Infrastructure/Logging/CorrelationLogProcessor.php |

| Message Stamp | src/Infrastructure/Messaging/CorrelationMessageStamp.php |

| Tests | tests/Unit/{layer}/...Test.php |

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.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.