Skip to content

Commit 342686b

Browse files
brettmcNevay
andauthored
support url-encoded OTEL_EXPORTER_OTLP_HEADERS values (#1242)
Per a recent spec clarification in open-telemetry/opentelemetry-specification#3832 OTLP headers should support url-encoded header values. Co-authored-by: Tobias Bachert <[email protected]>
1 parent eb29f80 commit 342686b

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

LogsExporterFactory.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ private function buildTransport(string $protocol): TransportInterface
4444
{
4545
$endpoint = $this->getEndpoint($protocol);
4646

47-
$headers = Configuration::has(Variables::OTEL_EXPORTER_OTLP_LOGS_HEADERS)
48-
? Configuration::getMap(Variables::OTEL_EXPORTER_OTLP_LOGS_HEADERS)
49-
: Configuration::getMap(Variables::OTEL_EXPORTER_OTLP_HEADERS);
50-
$headers += OtlpUtil::getUserAgentHeader();
47+
$headers = OtlpUtil::getHeaders(Signals::LOGS);
5148
$compression = $this->getCompression();
5249

5350
$factoryClass = Registry::transportFactory($protocol);

MetricExporterFactory.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ private function buildTransport(string $protocol): TransportInterface
5151
*/
5252
$endpoint = $this->getEndpoint($protocol);
5353

54-
$headers = Configuration::has(Variables::OTEL_EXPORTER_OTLP_METRICS_HEADERS)
55-
? Configuration::getMap(Variables::OTEL_EXPORTER_OTLP_METRICS_HEADERS)
56-
: Configuration::getMap(Variables::OTEL_EXPORTER_OTLP_HEADERS);
57-
$headers += OtlpUtil::getUserAgentHeader();
54+
$headers = OtlpUtil::getHeaders(Signals::METRICS);
5855
$compression = $this->getCompression();
5956

6057
$factoryClass = Registry::transportFactory($protocol);

OtlpUtil.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace OpenTelemetry\Contrib\Otlp;
66

77
use OpenTelemetry\API\Signals;
8+
use OpenTelemetry\SDK\Common\Configuration\Configuration;
9+
use OpenTelemetry\SDK\Common\Configuration\Variables;
810
use OpenTelemetry\SDK\Resource\Detectors\Sdk;
911
use OpenTelemetry\SemConv\ResourceAttributes;
1012
use UnexpectedValueException;
@@ -20,6 +22,11 @@ class OtlpUtil
2022
Signals::METRICS => '/opentelemetry.proto.collector.metrics.v1.MetricsService/Export',
2123
Signals::LOGS => '/opentelemetry.proto.collector.logs.v1.LogsService/Export',
2224
];
25+
private const HEADER_VARS = [
26+
Signals::TRACE => Variables::OTEL_EXPORTER_OTLP_TRACES_HEADERS,
27+
Signals::METRICS => Variables::OTEL_EXPORTER_OTLP_METRICS_HEADERS,
28+
Signals::LOGS => Variables::OTEL_EXPORTER_OTLP_LOGS_HEADERS,
29+
];
2330

2431
public static function method(string $signal): string
2532
{
@@ -30,6 +37,16 @@ public static function method(string $signal): string
3037
return self::METHODS[$signal];
3138
}
3239

40+
public static function getHeaders(string $signal): array
41+
{
42+
$headers = Configuration::has(self::HEADER_VARS[$signal]) ?
43+
Configuration::getMap(self::HEADER_VARS[$signal]) :
44+
Configuration::getMap(Variables::OTEL_EXPORTER_OTLP_HEADERS);
45+
$headers += self::getUserAgentHeader();
46+
47+
return array_map('rawurldecode', $headers);
48+
}
49+
3350
/**
3451
* @link https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#user-agent
3552
*/

SpanExporterFactory.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private function buildTransport(): TransportInterface
4747
$protocol = $this->getProtocol();
4848
$contentType = Protocols::contentType($protocol);
4949
$endpoint = $this->getEndpoint($protocol);
50-
$headers = $this->getHeaders();
50+
$headers = OtlpUtil::getHeaders(Signals::TRACE);
5151
$compression = $this->getCompression();
5252

5353
$factoryClass = Registry::transportFactory($protocol);
@@ -78,15 +78,6 @@ private function getEndpoint(string $protocol): string
7878
return HttpEndpointResolver::create()->resolveToString($endpoint, Signals::TRACE);
7979
}
8080

81-
private function getHeaders(): array
82-
{
83-
$headers = Configuration::has(Variables::OTEL_EXPORTER_OTLP_TRACES_HEADERS) ?
84-
Configuration::getMap(Variables::OTEL_EXPORTER_OTLP_TRACES_HEADERS) :
85-
Configuration::getMap(Variables::OTEL_EXPORTER_OTLP_HEADERS);
86-
87-
return $headers + OtlpUtil::getUserAgentHeader();
88-
}
89-
9081
private function getCompression(): string
9182
{
9283
return Configuration::has(Variables::OTEL_EXPORTER_OTLP_TRACES_COMPRESSION) ?

0 commit comments

Comments
 (0)