Skip to content

Commit

Permalink
Merge pull request #587 from rollbar/4.0/update-filter-interface
Browse files Browse the repository at this point in the history
Added typing / comments to the `FilterInterface` and removed the token argument
  • Loading branch information
danielmorell committed Jan 12, 2023
2 parents 44bf101 + 18fabc2 commit 2972bda
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,8 @@ public function getSendMessageTrace()
return $this->sendMessageTrace;
}

public function checkIgnored(Payload $payload, string $accessToken, $toLog, bool $isUncaught)

public function checkIgnored(Payload $payload, $toLog, bool $isUncaught)
{
if (isset($this->checkIgnore)) {
try {
Expand All @@ -856,7 +857,7 @@ public function checkIgnored(Payload $payload, string $accessToken, $toLog, bool
}

if (!is_null($this->filter)) {
$filter = $this->filter->shouldSend($payload, $accessToken);
$filter = $this->filter->shouldSend($payload, $isUncaught);
$this->verboseLogger()->debug("Custom filter result: " . var_export($filter, true));
return $filter;
}
Expand Down
16 changes: 15 additions & 1 deletion src/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@

use Rollbar\Payload\Payload;

/**
* The FilterInterface allows a custom payload filter to be used. It should be passed to the configs with the 'filter'
* key. A custom filter should implement this interface.
*/
interface FilterInterface
{
public function shouldSend(Payload $payload, string $accessToken);
/**
* Method called to determine if a payload should be sent to the Rollbar service. If true is returned the payload
* will not be sent.
*
* @param Payload $payload The payload instance that may be sent.
* @param bool $isUncaught True if the payload represents an error that was caught by one of the Rollbar
* exception or error handlers.
*
* @return bool
*/
public function shouldSend(Payload $payload, bool $isUncaught): bool;
}
2 changes: 1 addition & 1 deletion src/RollbarLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public function report($level, string|Stringable $message, array $context = arra
$payload = $this->getPayload($accessToken, $level, $message, $context);

$isUncaught = $this->isUncaughtLogData($message);
if ($this->config->checkIgnored($payload, $accessToken, $message, $isUncaught)) {
if ($this->config->checkIgnored($payload, $message, $isUncaught)) {
$this->verboseLogger()->info('Occurrence ignored');
$response = new Response(0, "Ignored");
} else {
Expand Down
10 changes: 4 additions & 6 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,12 @@ public function testMinimumLevel(): void

public function assertPayloadIgnored($config, $payload): void
{
$this->assertTrue($config->checkIgnored($payload, 'access-token', $this->error, false));
$this->assertTrue($config->checkIgnored($payload, $this->error, false));
}

public function assertPayloadNotIgnored($config, $payload): void
{
$this->assertFalse($config->checkIgnored($payload, 'access-token', $this->error, false));
$this->assertFalse($config->checkIgnored($payload, $this->error, false));
}

private function prepareMockPayload($level): Payload
Expand Down Expand Up @@ -421,8 +421,8 @@ public function testFilter(): void
"environment" => $this->env,
"filter" => $filter
));
$this->assertTrue($c->checkIgnored($p, "fake_access_token", $this->error, false));
$this->assertFalse($c->checkIgnored($p, "fake_access_token", $this->error, false));
$this->assertTrue($c->checkIgnored($p, $this->error, false));
$this->assertFalse($c->checkIgnored($p, $this->error, false));
}

public function testSender(): void
Expand Down Expand Up @@ -608,7 +608,6 @@ public function testCheckIgnore(): void
$data,
$config->getAccessToken()
),
$this->getTestAccessToken(),
$this->error,
false
);
Expand Down Expand Up @@ -647,7 +646,6 @@ public function testCheckIgnoreParameters(): void
$data,
$config->getAccessToken()
),
$this->getTestAccessToken(),
$this->error,
true
);
Expand Down
7 changes: 3 additions & 4 deletions tests/VerbosityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,6 @@ function () use ($config, $unitTest) {
$payloadMock->method('getData')->willReturn($dataMock);
$config->checkIgnored(
$payloadMock,
$unitTest->getTestAccessToken(),
$payloadMock,
false
);
Expand Down Expand Up @@ -651,7 +650,7 @@ function () use ($config, $unitTest) {
// logic under test
$data = $config->getRollbarData(\Rollbar\Payload\Level::INFO, 'some message', array());
$payload = new \Rollbar\Payload\Payload($data, $unitTest->getTestAccessToken());
$config->checkIgnored($payload, $unitTest->getTestAccessToken(), 'some message', false);
$config->checkIgnored($payload, 'some message', false);
},
function () use ($unitTest) {
// verbosity expectations
Expand Down Expand Up @@ -685,7 +684,7 @@ function () use ($config, $unitTest) {
// logic under test
$data = $config->getRollbarData(\Rollbar\Payload\Level::INFO, 'some message', array());
$payload = new \Rollbar\Payload\Payload($data, $unitTest->getTestAccessToken());
$config->checkIgnored($payload, $unitTest->getTestAccessToken(), 'some message', false);
$config->checkIgnored($payload, 'some message', false);
},
function () use ($unitTest) {
// verbosity expectations
Expand Down Expand Up @@ -722,7 +721,7 @@ function () use ($config, $unitTest) {
// logic under test
$data = $config->getRollbarData(\Rollbar\Payload\Level::INFO, 'some message', array());
$payload = new \Rollbar\Payload\Payload($data, $unitTest->getTestAccessToken());
$config->checkIgnored($payload, $unitTest->getTestAccessToken(), 'some message', false);
$config->checkIgnored($payload, 'some message', false);
},
function () use ($unitTest) {
// verbosity expectations
Expand Down

0 comments on commit 2972bda

Please sign in to comment.