Skip to content
This repository has been archived by the owner on Jun 23, 2021. It is now read-only.

Commit

Permalink
III-3528 Pass API key as user tag
Browse files Browse the repository at this point in the history
  • Loading branch information
LucWollants committed Oct 9, 2020
1 parent 7783096 commit 46fb893
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/ErrorHandlerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public function register(Application $app): void
function ($app) {
return new SentryErrorHandler(
$app[HubInterface::class],
$app['jwt'] ?? null
$app['jwt'] ?? null,
$app['auth.api_key'] ?? null
);
}
);
Expand Down
15 changes: 14 additions & 1 deletion app/SentryErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\UiTPASService;

use CultuurNet\UDB3\ApiGuard\ApiKey\ApiKey;
use CultuurNet\UDB3\Jwt\Udb3Token;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
Expand All @@ -17,10 +18,14 @@ class SentryErrorHandler
/** @var Udb3Token|null */
private $udb3Token;

public function __construct(HubInterface $sentryHub, ?Udb3Token $udb3Token)
/** @var ApiKey|null */
private $apiKey;

public function __construct(HubInterface $sentryHub, ?Udb3Token $udb3Token, ?ApiKey $apiKey)
{
$this->sentryHub = $sentryHub;
$this->udb3Token = $udb3Token;
$this->apiKey = $apiKey;
}

public function handle(Throwable $throwable): void
Expand All @@ -31,6 +36,7 @@ public function handle(Throwable $throwable): void

$this->sentryHub->configureScope(function (Scope $scope) {
$scope->setUser($this->createUser($this->udb3Token));
$scope->setTags($this->createTags($this->apiKey));
});

$this->sentryHub->captureException($throwable);
Expand All @@ -49,4 +55,11 @@ private function createUser(?Udb3Token $udb3Token): array
'sub' => $udb3Token->jwtToken()->getClaim('sub', 'null'),
];
}

private function createTags(?ApiKey $apiKey): array
{
return [
'api_key' => $apiKey ? $apiKey->toNative() : 'null',
];
}
}

0 comments on commit 46fb893

Please sign in to comment.