Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #394 Add Sentry Hub instance in DI container to resolve event…
… context issue (dedpikhto) This PR was merged into the 3.x-dev branch. Discussion ---------- Add Sentry Hub instance in DI container to resolve event context issue Fixing Symfony issue with different Hub's while decorating default Sentry handler for extended event context. Common way to extend log event context is to decorate Sentry\Monolog\Handler: ```php final class SentryContextHandlerDecorator extends AbstractProcessingHandler { private Handler $handler; public function __construct(Handler $handler) { $this->handler = $handler; parent::__construct($handler->getLevel(), $handler->getBubble()); } public function handle(array $record): bool { $result = false; withScope(function (Scope $scope) use ($record, &$result): void { if (isset($record['context']) && is_array($record['context']) && count($record['context']) > 0) { $scope->setContext('context', $record['context']); } $result = $this->handler->handle($record); }); return $result; } } ``` But if you do so with Symfony DI, you will get two instances of Hub: one from DI container, and one with SentrySdk in withScope(...) call. Changes to one Hub's scope won't affect second's hub scope. This PR adds ability to use shared Hub in custom decorators: ```php App\Logging\Handler\SentryContextHandlerDecorator: decorates: monolog.handler.sentry arguments: - '@app\Logging\Handler\SentryContextHandlerDecorator.inner' - '@monolog.handler.sentry.hub' ``` where `sentry` in `monolog.handler.sentry.hub` is name of handler. Decorator with shared Hub instance: ```php final class SentryContextHandlerDecorator extends AbstractProcessingHandler { private Handler $handler; private Hub $hub; public function __construct(Handler $handler, Hub $hub) { $this->handler = $handler; $this->hub = $hub; parent::__construct($handler->getLevel(), $handler->getBubble()); } public function handle(array $record): bool { $result = false; $this->hub->withScope(function (Scope $scope) use ($record, &$result): void { if (isset($record['context']) && is_array($record['context']) && count($record['context']) > 0) { $scope->setContext('context', $record['context']); } $result = $this->handler->handle($record); }); return $result; } } ``` More comments about problem: getsentry/sentry-php#848 (comment) Commits ------- 8a77eb9 Add Sentry Hub instance in DI container to resolve event context issue
- Loading branch information