-
-
Notifications
You must be signed in to change notification settings - Fork 463
feat: add attachment support #1925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 5.x
Are you sure you want to change the base?
Changes from all commits
69803b7
9bf4dba
276d203
8633944
9b2e0b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sentry\Attachment; | ||
|
||
abstract class Attachment | ||
{ | ||
private const DEFAULT_CONTENT_TYPE = 'application/octet-stream'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $filename; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $contentType; | ||
|
||
public function __construct(string $filename, string $contentType) | ||
{ | ||
$this->filename = $filename; | ||
$this->contentType = $contentType; | ||
} | ||
|
||
public function getFilename(): string | ||
{ | ||
return $this->filename; | ||
} | ||
|
||
public function getContentType(): string | ||
{ | ||
return $this->contentType; | ||
} | ||
|
||
/** | ||
* Returns the size in bytes for the attachment. This method should aim to use a low overhead | ||
* way of determining the size because it will be called more than once. | ||
* For example, for file attachments it should read the file size from the filesystem instead of | ||
* reading the file in memory and then calculating the length. | ||
* If no low overhead way exists, then the result should be cached so that calling it multiple times | ||
* does not decrease performance. | ||
* | ||
* @return int the size in bytes or null if the length could not be determined, for example if the file | ||
* does not exist | ||
*/ | ||
abstract public function getSize(): ?int; | ||
|
||
/** | ||
* Fetches and returns the data. Calling this can have a non-trivial impact on memory usage, depending | ||
* on the type and size of attachment. | ||
* | ||
* @return string the content as bytes or null if the content could not be retrieved, for example if the file | ||
* does not exist | ||
*/ | ||
abstract public function getData(): ?string; | ||
|
||
/** | ||
* Creates a new attachment representing a file referenced by a path. | ||
* The file is not validated and the content is not read when creating the attachment. | ||
*/ | ||
public static function fromFile(string $path, string $contentType = self::DEFAULT_CONTENT_TYPE): Attachment | ||
{ | ||
return new FileAttachment($path, $contentType); | ||
} | ||
|
||
/** | ||
* Creates a new attachment representing a slice of bytes that lives in memory. | ||
*/ | ||
public static function fromBytes(string $filename, string $data, string $contentType = self::DEFAULT_CONTENT_TYPE): Attachment | ||
{ | ||
return new ByteAttachment($filename, $contentType, $data); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sentry\Attachment; | ||
|
||
/** | ||
* Represents an attachment that is stored in memory and will not be read from the filesystem. | ||
*/ | ||
class ByteAttachment extends Attachment | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $data; | ||
|
||
public function __construct(string $filename, string $contentType, string $data) | ||
{ | ||
parent::__construct($filename, $contentType); | ||
$this->data = $data; | ||
} | ||
|
||
public function getSize(): ?int | ||
{ | ||
return \strlen($this->data); | ||
} | ||
|
||
public function getData(): ?string | ||
{ | ||
return $this->data; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sentry\Attachment; | ||
|
||
/** | ||
* Represents a file that is readable by using a path. | ||
*/ | ||
class FileAttachment extends Attachment | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $path; | ||
|
||
public function __construct(string $path, string $contentType) | ||
{ | ||
parent::__construct(basename($path), $contentType); | ||
$this->path = $path; | ||
} | ||
|
||
public function getSize(): ?int | ||
{ | ||
return @filesize($this->path) ?: null; | ||
} | ||
|
||
public function getData(): ?string | ||
{ | ||
return @file_get_contents($this->path) ?: null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sentry\Serializer\EnvelopItems; | ||
|
||
use Sentry\Attachment\Attachment; | ||
use Sentry\Event; | ||
use Sentry\Util\JSON; | ||
|
||
class AttachmentItem implements EnvelopeItemInterface | ||
{ | ||
public static function toAttachmentItem(Attachment $attachment): ?string | ||
{ | ||
$data = $attachment->getData(); | ||
if ($data === null) { | ||
return null; | ||
} | ||
|
||
$header = [ | ||
'type' => 'attachment', | ||
'filename' => $attachment->getFilename(), | ||
'content_type' => $attachment->getContentType(), | ||
'attachment_type' => 'event.attachment', | ||
'length' => $attachment->getSize(), | ||
]; | ||
|
||
return \sprintf("%s\n%s", JSON::encode($header), $data); | ||
} | ||
|
||
public static function toEnvelopeItem(Event $event): ?string | ||
{ | ||
$result = []; | ||
foreach ($event->getAttachments() as $attachment) { | ||
$result[] = self::toAttachmentItem($attachment); | ||
} | ||
|
||
return implode("\n", $result); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Null Attachments Cause Malformed Envelope DataThe |
||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -7,6 +7,7 @@ | |||||||||
use Sentry\Event; | ||||||||||
use Sentry\EventType; | ||||||||||
use Sentry\Options; | ||||||||||
use Sentry\Serializer\EnvelopItems\AttachmentItem; | ||||||||||
use Sentry\Serializer\EnvelopItems\CheckInItem; | ||||||||||
use Sentry\Serializer\EnvelopItems\EventItem; | ||||||||||
use Sentry\Serializer\EnvelopItems\LogsItem; | ||||||||||
|
@@ -60,12 +61,14 @@ public function serialize(Event $event): string | |||||||||
switch ($event->getType()) { | ||||||||||
case EventType::event(): | ||||||||||
$items[] = EventItem::toEnvelopeItem($event); | ||||||||||
$items[] = AttachmentItem::toEnvelopeItem($event); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For clarity I think I would go for this (also in the transaction) instead and move the loop here. Yes it's a little more duplicated code but I think the clarity is a little better to also keep all the
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True, very good point! |
||||||||||
break; | ||||||||||
case EventType::transaction(): | ||||||||||
$items[] = TransactionItem::toEnvelopeItem($event); | ||||||||||
if ($event->getSdkMetadata('profile') !== null) { | ||||||||||
$items[] = ProfileItem::toEnvelopeItem($event); | ||||||||||
} | ||||||||||
$items[] = AttachmentItem::toEnvelopeItem($event); | ||||||||||
break; | ||||||||||
case EventType::checkIn(): | ||||||||||
$items[] = CheckInItem::toEnvelopeItem($event); | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Null Handling Bug in File Operations
The
?: null
operator ingetSize()
andgetData()
incorrectly handles empty files. It convertsfilesize()
's0
andfile_get_contents()
's empty string''
intonull
. This causes empty files to be treated as unreadable, conflicting with tests expecting0
for size and''
for data.