Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 18 additions & 7 deletions src/Attributes/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Sentry\Attributes;

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 +71,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,14 +86,22 @@ public static function tryFromValue($value): ?self
return new self($value, 'double');
}

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

if (empty($stringValue)) {
return null;
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'))) {
return new self((string) $value, 'string');
}

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

return null;
Expand Down
4 changes: 2 additions & 2 deletions src/Logs/LogsAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ public function add(
$attributes = Arr::simpleDot($attributes);

foreach ($attributes as $key => $value) {
$attribute = Attribute::tryFromValue($value);

if (!\is_string($key)) {
if ($sdkLogger !== null) {
$sdkLogger->info(
Expand All @@ -121,6 +119,8 @@ public function add(
continue;
}

$attribute = Attribute::tryFromValue($value);

if ($attribute === null) {
if ($sdkLogger !== null) {
$sdkLogger->info(
Expand Down
48 changes: 44 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 @@ -43,6 +44,14 @@ public static function fromValueDataProvider(): \Generator
],
];

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

yield [
123,
[
Expand All @@ -67,6 +76,14 @@ public static function fromValueDataProvider(): \Generator
],
];

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

yield [
new class {
public function __toString(): string
Expand All @@ -80,19 +97,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 +151,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