From 4e8d9bdc2188b8f5e020521c5809bdee35636d64 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Wed, 2 Oct 2024 22:41:32 +1000 Subject: [PATCH] psr3: handle non-string attribute keys (#297) 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 --- src/Instrumentation/Psr3/src/Formatter.php | 13 ++++++------- .../Psr3/src/Psr3Instrumentation.php | 6 ++++-- .../tests/Integration/Psr3InstrumentationTest.php | 2 +- .../Psr3/tests/Unit/FormatterTest.php | 1 + .../Psr3/tests/phpt/export_apix.phpt | 5 +++-- .../Psr3/tests/phpt/export_monolog.phpt | 5 +++-- .../Psr3/tests/phpt/export_symfony.phpt | 5 +++-- 7 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/Instrumentation/Psr3/src/Formatter.php b/src/Instrumentation/Psr3/src/Formatter.php index 520648de..c88acdf8 100644 --- a/src/Instrumentation/Psr3/src/Formatter.php +++ b/src/Instrumentation/Psr3/src/Formatter.php @@ -10,21 +10,21 @@ class Formatter { public static function format(array $context): array { - $return = []; + $formatted = []; foreach ($context as $key => $value) { if ($key === 'exception' && $value instanceof Throwable) { - $return[$key] = self::formatThrowable($value); + $formatted[$key] = self::formatThrowable($value); } else { - $return[$key] = json_decode(json_encode($value)); + $formatted[$key] = json_decode(json_encode($value)); } } - return $return; + return $formatted; } - public static function formatThrowable(?Throwable $exception): array + private static function formatThrowable(?Throwable $exception): array { - if($exception) { + if ($exception) { return [ 'message' => $exception->getMessage(), 'code' => $exception->getCode(), @@ -36,6 +36,5 @@ public static function formatThrowable(?Throwable $exception): array } return []; - } } diff --git a/src/Instrumentation/Psr3/src/Psr3Instrumentation.php b/src/Instrumentation/Psr3/src/Psr3Instrumentation.php index 8b3c59be..2cca4e8e 100644 --- a/src/Instrumentation/Psr3/src/Psr3Instrumentation.php +++ b/src/Instrumentation/Psr3/src/Psr3Instrumentation.php @@ -76,8 +76,10 @@ public static function register(): void } $record = (new API\LogRecord($body)) - ->setSeverityNumber(API\Map\Psr3::severityNumber($level)) - ->setAttributes(Formatter::format($context)); + ->setSeverityNumber(API\Map\Psr3::severityNumber($level)); + foreach (Formatter::format($context) as $key => $value) { + $record->setAttribute((string) $key, $value); + } $instrumentation->logger()->emit($record); break; diff --git a/src/Instrumentation/Psr3/tests/Integration/Psr3InstrumentationTest.php b/src/Instrumentation/Psr3/tests/Integration/Psr3InstrumentationTest.php index b458e14d..e1ae8f11 100644 --- a/src/Instrumentation/Psr3/tests/Integration/Psr3InstrumentationTest.php +++ b/src/Instrumentation/Psr3/tests/Integration/Psr3InstrumentationTest.php @@ -49,7 +49,7 @@ public function test_log(): void { $level = LogLevel::EMERGENCY; $msg = 'log test'; - $context = ['user' => 'php', 'pid' => 1]; + $context = [0 => 'zero', 'user' => 'php', 'pid' => 1]; $this->logger ->expects($this->once()) diff --git a/src/Instrumentation/Psr3/tests/Unit/FormatterTest.php b/src/Instrumentation/Psr3/tests/Unit/FormatterTest.php index 97c5ba8d..b827abe8 100644 --- a/src/Instrumentation/Psr3/tests/Unit/FormatterTest.php +++ b/src/Instrumentation/Psr3/tests/Unit/FormatterTest.php @@ -12,6 +12,7 @@ class FormatterTest extends TestCase public function test_format(): void { $context = [ + 0 => 'zero', 'foo' => 'bar', 'exception' => new \Exception('foo', 500, new \RuntimeException('bar')), ]; diff --git a/src/Instrumentation/Psr3/tests/phpt/export_apix.phpt b/src/Instrumentation/Psr3/tests/phpt/export_apix.phpt index 67b24890..c506f205 100644 --- a/src/Instrumentation/Psr3/tests/phpt/export_apix.phpt +++ b/src/Instrumentation/Psr3/tests/phpt/export_apix.phpt @@ -22,7 +22,7 @@ $scope = $span->activate(); $input = require(__DIR__ . '/input.php'); -$logger->info($input['message'], $input['context']); +$logger->info($input['message'], $input['context'] + ['zero']); $scope->detach(); $span->end(); @@ -81,7 +81,8 @@ $span->end(); ], "previous": [] } - } + }, + "0": "zero" }, "dropped_attributes_count": 0 } diff --git a/src/Instrumentation/Psr3/tests/phpt/export_monolog.phpt b/src/Instrumentation/Psr3/tests/phpt/export_monolog.phpt index 919579ae..0ac95f04 100644 --- a/src/Instrumentation/Psr3/tests/phpt/export_monolog.phpt +++ b/src/Instrumentation/Psr3/tests/phpt/export_monolog.phpt @@ -21,7 +21,7 @@ $scope = $span->activate(); $input = require(__DIR__ . '/input.php'); -$logger->warning($input['message'], $input['context']); +$logger->warning($input['message'], $input['context'] + ['zero']); $scope->detach(); $span->end(); @@ -78,7 +78,8 @@ $span->end(); ], "previous": [] } - } + }, + "0": "zero" }, "dropped_attributes_count": 0 } diff --git a/src/Instrumentation/Psr3/tests/phpt/export_symfony.phpt b/src/Instrumentation/Psr3/tests/phpt/export_symfony.phpt index b7a07fab..d43517ff 100644 --- a/src/Instrumentation/Psr3/tests/phpt/export_symfony.phpt +++ b/src/Instrumentation/Psr3/tests/phpt/export_symfony.phpt @@ -21,7 +21,7 @@ $scope = $span->activate(); $input = require(__DIR__ . '/input.php'); -$logger->warning($input['message'], $input['context']); +$logger->warning($input['message'], $input['context'] + ['zero']); $scope->detach(); $span->end(); @@ -74,7 +74,8 @@ $span->end(); "trace": %a, "previous": [] } - } + }, + "0": "zero" }, "dropped_attributes_count": 0 }