PHPackages                             tiny-blocks/http-middleware-correlation-id - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. tiny-blocks/http-middleware-correlation-id

ActiveLibrary[HTTP &amp; Networking](/categories/http)

tiny-blocks/http-middleware-correlation-id
==========================================

PSR-15 middleware that ensures every request has a correlation identifier propagated through requests, responses, and logs.

1.0.0(today)001MITPythonPHP ^8.5CI passing

Since Jun 26Pushed todayCompare

[ Source](https://github.com/tiny-blocks/http-middleware-correlation-id)[ Packagist](https://packagist.org/packages/tiny-blocks/http-middleware-correlation-id)[ Docs](https://github.com/tiny-blocks/http-middleware-correlation-id)[ RSS](/packages/tiny-blocks-http-middleware-correlation-id/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (11)Versions (2)Used By (1)

Http Middleware Correlation Id
==============================

[](#http-middleware-correlation-id)

[![License](https://camo.githubusercontent.com/f8df3091bbe1149f398a5369b2c39e896766f9f6efba3477c63e9b4aa940ef14/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e)](https://github.com/tiny-blocks/http-middleware-correlation-id/blob/main/LICENSE)

- [Overview](#overview)
- [Installation](#installation)
- [How to use](#how-to-use)
    - [Wiring the middleware](#wiring-the-middleware)
    - [Configuring a custom provider](#configuring-a-custom-provider)
    - [Emitting correlated logs](#emitting-correlated-logs)
- [License](#license)
- [Contributing](#contributing)

Overview
--------

[](#overview)

Provides a PSR-15 middleware that guarantees every inbound HTTP request carries a correlation identifier. When the incoming request already includes a `Correlation-Id` header, the value is reused; otherwise a new identifier is generated through a configurable provider (UUID v4 by default). The correlation ID is exposed as the request attribute `correlationId` so downstream handlers can read it, and is echoed back through the `Correlation-Id`response header for end-to-end tracing across services.

The library also ships `CorrelatedLogger`, a thin wrapper over any PSR-3 `LoggerInterface` that resolves the correlation ID from the request and attaches it to the log context under the `correlation_id` key. This produces structured logs that can be grouped and filtered by the correlation ID without any extra plumbing in the consumer's log calls.

Installation
------------

[](#installation)

```
composer require tiny-blocks/http-middleware-correlation-id
```

How to use
----------

[](#how-to-use)

### Wiring the middleware

[](#wiring-the-middleware)

Builds the middleware with the default UUID v4 provider and processes a request through it.

```
