Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EasyRequestId] Improve resolvers #651

Merged
merged 5 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 2 additions & 22 deletions packages/EasyRequestId/src/Bridge/BridgeConstantsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,10 @@ interface BridgeConstantsInterface
/**
* @var string
*/
public const PARAM_CORRELATION_ID_KEY = 'easy_request_id.correlation_id_key';
public const PARAM_HTTP_HEADER_CORRELATION_ID = 'easy_request_id.http_header.correlation_id';

/**
* @var string
*/
public const PARAM_DEFAULT_CORRELATION_ID_HEADER = 'easy_request_id.default_correlation_id_header';

/**
* @var string
*/
public const PARAM_DEFAULT_REQUEST_ID_HEADER = 'easy_request_id.default_request_id_header';

/**
* @var string
*/
public const PARAM_REQUEST_ID_KEY = 'easy_request_id.request_id_key';

/**
* @var string
*/
public const TAG_CORRELATION_ID_RESOLVER = 'easy_request_id.correlation_id_resolver';

/**
* @var string
*/
public const TAG_REQUEST_ID_RESOLVER = 'easy_request_id.request_id_resolver';
public const PARAM_HTTP_HEADER_REQUEST_ID = 'easy_request_id.http_header.request_id';
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@

use EonX\EasyErrorHandler\Builders\AbstractErrorResponseBuilder;
use EonX\EasyErrorHandler\Interfaces\ErrorResponseBuilderProviderInterface;
use EonX\EasyRequestId\Interfaces\RequestIdKeysAwareInterface;
use EonX\EasyRequestId\Interfaces\RequestIdServiceInterface;
use EonX\EasyRequestId\Traits\RequestIdKeysAwareTrait;
use Throwable;

final class RequestIdErrorResponseBuilder extends AbstractErrorResponseBuilder implements RequestIdKeysAwareInterface, ErrorResponseBuilderProviderInterface
final class RequestIdErrorResponseBuilder extends AbstractErrorResponseBuilder implements ErrorResponseBuilderProviderInterface
{
use RequestIdKeysAwareTrait;

/**
* @var \EonX\EasyRequestId\Interfaces\RequestIdServiceInterface
*/
Expand All @@ -30,8 +26,8 @@ public function __construct(RequestIdServiceInterface $requestIdService, ?int $p
public function buildHeaders(Throwable $throwable, ?array $headers = null): ?array
{
$headers = $headers ?? [];
$headers[$this->getCorrelationIdKey()] = $this->requestIdService->getCorrelationId();
$headers[$this->getRequestIdKey()] = $this->requestIdService->getRequestId();
$headers[$this->requestIdService->getCorrelationIdHeaderName()] = $this->requestIdService->getCorrelationId();
$headers[$this->requestIdService->getRequestIdHeaderName()] = $this->requestIdService->getRequestId();

return parent::buildHeaders($throwable, $headers);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
namespace EonX\EasyRequestId\Bridge\EasyLogging;

use EonX\EasyLogging\Config\AbstractSelfProcessorConfigProvider;
use EonX\EasyRequestId\Interfaces\RequestIdKeysAwareInterface;
use EonX\EasyRequestId\Interfaces\RequestIdServiceInterface;
use EonX\EasyRequestId\Traits\RequestIdKeysAwareTrait;

final class RequestIdProcessor extends AbstractSelfProcessorConfigProvider implements RequestIdKeysAwareInterface
final class RequestIdProcessor extends AbstractSelfProcessorConfigProvider
{
use RequestIdKeysAwareTrait;

/**
* @var \EonX\EasyRequestId\Interfaces\RequestIdServiceInterface
*/
Expand All @@ -31,8 +27,8 @@ public function __construct(RequestIdServiceInterface $requestIdService)
public function __invoke(array $records): array
{
$extra = $records['extra'] ?? [];
$extra[$this->getRequestIdKey()] = $this->requestIdService->getRequestId();
$extra[$this->getCorrelationIdKey()] = $this->requestIdService->getCorrelationId();
$extra[$this->requestIdService->getCorrelationIdHeaderName()] = $this->requestIdService->getCorrelationId();
$extra[$this->requestIdService->getRequestIdHeaderName()] = $this->requestIdService->getRequestId();

$records['extra'] = $extra;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@

namespace EonX\EasyRequestId\Bridge\EasyWebhook;

use EonX\EasyRequestId\Interfaces\RequestIdKeysAwareInterface;
use EonX\EasyRequestId\Interfaces\RequestIdServiceInterface;
use EonX\EasyRequestId\Traits\RequestIdKeysAwareTrait;
use EonX\EasyWebhook\Interfaces\StackInterface;
use EonX\EasyWebhook\Interfaces\WebhookInterface;
use EonX\EasyWebhook\Interfaces\WebhookResultInterface;
use EonX\EasyWebhook\Middleware\AbstractConfigureOnceMiddleware;

final class RequestIdWebhookMiddleware extends AbstractConfigureOnceMiddleware implements RequestIdKeysAwareInterface
final class RequestIdWebhookMiddleware extends AbstractConfigureOnceMiddleware
{
use RequestIdKeysAwareTrait;

/**
* @var \EonX\EasyRequestId\Interfaces\RequestIdServiceInterface
*/
Expand All @@ -30,14 +26,12 @@ public function __construct(RequestIdServiceInterface $requestIdService, ?int $p

protected function doProcess(WebhookInterface $webhook, StackInterface $stack): WebhookResultInterface
{
$webhook->mergeHttpClientOptions([
'headers' => [
$this->getCorrelationIdKey() => $this->requestIdService->getCorrelationId(),
],
]);

return $stack
->next()
->process($webhook, $stack);
$webhook->header(
$this->requestIdService->getCorrelationIdHeaderName(),
$this->requestIdService->getCorrelationId()
);
$webhook->header($this->requestIdService->getRequestIdHeaderName(), $this->requestIdService->getRequestId());

return $this->passOn($webhook, $stack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,66 @@

namespace EonX\EasyRequestId\Bridge\Laravel;

use EonX\EasyBugsnag\Bridge\BridgeConstantsInterface as EasyBugsnagBridgeConstantsInterface;
use EonX\EasyErrorHandler\Bridge\BridgeConstantsInterface as EasyErrorHandlerBridgeConstantsInterface;
use EonX\EasyLogging\Bridge\BridgeConstantsInterface as EasyLoggingBridgeConstantsInterface;
use EonX\EasyRequestId\Bridge\BridgeConstantsInterface;
use EonX\EasyRequestId\Bridge\EasyBugsnag\RequestIdConfigurator;
use EonX\EasyRequestId\Bridge\EasyErrorHandler\RequestIdErrorResponseBuilder;
use EonX\EasyRequestId\Bridge\EasyLogging\RequestIdProcessor;
use EonX\EasyRequestId\Bridge\EasyWebhook\RequestIdWebhookMiddleware;
use EonX\EasyRequestId\DefaultResolver;
use EonX\EasyRequestId\Interfaces\FallbackResolverInterface;
use EonX\EasyRequestId\Interfaces\RequestIdKeysAwareInterface;
use EonX\EasyRequestId\Interfaces\RequestIdServiceInterface;
use EonX\EasyRequestId\RequestIdService;
use EonX\EasyRequestId\UuidV4FallbackResolver;
use EonX\EasyWebhook\Bridge\BridgeConstantsInterface as EasyWebhookBridgeConstantsInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Queue\Queue;
use Illuminate\Support\ServiceProvider;

final class EasyRequestIdServiceProvider extends ServiceProvider
{
/**
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function boot(): void
{
$this->publishes([
__DIR__ . '/config/easy-request-id.php' => \base_path('config/easy-request-id.php'),
]);

/** @var \EonX\EasyRequestId\Interfaces\RequestIdServiceInterface $requestIdService */
$requestIdService = $this->app->make(RequestIdServiceInterface::class);

// Queue
// Add IDs to jobs pushed to the queue
Queue::createPayloadUsing(static function () use ($requestIdService): array {
return [
'easy_request_id' => [
$requestIdService->getCorrelationIdHeaderName() => $requestIdService->getCorrelationId(),
$requestIdService->getRequestIdHeaderName() => $requestIdService->getRequestId(),
],
];
});

// Resolve IDs from jobs from the queue
$this->app->make('events')->listen(
JobProcessing::class,
static function (JobProcessing $event) use ($requestIdService): void {
$body = \json_decode($event->job->getRawBody(), true);

if (\is_array($body) === false) {
return;
}

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

return [
RequestIdServiceInterface::KEY_RESOLVED_CORRELATION_ID => $ids[$requestIdService->getCorrelationIdHeaderName()] ?? null,
RequestIdServiceInterface::KEY_RESOLVED_REQUEST_ID => $ids[$requestIdService->getRequestIdHeaderName()] ?? null,
];
});
}
);
}

public function register(): void
Expand All @@ -41,40 +76,16 @@ public function register(): void
RequestIdServiceInterface::class,
static function (Container $app): RequestIdServiceInterface {
return new RequestIdService(
$app->tagged(BridgeConstantsInterface::TAG_REQUEST_ID_RESOLVER),
$app->tagged(BridgeConstantsInterface::TAG_CORRELATION_ID_RESOLVER),
$app->make(FallbackResolverInterface::class)
$app->make(FallbackResolverInterface::class),
\config('easy-request-id.http_headers.correlation_id'),
\config('easy-request-id.http_headers.request_id')
);
}
);

if ((bool)\config('easy-request-id.default_resolver', true)) {
$this->app->singleton(DefaultResolver::class, static function (): DefaultResolver {
return new DefaultResolver(
\config('easy-request-id.default_request_id_header'),
\config('easy-request-id.default_correlation_id_header')
);
});
$this->app->tag(DefaultResolver::class, [
BridgeConstantsInterface::TAG_CORRELATION_ID_RESOLVER,
BridgeConstantsInterface::TAG_REQUEST_ID_RESOLVER,
]);
}

// EasyBugsnag
if ($this->bridgeEnabled('easy_bugsnag', EasyBugsnagBridgeConstantsInterface::class)) {
$this->app->singleton(RequestIdConfigurator::class);
$this->app->extend(RequestIdConfigurator::class, $this->getSetKeysClosure());
$this->app->tag(
RequestIdConfigurator::class,
[EasyBugsnagBridgeConstantsInterface::TAG_CLIENT_CONFIGURATOR]
);
}

// EasyErrorHandler
if ($this->bridgeEnabled('easy_error_handler', EasyErrorHandlerBridgeConstantsInterface::class)) {
$this->app->singleton(RequestIdErrorResponseBuilder::class);
$this->app->extend(RequestIdErrorResponseBuilder::class, $this->getSetKeysClosure());
$this->app->tag(
RequestIdErrorResponseBuilder::class,
[EasyErrorHandlerBridgeConstantsInterface::TAG_ERROR_RESPONSE_BUILDER_PROVIDER]
Expand All @@ -84,7 +95,6 @@ static function (Container $app): RequestIdServiceInterface {
// EasyLogging
if ($this->bridgeEnabled('easy_logging', EasyLoggingBridgeConstantsInterface::class)) {
$this->app->singleton(RequestIdProcessor::class);
$this->app->extend(RequestIdProcessor::class, $this->getSetKeysClosure());
$this->app->tag(
RequestIdProcessor::class,
[EasyLoggingBridgeConstantsInterface::TAG_PROCESSOR_CONFIG_PROVIDER]
Expand All @@ -94,7 +104,6 @@ static function (Container $app): RequestIdServiceInterface {
// EasyWebhook
if ($this->bridgeEnabled('easy_webhook', EasyWebhookBridgeConstantsInterface::class)) {
$this->app->singleton(RequestIdWebhookMiddleware::class);
$this->app->extend(RequestIdWebhookMiddleware::class, $this->getSetKeysClosure());
$this->app->tag(
RequestIdWebhookMiddleware::class,
[EasyWebhookBridgeConstantsInterface::TAG_MIDDLEWARE]
Expand All @@ -108,14 +117,4 @@ private function bridgeEnabled(string $config, string $interface): bool

return $enabled && \interface_exists($interface);
}

private function getSetKeysClosure(): \Closure
{
return static function (RequestIdKeysAwareInterface $requestIdKeysAware): RequestIdKeysAwareInterface {
$requestIdKeysAware->setCorrelationIdKey(\config('easy-request-id.correlation_id_key'));
$requestIdKeysAware->setRequestIdKey(\config('easy-request-id.request_id_key'));

return $requestIdKeysAware;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
namespace EonX\EasyRequestId\Bridge\Laravel;

use EonX\EasyRequestId\Interfaces\RequestIdServiceInterface;
use EonX\EasyRequestId\Traits\ResolvesFromHttpFoundationRequest;
use Illuminate\Http\Request;

final class RequestIdMiddleware
{
use ResolvesFromHttpFoundationRequest;

/**
* @var \EonX\EasyRequestId\Interfaces\RequestIdServiceInterface
*/
Expand All @@ -24,7 +27,7 @@ public function __construct(RequestIdServiceInterface $requestIdService)
*/
public function handle(Request $request, \Closure $next)
{
$this->requestIdService->setRequest($request);
$this->setResolver($request, $this->requestIdService);

return $next($request);
}
Expand Down
Loading