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 Dict , 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 TextMapPropagator
22
22
23
23
24
- class W3CBaggagePropagator (textmap . TextMapPropagator ):
24
+ class W3CBaggagePropagator (TextMapPropagator ):
25
25
"""Extracts and injects Baggage which is used to annotate telemetry."""
26
26
27
- _MAX_HEADER_LENGTH = 8192
28
- _MAX_PAIR_LENGTH = 4096
29
- _MAX_PAIRS = 180
30
- _BAGGAGE_HEADER_NAME = "baggage"
27
+ _baggage_header_name = "baggage"
28
+ _max_header_length = 9182
29
+ _max_pairs = 180
30
+ _max_pair_length = 4096
31
31
32
32
def extract (
33
- self ,
34
- getter : textmap .Getter [textmap .TextMapPropagatorT ],
35
- carrier : textmap .TextMapPropagatorT ,
36
- context : typing .Optional [Context ] = None ,
33
+ self , carrier : Dict [str , str ], context : Optional [Context ] = None ,
37
34
) -> Context :
38
35
"""Extract Baggage from the carrier.
39
36
@@ -44,67 +41,46 @@ def extract(
44
41
if context is None :
45
42
context = get_current ()
46
43
47
- header = _extract_first_element (
48
- getter .get (carrier , self ._BAGGAGE_HEADER_NAME )
49
- )
44
+ header = carrier .get (self ._baggage_header_name )
50
45
51
- if not header or len (header ) > self ._MAX_HEADER_LENGTH :
46
+ if header is None or len (header ) > self ._max_header_length :
52
47
return context
53
48
54
- baggage_entries = header . split ( "," )
55
- total_baggage_entries = self . _MAX_PAIRS
56
- for entry in baggage_entries :
49
+ total_baggage_entries = self . _max_pairs
50
+
51
+ for entry in header . split ( "," ) :
57
52
if total_baggage_entries <= 0 :
58
53
return context
59
54
total_baggage_entries -= 1
60
- if len (entry ) > self ._MAX_PAIR_LENGTH :
55
+ if len (entry ) > self ._max_pair_length :
61
56
continue
62
- try :
57
+ if "=" in entry :
63
58
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
- )
59
+ context = baggage .set_baggage (
60
+ unquote (name ).strip (),
61
+ unquote (value ).strip (),
62
+ context = context ,
63
+ )
71
64
72
65
return context
73
66
74
67
def inject (
75
- self ,
76
- set_in_carrier : textmap .Setter [textmap .TextMapPropagatorT ],
77
- carrier : textmap .TextMapPropagatorT ,
78
- context : typing .Optional [Context ] = None ,
68
+ self , carrier : Dict [str , str ], context : Optional [Context ] = None ,
79
69
) -> None :
80
70
"""Injects Baggage into the carrier.
81
71
82
72
See
83
73
`opentelemetry.propagators.textmap.TextMapPropagator.inject`
84
74
"""
85
75
baggage_entries = baggage .get_all (context = context )
86
- if not baggage_entries :
87
- return
88
76
89
- baggage_string = _format_baggage (baggage_entries )
90
- set_in_carrier (carrier , self ._BAGGAGE_HEADER_NAME , baggage_string )
77
+ if baggage_entries :
78
+ carrier [self ._baggage_header_name ] = "," .join (
79
+ key + "=" + quote_plus (str (value ))
80
+ for key , value in baggage_entries .items ()
81
+ )
91
82
92
83
@property
93
- def fields (self ) -> typing . Set [str ]:
84
+ def fields (self ) -> Set [str ]:
94
85
"""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 )
86
+ return {self ._baggage_header_name }
0 commit comments