Skip to content

Commit f90ab6c

Browse files
committed
Update BaggageSpanProcessor to support multiple predicates for consistency
1 parent 51e9774 commit f90ab6c

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

  • processor/opentelemetry-processor-baggage/src/opentelemetry/processor/baggage

processor/opentelemetry-processor-baggage/src/opentelemetry/processor/baggage/processor.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import Callable, Optional
15+
from typing import Callable, Optional, Sequence, Union
1616

1717
from opentelemetry.baggage import get_all as get_all_baggage
1818
from opentelemetry.context import Context
@@ -21,6 +21,9 @@
2121

2222
# A BaggageKeyPredicate is a function that takes a baggage key and returns a boolean
2323
BaggageKeyPredicateT = Callable[[str], bool]
24+
BaggageKeyPredicates = Union[
25+
BaggageKeyPredicateT, Sequence[BaggageKeyPredicateT]
26+
]
2427

2528
# A BaggageKeyPredicate that always returns True, allowing all baggage keys to be added to spans
2629
ALLOW_ALL_BAGGAGE_KEYS: BaggageKeyPredicateT = lambda _: True # noqa: E731
@@ -50,13 +53,16 @@ class BaggageSpanProcessor(SpanProcessor):
5053
5154
"""
5255

53-
def __init__(self, baggage_key_predicate: BaggageKeyPredicateT) -> None:
54-
self._baggage_key_predicate = baggage_key_predicate
56+
def __init__(self, baggage_key_predicate: BaggageKeyPredicates) -> None:
57+
if callable(baggage_key_predicate):
58+
self._predicates = [baggage_key_predicate]
59+
else:
60+
self._predicates = list(baggage_key_predicate)
5561

5662
def on_start(
5763
self, span: "Span", parent_context: Optional[Context] = None
5864
) -> None:
5965
baggage = get_all_baggage(parent_context)
6066
for key, value in baggage.items():
61-
if self._baggage_key_predicate(key):
67+
if any(predicate(key) for predicate in self._predicates):
6268
span.set_attribute(key, value)

0 commit comments

Comments
 (0)