diff --git a/composer.json b/composer.json index 298ddee..b0db656 100644 --- a/composer.json +++ b/composer.json @@ -28,11 +28,10 @@ "require-dev": { "doctrine/dbal": "^3.0", "friends-of-phpspec/phpspec-expect": "^4.0", - "open-telemetry/transport-grpc": "^1.0", "open-telemetry/gen-otlp-protobuf": "^1.0", "php-http/httplug": "^2.3", "phpspec/phpspec": "^7.5", - "phpstan/phpstan": "<1.11", + "phpstan/phpstan": "^1.4", "symfony/framework-bundle": "*", "symfony/http-client": "*", "symfony/messenger": "*", diff --git a/src/Bridge/GoogleCloud/Logging/Formatter/StdOutFormatter.php b/src/Bridge/GoogleCloud/Logging/Formatter/StdOutFormatter.php index 2cd6b34..3d157d6 100644 --- a/src/Bridge/GoogleCloud/Logging/Formatter/StdOutFormatter.php +++ b/src/Bridge/GoogleCloud/Logging/Formatter/StdOutFormatter.php @@ -45,30 +45,32 @@ protected function normalize(mixed $data, int $depth = 0): mixed $data['severity'] = $data['level_name']; unset($data['level_name']); - // Map tracing - if (isset($data['context']['trace'])) { - $data['logging.googleapis.com/trace'] = 'projects/'.$this->project.'/traces/'.$data['context']['trace']; - } - if (isset($data['context']['span'])) { - $data['logging.googleapis.com/spanId'] = $data['context']['span']; - } - if (isset($data['context']['sampled'])) { - $data['logging.googleapis.com/trace_sampled'] = $data['context']['sampled']; - } - if (isset($data['context']['operation'])) { - $data['logging.googleapis.com/operation'] = $data['context']['operation']; + if (\is_array($data['context'])) { + // Map tracing + if (isset($data['context']['trace'])) { + $data['logging.googleapis.com/trace'] = 'projects/'.$this->project.'/traces/'.$data['context']['trace']; + } + if (isset($data['context']['span'])) { + $data['logging.googleapis.com/spanId'] = $data['context']['span']; + } + if (isset($data['context']['sampled'])) { + $data['logging.googleapis.com/trace_sampled'] = $data['context']['sampled']; + } + if (isset($data['context']['operation'])) { + $data['logging.googleapis.com/operation'] = $data['context']['operation']; + } + unset($data['context']['trace'], $data['context']['span'], $data['context']['sampled'], $data['context']['operation']); + + if ($exception = $data['context']['exception'] ?? false) { + $data['message'] = $exception['message']; + $data['context'] = array_merge($data['context'], $exception['context']); + unset($data['context']['exception']); + } } - unset($data['context']['trace'], $data['context']['span'], $data['context']['sampled'], $data['context']['operation']); // Map channel $data['logging.googleapis.com/labels'] = ['channel' => $data['channel']]; unset($data['channel']); - - if ($exception = $data['context']['exception'] ?? false) { - $data['message'] = $exception['message']; - $data['context'] = array_merge($data['context'], $exception['context']); - unset($data['context']['exception']); - } } return $data; diff --git a/src/Metrics/CounterAdapter.php b/src/Metrics/CounterAdapter.php index d76fc2f..8334bd8 100644 --- a/src/Metrics/CounterAdapter.php +++ b/src/Metrics/CounterAdapter.php @@ -12,6 +12,8 @@ class CounterAdapter implements CounterInterface { + use IterableAttributesTrait; + public function __construct( private string $name, private string $description, @@ -20,11 +22,12 @@ public function __construct( } /** - * @param int $amount - * @param array{labels: array} $attributes + * @param int $amount + * @param iterable> $attributes */ public function add($amount, iterable $attributes = [], $context = null): void { + $attributes = $this->normalizeAttributes($attributes); /** @var array $labelNames */ $labelNames = array_keys($attributes['labels'] ?? []); $labelValues = array_values($attributes['labels'] ?? []); diff --git a/src/Metrics/HistogramAdapter.php b/src/Metrics/HistogramAdapter.php index 4088a4c..6bad420 100644 --- a/src/Metrics/HistogramAdapter.php +++ b/src/Metrics/HistogramAdapter.php @@ -13,6 +13,8 @@ class HistogramAdapter implements HistogramInterface { + use IterableAttributesTrait; + public function __construct( private string $name, private string $description, @@ -25,6 +27,7 @@ public function __construct( */ public function record($amount, iterable $attributes = [], $context = null): void { + $attributes = $this->normalizeAttributes($attributes); /** @var array $labelNames */ $labelNames = array_keys($attributes['labels'] ?? []); $labelValues = array_values($attributes['labels'] ?? []); diff --git a/src/Metrics/IterableAttributesTrait.php b/src/Metrics/IterableAttributesTrait.php new file mode 100644 index 0000000..161684c --- /dev/null +++ b/src/Metrics/IterableAttributesTrait.php @@ -0,0 +1,26 @@ + + */ + +namespace Instrumentation\Metrics; + +trait IterableAttributesTrait +{ + /** + * @param iterable> $attributes + * + * @return array> + */ + public function normalizeAttributes(iterable $attributes): array + { + $newAttr = []; + foreach ($attributes as $key => $value) { + $newAttr[$key] = $value; + } + + return $newAttr; + } +} diff --git a/src/Metrics/UpDownCounterAdapter.php b/src/Metrics/UpDownCounterAdapter.php index 8d64be2..21f2655 100644 --- a/src/Metrics/UpDownCounterAdapter.php +++ b/src/Metrics/UpDownCounterAdapter.php @@ -12,6 +12,8 @@ class UpDownCounterAdapter implements UpDownCounterInterface { + use IterableAttributesTrait; + public function __construct( private string $name, private string $description, @@ -25,6 +27,7 @@ public function __construct( */ public function add($amount, iterable $attributes = [], $context = null): void { + $attributes = $this->normalizeAttributes($attributes); /** @var array $labelNames */ $labelNames = array_keys($attributes['labels'] ?? []); $labelValues = array_values($attributes['labels'] ?? []);