Skip to content

Commit

Permalink
Merge pull request #12 from Clearfacts/CLEARFACTS-8492
Browse files Browse the repository at this point in the history
[CLEARFACTS-8492] correlation id
  • Loading branch information
ctrl-f5 authored Apr 7, 2023
2 parents b19381e + 9226912 commit e60093e
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 9 deletions.
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,16 @@ For more information see [cf-docs](https://github.com/Clearfacts/cf-docs/blob/66

### Installation

- Make sure you have composer installed globally and have php 7.3 or 7.4
- Make sure you have composer installed globally and have php 7.4 or higher
- Clone the project from github
- `cd <folder-name>`
- `make init`
- register the processor

```
Datalog\Processor\SessionRequestProcessor:
arguments:
- '@session'
tags:
- { name: monolog.processor, method: processRecord }
arguments:
- '@session'
tags:
- { name: monolog.processor, method: processRecord }
```
60 changes: 60 additions & 0 deletions src/Correlation/Correlation.php
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;
}
}
27 changes: 27 additions & 0 deletions src/Processor/CorrelationProcessor.php
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;
}
}
60 changes: 60 additions & 0 deletions tests/Correlation/CorrelationTest.php
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());
}
}

0 comments on commit e60093e

Please sign in to comment.