11from typing import Dict
22from typing import FrozenSet
33from typing import Optional
4- from typing import Union
5- from typing import cast
64
75from ..context import Context
8- from ..internal ._tagset import TagsetDecodeError
9- from ..internal ._tagset import TagsetEncodeError
10- from ..internal ._tagset import TagsetMaxSizeError
11- from ..internal ._tagset import decode_tagset_string
12- from ..internal ._tagset import encode_tagset_values
13- from ..internal .compat import ensure_str
146from ..internal .logger import get_logger
157from ._utils import get_wsgi_header
168
2315HTTP_HEADER_PARENT_ID = "x-datadog-parent-id"
2416HTTP_HEADER_SAMPLING_PRIORITY = "x-datadog-sampling-priority"
2517HTTP_HEADER_ORIGIN = "x-datadog-origin"
26- HTTP_HEADER_TAGS = "x-datadog-tags"
2718
2819
2920# Note that due to WSGI spec we have to also check for uppercased and prefixed
3425 [HTTP_HEADER_SAMPLING_PRIORITY , get_wsgi_header (HTTP_HEADER_SAMPLING_PRIORITY ).lower ()]
3526)
3627POSSIBLE_HTTP_HEADER_ORIGIN = frozenset ([HTTP_HEADER_ORIGIN , get_wsgi_header (HTTP_HEADER_ORIGIN ).lower ()])
37- POSSIBLE_HTTP_HEADER_TAGS = frozenset ([HTTP_HEADER_TAGS , get_wsgi_header (HTTP_HEADER_TAGS ).lower ()])
3828
3929
4030class HTTPPropagator (object ):
@@ -74,30 +64,6 @@ def parent_call():
7464 if "_dd.propagation_error" in span_context ._meta :
7565 return
7666
77- # Only propagate tags that start with `_dd.p.`
78- tags_to_encode = {} # type: Dict[str, str]
79- for key , value in span_context ._meta .items ():
80- # DEV: encoding will fail if the key or value are not `str`
81- key = ensure_str (key )
82- if key .startswith ("_dd.p." ):
83- tags_to_encode [key ] = ensure_str (value )
84-
85- if tags_to_encode :
86- encoded_tags = None
87-
88- try :
89- encoded_tags = encode_tagset_values (tags_to_encode )
90- except TagsetMaxSizeError :
91- # We hit the max size allowed, add a tag to the context to indicate this happened
92- span_context ._meta ["_dd.propagation_error" ] = "max_size"
93- log .warning ("failed to encode x-datadog-tags" , exc_info = True )
94- except TagsetEncodeError :
95- # We hit an encoding error, add a tag to the context to indicate this happened
96- span_context ._meta ["_dd.propagation_error" ] = "encoding_error"
97- log .warning ("failed to encode x-datadog-tags" , exc_info = True )
98- if encoded_tags :
99- headers [HTTP_HEADER_TAGS ] = encoded_tags
100-
10167 @staticmethod
10268 def _extract_header_value (possible_header_names , headers , default = None ):
10369 # type: (FrozenSet[str], Dict[str, str], Optional[str]) -> Optional[str]
@@ -155,19 +121,6 @@ def my_controller(url, headers):
155121 POSSIBLE_HTTP_HEADER_ORIGIN ,
156122 normalized_headers ,
157123 )
158- meta = None
159- tags_value = HTTPPropagator ._extract_header_value (
160- POSSIBLE_HTTP_HEADER_TAGS ,
161- normalized_headers ,
162- default = "" ,
163- )
164- if tags_value :
165- # Do not fail if the tags are malformed
166- try :
167- # We get a Dict[str, str], but need it to be Dict[Union[str, bytes], str] (e.g. _MetaDictType)
168- meta = cast (Dict [Union [str , bytes ], str ], decode_tagset_string (tags_value ))
169- except TagsetDecodeError :
170- log .debug ("failed to decode x-datadog-tags: %r" , tags_value , exc_info = True )
171124
172125 # Try to parse values into their expected types
173126 try :
@@ -182,20 +135,15 @@ def my_controller(url, headers):
182135 span_id = int (parent_span_id ) or None , # type: ignore[arg-type]
183136 sampling_priority = sampling_priority , # type: ignore[arg-type]
184137 dd_origin = origin ,
185- meta = meta ,
186138 )
187139 # If headers are invalid and cannot be parsed, return a new context and log the issue.
188140 except (TypeError , ValueError ):
189141 log .debug (
190- (
191- "received invalid x-datadog-* headers, "
192- "trace-id: %r, parent-id: %r, priority: %r, origin: %r, tags: %r"
193- ),
142+ "received invalid x-datadog-* headers, " "trace-id: %r, parent-id: %r, priority: %r, origin: %r" ,
194143 trace_id ,
195144 parent_span_id ,
196145 sampling_priority ,
197146 origin ,
198- tags_value ,
199147 )
200148 return Context ()
201149 except Exception :
0 commit comments