-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from pj8/before-send
before_sendイベントでタグを付与する処理の追加
- Loading branch information
Showing
3 changed files
with
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pj8\SentryModule; | ||
|
||
use Sentry\Event; | ||
|
||
final class BeforeSend implements BeforeSendInterface | ||
{ | ||
/** @param array<string, string> $tags セットしたいタグの配列(オプション) */ | ||
public function __construct(private readonly array $tags = []) | ||
{ | ||
} | ||
|
||
public function __invoke(Event $event): Event | ||
{ | ||
if ($this->tags !== []) { | ||
$event->setTags($this->tags); | ||
} | ||
|
||
return $event; | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pj8\SentryModule; | ||
|
||
use Sentry\Event; | ||
|
||
interface BeforeSendInterface | ||
{ | ||
public function __invoke(Event $event): Event|null; | ||
} |
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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pj8\SentryModule; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sentry\Event; | ||
|
||
class BeforeSendTest extends TestCase | ||
{ | ||
public function testInvokeSetServiceNameTag(): void | ||
{ | ||
$tags = ['service' => 'test-service']; | ||
$beforeSend = new BeforeSend($tags); | ||
$event = Event::createEvent(); | ||
|
||
$modifiedEvent = $beforeSend($event); | ||
|
||
$this->assertSame('test-service', $modifiedEvent->getTags()['service']); | ||
} | ||
|
||
public function testInvokeNotSetServiceNameTag(): void | ||
{ | ||
$beforeSend = new BeforeSend(); | ||
$event = Event::createEvent(); | ||
|
||
$modifiedEvent = $beforeSend($event); | ||
|
||
$this->assertEmpty($modifiedEvent->getTags()); | ||
} | ||
} |