Skip to content
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

New seralizer logic #1613

Merged
merged 4 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/Serializer/EnvelopItems/CheckInItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Sentry\Serializer\EnvelopItems;

use Sentry\Event;
use Sentry\Util\JSON;

/**
* @internal
*/
class CheckInItem implements EnvelopeItemInterface
{
public static function toEnvelopeItem(Event $event): string
{
$header = [
'type' => (string) $event->getType(),
'content_type' => 'application/json',
];

$payload = [];

$checkIn = $event->getCheckIn();
if ($checkIn !== null) {
$payload = [
'check_in_id' => $checkIn->getId(),
'monitor_slug' => $checkIn->getMonitorSlug(),
'status' => (string) $checkIn->getStatus(),
'duration' => $checkIn->getDuration(),
'release' => $checkIn->getRelease(),
'environment' => $checkIn->getEnvironment(),
];

if ($checkIn->getMonitorConfig() !== null) {
$payload['monitor_config'] = $checkIn->getMonitorConfig()->toArray();
}

if (!empty($event->getContexts()['trace'])) {
$payload['contexts']['trace'] = $event->getContexts()['trace'];
}
}

return sprintf("%s\n%s", JSON::encode($header), JSON::encode($payload));
}
}
15 changes: 15 additions & 0 deletions src/Serializer/EnvelopItems/EnvelopeItemInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Sentry\Serializer\EnvelopItems;

use Sentry\Event;

/**
* @internal
*/
interface EnvelopeItemInterface
{
public static function toEnvelopeItem(Event $event): string;
}
248 changes: 248 additions & 0 deletions src/Serializer/EnvelopItems/EventItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
<?php

declare(strict_types=1);

namespace Sentry\Serializer\EnvelopItems;

use Sentry\Event;
use Sentry\ExceptionDataBag;
use Sentry\Frame;
use Sentry\Serializer\Traits\BreadcrumbSeralizerTrait;
use Sentry\Util\JSON;

/**
* @internal
*/
class EventItem implements EnvelopeItemInterface
{
use BreadcrumbSeralizerTrait;

public static function toEnvelopeItem(Event $event): string
{
$header = [
'type' => (string) $event->getType(),
'content_type' => 'application/json',
];

$payload = [
'timestamp' => $event->getTimestamp(),
'platform' => 'php',
'sdk' => [
'name' => $event->getSdkIdentifier(),
'version' => $event->getSdkVersion(),
],
];

if ($event->getStartTimestamp() !== null) {
$payload['start_timestamp'] = $event->getStartTimestamp();
}

if ($event->getLevel() !== null) {
$payload['level'] = (string) $event->getLevel();
}

// TODO(michi) remove
cleptric marked this conversation as resolved.
Show resolved Hide resolved
// if ($event->getLogger() !== null) {
// $payload['logger'] = $event->getLogger();
// }

if ($event->getTransaction() !== null) {
$payload['transaction'] = $event->getTransaction();
}

if ($event->getServerName() !== null) {
$payload['server_name'] = $event->getServerName();
}

if ($event->getRelease() !== null) {
$payload['release'] = $event->getRelease();
}

if ($event->getEnvironment() !== null) {
$payload['environment'] = $event->getEnvironment();
}

if (!empty($event->getFingerprint())) {
$payload['fingerprint'] = $event->getFingerprint();
}

if (!empty($event->getModules())) {
$payload['modules'] = $event->getModules();
}

if (!empty($event->getExtra())) {
$payload['extra'] = $event->getExtra();
}

if (!empty($event->getTags())) {
$payload['tags'] = $event->getTags();
}

$user = $event->getUser();
if ($user !== null) {
$payload['user'] = array_merge($user->getMetadata(), [
'id' => $user->getId(),
'username' => $user->getUsername(),
'email' => $user->getEmail(),
'ip_address' => $user->getIpAddress(),
'segment' => $user->getSegment(),
]);
}

$osContext = $event->getOsContext();
if ($osContext !== null) {
$payload['contexts']['os'] = [
'name' => $osContext->getName(),
'version' => $osContext->getVersion(),
'build' => $osContext->getBuild(),
'kernel_version' => $osContext->getKernelVersion(),
];
}

$runtimeContext = $event->getRuntimeContext();
if ($runtimeContext !== null) {
$payload['contexts']['runtime'] = [
'name' => $runtimeContext->getName(),
'version' => $runtimeContext->getVersion(),
];
}

if (!empty($event->getContexts())) {
$payload['contexts'] = array_merge($payload['contexts'] ?? [], $event->getContexts());
}

if (!empty($event->getBreadcrumbs())) {
$payload['breadcrumbs']['values'] = array_map([self::class, 'serializeBreadcrumb'], $event->getBreadcrumbs());
}

if (!empty($event->getRequest())) {
$payload['request'] = $event->getRequest();
}

if ($event->getMessage() !== null) {
if (empty($event->getMessageParams())) {
$payload['message'] = $event->getMessage();
} else {
$payload['message'] = [
'message' => $event->getMessage(),
'params' => $event->getMessageParams(),
'formatted' => $event->getMessageFormatted() ?? vsprintf($event->getMessage(), $event->getMessageParams()),
];
}
}

$exceptions = $event->getExceptions();
for ($i = \count($exceptions) - 1; $i >= 0; --$i) {
$payload['exception']['values'][] = self::serializeException($exceptions[$i]);
}

$stacktrace = $event->getStacktrace();
if ($stacktrace !== null) {
$payload['stacktrace'] = [
'frames' => array_map([self::class, 'serializeStacktraceFrame'], $stacktrace->getFrames()),
];
}

return sprintf("%s\n%s", JSON::encode($header), JSON::encode($payload));
}

/**
* @return array<string, mixed>
*
* @psalm-return array{
* type: string,
* value: string,
* stacktrace?: array{
* frames: array<array<string, mixed>>
* },
* mechanism?: array{
* type: string,
* handled: boolean,
* data?: array<string, mixed>
* }
* }
*/
protected static function serializeException(ExceptionDataBag $exception): array
{
$exceptionMechanism = $exception->getMechanism();
$exceptionStacktrace = $exception->getStacktrace();
$result = [
'type' => $exception->getType(),
'value' => $exception->getValue(),
];

if ($exceptionStacktrace !== null) {
$result['stacktrace'] = [
'frames' => array_map([self::class, 'serializeStacktraceFrame'], $exceptionStacktrace->getFrames()),
];
}

if ($exceptionMechanism !== null) {
$result['mechanism'] = [
'type' => $exceptionMechanism->getType(),
'handled' => $exceptionMechanism->isHandled(),
];

if ($exceptionMechanism->getData() !== []) {
$result['mechanism']['data'] = $exceptionMechanism->getData();
}
}

return $result;
}

/**
* @return array<string, mixed>
*
* @psalm-return array{
* filename: string,
* lineno: int,
* in_app: bool,
* abs_path?: string,
* function?: string,
* raw_function?: string,
* pre_context?: string[],
* context_line?: string,
* post_context?: string[],
* vars?: array<string, mixed>
* }
*/
protected static function serializeStacktraceFrame(Frame $frame): array
{
$result = [
'filename' => $frame->getFile(),
'lineno' => $frame->getLine(),
'in_app' => $frame->isInApp(),
];

if ($frame->getAbsoluteFilePath() !== null) {
$result['abs_path'] = $frame->getAbsoluteFilePath();
}

if ($frame->getFunctionName() !== null) {
$result['function'] = $frame->getFunctionName();
}

if ($frame->getRawFunctionName() !== null) {
$result['raw_function'] = $frame->getRawFunctionName();
}

if (!empty($frame->getPreContext())) {
$result['pre_context'] = $frame->getPreContext();
}

if ($frame->getContextLine() !== null) {
$result['context_line'] = $frame->getContextLine();
}

if (!empty($frame->getPostContext())) {
$result['post_context'] = $frame->getPostContext();
}

if (!empty($frame->getVars())) {
$result['vars'] = $frame->getVars();
}

return $result;
}
}
35 changes: 35 additions & 0 deletions src/Serializer/EnvelopItems/ProfileItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Sentry\Serializer\EnvelopItems;

use Sentry\Event;
use Sentry\Profiling\Profile;
use Sentry\Util\JSON;

/**
* @internal
*/
class ProfileItem implements EnvelopeItemInterface
{
public static function toEnvelopeItem(Event $event): string
{
$header = [
'type' => 'profile',
'content_type' => 'application/json',
];

$profile = $event->getSdkMetadata('profile');
if (!$profile instanceof Profile) {
return '';
}

$payload = $profile->getFormattedData($event);
if ($payload === null) {
return '';
}

return sprintf("%s\n%s", JSON::encode($header), JSON::encode($payload));
}
}
Loading