Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 19 additions & 1 deletion src/Attributes/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Sentry\Attributes;

use Sentry\Exception\JsonException;
use Sentry\Serializer\SerializableInterface;
use Sentry\Util\JSON;

/**
* @phpstan-type AttributeType 'string'|'boolean'|'integer'|'double'
* @phpstan-type AttributeValue string|bool|int|float
Expand Down Expand Up @@ -68,7 +72,7 @@ public static function fromValue($value): self
public static function tryFromValue($value): ?self
{
if ($value === null) {
return null;
return new self('null', 'string');
}

if (\is_bool($value)) {
Expand All @@ -83,6 +87,14 @@ public static function tryFromValue($value): ?self
return new self($value, 'double');
}

if ($value instanceof SerializableInterface) {
try {
return new self(JSON::encode($value->toSentry()), 'string');
} catch (\Throwable $e) {
// Ignore the exception and continue trying other methods
}
}

if (\is_string($value) || (\is_object($value) && method_exists($value, '__toString'))) {
$stringValue = (string) $value;

Expand All @@ -93,6 +105,12 @@ public static function tryFromValue($value): ?self
return new self($stringValue, 'string');
}

try {
return new self(JSON::encode($value), 'string');
} catch (\Throwable $e) {
// Ignore the exception
}

return null;
}

Expand Down
32 changes: 28 additions & 4 deletions tests/Attributes/AttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\TestCase;
use Sentry\Attributes\Attribute;
use Sentry\Serializer\SerializableInterface;

/**
* @phpstan-import-type AttributeType from Attribute
Expand Down Expand Up @@ -80,19 +81,41 @@ public function __toString(): string
],
];

yield [
new class implements SerializableInterface {
public function toSentry(): ?array
{
return ['foo' => 'bar'];
}
},
[
'type' => 'string',
'value' => '{"foo":"bar"}',
],
];

yield [
new class {},
null,
[
'type' => 'string',
'value' => '{}',
],
];

yield [
new \stdClass(),
null,
[
'type' => 'string',
'value' => '{}',
],
];

yield [
[],
null,
[
'type' => 'string',
'value' => '[]',
],
];
}

Expand All @@ -112,6 +135,7 @@ public function testFromValueFactoryMethod(): void
{
$this->expectException(\InvalidArgumentException::class);

Attribute::fromValue([]);
// Since we support almost any type, we use a resource to trigger the exception
Attribute::fromValue(fopen(__FILE__, 'r'));
}
}
2 changes: 1 addition & 1 deletion tests/Logs/LogsAggregatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static function attributesDataProvider(): \Generator

yield [
['foo' => ['bar']],
[],
['foo' => '["bar"]'],
];
}

Expand Down
7 changes: 6 additions & 1 deletion tests/Logs/LogsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,17 @@ public function testLogWithNestedAttributes(): void

$this->assertNotNull($attribute);
$this->assertEquals('bar', $attribute->getValue());

$attribute = $logItem->attributes()->get('nested.baz');

$this->assertNotNull($attribute);
$this->assertEquals(json_encode([1, 2, 3]), $attribute->getValue());
});

logger()->info('Some message', [], [
'nested' => [
'foo' => 'bar',
'should-be-missing' => [1, 2, 3],
'baz' => [1, 2, 3],
],
]);

Expand Down
Loading