11
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
- #
15
- import typing
16
- import urllib .parse
14
+
15
+ from typing import Optional , Set
16
+ from urllib .parse import quote_plus , unquote
17
17
18
18
from opentelemetry import baggage
19
19
from opentelemetry .context import get_current
20
20
from opentelemetry .context .context import Context
21
- from opentelemetry .propagators import textmap
21
+ from opentelemetry .propagators .textmap import (
22
+ TextMapPropagator ,
23
+ TextMapPropagatorT ,
24
+ )
22
25
23
26
24
- class W3CBaggagePropagator (textmap . TextMapPropagator ):
27
+ class W3CBaggagePropagator (TextMapPropagator ):
25
28
"""Extracts and injects Baggage which is used to annotate telemetry."""
26
29
27
- _MAX_HEADER_LENGTH = 8192
28
- _MAX_PAIR_LENGTH = 4096
29
- _MAX_PAIRS = 180
30
- _BAGGAGE_HEADER_NAME = "baggage"
30
+ _baggage_header_name = "baggage"
31
+ _max_header_length = 9182
32
+ _max_pairs = 180
33
+ _max_pair_length = 4096
31
34
32
35
def extract (
33
- self ,
34
- getter : textmap .Getter [textmap .TextMapPropagatorT ],
35
- carrier : textmap .TextMapPropagatorT ,
36
- context : typing .Optional [Context ] = None ,
36
+ self , carrier : TextMapPropagatorT , context : Optional [Context ] = None ,
37
37
) -> Context :
38
38
"""Extract Baggage from the carrier.
39
39
@@ -44,67 +44,51 @@ def extract(
44
44
if context is None :
45
45
context = get_current ()
46
46
47
- header = _extract_first_element (
48
- getter .get (carrier , self ._BAGGAGE_HEADER_NAME )
49
- )
47
+ value = carrier .get (self ._baggage_header_name )
48
+
49
+ if value is None :
50
+ header = None
51
+ else :
52
+ header = next (iter (value ), None )
50
53
51
- if not header or len (header ) > self ._MAX_HEADER_LENGTH :
54
+ if header is None or len (header ) > self ._max_header_length :
52
55
return context
53
56
54
- baggage_entries = header . split ( "," )
55
- total_baggage_entries = self . _MAX_PAIRS
56
- for entry in baggage_entries :
57
+ total_baggage_entries = self . _max_pairs
58
+
59
+ for entry in header . split ( "," ) :
57
60
if total_baggage_entries <= 0 :
58
61
return context
59
62
total_baggage_entries -= 1
60
- if len (entry ) > self ._MAX_PAIR_LENGTH :
63
+ if len (entry ) > self ._max_pair_length :
61
64
continue
62
- try :
65
+ if "=" in entry :
63
66
name , value = entry .split ("=" , 1 )
64
- except Exception : # pylint: disable=broad-except
65
- continue
66
- context = baggage .set_baggage (
67
- urllib .parse .unquote (name ).strip (),
68
- urllib .parse .unquote (value ).strip (),
69
- context = context ,
70
- )
67
+ context = baggage .set_baggage (
68
+ unquote (name ).strip (),
69
+ unquote (value ).strip (),
70
+ context = context ,
71
+ )
71
72
72
73
return context
73
74
74
75
def inject (
75
- self ,
76
- set_in_carrier : textmap .Setter [textmap .TextMapPropagatorT ],
77
- carrier : textmap .TextMapPropagatorT ,
78
- context : typing .Optional [Context ] = None ,
76
+ self , carrier : TextMapPropagatorT , context : Optional [Context ] = None ,
79
77
) -> None :
80
78
"""Injects Baggage into the carrier.
81
79
82
80
See
83
81
`opentelemetry.propagators.textmap.TextMapPropagator.inject`
84
82
"""
85
83
baggage_entries = baggage .get_all (context = context )
86
- if not baggage_entries :
87
- return
88
84
89
- baggage_string = _format_baggage (baggage_entries )
90
- set_in_carrier (carrier , self ._BAGGAGE_HEADER_NAME , baggage_string )
85
+ if baggage_entries :
86
+ carrier [self ._baggage_header_name ] = "," .join (
87
+ key + "=" + quote_plus (str (value ))
88
+ for key , value in baggage_entries .items ()
89
+ )
91
90
92
91
@property
93
- def fields (self ) -> typing . Set [str ]:
92
+ def fields (self ) -> Set [str ]:
94
93
"""Returns a set with the fields set in `inject`."""
95
- return {self ._BAGGAGE_HEADER_NAME }
96
-
97
-
98
- def _format_baggage (baggage_entries : typing .Mapping [str , object ]) -> str :
99
- return "," .join (
100
- key + "=" + urllib .parse .quote_plus (str (value ))
101
- for key , value in baggage_entries .items ()
102
- )
103
-
104
-
105
- def _extract_first_element (
106
- items : typing .Optional [typing .Iterable [textmap .TextMapPropagatorT ]],
107
- ) -> typing .Optional [textmap .TextMapPropagatorT ]:
108
- if items is None :
109
- return None
110
- return next (iter (items ), None )
94
+ return {self ._baggage_header_name }
0 commit comments