<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
philiprehberger / laravel-correlation-id example snippets
use PhilipRehberger\CorrelationId\AddCorrelationId;
// Register the middleware in bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
$middleware->prepend(AddCorrelationId::class);
})
use PhilipRehberger\CorrelationId\CorrelationId;
// Via the helper class
$id = CorrelationId::get();
CorrelationId::set('my-custom-id');
// Via the request macro
$id = $request->correlationId();
// config/correlation-id.php
// Reject incoming IDs longer than this. Set to null to disable.
'max_length' => 255,
// Incoming IDs must fully match this pattern. The default accepts any
// printable, non-space ASCII (UUID, ULID, nanoid, hex, base64url). Set to
// null to disable pattern validation.
'validation_pattern' => '/^[\x21-\x7e]+$/',
use PhilipRehberger\CorrelationId\CorrelationId;
CorrelationId::isValid('3f2504e0-4f89-41d3-9a0c-0305e82c3301'); // true
CorrelationId::isValid("bad\r\nvalue"); // false
use PhilipRehberger\CorrelationId\Concerns\TracksCorrelationId;
use PhilipRehberger\CorrelationId\Middleware\CorrelationIdJobMiddleware;
class ProcessOrder implements ShouldQueue
{
use TracksCorrelationId;
public function middleware(): array
{
return [new CorrelationIdJobMiddleware];
}
public function handle(): void
{
// CorrelationId::get() returns the same ID from dispatch time
}
}
use Illuminate\Support\Facades\Http;
use PhilipRehberger\CorrelationId\CorrelationId;
$response = Http::withMiddleware(CorrelationId::httpMiddleware())
->get('https://api.example.com/orders');
// Uses a custom header name
$response = Http::withMiddleware(CorrelationId::httpMiddleware('X-Request-Id'))
->get('https://api.example.com/orders');
use PhilipRehberger\CorrelationId\CorrelationId;
$span = CorrelationId::startSpan('external-api-call', ['url' => $url]);
$response = Http::get($url);
$ended = CorrelationId::endSpan($span);
// Access span data
$ended->durationMs(); // Duration in milliseconds
$ended->toArray(); // Full array representation
// Retrieve all completed spans
$spans = CorrelationId::spans();
// Clear spans (e.g., between tests)
CorrelationId::clearSpans();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.