18
18
from opentelemetry .context .context import Context
19
19
20
20
TextMapPropagatorT = typing .TypeVar ("TextMapPropagatorT" )
21
- CarrierValT = typing .Union [ typing . List [ str ], str ]
21
+ CarrierValT = typing .TypeVar ( "CarrierValT" )
22
22
23
23
Setter = typing .Callable [[TextMapPropagatorT , str , str ], None ]
24
24
@@ -34,22 +34,21 @@ def get(self, carrier: TextMapPropagatorT, key: str) -> typing.List[str]:
34
34
or more values from the carrier. In the case that
35
35
the value does not exist, returns an empty list.
36
36
37
- Args: carrier: and object which contains values that are used to
38
- construct a Context. This object must be paired with an appropriate
39
- getter which understands how to extract a value from it.
40
- key: key of a field in carrier. Returns: first value of the
41
- propagation key or an empty list if the key doesn't exist.
37
+ Args:
38
+ carrier: An object which contains values that are used to
39
+ construct a Context.
40
+ key: key of a field in carrier.
41
+ Returns: first value of the propagation key or an empty list if the
42
+ key doesn't exist.
42
43
"""
43
44
raise NotImplementedError ()
44
45
45
46
def keys (self , carrier : TextMapPropagatorT ) -> typing .List [str ]:
46
47
"""Function that can retrieve all the keys in a carrier object.
47
48
48
49
Args:
49
- carrier: and object which contains values that are
50
- used to construct a Context. This object
51
- must be paired with an appropriate getter
52
- which understands how to extract a value from it.
50
+ carrier: An object which contains values that are
51
+ used to construct a Context.
53
52
Returns:
54
53
list of keys from the carrier.
55
54
"""
@@ -60,31 +59,17 @@ class DictGetter(Getter[typing.Dict[str, CarrierValT]]):
60
59
def get (
61
60
self , carrier : typing .Dict [str , CarrierValT ], key : str
62
61
) -> typing .List [str ]:
63
- val = carrier .get (key , None )
64
- if not val :
62
+ value = carrier .get (key , None )
63
+ if not value :
65
64
return []
66
- return val if isinstance (val , typing .List ) else [val ]
65
+ if not isinstance (value , str ) and isinstance (value , typing .Iterable ):
66
+ return list (value )
67
+ return [value ]
67
68
68
69
def keys (self , carrier : typing .Dict [str , CarrierValT ]) -> typing .List [str ]:
69
70
return list (carrier .keys ())
70
71
71
72
72
- class CustomGetter (Getter [TextMapPropagatorT ]):
73
- def __init__ (
74
- self ,
75
- get : typing .Callable [[TextMapPropagatorT , str ], typing .List [str ]],
76
- keys : typing .Callable [[TextMapPropagatorT ], typing .List [str ]],
77
- ):
78
- self ._get = get
79
- self ._keys = keys
80
-
81
- def get (self , carrier : TextMapPropagatorT , key : str ) -> typing .List [str ]:
82
- return self ._get (carrier , key )
83
-
84
- def keys (self , carrier : TextMapPropagatorT ) -> typing .List [str ]:
85
- return self ._keys (carrier )
86
-
87
-
88
73
class TextMapPropagator (abc .ABC ):
89
74
"""This class provides an interface that enables extracting and injecting
90
75
context into headers of HTTP requests. HTTP frameworks and clients
0 commit comments