Skip to content

Commit

Permalink
feature/MTCE-260-EasyRequestId (#1485)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexndlm authored Jul 29, 2024
1 parent eafd668 commit 5abc771
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 78 deletions.
5 changes: 2 additions & 3 deletions packages/EasyRequestId/bundle/config/definition.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
declare(strict_types=1);

use EonX\EasyRequestId\Common\Provider\RequestIdProviderInterface;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;

return static function (DefinitionConfigurator $definition) {
Expand All @@ -11,11 +10,11 @@
->addDefaultsIfNotSet()
->children()
->scalarNode('correlation_id')
->defaultValue(RequestIdProviderInterface::DEFAULT_HTTP_HEADER_CORRELATION_ID)
->defaultValue('X-CORRELATION-ID')
->info('Header used to resolve/send the correlation id from the HTTP request')
->end()
->scalarNode('request_id')
->defaultValue(RequestIdProviderInterface::DEFAULT_HTTP_HEADER_REQUEST_ID)
->defaultValue('X-REQUEST-ID')
->info('Header used to resolve/send the request id from the HTTP request')
->end()
->end()
Expand Down
13 changes: 6 additions & 7 deletions packages/EasyRequestId/laravel/EasyRequestIdServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use EonX\EasyRequestId\Common\Provider\RequestIdProviderInterface;
use EonX\EasyRequestId\Common\Resolver\FallbackResolverInterface;
use EonX\EasyRequestId\Common\Resolver\UuidFallbackResolver;
use EonX\EasyRequestId\Common\ValueObject\RequestIdInfo;
use EonX\EasyRequestId\EasyErrorHandler\Builder\RequestIdErrorResponseBuilder;
use EonX\EasyRequestId\EasyHttpClient\Modifier\RequestIdRequestDataModifier;
use EonX\EasyRequestId\EasyLogging\Processor\RequestIdProcessor;
Expand Down Expand Up @@ -58,15 +59,13 @@ static function (JobProcessing $event) use ($requestIdProvider): void {
return;
}

$requestIdProvider->setResolver(static function () use ($body, $requestIdProvider): array {
$requestIdProvider->setResolver(static function () use ($body, $requestIdProvider): RequestIdInfo {
$ids = $body['easy_request_id'] ?? [];

return [
RequestIdProviderInterface::KEY_RESOLVED_CORRELATION_ID =>
$ids[$requestIdProvider->getCorrelationIdHeaderName()] ?? null,
RequestIdProviderInterface::KEY_RESOLVED_REQUEST_ID =>
$ids[$requestIdProvider->getRequestIdHeaderName()] ?? null,
];
return new RequestIdInfo(
$ids[$requestIdProvider->getCorrelationIdHeaderName()] ?? null,
$ids[$requestIdProvider->getRequestIdHeaderName()] ?? null
);
});
}
);
Expand Down
6 changes: 2 additions & 4 deletions packages/EasyRequestId/laravel/config/easy-request-id.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
<?php
declare(strict_types=1);

use EonX\EasyRequestId\Common\Provider\RequestIdProviderInterface;

return [
'http_headers' => [
/**
* Header used to resolve/send the correlation id from the HTTP request.
*/
'correlation_id' => RequestIdProviderInterface::DEFAULT_HTTP_HEADER_CORRELATION_ID,
'correlation_id' => 'X-CORRELATION-ID',

/**
* Header used to resolve/send the request id from the HTTP request.
*/
'request_id' => RequestIdProviderInterface::DEFAULT_HTTP_HEADER_REQUEST_ID,
'request_id' => 'X-REQUEST-ID',
],

/**
Expand Down
25 changes: 10 additions & 15 deletions packages/EasyRequestId/src/Common/Provider/RequestIdProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,24 @@
namespace EonX\EasyRequestId\Common\Provider;

use EonX\EasyRequestId\Common\Resolver\FallbackResolverInterface;
use EonX\EasyRequestId\Common\Resolver\ResolverInterface;

final class RequestIdProvider implements RequestIdProviderInterface
{
private ?string $correlationId = null;

private string $correlationIdHeaderName;

private ?string $requestId = null;

private string $requestIdHeaderName;

public function __construct(
private FallbackResolverInterface $fallback,
?string $correlationIdHeaderName = null,
?string $requestIdHeaderName = null,
private FallbackResolverInterface $fallbackResolver,
private string $correlationIdHeaderName,
private string $requestIdHeaderName,
) {
$this->correlationIdHeaderName = $correlationIdHeaderName ?? self::DEFAULT_HTTP_HEADER_CORRELATION_ID;
$this->requestIdHeaderName = $requestIdHeaderName ?? self::DEFAULT_HTTP_HEADER_REQUEST_ID;
}

public function getCorrelationId(): string
{
return $this->correlationId ??= $this->fallback->fallbackCorrelationId();
return $this->correlationId ??= $this->fallbackResolver->fallbackCorrelationId();
}

public function getCorrelationIdHeaderName(): string
Expand All @@ -36,20 +31,20 @@ public function getCorrelationIdHeaderName(): string

public function getRequestId(): string
{
return $this->requestId ??= $this->fallback->fallbackRequestId();
return $this->requestId ??= $this->fallbackResolver->fallbackRequestId();
}

public function getRequestIdHeaderName(): string
{
return $this->requestIdHeaderName;
}

public function setResolver(callable $resolver): RequestIdProviderInterface
public function setResolver(ResolverInterface|callable $resolver): RequestIdProviderInterface
{
$ids = $resolver();
$requestIdInfo = $resolver();

$this->correlationId = $ids[self::KEY_RESOLVED_CORRELATION_ID] ?? null;
$this->requestId = $ids[self::KEY_RESOLVED_REQUEST_ID] ?? null;
$this->correlationId = $requestIdInfo->getCorrelationId();
$this->requestId = $requestIdInfo->getRequestId();

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@

namespace EonX\EasyRequestId\Common\Provider;

use EonX\EasyRequestId\Common\Resolver\ResolverInterface;

interface RequestIdProviderInterface
{
public const DEFAULT_HTTP_HEADER_CORRELATION_ID = 'X-CORRELATION-ID';

public const DEFAULT_HTTP_HEADER_REQUEST_ID = 'X-REQUEST-ID';

public const KEY_RESOLVED_CORRELATION_ID = 'resolved_correlation_id';

public const KEY_RESOLVED_REQUEST_ID = 'resolved_request_id';

public function getCorrelationId(): string;

public function getCorrelationIdHeaderName(): string;
Expand All @@ -21,5 +15,8 @@ public function getRequestId(): string;

public function getRequestIdHeaderName(): string;

public function setResolver(callable $resolver): self;
/**
* @param \EonX\EasyRequestId\Common\Resolver\ResolverInterface|callable():\EonX\EasyRequestId\Common\ValueObject\RequestIdInfo $resolver
*/
public function setResolver(ResolverInterface|callable $resolver): self;
}
20 changes: 7 additions & 13 deletions packages/EasyRequestId/src/Common/Resolver/FromArrayResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,22 @@
namespace EonX\EasyRequestId\Common\Resolver;

use EonX\EasyRequestId\Common\Provider\RequestIdProviderInterface;
use EonX\EasyRequestId\Common\ValueObject\RequestIdInfo;

final class FromArrayResolver
final class FromArrayResolver implements ResolverInterface
{
public function __construct(
private array $array,
private RequestIdProviderInterface $requestIdProvider,
) {
}

/**
* @return null[]|string[]
*/
public function __invoke(): array
public function __invoke(): RequestIdInfo
{
return [
RequestIdProviderInterface::KEY_RESOLVED_CORRELATION_ID => $this->getIdValue(
$this->requestIdProvider->getCorrelationIdHeaderName()
),
RequestIdProviderInterface::KEY_RESOLVED_REQUEST_ID => $this->getIdValue(
$this->requestIdProvider->getRequestIdHeaderName()
),
];
return new RequestIdInfo(
$this->getIdValue($this->requestIdProvider->getCorrelationIdHeaderName()),
$this->getIdValue($this->requestIdProvider->getRequestIdHeaderName())
);
}

private function getIdValue(string $id): ?string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,23 @@
namespace EonX\EasyRequestId\Common\Resolver;

use EonX\EasyRequestId\Common\Provider\RequestIdProviderInterface;
use EonX\EasyRequestId\Common\ValueObject\RequestIdInfo;
use Symfony\Component\HttpFoundation\Request;

final class HttpFoundationRequestResolver
final class HttpFoundationRequestResolver implements ResolverInterface
{
public function __construct(
private Request $request,
private RequestIdProviderInterface $requestIdProvider,
) {
}

/**
* @return null[]|string[]
*/
public function __invoke(): array
public function __invoke(): RequestIdInfo
{
$correlationIdHeader = $this->getHeader($this->requestIdProvider->getCorrelationIdHeaderName());
$requestIdHeader = $this->getHeader($this->requestIdProvider->getRequestIdHeaderName());

return [
RequestIdProviderInterface::KEY_RESOLVED_CORRELATION_ID => $correlationIdHeader,
RequestIdProviderInterface::KEY_RESOLVED_REQUEST_ID => $requestIdHeader,
];
return new RequestIdInfo(
$this->getHeader($this->requestIdProvider->getCorrelationIdHeaderName()),
$this->getHeader($this->requestIdProvider->getRequestIdHeaderName())
);
}

private function getHeader(string $header): ?string
Expand Down
11 changes: 11 additions & 0 deletions packages/EasyRequestId/src/Common/Resolver/ResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);

namespace EonX\EasyRequestId\Common\Resolver;

use EonX\EasyRequestId\Common\ValueObject\RequestIdInfo;

interface ResolverInterface
{
public function __invoke(): RequestIdInfo;
}
23 changes: 23 additions & 0 deletions packages/EasyRequestId/src/Common/ValueObject/RequestIdInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

namespace EonX\EasyRequestId\Common\ValueObject;

final class RequestIdInfo
{
public function __construct(
private ?string $correlationId = null,
private ?string $requestId = null,
) {
}

public function getCorrelationId(): ?string
{
return $this->correlationId;
}

public function getRequestId(): ?string
{
return $this->requestId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,26 @@

namespace EonX\EasyRequestId\Messenger\Resolver;

use EonX\EasyRequestId\Common\Provider\RequestIdProviderInterface;
use EonX\EasyRequestId\Common\Resolver\ResolverInterface;
use EonX\EasyRequestId\Common\ValueObject\RequestIdInfo;
use EonX\EasyRequestId\Messenger\Stamp\RequestIdStamp;
use Symfony\Component\Messenger\Envelope;

final class MessengerMessageResolver
final class MessengerMessageResolver implements ResolverInterface
{
public function __construct(
private Envelope $envelope,
) {
}

/**
* @return null[]|string[]
*/
public function __invoke(): array
public function __invoke(): RequestIdInfo
{
$stamp = $this->envelope->last(RequestIdStamp::class);

if ($stamp instanceof RequestIdStamp === false) {
return [];
return new RequestIdInfo();
}

return [
RequestIdProviderInterface::KEY_RESOLVED_CORRELATION_ID => $stamp->getCorrelationId(),
RequestIdProviderInterface::KEY_RESOLVED_REQUEST_ID => $stamp->getRequestId(),
];
return new RequestIdInfo($stamp->getCorrelationId(), $stamp->getRequestId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use EonX\EasyRandom\Generator\RandomGenerator;
use EonX\EasyRandom\Generator\SymfonyUuidV6Generator;
use EonX\EasyRequestId\Common\Provider\RequestIdProvider;
use EonX\EasyRequestId\Common\Provider\RequestIdProviderInterface;
use EonX\EasyRequestId\Common\Resolver\FallbackResolverInterface;
use EonX\EasyRequestId\Common\Resolver\HttpFoundationRequestResolver;
use EonX\EasyRequestId\Common\Resolver\UuidFallbackResolver;
Expand Down Expand Up @@ -34,8 +33,8 @@ static function (string $requestId, string $correlationId): void {

yield 'Default resolver with default values' => [
self::getRequestWithHeaders([
RequestIdProviderInterface::DEFAULT_HTTP_HEADER_CORRELATION_ID => 'correlation-id',
RequestIdProviderInterface::DEFAULT_HTTP_HEADER_REQUEST_ID => 'request-id',
'some-correlation-id-header' => 'correlation-id',
'some-request-id-header' => 'request-id',
]),
'request-id',
'correlation-id',
Expand All @@ -51,7 +50,11 @@ public function testGetIds(
?FallbackResolverInterface $fallbackResolver = null,
): void {
$fallbackResolver ??= $this->defaultFallbackResolver();
$requestIdProvider = new RequestIdProvider($fallbackResolver);
$requestIdProvider = new RequestIdProvider(
$fallbackResolver,
'some-correlation-id-header',
'some-request-id-header',
);
$requestIdProvider->setResolver(new HttpFoundationRequestResolver($request, $requestIdProvider));

// For caching coverage
Expand Down

0 comments on commit 5abc771

Please sign in to comment.