Commit 6ba90b9
committed
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 issueFile tree
2 files changed
+6
-0
lines changed- DependencyInjection
- Tests/DependencyInjection
2 files changed
+6
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
757 | 757 | | |
758 | 758 | | |
759 | 759 | | |
| 760 | + | |
760 | 761 | | |
761 | 762 | | |
762 | 763 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
393 | 393 | | |
394 | 394 | | |
395 | 395 | | |
| 396 | + | |
396 | 397 | | |
397 | 398 | | |
398 | 399 | | |
399 | 400 | | |
400 | 401 | | |
401 | 402 | | |
402 | 403 | | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
403 | 408 | | |
404 | 409 | | |
405 | 410 | | |
| |||
0 commit comments