diff --git a/src/Http/HttpMessageHelper.php b/src/Http/HttpMessageHelper.php new file mode 100644 index 0000000..92ddf4e --- /dev/null +++ b/src/Http/HttpMessageHelper.php @@ -0,0 +1,28 @@ + + */ + +namespace Instrumentation\Http; + +class HttpMessageHelper +{ + /** + * @param array $headers + */ + public static function formatHeadersForSpanAttribute(array $headers): string + { + $lines = []; + foreach ($headers as $name => $values) { + foreach ($values as $value) { + $lines[] = sprintf('%s: %s', mb_strtolower($name), $value); + } + } + + return implode(\PHP_EOL, $lines); + } +} diff --git a/src/Http/TracedResponse.php b/src/Http/TracedResponse.php index 988768f..8a6a1de 100644 --- a/src/Http/TracedResponse.php +++ b/src/Http/TracedResponse.php @@ -182,13 +182,7 @@ protected function endTracing(): void try { if (\in_array('response.headers', $info['user_data']['span_attributes'] ?? [])) { - $headers = []; - $raw = $this->getHeaders(false); - foreach ($raw as $header => $value) { - $headers[$header] = $this->toReadableHeaderValue($value); - } - - $this->span->setAttribute('response.headers', $headers); + $this->span->setAttribute('response.headers', HttpMessageHelper::formatHeadersForSpanAttribute($this->getHeaders(false))); } if (\in_array('response.body', $info['user_data']['span_attributes'] ?? [])) { diff --git a/src/Http/TracingHttpClient.php b/src/Http/TracingHttpClient.php index 6da6f49..5cda904 100644 --- a/src/Http/TracingHttpClient.php +++ b/src/Http/TracingHttpClient.php @@ -93,6 +93,9 @@ public function request(string $method, string $url, array $options = []): Respo if (\in_array('request.body', $options['user_data']['span_attributes'])) { $attributes['request.body'] = self::getRequestBody($options); } + if (\in_array('request.headers', $options['user_data']['span_attributes'])) { + $attributes['request.headers'] = HttpMessageHelper::formatHeadersForSpanAttribute($headers); + } } catch (\Throwable) { }