Skip to content

Commit

Permalink
Merge pull request #84 from konstantin-stepanov/span_new_child
Browse files Browse the repository at this point in the history
Span new child
  • Loading branch information
jettify authored Jan 26, 2018
2 parents a4e3659 + 9f25205 commit 6723b84
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
18 changes: 18 additions & 0 deletions aiozipkin/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def kind(self: T, span_kind: str) -> T:
def name(self: T, span_name: str) -> T:
pass # pragma: no cover

@abstractmethod
def new_child(self: T, name: OptStr = None, kind: OptStr = None) -> T:
pass # pragma: no cover

def __enter__(self: T) -> T:
self.start()
return self
Expand Down Expand Up @@ -117,6 +121,12 @@ def kind(self, span_kind: str) -> 'NoopSpan':
def name(self, span_name: str) -> 'NoopSpan':
return self

def new_child(self,
name: OptStr = None,
kind: OptStr = None) -> 'NoopSpan':
context = self._tracer._next_context(self.context)
return NoopSpan(self.tracer, context)


class Span(SpanAbc):
def __init__(self, tracer: 'Tracer',
Expand Down Expand Up @@ -177,3 +187,11 @@ def kind(self, span_kind: str) -> 'Span':
def name(self, span_name: str) -> 'Span':
self._record.name(span_name)
return self

def new_child(self, name: OptStr = None, kind: OptStr = None) -> 'Span':
span = self.tracer.new_child(self.context)
if name is not None:
span.name(name)
if kind is not None:
span.kind(kind)
return span # type: ignore
30 changes: 30 additions & 0 deletions tests/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ def test_noop_span_methods(tracer):
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
Expand All @@ -73,6 +76,8 @@ def test_noop_span_methods(tracer):
assert isinstance(span, NoopSpan)
assert span.tracer is tracer

assert isinstance(child_span, NoopSpan)


def test_trace_join_span(tracer, context):

Expand Down Expand Up @@ -100,6 +105,31 @@ def test_trace_new_child(tracer, context):
assert span.context.span_id is not None


def test_span_new_child(tracer, context, fake_transport):

with tracer.new_child(context) as span:
span.name('name')
with span.new_child('child', 'CLIENT') as child_span1:
pass
with span.new_child() as child_span2:
pass

assert span.context.trace_id == child_span1.context.trace_id
assert span.context.span_id == child_span1.context.parent_id
assert span.context.trace_id == child_span2.context.trace_id
assert span.context.span_id == child_span2.context.parent_id

record = fake_transport.records[0]
data = record.asdict()
assert data['name'] == 'child'
assert data['kind'] == 'CLIENT'

record = fake_transport.records[1]
data = record.asdict()
assert data['name'] == 'unknown'
assert 'kind' not in data


def test_error(tracer, fake_transport):
def func():
with tracer.new_trace() as span:
Expand Down

0 comments on commit 6723b84

Please sign in to comment.