def test_valid_header(self): header = "{}/{};o=1".format( get_hexadecimal_trace_id(self.valid_trace_id), self.valid_span_id) new_span_context = self._extract(header) self.assertEqual(new_span_context.trace_id, self.valid_trace_id) self.assertEqual(new_span_context.span_id, self.valid_span_id) self.assertEqual(new_span_context.trace_flags, TraceFlags(1)) self.assertTrue(new_span_context.is_remote) header = "{}/{};o=10".format( get_hexadecimal_trace_id(self.valid_trace_id), self.valid_span_id) new_span_context = self._extract(header) self.assertEqual(new_span_context.trace_id, self.valid_trace_id) self.assertEqual(new_span_context.span_id, self.valid_span_id) self.assertEqual(new_span_context.trace_flags, TraceFlags(10)) self.assertTrue(new_span_context.is_remote) header = "{}/{};o=0".format( get_hexadecimal_trace_id(self.valid_trace_id), self.valid_span_id) new_span_context = self._extract(header) self.assertEqual(new_span_context.trace_id, self.valid_trace_id) self.assertEqual(new_span_context.span_id, self.valid_span_id) self.assertEqual(new_span_context.trace_flags, TraceFlags(0)) self.assertTrue(new_span_context.is_remote) header = "{}/{};o=0".format( get_hexadecimal_trace_id(self.valid_trace_id), 345) new_span_context = self._extract(header) self.assertEqual(new_span_context.trace_id, self.valid_trace_id) self.assertEqual(new_span_context.span_id, 345) self.assertEqual(new_span_context.trace_flags, TraceFlags(0)) self.assertTrue(new_span_context.is_remote)
def extract( self, get_from_carrier: httptextformat.Getter[ httptextformat.HTTPTextFormatT], carrier: httptextformat.HTTPTextFormatT, context: typing.Optional[Context] = None, ) -> Context: header = get_from_carrier(carrier, _TRACE_CONTEXT_HEADER_NAME) if not header: return trace.set_span_in_context(trace.INVALID_SPAN, context) match = re.fullmatch(_TRACE_CONTEXT_HEADER_RE, header[0]) if match is None: return trace.set_span_in_context(trace.INVALID_SPAN, context) trace_id = match.group("trace_id") span_id = match.group("span_id") trace_options = match.group("trace_flags") if trace_id == "0" * 32 or int(span_id) == 0: return trace.set_span_in_context(trace.INVALID_SPAN, context) span_context = SpanContext( trace_id=int(trace_id, 16), span_id=int(span_id), is_remote=True, trace_flags=TraceFlags(trace_options), ) return trace.set_span_in_context(trace.DefaultSpan(span_context), context)
def extract( self, getter: textmap.Getter[textmap.TextMapPropagatorT], carrier: textmap.TextMapPropagatorT, context: typing.Optional[Context] = None, ) -> Context: header = self._get_header_value(getter, carrier) if not header: return trace.set_span_in_context(trace.INVALID_SPAN, context) match = re.fullmatch(_TRACE_CONTEXT_HEADER_RE, header) if match is None: return trace.set_span_in_context(trace.INVALID_SPAN, context) trace_id = match.group("trace_id") span_id = match.group("span_id") trace_options = match.group("trace_flags") if trace_id == "0" * 32 or int(span_id) == 0: return trace.set_span_in_context(trace.INVALID_SPAN, context) span_context = SpanContext( trace_id=int(trace_id, 16), span_id=int(span_id), is_remote=True, trace_flags=TraceFlags(trace_options), ) return trace.set_span_in_context( trace.DefaultSpan(span_context), context )
def extract( self, carrier: textmap.CarrierT, context: typing.Optional[Context] = None, getter: textmap.Getter = textmap.default_getter, ) -> Context: if context is None: context = Context() header = self._get_header_value(getter, carrier) if not header: return context match = re.fullmatch(_TRACE_CONTEXT_HEADER_RE, header) if match is None: return context trace_id = match.group("trace_id") span_id = match.group("span_id") trace_options = match.group("trace_flags") if trace_id == "0" * 32 or int(span_id) == 0: return context span_context = SpanContext( trace_id=int(trace_id, 16), span_id=int(span_id), is_remote=True, trace_flags=TraceFlags(trace_options), ) return trace.set_span_in_context(trace.NonRecordingSpan(span_context), context)
def test_inject_with_valid_context(self): span_context = SpanContext( trace_id=self.valid_trace_id, span_id=self.valid_span_id, is_remote=True, trace_flags=TraceFlags(1), ) output = self._inject(trace.DefaultSpan(span_context)) self.assertEqual( output, "{}/{};o={}".format( get_hexadecimal_trace_id(self.valid_trace_id), self.valid_span_id, 1, ), )
def test_inject_with_valid_context(self): span_context = SpanContext( trace_id=self.valid_trace_id, span_id=self.valid_span_id, is_remote=True, trace_flags=TraceFlags(1), ) output = self._inject(trace.NonRecordingSpan(span_context)) self.assertEqual( output, "{}/{};o={}".format( format_trace_id(self.valid_trace_id), self.valid_span_id, 1, ), )
def test_mixed_case_header_key(self): header_value = "{}/{};o=1".format( get_hexadecimal_trace_id(self.valid_trace_id), self.valid_span_id) for header_key in ( "X-Cloud-Trace-Context", "X-ClOuD-tRace-ConTeXt", "X-CLOUD-TRACE-CONTEXT", ): header_map = {header_key: [header_value]} new_context = self.propagator.extract(dict_getter, header_map) new_span_context = trace.get_current_span( new_context).get_span_context() self.assertEqual(new_span_context.trace_id, self.valid_trace_id) self.assertEqual(new_span_context.span_id, self.valid_span_id) self.assertEqual(new_span_context.trace_flags, TraceFlags(1)) self.assertTrue(new_span_context.is_remote)