Skip to content

Commit e8f9bca

Browse files
authored
Logs improvements (#1848)
1 parent 1d45188 commit e8f9bca

10 files changed

Lines changed: 134 additions & 174 deletions

File tree

src/Attributes/Attribute.php

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@
77
/**
88
* @phpstan-type AttributeType 'string'|'boolean'|'integer'|'double'
99
* @phpstan-type AttributeValue string|bool|int|float
10-
* @phpstan-type AttributeSerialized array{
11-
* type: AttributeType,
12-
* value: AttributeValue
13-
* }
1410
*/
15-
class Attribute implements \JsonSerializable
11+
class Attribute
1612
{
1713
/**
1814
* @var AttributeType
@@ -100,25 +96,6 @@ public static function tryFromValue($value): ?self
10096
return null;
10197
}
10298

103-
/**
104-
* @return AttributeSerialized
105-
*/
106-
public function toArray(): array
107-
{
108-
return [
109-
'type' => $this->type,
110-
'value' => $this->value,
111-
];
112-
}
113-
114-
/**
115-
* @return AttributeSerialized
116-
*/
117-
public function jsonSerialize(): array
118-
{
119-
return $this->toArray();
120-
}
121-
12299
public function __toString(): string
123100
{
124101
return "{$this->value} ({$this->type})";

src/Attributes/AttributeBag.php

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66

77
/**
88
* @phpstan-import-type AttributeValue from Attribute
9-
* @phpstan-import-type AttributeSerialized from Attribute
109
*/
11-
class AttributeBag implements \JsonSerializable
10+
class AttributeBag
1211
{
1312
/**
1413
* @var array<string, Attribute>
@@ -36,33 +35,24 @@ public function get(string $key): ?Attribute
3635
return $this->attributes[$key] ?? null;
3736
}
3837

39-
/**
40-
* @return array<string, Attribute>
41-
*/
42-
public function all(): array
38+
public function forget(string $key): self
4339
{
44-
return $this->attributes;
45-
}
40+
unset($this->attributes[$key]);
4641

47-
/**
48-
* @return array<string, AttributeSerialized>
49-
*/
50-
public function toArray(): array
51-
{
52-
return array_map(static function (Attribute $attribute) {
53-
return $attribute->jsonSerialize();
54-
}, $this->attributes);
42+
return $this;
5543
}
5644

5745
/**
58-
* @return array<string, AttributeSerialized>
46+
* @return array<string, Attribute>
5947
*/
60-
public function jsonSerialize(): array
48+
public function all(): array
6149
{
62-
return $this->toArray();
50+
return $this->attributes;
6351
}
6452

6553
/**
54+
* Get a simplified representation of the attributes as a key-value array, main purpose is for logging output.
55+
*
6656
* @return array<string, AttributeValue>
6757
*/
6858
public function toSimpleArray(): array

src/Logs/Log.php

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,9 @@
44

55
namespace Sentry\Logs;
66

7-
use Sentry\Attributes\Attribute;
87
use Sentry\Attributes\AttributeBag;
98

10-
/**
11-
* @phpstan-import-type AttributeSerialized from Attribute
12-
*
13-
* @phpstan-type LogEnvelopeItem array{
14-
* timestamp: int|float,
15-
* trace_id: string,
16-
* level: string,
17-
* body: string,
18-
* attributes: array<string, AttributeSerialized>
19-
* }
20-
*/
21-
class Log implements \JsonSerializable
9+
class Log
2210
{
2311
/**
2412
* @var float
@@ -63,21 +51,49 @@ public function getTimestamp(): float
6351
return $this->timestamp;
6452
}
6553

54+
public function setTimestamp(float $timestamp): self
55+
{
56+
$this->timestamp = $timestamp;
57+
58+
return $this;
59+
}
60+
6661
public function getTraceId(): string
6762
{
6863
return $this->traceId;
6964
}
7065

66+
public function setTraceId(string $traceId): self
67+
{
68+
$this->traceId = $traceId;
69+
70+
return $this;
71+
}
72+
7173
public function getLevel(): LogLevel
7274
{
7375
return $this->level;
7476
}
7577

78+
public function setLevel(LogLevel $level): self
79+
{
80+
$this->level = $level;
81+
82+
return $this;
83+
}
84+
7685
public function getBody(): string
7786
{
7887
return $this->body;
7988
}
8089

90+
public function setBody(string $body): self
91+
{
92+
$this->body = $body;
93+
94+
return $this;
95+
}
96+
8197
public function attributes(): AttributeBag
8298
{
8399
return $this->attributes;
@@ -92,18 +108,4 @@ public function setAttribute(string $key, $value): self
92108

93109
return $this;
94110
}
95-
96-
/**
97-
* @return LogEnvelopeItem
98-
*/
99-
public function jsonSerialize(): array
100-
{
101-
return [
102-
'timestamp' => $this->timestamp,
103-
'trace_id' => $this->traceId,
104-
'level' => (string) $this->level,
105-
'body' => $this->body,
106-
'attributes' => $this->attributes->toArray(),
107-
];
108-
}
109111
}

src/Logs/LogsAggregator.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
use Sentry\Util\Arr;
1515

1616
/**
17-
* @phpstan-import-type AttributeValue from Attribute
18-
*
1917
* @internal
2018
*/
2119
final class LogsAggregator

src/Serializer/EnvelopItems/LogsItem.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
namespace Sentry\Serializer\EnvelopItems;
66

7+
use Sentry\Attributes\Attribute;
78
use Sentry\Event;
89
use Sentry\EventType;
10+
use Sentry\Logs\Log;
911
use Sentry\Util\JSON;
1012

1113
/**
@@ -27,7 +29,20 @@ public static function toEnvelopeItem(Event $event): string
2729
"%s\n%s",
2830
JSON::encode($header),
2931
JSON::encode([
30-
'items' => $logs,
32+
'items' => array_map(static function (Log $log): array {
33+
return [
34+
'timestamp' => $log->getTimestamp(),
35+
'trace_id' => $log->getTraceId(),
36+
'level' => (string) $log->getLevel(),
37+
'body' => $log->getBody(),
38+
'attributes' => array_map(static function (Attribute $attribute): array {
39+
return [
40+
'type' => $attribute->getType(),
41+
'value' => $attribute->getValue(),
42+
];
43+
}, $log->attributes()->all()),
44+
];
45+
}, $logs),
3146
])
3247
);
3348
}

tests/Attributes/AttributeBagTest.php

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
use Sentry\Attributes\Attribute;
99
use Sentry\Attributes\AttributeBag;
1010

11-
/**
12-
* @phpstan-import-type AttributeValue from Attribute
13-
* @phpstan-import-type AttributeSerialized from Attribute
14-
*/
1511
final class AttributeBagTest extends TestCase
1612
{
1713
public function testGettersAndSetters(): void
@@ -25,34 +21,13 @@ public function testGettersAndSetters(): void
2521
$this->assertCount(1, $bag->all());
2622
$this->assertInstanceOf(Attribute::class, $bag->get('foo'));
2723

28-
$this->assertNull($bag->get('non-existing'));
29-
}
30-
31-
public function testSerializeAsJson(): void
32-
{
33-
$bag = new AttributeBag();
34-
$bag->set('foo', 'bar');
35-
36-
$this->assertEquals(
37-
['foo' => ['type' => 'string', 'value' => 'bar']],
38-
$bag->jsonSerialize()
39-
);
24+
$bag->set('will-be-removed', 'baz');
4025

41-
$this->assertEquals(
42-
'{"foo":{"type":"string","value":"bar"}}',
43-
json_encode($bag)
44-
);
45-
}
26+
$this->assertNotNull($bag->get('will-be-removed'));
4627

47-
public function testSerializeAsArray(): void
48-
{
49-
$bag = new AttributeBag();
50-
$bag->set('foo', 'bar');
28+
$bag->forget('will-be-removed');
5129

52-
$this->assertEquals(
53-
['foo' => ['type' => 'string', 'value' => 'bar']],
54-
$bag->toArray()
55-
);
30+
$this->assertNull($bag->get('will-be-removed'));
5631
}
5732

5833
public function testSerializeAsSimpleArray(): void

tests/Attributes/AttributeTest.php

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
use Sentry\Attributes\Attribute;
99

1010
/**
11+
* @phpstan-import-type AttributeType from Attribute
1112
* @phpstan-import-type AttributeValue from Attribute
12-
* @phpstan-import-type AttributeSerialized from Attribute
1313
*/
1414
final class AttributeTest extends TestCase
1515
{
1616
/**
17-
* @param AttributeValue $value
18-
* @param AttributeSerialized|null $expected
17+
* @param AttributeValue $value
18+
* @param array{type: AttributeType, value: AttributeValue}|null $expected
1919
*
2020
* @dataProvider fromValueDataProvider
2121
*/
22-
public function testFromValue($value, $expected): void
22+
public function testFromValue($value, ?array $expected): void
2323
{
2424
$attribute = Attribute::tryFromValue($value);
2525

@@ -29,7 +29,6 @@ public function testFromValue($value, $expected): void
2929
return;
3030
}
3131

32-
$this->assertEquals($expected, $attribute->toArray());
3332
$this->assertEquals($expected['type'], $attribute->getType());
3433
$this->assertEquals($expected['value'], $attribute->getValue());
3534
}
@@ -97,35 +96,6 @@ public function __toString(): string
9796
];
9897
}
9998

100-
public function testSerializeAsJson(): void
101-
{
102-
$attribute = Attribute::tryFromValue('foo');
103-
104-
$this->assertInstanceOf(Attribute::class, $attribute);
105-
106-
$this->assertEquals(
107-
['type' => 'string', 'value' => 'foo'],
108-
$attribute->jsonSerialize()
109-
);
110-
111-
$this->assertEquals(
112-
'{"type":"string","value":"foo"}',
113-
json_encode($attribute)
114-
);
115-
}
116-
117-
public function testSerializeAsArray(): void
118-
{
119-
$attribute = Attribute::tryFromValue('foo');
120-
121-
$this->assertInstanceOf(Attribute::class, $attribute);
122-
123-
$this->assertEquals(
124-
['type' => 'string', 'value' => 'foo'],
125-
$attribute->toArray()
126-
);
127-
}
128-
12999
public function testSerializeAsString(): void
130100
{
131101
$attribute = Attribute::tryFromValue('foo');

tests/Logs/LogTest.php

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,26 @@
1515
*/
1616
final class LogTest extends TestCase
1717
{
18-
public function testJsonSerializesToExpected(): void
18+
public function testGettersAndSetters(): void
1919
{
20-
$timestamp = microtime(true);
21-
22-
$log = new Log($timestamp, '123', LogLevel::debug(), 'foo');
23-
24-
$log->setAttribute('foo', 'bar');
25-
$log->setAttribute('should-be-missing', ['foo' => 'bar']);
26-
27-
$this->assertEquals(
28-
[
29-
'timestamp' => $timestamp,
30-
'trace_id' => '123',
31-
'level' => 'debug',
32-
'body' => 'foo',
33-
'attributes' => [
34-
'foo' => [
35-
'type' => 'string',
36-
'value' => 'bar',
37-
],
38-
],
39-
],
40-
$log->jsonSerialize()
41-
);
20+
$log = new Log(1.0, '123', LogLevel::debug(), 'foo');
21+
22+
$this->assertSame(1.0, $log->getTimestamp());
23+
$this->assertSame('123', $log->getTraceId());
24+
$this->assertSame(LogLevel::debug(), $log->getLevel());
25+
$this->assertSame('foo', $log->getBody());
26+
$this->assertSame([], $log->attributes()->all());
27+
28+
$log->setTimestamp(2.0);
29+
$this->assertSame(2.0, $log->getTimestamp());
30+
31+
$log->setTraceId('456');
32+
$this->assertSame('456', $log->getTraceId());
33+
34+
$log->setLevel(LogLevel::warn());
35+
$this->assertSame(LogLevel::warn(), $log->getLevel());
36+
37+
$log->setBody('bar');
38+
$this->assertSame('bar', $log->getBody());
4239
}
4340
}

0 commit comments

Comments
 (0)