Skip to content

Commit

Permalink
psr3: handle non-string attribute keys (#297)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
brettmc authored Oct 2, 2024
1 parent 3dd4f06 commit 4e8d9bd
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 16 deletions.
13 changes: 6 additions & 7 deletions src/Instrumentation/Psr3/src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -36,6 +36,5 @@ public static function formatThrowable(?Throwable $exception): array
}

return [];

}
}
6 changes: 4 additions & 2 deletions src/Instrumentation/Psr3/src/Psr3Instrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
1 change: 1 addition & 0 deletions src/Instrumentation/Psr3/tests/Unit/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
];
Expand Down
5 changes: 3 additions & 2 deletions src/Instrumentation/Psr3/tests/phpt/export_apix.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -81,7 +81,8 @@ $span->end();
],
"previous": []
}
}
},
"0": "zero"
},
"dropped_attributes_count": 0
}
Expand Down
5 changes: 3 additions & 2 deletions src/Instrumentation/Psr3/tests/phpt/export_monolog.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -78,7 +78,8 @@ $span->end();
],
"previous": []
}
}
},
"0": "zero"
},
"dropped_attributes_count": 0
}
Expand Down
5 changes: 3 additions & 2 deletions src/Instrumentation/Psr3/tests/phpt/export_symfony.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -74,7 +74,8 @@ $span->end();
"trace": %a,
"previous": []
}
}
},
"0": "zero"
},
"dropped_attributes_count": 0
}
Expand Down

0 comments on commit 4e8d9bd

Please sign in to comment.