From 8562d0d510d133eac0dcd40376962bd1a214a9ba Mon Sep 17 00:00:00 2001 From: leo108 Date: Mon, 10 Oct 2022 06:17:43 +0800 Subject: [PATCH] Grpc metadata value must be an array (#839) --- src/Contrib/OtlpGrpc/Exporter.php | 21 ++++++++++++++++--- .../Contrib/OtlpGrpc/OTLPGrpcExporterTest.php | 8 +++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/Contrib/OtlpGrpc/Exporter.php b/src/Contrib/OtlpGrpc/Exporter.php index 218ef0dc8..e8db779d5 100644 --- a/src/Contrib/OtlpGrpc/Exporter.php +++ b/src/Contrib/OtlpGrpc/Exporter.php @@ -67,9 +67,9 @@ public function __construct( $this->getIntFromEnvironment(Env::OTEL_EXPORTER_OTLP_TRACES_TIMEOUT, $timeout) : $this->getIntFromEnvironment(Env::OTEL_EXPORTER_OTLP_TIMEOUT, $timeout); - $this->metadata = $this->hasEnvironmentVariable(Env::OTEL_EXPORTER_OTLP_TRACES_HEADERS) ? + $this->metadata = self::transformToGrpcMetadata($this->hasEnvironmentVariable(Env::OTEL_EXPORTER_OTLP_TRACES_HEADERS) ? $this->getMapFromEnvironment(Env::OTEL_EXPORTER_OTLP_TRACES_HEADERS, $headers) : - $this->getMapFromEnvironment(Env::OTEL_EXPORTER_OTLP_HEADERS, $headers); + $this->getMapFromEnvironment(Env::OTEL_EXPORTER_OTLP_HEADERS, $headers)); $opts = $this->getClientOptions(); @@ -160,8 +160,12 @@ protected function doExport(iterable $spans): bool public function setHeader($key, $value): void { - // metadata is supposed to be key-value pairs + // metadata is supposed to be key-value pairs and the value must be an array // @see https://grpc.io/docs/what-is-grpc/core-concepts/#metadata + if (!is_array($value)) { + $value = [$value]; + } + $this->metadata[$key] = $value; } @@ -190,4 +194,15 @@ public static function fromConnectionString(string $endpointUrl = null, string $ { return is_string($endpointUrl) ? new Exporter($endpointUrl) : new Exporter(); } + + private static function transformToGrpcMetadata(array $metadata): array + { + foreach ($metadata as $key => $value) { + if (!is_array($value)) { + $metadata[$key] = [$value]; + } + } + + return $metadata; + } } diff --git a/tests/Unit/Contrib/OtlpGrpc/OTLPGrpcExporterTest.php b/tests/Unit/Contrib/OtlpGrpc/OTLPGrpcExporterTest.php index 3fe9764b6..b5fb6ef4f 100644 --- a/tests/Unit/Contrib/OtlpGrpc/OTLPGrpcExporterTest.php +++ b/tests/Unit/Contrib/OtlpGrpc/OTLPGrpcExporterTest.php @@ -107,7 +107,7 @@ public function test_set_headers_with_environment_variables(): void $exporter = new Exporter(); - $this->assertEquals(['x-aaa' => 'foo', 'x-bbb' => 'barf'], $exporter->getHeaders()); + $this->assertEquals(['x-aaa' => ['foo'], 'x-bbb' => ['barf']], $exporter->getHeaders()); } public function test_set_header(): void @@ -116,18 +116,18 @@ public function test_set_header(): void $exporter->setHeader('foo', 'bar'); $headers = $exporter->getHeaders(); $this->assertArrayHasKey('foo', $headers); - $this->assertEquals('bar', $headers['foo']); + $this->assertEquals(['bar'], $headers['foo']); } public function test_set_headers_in_constructor(): void { $exporter = new Exporter('localhost:4317', true, '', 'x-aaa=foo,x-bbb=bar'); - $this->assertEquals(['x-aaa' => 'foo', 'x-bbb' => 'bar'], $exporter->getHeaders()); + $this->assertEquals(['x-aaa' => ['foo'], 'x-bbb' => ['bar']], $exporter->getHeaders()); $exporter->setHeader('key', 'value'); - $this->assertEquals(['x-aaa' => 'foo', 'x-bbb' => 'bar', 'key' => 'value'], $exporter->getHeaders()); + $this->assertEquals(['x-aaa' => ['foo'], 'x-bbb' => ['bar'], 'key' => ['value']], $exporter->getHeaders()); } private function isInsecure(Exporter $exporter) : bool