def test_noop_span_methods(tracer): context = TraceContext(trace_id='6f9a20b5092fa5e144fd15cc31141cd4', parent_id=None, span_id='41baf1be2fb9bfc5', sampled=False, debug=False, shared=True) with tracer.new_child(context) as span: span.name('root_span') span.tag('span_type', 'root') span.kind('CLIENT') span.annotate('start:sql', ts=1506970524) span.annotate('end:sql', ts=1506970524) span.remote_endpoint('service_a', ipv4='127.0.0.1', port=8080) with span.new_child() as child_span: pass assert isinstance(span, NoopSpan) assert span.context.parent_id is not None assert not span.context.sampled assert span.is_noop span = tracer.to_span(context) assert isinstance(span, NoopSpan) assert span.tracer is tracer assert isinstance(child_span, NoopSpan)
def test_noop_span_methods(tracer: Tracer) -> Any: context = TraceContext( trace_id="6f9a20b5092fa5e144fd15cc31141cd4", parent_id=None, span_id="41baf1be2fb9bfc5", sampled=False, debug=False, shared=True, ) with tracer.new_child(context) as span: span.name("root_span") span.tag("span_type", "root") span.kind("CLIENT") span.annotate("start:sql", ts=1506970524) span.annotate("end:sql", ts=1506970524) span.remote_endpoint("service_a", ipv4="127.0.0.1", port=8080) with span.new_child() as child_span: pass assert isinstance(span, NoopSpan) assert span.context.parent_id is not None assert not span.context.sampled assert span.is_noop span = tracer.to_span(context) assert isinstance(span, NoopSpan) assert span.tracer is tracer assert isinstance(child_span, NoopSpan)
def context(): context = TraceContext(trace_id='6f9a20b5092fa5e144fd15cc31141cd4', parent_id=None, span_id='41baf1be2fb9bfc5', sampled=True, debug=False, shared=True) return context
def other_trace_context(): context = TraceContext(trace_id='6f9a20b5092fa5e144fd15cc31141cd4', parent_id='05e3ac9a4f6e3b90', span_id='41baf1be2fb9bfc5', sampled=None, debug=False, shared=False) return context
def context() -> TraceContext: context = TraceContext( trace_id="6f9a20b5092fa5e144fd15cc31141cd4", parent_id=None, span_id="41baf1be2fb9bfc5", sampled=True, debug=False, shared=True, ) return context
def test_make_headers(trace_context: TraceContext) -> None: headers = make_headers(trace_context) expected = { "X-B3-Flags": "0", "X-B3-Sampled": "1", "X-B3-SpanId": "41baf1be2fb9bfc5", "X-B3-TraceId": "6f9a20b5092fa5e144fd15cc31141cd4", } headers2 = trace_context.make_headers() assert headers == expected == headers2
def other_trace_context() -> TraceContext: context = TraceContext( trace_id="6f9a20b5092fa5e144fd15cc31141cd4", parent_id="05e3ac9a4f6e3b90", span_id="41baf1be2fb9bfc5", sampled=True, debug=True, shared=False, ) return context
def test_make_single_header(trace_context: TraceContext, other_trace_context: TraceContext) -> None: headers = make_single_header(trace_context) expected = {"b3": "6f9a20b5092fa5e144fd15cc31141cd4-41baf1be2fb9bfc5-1"} headers2 = trace_context.make_single_header() assert headers == expected == headers2 headers = make_single_header(other_trace_context) h = "6f9a20b5092fa5e144fd15cc31141cd4-41baf1be2fb9bfc5-d-05e3ac9a4f6e3b90" headers2 = other_trace_context.make_single_header() expected = {"b3": h} assert headers == expected == headers2 new_context = trace_context._replace(debug=True, sampled=None) headers = make_single_header(new_context) expected = {"b3": "6f9a20b5092fa5e144fd15cc31141cd4-41baf1be2fb9bfc5-d"} assert headers == expected new_context = trace_context._replace(debug=False, sampled=None) headers = make_single_header(new_context) expected = {"b3": "6f9a20b5092fa5e144fd15cc31141cd4-41baf1be2fb9bfc5-0"} assert headers == expected
def test_basic_ctr(): context = TraceContext('string', 'string', 'string', True, True, True) local_endpoint = Endpoint('string', 'string', 'string', 0) remote_endpoint = Endpoint('string', 'string', 'string', 0) record = (Record(context, local_endpoint).start(0).name('string').set_tag( 'additionalProp1', 'string').set_tag('additionalProp2', 'string').set_tag( 'additionalProp3', 'string').kind('CLIENT').annotate( 'string', 0).remote_endpoint(remote_endpoint).finish(0)) dict_record = record.asdict() expected = { 'traceId': 'string', 'name': 'string', 'parentId': 'string', 'id': 'string', 'kind': 'CLIENT', 'timestamp': 0, 'duration': 1, 'debug': True, 'shared': True, 'localEndpoint': { 'serviceName': 'string', 'ipv4': 'string', 'ipv6': 'string', 'port': 0 }, 'remoteEndpoint': { 'serviceName': 'string', 'ipv4': 'string', 'ipv6': 'string', 'port': 0 }, 'annotations': [{ 'timestamp': 0, 'value': 'string' }], 'tags': { 'additionalProp1': 'string', 'additionalProp2': 'string', 'additionalProp3': 'string' } } assert dict_record == expected
def test_basic_ctr() -> None: context = TraceContext("string", "string", "string", True, True, True) local_endpoint = Endpoint("string", "string", "string", 0) remote_endpoint = Endpoint("string", "string", "string", 0) record = (Record(context, local_endpoint).start(0).name("string").set_tag( "additionalProp1", "string").set_tag("additionalProp2", "string").set_tag( "additionalProp3", "string").kind("CLIENT").annotate( "string", 0).remote_endpoint(remote_endpoint).finish(0)) dict_record = record.asdict() expected = { "traceId": "string", "name": "string", "parentId": "string", "id": "string", "kind": "CLIENT", "timestamp": 0, "duration": 1, "debug": True, "shared": True, "localEndpoint": { "serviceName": "string", "ipv4": "string", "ipv6": "string", "port": 0, }, "remoteEndpoint": { "serviceName": "string", "ipv4": "string", "ipv6": "string", "port": 0, }, "annotations": [{ "timestamp": 0, "value": "string" }], "tags": { "additionalProp1": "string", "additionalProp2": "string", "additionalProp3": "string", }, } assert dict_record == expected
def make_context(self, headers: dict) -> dict: has_uber = self.TRACE_ID_HEADER in headers if has_uber: trace_id, span_id, parent_id, debug, sampled = self._parse_uber_headers( headers) tc = TraceContext( trace_id=trace_id, parent_id=parent_id, span_id=span_id, sampled=sampled, debug=debug, shared=False, ) return tc else: # create context from B3 headers - used as a shortcut # for make_headers. It is NOT recommended to mix b3 # and uber-trace-id formats together, as it is untested return make_context(headers)
def make_headers(self, context: TraceContext, response_headers: dict) -> dict: return context.make_headers()