Skip to content

Commit d614650

Browse files
committed
psr3: handle non-string attribute keys
non-string attribute keys cause an error, and stop log messages from being exported. cast the numeric keys to a string to ensure they make it through alive. update tests, skipping cake because it does weird stuff when presented with a numeric key which completely changes the structure of the incoming context
1 parent 3dd4f06 commit d614650

File tree

7 files changed

+21
-16
lines changed

7 files changed

+21
-16
lines changed

src/Instrumentation/Psr3/src/Formatter.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ class Formatter
1010
{
1111
public static function format(array $context): array
1212
{
13-
$return = [];
13+
$formatted = [];
1414
foreach ($context as $key => $value) {
1515
if ($key === 'exception' && $value instanceof Throwable) {
16-
$return[$key] = self::formatThrowable($value);
16+
$formatted[$key] = self::formatThrowable($value);
1717
} else {
18-
$return[$key] = json_decode(json_encode($value));
18+
$formatted[$key] = json_decode(json_encode($value));
1919
}
2020
}
2121

22-
return $return;
22+
return $formatted;
2323
}
2424

25-
public static function formatThrowable(?Throwable $exception): array
25+
private static function formatThrowable(?Throwable $exception): array
2626
{
27-
if($exception) {
27+
if ($exception) {
2828
return [
2929
'message' => $exception->getMessage(),
3030
'code' => $exception->getCode(),
@@ -36,6 +36,5 @@ public static function formatThrowable(?Throwable $exception): array
3636
}
3737

3838
return [];
39-
4039
}
4140
}

src/Instrumentation/Psr3/src/Psr3Instrumentation.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ public static function register(): void
7676
}
7777

7878
$record = (new API\LogRecord($body))
79-
->setSeverityNumber(API\Map\Psr3::severityNumber($level))
80-
->setAttributes(Formatter::format($context));
79+
->setSeverityNumber(API\Map\Psr3::severityNumber($level));
80+
foreach (Formatter::format($context) as $key => $value) {
81+
$record->setAttribute((string) $key, $value);
82+
}
8183
$instrumentation->logger()->emit($record);
8284

8385
break;

src/Instrumentation/Psr3/tests/Integration/Psr3InstrumentationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function test_log(): void
4949
{
5050
$level = LogLevel::EMERGENCY;
5151
$msg = 'log test';
52-
$context = ['user' => 'php', 'pid' => 1];
52+
$context = [0 => 'zero', 'user' => 'php', 'pid' => 1];
5353

5454
$this->logger
5555
->expects($this->once())

src/Instrumentation/Psr3/tests/Unit/FormatterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class FormatterTest extends TestCase
1212
public function test_format(): void
1313
{
1414
$context = [
15+
0 => 'zero',
1516
'foo' => 'bar',
1617
'exception' => new \Exception('foo', 500, new \RuntimeException('bar')),
1718
];

src/Instrumentation/Psr3/tests/phpt/export_apix.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ $scope = $span->activate();
2222

2323
$input = require(__DIR__ . '/input.php');
2424

25-
$logger->info($input['message'], $input['context']);
25+
$logger->info($input['message'], $input['context'] + ['zero']);
2626

2727
$scope->detach();
2828
$span->end();
@@ -81,7 +81,8 @@ $span->end();
8181
],
8282
"previous": []
8383
}
84-
}
84+
},
85+
"0": "zero"
8586
},
8687
"dropped_attributes_count": 0
8788
}

src/Instrumentation/Psr3/tests/phpt/export_monolog.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ $scope = $span->activate();
2121

2222
$input = require(__DIR__ . '/input.php');
2323

24-
$logger->warning($input['message'], $input['context']);
24+
$logger->warning($input['message'], $input['context'] + ['zero']);
2525

2626
$scope->detach();
2727
$span->end();
@@ -78,7 +78,8 @@ $span->end();
7878
],
7979
"previous": []
8080
}
81-
}
81+
},
82+
"0": "zero"
8283
},
8384
"dropped_attributes_count": 0
8485
}

src/Instrumentation/Psr3/tests/phpt/export_symfony.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ $scope = $span->activate();
2121

2222
$input = require(__DIR__ . '/input.php');
2323

24-
$logger->warning($input['message'], $input['context']);
24+
$logger->warning($input['message'], $input['context'] + ['zero']);
2525

2626
$scope->detach();
2727
$span->end();
@@ -74,7 +74,8 @@ $span->end();
7474
"trace": %a,
7575
"previous": []
7676
}
77-
}
77+
},
78+
"0": "zero"
7879
},
7980
"dropped_attributes_count": 0
8081
}

0 commit comments

Comments
 (0)