Skip to content

Commit 5f55304

Browse files
authored
implement getAll function in TextMap Extract (#1570)
* implement getAll function in TextMap Extract * refactor propagation interfaces to introduce ExtendedPropagationGetterInterface * Refactor getAll method in SanitizeCombinedHeadersPropagationGetter to streamline conditional logic for getter instance check
1 parent 0cba875 commit 5f55304

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

Propagation/ArrayAccessGetterSetter.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
/**
1717
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.6.1/specification/context/api-propagators.md#textmap-propagator Getter and Setter.
1818
*
19-
* Default implementation of {@see PropagationGetterInterface} and {@see PropagationSetterInterface}.
19+
* Default implementation of {@see ExtendedPropagationGetterInterface} and {@see PropagationSetterInterface}.
2020
* This type is used if no custom getter/setter is provided to {@see TextMapPropagatorInterface::inject()} or {@see TextMapPropagatorInterface::extract()}.
2121
*/
22-
final class ArrayAccessGetterSetter implements PropagationGetterInterface, PropagationSetterInterface
22+
final class ArrayAccessGetterSetter implements ExtendedPropagationGetterInterface, PropagationSetterInterface
2323
{
2424
private static ?self $instance = null;
2525

@@ -78,6 +78,29 @@ public function get($carrier, string $key): ?string
7878
);
7979
}
8080

81+
/** {@inheritdoc} */
82+
public function getAll($carrier, string $key): array
83+
{
84+
if ($this->isSupportedCarrier($carrier)) {
85+
$value = $carrier[$this->resolveKey($carrier, $key)] ?? null;
86+
if (is_array($value) && $value) {
87+
return array_values(array_filter($value, 'is_string'));
88+
}
89+
90+
return is_string($value)
91+
? [$value]
92+
: [];
93+
}
94+
95+
throw new InvalidArgumentException(
96+
sprintf(
97+
'Unsupported carrier type: %s. Unable to get value associated with key:%s',
98+
get_debug_type($carrier),
99+
$key
100+
)
101+
);
102+
}
103+
81104
/** {@inheritdoc} */
82105
public function set(&$carrier, string $key, string $value): void
83106
{
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Context\Propagation;
6+
7+
/**
8+
* Interface for getting values from a carrier.
9+
* This interface extends the base PropagationGetterInterface to avoid breaking changes.
10+
*/
11+
interface ExtendedPropagationGetterInterface extends PropagationGetterInterface
12+
{
13+
/**
14+
* Gets all values of a given key from a carrier.
15+
*
16+
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.44.0/specification/context/api-propagators.md#getall
17+
*
18+
* @return list<string>
19+
*/
20+
public function getAll($carrier, string $key): array;
21+
}

Propagation/SanitizeCombinedHeadersPropagationGetter.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* handle edge cases where the header has a trailing ';' or an empty trace state.
1313
* We also need to trim trailing separators from the header, found when a header is empty.
1414
*/
15-
final class SanitizeCombinedHeadersPropagationGetter implements PropagationGetterInterface
15+
final class SanitizeCombinedHeadersPropagationGetter implements ExtendedPropagationGetterInterface
1616
{
1717
private const LIST_MEMBERS_SEPARATOR = ',';
1818
private const SERVER_CONCAT_HEADERS_REGEX = '/;(?=[^,=;]*=|$)/';
@@ -40,4 +40,23 @@ public function get($carrier, string $key): ?string
4040
$value,
4141
);
4242
}
43+
44+
public function getAll($carrier, string $key): array
45+
{
46+
$value = $this->getter instanceof ExtendedPropagationGetterInterface
47+
? $this->getter->getAll($carrier, $key)
48+
: (array) $this->getter->get($carrier, $key);
49+
50+
if ($value === []) {
51+
return [];
52+
}
53+
54+
$value = preg_replace(
55+
[self::SERVER_CONCAT_HEADERS_REGEX, self::TRAILING_LEADING_SEPARATOR_REGEX],
56+
[self::LIST_MEMBERS_SEPARATOR],
57+
$value,
58+
);
59+
60+
return array_values($value);
61+
}
4362
}

0 commit comments

Comments
 (0)