|
1 | | -Laravel Correlation ID Middleware |
2 | | -================================= |
| 1 | +# Laravel Correlation ID Middleware |
3 | 2 |
|
4 | | -A package to manage correlation IDs for request tracing in Laravel applications, |
5 | | -A correlation ID is a randomly generated identifier for every request entering a distributed system. Developers use the correlation identifier to trace the request as it makes its way through the system, identify any cyber security threats, and prevent them. |
6 | | -The correlation ID basically serves as a thread that connects the various parts of a request as it moves through the system. This greatly simplifies distributed system debugging and troubleshooting by allowing developers to track a request’s progress and readily pinpoint the service or component where an issue occurred. |
| 3 | +A package to manage correlation IDs for request tracing in Laravel applications. |
7 | 4 |
|
| 5 | +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. |
8 | 6 |
|
9 | | -Installation |
10 | | ------------- |
| 7 | +--- |
11 | 8 |
|
12 | | - composer require mohamedahmed01/laravel-correlation |
| 9 | +## 📌 Installation |
13 | 10 |
|
14 | | -Configuration |
15 | | -------------- |
| 11 | +```sh |
| 12 | +composer require mohamedahmed01/laravel-correlation |
| 13 | +``` |
16 | 14 |
|
17 | | -Publish the config file: |
| 15 | +--- |
18 | 16 |
|
19 | | - php artisan vendor:publish --tag=correlation-config |
| 17 | +## ⚙️ Configuration |
20 | 18 |
|
21 | | -Config options (`config/correlation.php`): |
| 19 | +### Publish the Config File |
22 | 20 |
|
23 | | -* `header`: Header name to use (default: X-Correlation-ID) |
24 | | -* `auto_register_middleware`: Automatically register middleware (default: true) |
| 21 | +```sh |
| 22 | +php artisan vendor:publish --tag=correlation-config |
| 23 | +``` |
25 | 24 |
|
26 | | -Usage |
27 | | ------ |
| 25 | +### Config Options (`config/correlation.php`) |
| 26 | + |
| 27 | +- **`header`**: Header name to use (default: `X-Correlation-ID`) |
| 28 | +- **`alternate_headers`**: Additional headers to check for a correlation ID (e.g., `X-Request-ID`, `Trace-ID`) |
| 29 | +- **`generator`**: Strategy for generating correlation IDs (`uuid`, `timestamp`, `hash`) |
| 30 | +- **`storage`**: Store correlation IDs in `cache`, `session`, or `none` |
| 31 | +- **`queue`**: Enable correlation ID propagation in queued jobs (default: `true`) |
| 32 | +- **`propagate`**: Automatically include correlation ID in outgoing HTTP requests (default: `true`) |
| 33 | +- **`auto_register_middleware`**: Automatically register middleware (default: `true`) |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +## 🚀 Usage |
28 | 38 |
|
29 | 39 | The correlation ID will be: |
30 | 40 |
|
31 | | -1. Read from incoming requests |
32 | | -2. Generated if missing |
33 | | -3. Added to all responses |
34 | | -4. Available in logs |
35 | | -5. Accessible via `correlation_id()` helper |
| 41 | +1. Extracted from incoming requests (from configured headers) |
| 42 | +2. Generated if missing (based on configured strategy) |
| 43 | +3. Stored in cache (if enabled) |
| 44 | +4. Included in all responses |
| 45 | +5. Available in logs |
| 46 | +6. Passed through queued jobs |
| 47 | +7. Propagated in HTTP requests |
| 48 | +8. Accessible via helper functions and Blade directives |
36 | 49 |
|
37 | | -### Manual Middleware Registration |
| 50 | +### Middleware Registration |
38 | 51 |
|
39 | | -Add to `app/Http/Kernel.php`: |
| 52 | +If `auto_register_middleware` is disabled, manually register the middleware in `app/Http/Kernel.php`: |
40 | 53 |
|
41 | | - protected $middleware = [ |
42 | | - \Mohamedahmed01\LaravelCorrelation\Http\Middleware\CorrelationMiddleware::class, |
43 | | - ]; |
| 54 | +```php |
| 55 | +protected $middleware = [ |
| 56 | + \Mohamedahmed01\LaravelCorrelation\Http\Middleware\CorrelationMiddleware::class, |
| 57 | +]; |
| 58 | +``` |
44 | 59 |
|
45 | 60 | ### Accessing the Correlation ID |
46 | 61 |
|
| 62 | +#### 📌 In Controllers or Services |
| 63 | + |
| 64 | +```php |
| 65 | +$correlationId = correlation_id(); |
| 66 | +``` |
| 67 | + |
| 68 | +#### 📌 In Blade Views |
| 69 | + |
| 70 | +```blade |
| 71 | +@correlationId |
| 72 | +``` |
| 73 | + |
| 74 | +#### 📌 In Jobs (Queued Work) |
| 75 | + |
| 76 | +```php |
| 77 | +public function handle() |
| 78 | +{ |
47 | 79 | $correlationId = correlation_id(); |
| 80 | + Log::info("Processing job", ['correlation_id' => $correlationId]); |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +#### 📌 In Logs |
| 85 | + |
| 86 | +All logs during a request will automatically include the correlation ID: |
| 87 | + |
| 88 | +```json |
| 89 | +{ |
| 90 | + "message": "User created", |
| 91 | + "context": { |
| 92 | + "correlation_id": "123e4567-e89b-12d3-a456-426614174000" |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### 🌍 HTTP Client Propagation |
| 98 | + |
| 99 | +If `propagate` is enabled, correlation IDs will be automatically included in outgoing HTTP requests: |
| 100 | + |
| 101 | +```php |
| 102 | +$response = Http::withCorrelationId()->get('https://api.example.com/data'); |
| 103 | +``` |
| 104 | + |
| 105 | +### 🔧 Artisan Commands |
| 106 | + |
| 107 | +List stored correlation IDs: |
| 108 | + |
| 109 | +```sh |
| 110 | +php artisan correlation:list |
| 111 | +``` |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +## ✅ Testing |
| 116 | + |
| 117 | +Run the test suite to ensure functionality: |
| 118 | + |
| 119 | +```sh |
| 120 | +php artisan test |
| 121 | +``` |
48 | 122 |
|
49 | | -### Logging |
| 123 | +--- |
50 | 124 |
|
51 | | -All logs during a request will automatically include: |
| 125 | +## 📜 License |
52 | 126 |
|
53 | | - ['correlation_id' => 'your-uuid'] |
| 127 | +MIT License |
0 commit comments