Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def other_trace_context():
context = TraceContext(
trace_id='6f9a20b5092fa5e144fd15cc31141cd4',
parent_id='05e3ac9a4f6e3b90',
span_id='41baf1be2fb9bfc5',
sampled=None,
debug=False,
shared=False)
return context
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',
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:
def _parse_single_header(headers: Headers) -> Optional[TraceContext]:
# Makes TraceContext from zipkin single header format.
# https://github.com/openzipkin/b3-propagation
# b3={TraceId}-{SpanId}-{SamplingState}-{ParentSpanId}
if headers[SINGLE_HEADER] == '0':
return None
payload = headers[SINGLE_HEADER].lower()
parts: List[str] = payload.split(DELIMITER)
if len(parts) < 2:
return None
debug = _parse_debug(parts)
sampled = debug if debug else _parse_sampled(parts)
context = TraceContext(
trace_id=parts[0],
span_id=parts[1],
parent_id=_parse_parent_id(parts),
sampled=sampled,
debug=debug,
shared=False)
return context
span.tag(HTTP_METHOD, request.method.upper())
resource = request.match_info.route.resource
# available only in aiohttp >= 3.3.1
if getattr(resource, 'canonical', None) is not None:
route = request.match_info.route.resource.canonical
span.tag(HTTP_ROUTE, route)
_set_remote_endpoint(span, request)
PY37 = sys.version_info >= (3, 7)
if PY37:
from contextvars import ContextVar
OptTraceVar = ContextVar[Optional[TraceContext]]
zipkin_context: OptTraceVar = ContextVar(
'zipkin_context', default=None)
@contextmanager
def set_context_value(
context_var: OptTraceVar,
value: TraceContext
) -> Generator[OptTraceVar, None, None]:
token = context_var.set(value)
try:
yield context_var
finally:
context_var.reset(token)
def middleware_maker(skip_routes: Optional[AbstractRoute] = None,
context: Optional[TraceContext] = None,
sampled: OptBool = None,
debug: bool = False) -> TraceContext:
span_id = generate_random_64bit_string()
if context is not None:
new_context = context._replace(
span_id=span_id,
parent_id=context.span_id,
shared=False)
return new_context
trace_id = generate_random_128bit_string()
if sampled is None:
sampled = self._sampler.is_sampled(trace_id)
new_context = TraceContext(
trace_id=trace_id,
parent_id=None,
span_id=span_id,
sampled=sampled,
debug=debug,
shared=False)
return new_context