Skip to content

Commit

Permalink
Merge pull request #56 from pj8/before-send
Browse files Browse the repository at this point in the history
before_sendイベントでタグを付与する処理の追加
  • Loading branch information
momospnr authored Nov 27, 2024
2 parents 5adc6f8 + 1ef2b1b commit 7df64b1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/BeforeSend.php
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;
}
}
12 changes: 12 additions & 0 deletions src/BeforeSendInterface.php
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;
}
32 changes: 32 additions & 0 deletions tests/BeforeSendTest.php
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());
}
}

0 comments on commit 7df64b1

Please sign in to comment.