PHP Classes

How to Use a PHP Correlation ID Class to Track Requests Using the Package Laravel Correlation: Generate a correlation identifier

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-02-20 (Yesterday) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
laravel-correlation 1.0MIT/X Consortium ...8HTTP, Libraries, PHP 8
Description 

Author

This package can generate a correlation identifier.

It provides a Laravel service class and a middleware class that can generate a unique identifier for the current HTTP request.

The middleware class can set the X-Correlation-ID response header with the correlation identifier value, which it can reuse in the next request if the HTTP client passes it in the X-Correlation-ID HTTP request header.

Picture of Mohamed Ahmed
  Performance   Level  
Name: Mohamed Ahmed <contact>
Classes: 3 packages by
Country: Egypt Egypt
Innovation award
Innovation award
Nominee: 3x

Documentation

Laravel Correlation ID Middleware

A package to manage correlation IDs for request tracing in Laravel applications.

A correlation ID is a unique identifier assigned to each request entering a distributed system. It helps developers trace requests, debug issues, and identify potential security threats. By attaching a correlation ID to each request, you can track its journey through various services and components, simplifying troubleshooting and monitoring.

? Installation

composer require mohamedahmed01/laravel-correlation

?? Configuration

Publish the Config File

php artisan vendor:publish --tag=correlation-config

Config Options (config/correlation.php)

  • header: Header name to use (default: `X-Correlation-ID`)
  • alternate_headers: Additional headers to check for a correlation ID (e.g., `X-Request-ID`, `Trace-ID`)
  • generator: Strategy for generating correlation IDs (`uuid`, `timestamp`, `hash`)
  • storage: Store correlation IDs in `cache`, `session`, or `none`
  • queue: Enable correlation ID propagation in queued jobs (default: `true`)
  • propagate: Automatically include correlation ID in outgoing HTTP requests (default: `true`)
  • auto_register_middleware: Automatically register middleware (default: `true`)

? Usage

The correlation ID will be:

  1. Extracted from incoming requests (from configured headers)
  2. Generated if missing (based on configured strategy)
  3. Stored in cache (if enabled)
  4. Included in all responses
  5. Available in logs
  6. Passed through queued jobs
  7. Propagated in HTTP requests
  8. Accessible via helper functions and Blade directives

Middleware Registration

If auto_register_middleware is disabled, manually register the middleware in app/Http/Kernel.php:

protected $middleware = [
    \Mohamedahmed01\LaravelCorrelation\Http\Middleware\CorrelationMiddleware::class,
];

Accessing the Correlation ID

? In Controllers or Services

$correlationId = correlation_id();

? In Blade Views

@correlationId

? In Jobs (Queued Work)

public function handle()
{
    $correlationId = correlation_id();
    Log::info("Processing job", ['correlation_id' => $correlationId]);
}

? In Logs

All logs during a request will automatically include the correlation ID:

{
    "message": "User created",
    "context": {
        "correlation_id": "123e4567-e89b-12d3-a456-426614174000"
    }
}

? HTTP Client Propagation

If propagate is enabled, correlation IDs will be automatically included in outgoing HTTP requests:

$response = Http::withCorrelationId()->get('https://api.example.com/data');

? Artisan Commands

List stored correlation IDs:

php artisan correlation:list

? Testing

Run the test suite to ensure functionality:

php artisan test

? License

MIT License


  Files folder image Files (12)  
File Role Description
Files folder image.github (1 directory)
Files folder imageconfig (1 file)
Files folder imagesrc (2 files, 2 directories)
Files folder imagetests (2 files, 1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file ReadME.md Doc. Documentation

  Files folder image Files (12)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (12)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file tests.yml Data Auxiliary data

  Files folder image Files (12)  /  config  
File Role Description
  Accessible without login Plain text file correlation.php Conf. Configuration script

  Files folder image Files (12)  /  src  
File Role Description
Files folder imageConsole (1 directory)
Files folder imageHttp (1 directory)
  Plain text file CorrelationServiceProvider.php Class Class source
  Accessible without login Plain text file helpers.php Aux. Auxiliary script

  Files folder image Files (12)  /  src  /  Console  
File Role Description
Files folder imageCommands (1 file)

  Files folder image Files (12)  /  src  /  Console  /  Commands  
File Role Description
  Plain text file CorrelationListCommand.php Class Class source

  Files folder image Files (12)  /  src  /  Http  
File Role Description
Files folder imageMiddleware (1 file)

  Files folder image Files (12)  /  src  /  Http  /  Middleware  
File Role Description
  Plain text file CorrelationMiddleware.php Class Class source

  Files folder image Files (12)  /  tests  
File Role Description
Files folder imageFeature (1 file)
  Plain text file Pest.php Class Class source
  Plain text file TestCase.php Class Class source

  Files folder image Files (12)  /  tests  /  Feature  
File Role Description
  Plain text file CorrelationTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0