Skip to content

Commit

Permalink
Grpc metadata value must be an array (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
leo108 authored Oct 9, 2022
1 parent a3a47a6 commit 8562d0d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
21 changes: 18 additions & 3 deletions src/Contrib/OtlpGrpc/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
8 changes: 4 additions & 4 deletions tests/Unit/Contrib/OtlpGrpc/OTLPGrpcExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 8562d0d

Please sign in to comment.