-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ability to filter incoming requests (#45)
* [BUGFIX] #36 Changed `getCallerClass`, on Backtracer trait to return null if the backtrace is empty. Changed DatebaseQueryCollector and CacheCollector to add '(too deeply nested)' to their respective segment name, if the caller name is deeper than 50 calls in the backtrace. Added tests for all affected classes. * [BUGFIX] #36 Applied suggested diff. * [BUGFIX] #44 WIP: working on adding support to filter request to capture. * [BUGFIX] #44 Finished adding support to filter requests. * [BUGFIX] #44 Added new filtering option to the doc. Added `addRequestFilterCallback` as an available function on the Xray facade. * [BUGFIX] #44 Changed XrayServiceProvider to check request in the `boot` function rather than the `registerCollectors`. * Applied suggested changes. --------- Co-authored-by: Nicolas D'Amours <[email protected]>
- Loading branch information
1 parent
c7c6c8f
commit 2e7176b
Showing
6 changed files
with
378 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,95 @@ | ||
<?php | ||
|
||
namespace Napp\Xray\Tests\Facades; | ||
|
||
use Napp\Xray\Collectors\SegmentCollector; | ||
use Napp\Xray\Xray; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class XrayTest extends TestCase | ||
{ | ||
public function test_it_should_register_request_filtering_callback() { | ||
// given an Xray object | ||
/** @var Xray $xrayObject */ | ||
$xrayObject = new Xray(new SegmentCollector()); | ||
|
||
// and a mock of a callback to check if the current request should be captured. | ||
$callbackCalled = false; | ||
$requestCapturingCallbackMock = function(Request $request) use (&$callbackCalled) { | ||
$callbackCalled = true; | ||
}; | ||
|
||
// and the callback has been registered on the facade as a filter callback | ||
$xrayObject->addRequestFilterCallback($requestCapturingCallbackMock); | ||
|
||
// when checking, on the facade, if the request should be captured | ||
$request = new Request(); | ||
$xrayObject->shouldCaptureRequest($request); | ||
|
||
// then the callback should have been called | ||
$this->assertTrue($callbackCalled); | ||
} | ||
|
||
public function test_it_should_return_true_when_no_request_filter_callable_returns_false() { | ||
// given a xray object | ||
/** @var Xray $xrayObject */ | ||
$xrayObject = new Xray(new SegmentCollector()); | ||
|
||
// and some request filter callback that return true | ||
$givenRequestCallbackCount = 3; | ||
for($i = 0; $i < $givenRequestCallbackCount; $i ++) { | ||
$xrayObject->addRequestFilterCallback(function() { | ||
return true; | ||
}); | ||
} | ||
|
||
// when checking, on the facade, if the request should be captured | ||
$request = new Request(); | ||
$result = $xrayObject->shouldCaptureRequest($request); | ||
|
||
// then it should return true | ||
$this->assertTrue($result); | ||
} | ||
|
||
public function test_it_should_return_false_if_at_least_one_request_filter_callback_returns_false() { | ||
// given a xray object | ||
/** @var Xray $xrayObject */ | ||
$xrayObject = new Xray(new SegmentCollector()); | ||
|
||
// and some request filter callback that return true | ||
$givenRequestCallbackCount = 3; | ||
for($i = 0; $i < $givenRequestCallbackCount; $i ++) { | ||
$xrayObject->addRequestFilterCallback(function() { | ||
return true; | ||
}); | ||
} | ||
|
||
// and one request filter callback that returns false | ||
$xrayObject->addRequestFilterCallback(function() { | ||
return false; | ||
}); | ||
|
||
// when checking, on the facade, if the request should be captured | ||
$request = new Request(); | ||
$result = $xrayObject->shouldCaptureRequest($request); | ||
|
||
// then it should return false | ||
$this->assertFalse($result); | ||
} | ||
|
||
public function test_it_should_return_true_if_no_request_filtering_callback_are_defined() { | ||
// given a xray object | ||
/** @var Xray $xrayObject */ | ||
$xrayObject = new Xray(new SegmentCollector()); | ||
|
||
// and no request filtering callback defined | ||
|
||
// when checking, on the facade, if the request should be captured | ||
$request = new Request(); | ||
$result = $xrayObject->shouldCaptureRequest($request); | ||
|
||
// then it should return true | ||
$this->assertTrue($result); | ||
} | ||
} |
Oops, something went wrong.