def test_create_http_headers_new_span_id(gen_mock): tracer = MockTracer() tracer.push_zipkin_attrs( ZipkinAttrs( trace_id="17133d482ba4f605", span_id="37133d482ba4f605", is_sampled=True, parent_span_id="27133d482ba4f605", flags=None, )) gen_mock.return_value = "47133d482ba4f605" # if new_span_id = True we generate a new span id assert request_helpers.create_http_headers(tracer=tracer, new_span_id=True) == { "X-B3-TraceId": "17133d482ba4f605", "X-B3-SpanId": "47133d482ba4f605", "X-B3-ParentSpanId": "37133d482ba4f605", "X-B3-Flags": "0", "X-B3-Sampled": "1", } # by default we keep the same span id as the current span assert request_helpers.create_http_headers(tracer=tracer) == { "X-B3-TraceId": "17133d482ba4f605", "X-B3-SpanId": "37133d482ba4f605", "X-B3-ParentSpanId": "27133d482ba4f605", "X-B3-Flags": "0", "X-B3-Sampled": "1", }
def call_service3(): # Intentional chance to fail if INTENTIONAL_FAILURE_PCT and random.randint( 0, 100) <= INTENTIONAL_FAILURE_PCT: raise Exception(f"Intentional {INTENTIONAL_FAILURE_PCT}% failure") return requests.get(SERVICE3_URL, headers=create_http_headers())
def create_http_headers_for_new_span(context_stack=None, tracer=None): """ Generate the headers for a new zipkin span. .. note:: If the method is not called from within a zipkin_trace context, empty dict will be returned back. :returns: dict containing (X-B3-TraceId, X-B3-SpanId, X-B3-ParentSpanId, X-B3-Flags and X-B3-Sampled) keys OR an empty dict. """ return create_http_headers(context_stack, tracer, True)
def test_create_http_headers_custom_tracer(): tracer = MockTracer() tracer.push_zipkin_attrs( ZipkinAttrs( trace_id="17133d482ba4f605", span_id="37133d482ba4f605", is_sampled=True, parent_span_id="27133d482ba4f605", flags=None, )) expected = { "X-B3-TraceId": "17133d482ba4f605", "X-B3-SpanId": "37133d482ba4f605", "X-B3-ParentSpanId": "27133d482ba4f605", "X-B3-Flags": "0", "X-B3-Sampled": "1", } assert expected == request_helpers.create_http_headers(tracer=tracer)
def test_create_http_headers_context_stack(): mock_context_stack = mock.Mock() mock_context_stack.get.return_value = ZipkinAttrs( trace_id="17133d482ba4f605", span_id="37133d482ba4f605", is_sampled=True, parent_span_id="27133d482ba4f605", flags=None, ) expected = { "X-B3-TraceId": "17133d482ba4f605", "X-B3-SpanId": "37133d482ba4f605", "X-B3-ParentSpanId": "27133d482ba4f605", "X-B3-Flags": "0", "X-B3-Sampled": "1", } assert expected == request_helpers.create_http_headers( context_stack=mock_context_stack, )
def call_api_03(): headers = create_http_headers() requests.get('http://127.0.0.1:5003/', headers=headers) return 'OK'
def call_api_03(): headers = create_http_headers() requests.get('http://api_03:5000/', headers=headers) return 'OK'
def call_service3(): return requests.get(SERVICE3_URL, headers=create_http_headers())
def do_stuff(): headers = create_http_headers() # res = requests.get('http://localhost:6000/service1/s1', headers=headers) res = requests.get('http://localhost:6001/service2/s3', headers=headers) print(res.text) return 'OK'