-
Notifications
You must be signed in to change notification settings - Fork 179
Track subrequests with scopes #198
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2cfdc48
Add SubRequestListener
Jean85 52284d5
Remove unused public aliases in tests
Jean85 3933c8a
Configure the SubRequestListener
Jean85 a57fb74
Add tests for listener priorities
Jean85 23be9d0
Merge branch 'master' into track-subrequests-with-scopes to resolve c…
Jean85 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Sentry\SentryBundle\EventListener; | ||
|
||
use Sentry\State\Hub; | ||
use Symfony\Component\HttpKernel\Event\FinishRequestEvent; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
|
||
final class SubRequestListener | ||
{ | ||
/** | ||
* Pushes a new {@see Scope} for each SubRequest | ||
* | ||
* @param GetResponseEvent $event | ||
*/ | ||
public function onKernelRequest(GetResponseEvent $event): void | ||
{ | ||
if ($event->isMasterRequest()) { | ||
return; | ||
} | ||
|
||
Hub::getCurrent()->pushScope(); | ||
} | ||
|
||
/** | ||
* Pops a {@see Scope} for each finished SubRequest | ||
* | ||
* @param FinishRequestEvent $event | ||
*/ | ||
public function onKernelFinishRequest(FinishRequestEvent $event): void | ||
{ | ||
if ($event->isMasterRequest()) { | ||
return; | ||
} | ||
|
||
Hub::getCurrent()->popScope(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace Sentry\SentryBundle\Test\EventListener; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sentry\SentryBundle\EventListener\SubRequestListener; | ||
use Sentry\State\Hub; | ||
use Sentry\State\HubInterface; | ||
use Sentry\State\Scope; | ||
use Symfony\Component\HttpKernel\Event\FinishRequestEvent; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
|
||
class SubRequestListenerTest extends TestCase | ||
{ | ||
private $currentHub; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->currentHub = $this->prophesize(HubInterface::class); | ||
|
||
Hub::setCurrent($this->currentHub->reveal()); | ||
} | ||
|
||
public function testOnKernelRequestWithMasterRequest(): void | ||
{ | ||
$listener = new SubRequestListener(); | ||
|
||
$subRequestEvent = $this->prophesize(GetResponseEvent::class); | ||
$subRequestEvent->isMasterRequest() | ||
->willReturn(true); | ||
|
||
$this->currentHub->pushScope() | ||
->shouldNotBeCalled(); | ||
|
||
$listener->onKernelRequest($subRequestEvent->reveal()); | ||
} | ||
|
||
public function testOnKernelRequestWithSubRequest(): void | ||
{ | ||
$listener = new SubRequestListener(); | ||
|
||
$subRequestEvent = $this->prophesize(GetResponseEvent::class); | ||
$subRequestEvent->isMasterRequest() | ||
->willReturn(false); | ||
|
||
$this->currentHub->pushScope() | ||
->shouldBeCalledTimes(1) | ||
->willReturn(new Scope()); | ||
|
||
$listener->onKernelRequest($subRequestEvent->reveal()); | ||
} | ||
|
||
public function testOnKernelFinishRequestWithMasterRequest(): void | ||
{ | ||
$listener = new SubRequestListener(); | ||
|
||
$subRequestEvent = $this->prophesize(FinishRequestEvent::class); | ||
$subRequestEvent->isMasterRequest() | ||
->willReturn(true); | ||
|
||
$this->currentHub->popScope() | ||
->shouldNotBeCalled(); | ||
|
||
$listener->onKernelFinishRequest($subRequestEvent->reveal()); | ||
} | ||
|
||
public function testOnKernelFinishRequestWithSubRequest(): void | ||
{ | ||
$listener = new SubRequestListener(); | ||
|
||
$subRequestEvent = $this->prophesize(FinishRequestEvent::class); | ||
$subRequestEvent->isMasterRequest() | ||
->willReturn(false); | ||
|
||
$this->currentHub->popScope() | ||
->shouldBeCalledTimes(1) | ||
->willReturn(true); | ||
|
||
$listener->onKernelFinishRequest($subRequestEvent->reveal()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should you not be adding the scope variables, such as route, in the same way as is done in https://github.com/getsentry/sentry-symfony/blob/master/src/EventListener/RequestListener.php#L88
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may depend on what we want to report. The main route is still the one of the master request, and I'm still thinking about if we need to add more data in that scope, or override the previous information.
IMHO, the subrequest info shouldn't override, but I'm not sure if it's in this bundle's scope to add that, or if I should leave it to the end user.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I see what you mean, you still want to see the actual request,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly. You'll still free to add tags, extra context or whatever to the scope, and if you do it inside the subrequest, it will be popped out and reverted at the end of it.
Does it make sense to you? Do you think it's a good approach?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, though like you say, would be nice to automatically add something about the subrequest, like
$scope->setTag('route', $matchedRoute);
is done for master, maybe just withsub_request_route
or something.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, for example at work we use subrequests and we add some information to the
extra
context such as:current_request_url
current_request_method
master_request_url
master_request_method
is_master_request
This way we know if an event got generated from a request or a sub request