Skip to content

Commit 9bf4dba

Browse files
committed
remove attachment event type
1 parent 69803b7 commit 9bf4dba

12 files changed

Lines changed: 64 additions & 47 deletions

File tree

src/Attachment/ByteAttachment.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Sentry\Attachment;
46

57
/**

src/Attachment/FileAttachment.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Sentry\Attachment;
46

57
/**
68
* Represents a file that is readable by using a path.
79
*/
810
class FileAttachment extends Attachment
911
{
10-
1112
/**
1213
* @var string
1314
*/

src/Event.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,6 @@ public static function createLogs(?EventId $eventId = null): self
247247
return new self($eventId, EventType::logs());
248248
}
249249

250-
public static function createAttachments(?EventId $eventId = null): self
251-
{
252-
return new self($eventId, EventType::attachment());
253-
}
254-
255250
/**
256251
* Gets the ID of this event.
257252
*/

src/EventType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static function cases(): array
6464
self::transaction(),
6565
self::checkIn(),
6666
self::logs(),
67-
self::attachment()
67+
self::attachment(),
6868
];
6969
}
7070

src/Serializer/EnvelopItems/AttachmentItem.php

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,6 @@
1010

1111
class AttachmentItem implements EnvelopeItemInterface
1212
{
13-
/**
14-
* {@inheritDoc}
15-
*/
16-
public static function toEnvelopeItem(Event $event)
17-
{
18-
$attachments = $event->getAttachments();
19-
20-
$items = [];
21-
22-
foreach ($attachments as $attachment) {
23-
$header = [
24-
'filename' => $attachment->getFilename(),
25-
'content_type' => $attachment->getContentType(),
26-
'attachment_type' => 'event.attachment',
27-
];
28-
29-
$items[] = \sprintf("%s\n%s", JSON::encode($header), file_get_contents($attachment->getFilename()));
30-
}
31-
32-
return $items;
33-
}
34-
3513
public static function toAttachmentItem(Attachment $attachment): ?string
3614
{
3715
$data = $attachment->getData();
@@ -64,4 +42,14 @@ public static function totalAttachmentSize(array $attachments): int
6442

6543
return $sum;
6644
}
45+
46+
public static function toEnvelopeItem(Event $event): ?string
47+
{
48+
$result = [];
49+
foreach ($event->getAttachments() as $attachment) {
50+
$result[] = self::toAttachmentItem($attachment);
51+
}
52+
53+
return implode("\n", $result);
54+
}
6755
}

src/Serializer/EnvelopItems/EnvelopeItemInterface.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,5 @@
1111
*/
1212
interface EnvelopeItemInterface
1313
{
14-
/**
15-
* @param Event $event
16-
* @return ?string|string[]
17-
*/
18-
public static function toEnvelopeItem(Event $event);
14+
public static function toEnvelopeItem(Event $event): ?string;
1915
}

src/Serializer/PayloadSerializer.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,14 @@ public function serialize(Event $event): string
6666
switch ($event->getType()) {
6767
case EventType::event():
6868
$items[] = EventItem::toEnvelopeItem($event);
69+
$items[] = AttachmentItem::toEnvelopeItem($event);
6970
break;
7071
case EventType::transaction():
7172
$items[] = TransactionItem::toEnvelopeItem($event);
7273
if ($event->getSdkMetadata('profile') !== null) {
7374
$items[] = ProfileItem::toEnvelopeItem($event);
7475
}
76+
$items[] = AttachmentItem::toEnvelopeItem($event);
7577
break;
7678
case EventType::checkIn():
7779
$items[] = CheckInItem::toEnvelopeItem($event);
@@ -81,13 +83,6 @@ public function serialize(Event $event): string
8183
break;
8284
}
8385

84-
// If attachments are exceeding the limit, then we drop all attachments
85-
if (AttachmentItem::totalAttachmentSize($event->getAttachments()) <= self::MAX_ATTACHMENT_SIZE) {
86-
foreach ($event->getAttachments() as $attachment) {
87-
$items[] = AttachmentItem::toAttachmentItem($attachment);
88-
}
89-
}
90-
9186
return \sprintf("%s\n%s", JSON::encode($envelopeHeader), implode("\n", array_filter($items)));
9287
}
9388
}

src/State/HubAdapter.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Sentry\State;
66

7+
use Sentry\Attachment\Attachment;
78
use Sentry\Breadcrumb;
89
use Sentry\CheckInStatus;
910
use Sentry\ClientInterface;
@@ -155,6 +156,14 @@ public function addBreadcrumb(Breadcrumb $breadcrumb): bool
155156
return SentrySdk::getCurrentHub()->addBreadcrumb($breadcrumb);
156157
}
157158

159+
/**
160+
* {@inheritDoc}
161+
*/
162+
public function addAttachment(Attachment $attachment): bool
163+
{
164+
return SentrySdk::getCurrentHub()->addAttachment($attachment);
165+
}
166+
158167
/**
159168
* {@inheritdoc}
160169
*/

src/State/HubInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,8 @@ public function getSpan(): ?Span;
154154
*/
155155
public function setSpan(?Span $span): HubInterface;
156156

157+
/**
158+
* Records a new attachment that will be attached to error and transaction events.
159+
*/
157160
public function addAttachment(Attachment $attachment): bool;
158161
}

src/State/Scope.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Sentry\Breadcrumb;
99
use Sentry\Event;
1010
use Sentry\EventHint;
11+
use Sentry\EventType;
1112
use Sentry\Options;
1213
use Sentry\Severity;
1314
use Sentry\Tracing\DynamicSamplingContext;
@@ -418,8 +419,10 @@ public function applyToEvent(Event $event, ?EventHint $hint = null, ?Options $op
418419
$hint = new EventHint();
419420
}
420421

421-
if (empty($event->getAttachments())) {
422-
$event->setAttachments($this->attachments);
422+
if ($event->getType() === EventType::event() || $event->getType() === EventType::transaction()) {
423+
if (empty($event->getAttachments())) {
424+
$event->setAttachments($this->attachments);
425+
}
423426
}
424427

425428
foreach (array_merge(self::$globalEventProcessors, $this->eventProcessors) as $processor) {

0 commit comments

Comments
 (0)