-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Clearfacts/CLEARFACTS-8492
[CLEARFACTS-8492] correlation id
- Loading branch information
Showing
4 changed files
with
152 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Datalog\Correlation; | ||
|
||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
class Correlation | ||
{ | ||
private ?RequestStack $requestStack; | ||
private static ?string $correlationId = null; | ||
|
||
public function __construct(?RequestStack $requestStack = null) | ||
{ | ||
$this->requestStack = $requestStack; | ||
} | ||
|
||
public function init(): void | ||
{ | ||
if (!self::$correlationId) { | ||
self::$correlationId = $this->getIdFromRequestStack() | ||
?? $this->getIdFromGlobals() | ||
?? uniqid('cf', true); | ||
} | ||
} | ||
|
||
public static function setId(?string $correlationId): void | ||
{ | ||
self::$correlationId = $correlationId; | ||
} | ||
|
||
public static function getId(): ?string | ||
{ | ||
return self::$correlationId; | ||
} | ||
|
||
public function getIdFromRequestStack(): ?string | ||
{ | ||
if (!$this->requestStack) { | ||
return null; | ||
} | ||
|
||
$request = $this->requestStack->getMainRequest(); | ||
|
||
if (!$request) { | ||
return null; | ||
} | ||
|
||
return $request->headers->get('X-Correlation-ID'); | ||
} | ||
|
||
public function getIdFromGlobals(): ?string | ||
{ | ||
return $_SERVER['HTTP_X_CORRELATION_ID'] | ||
?? $_SERVER['X_CORRELATION_ID'] | ||
?? $_SERVER['CORRELATION_ID'] | ||
?? $_ENV['CORRELATION_ID'] | ||
?? null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Datalog\Processor; | ||
|
||
use Datalog\Correlation\Correlation; | ||
use Monolog\Processor\ProcessorInterface; | ||
|
||
class CorrelationProcessor implements ProcessorInterface | ||
{ | ||
private Correlation $correlation; | ||
|
||
public function __construct(Correlation $correlation) | ||
{ | ||
$this->correlation = $correlation; | ||
} | ||
|
||
public function __invoke(array $record): array | ||
{ | ||
$this->correlation->init(); | ||
|
||
$record['extra']['correlation_id'] = $this->correlation::getId(); | ||
|
||
return $record; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Datalog\Correlation; | ||
|
||
use Datalog\Correlation\Correlation; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Tests\Datalog\TestCase; | ||
|
||
class CorrelationTest extends TestCase | ||
{ | ||
private Correlation $correlation; | ||
private RequestStack $requestStack; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->requestStack = new RequestStack(); | ||
$this->correlation = new Correlation($this->requestStack); | ||
$this->correlation::setId(null); | ||
} | ||
|
||
public function testInit(): void | ||
{ | ||
$this->assertEmpty($this->correlation::getId()); | ||
|
||
$this->correlation->init(); | ||
|
||
$this->assertNotEmpty($this->correlation::getId()); | ||
|
||
$firstId = $this->correlation::getId(); | ||
$this->correlation->init(); | ||
|
||
$this->assertSame($firstId, $this->correlation::getId()); | ||
} | ||
|
||
public function testInitPrefersRequestStack(): void | ||
{ | ||
$_SERVER['HTTP_X_CORRELATION_ID'] = 'test server'; | ||
$request = new Request(); | ||
$request->headers->set('X-Correlation-ID', 'test request'); | ||
$this->requestStack->push($request); | ||
|
||
$this->correlation->init(); | ||
|
||
$this->assertSame('test request', $this->correlation::getId()); | ||
} | ||
|
||
public function testInitFallsBackToGlobals(): void | ||
{ | ||
$_SERVER['HTTP_X_CORRELATION_ID'] = 'test server'; | ||
$request = new Request(); | ||
$this->requestStack->push($request); | ||
|
||
$this->correlation->init(); | ||
|
||
$this->assertSame('test server', $this->correlation::getId()); | ||
} | ||
} |