def test_setters(self): span = Span.new(self.trace, Span.new_span_id()) self.assertEqual(span.name, '') new_name = '1324' span.name = new_name self.assertEqual(span.name, new_name) new_span_kind = SpanKind.client span.span_kind = new_span_kind self.assertEqual(span.span_kind, new_span_kind) new_span_kind = SpanKind.client.value span.span_kind = new_span_kind self.assertEqual(span.span_kind, SpanKind.client) self.assertIsNone(span.parent_span_id) new_span = Span.new(self.trace, Span.new_span_id()) span.parent_span_id = new_span.span_id self.assertIs(span.parent_span_id, new_span.span_id) self.assertIsNone(span.start_time) self.assertIsNone(span.end_time) start_time = datetime.datetime.utcnow() span.start_time = start_time self.assertEqual(span.start_time, start_time) end_time = datetime.datetime.utcnow() span.end_time = end_time self.assertEqual(span.end_time, end_time)
def test_lshift_span(self): span_id = Span.new_span_id() span_a = Span.new(self.trace, span_id) new_span_id = Span.new_span_id() span_b = Span.new(self.trace, new_span_id) operator.lshift(span_b, span_a) self.assertIs(span_a.parent_span, span_b)
def test_rshift_span(self): span_id = Span.new_span_id() span_a = Span.new(self.trace, span_id) new_span_id = Span.new_span_id() span_b = Span.new(self.trace, new_span_id) operator.rshift(span_a, span_b) self.assertIs(span_a.parent_span_id, span_b.span_id)
def test_add_span(self): span_id = Span.new_span_id() parent_span = Span.new(self.trace, span_id) new_span_id = Span.new_span_id() span = Span.new(self.trace, new_span_id) operator.add(parent_span, span) self.assertIs(span.parent_span_id, span_id)
def test_iadd_span(self): span_id = Span.new_span_id() parent_span = Span.new(self.trace, span_id) new_span_id = Span.new_span_id() span = Span.new(self.trace, new_span_id) result = operator.iadd(parent_span, span) self.assertIs(span.parent_span_id, span_id) self.assertIsInstance(result, Span)
def test_ilshift_span(self): span_id = Span.new_span_id() span_a = Span.new(self.trace, span_id) new_span_id = Span.new_span_id() span_b = Span.new(self.trace, new_span_id) result = operator.ilshift(span_b, span_a) self.assertIs(span_a.parent_span, span_b) self.assertIsInstance(result, Span)
def test_add_span_adds_at_top_level(self): trace = self.sdk.current_trace span_id = Span.new_span_id() span_a = Span.new(trace, span_id) operator.add(trace, span_a) self.assertEqual(len(trace), 1) new_span_id = Span.new_span_id() span_b = Span.new(trace, new_span_id) operator.add(trace, span_b) self.assertEqual(len(trace), 2)
def test_brackets_nested_not_override_default_parent_span(self): current_trace = self.sdk.current_trace current_trace.root_span_id = 'smith' parent_span_id = 1234 parent_span = Span.new(current_trace, span_id=parent_span_id) # @current_trace.decorators.span(name='bob', nested=True, parent_span=parent_span) def func_a(a, c=1, d='two'): self.assertEqual(a, 'y') self.assertEqual(c, 3) self.assertEqual(d, 'four') return 567 func_a = self.decorate_with_brackets(func_a, name='bob', nested=True, parent_span=parent_span) result = func_a('y', c=3, d='four') self.assertEqual(result, 567) traces = self.sdk.dispatcher.traces self.assertEqual(len(traces), 1) spans = traces[0].spans self.assertEqual(len(spans), 1) self.assertEqual(spans[0].name, 'bob') self.assertEqual(spans[0].parent_span_id, parent_span_id)
def test_iadd_span_adds_at_top_level(self): trace = self.sdk.current_trace span_id = Span.new_span_id() span_a = Span.new(trace, span_id) result = operator.iadd(trace, span_a) self.assertEqual(len(trace), 1) self.assertIsInstance(result, Trace) new_span_id = Span.new_span_id() span_b = Span.new(trace, new_span_id) operator.iadd(trace, span_b) self.assertEqual(len(trace), 2) self.assertIsInstance(result, Trace)
def test_isub_not_in_trace_raises(self): trace = self.sdk.current_trace span_id = Span.new_span_id() span = Span.new(trace, span_id) self.assertRaises(ValueError, operator.isub, trace, span)
def test_rshift_trace(self): trace = self.sdk.current_trace other_trace = self.sdk.current_trace span = Span.new(other_trace, Span.new_span_id()) operator.rshift(span, trace) self.assertIn(span, trace.spans) self.assertIn(span.span_id, trace.span_ids)
def test_irshift_trace(self): trace = self.sdk.current_trace other_trace = self.sdk.current_trace span = Span.new(other_trace, Span.new_span_id()) result = operator.irshift(span, trace) self.assertIn(span, trace.spans) self.assertIn(span.span_id, trace.span_ids) self.assertIsInstance(result, Span)
def test_span_parent(self): trace_id = Trace.new_trace_id() trace = Trace.new(self.sdk, trace_id=trace_id) parent_span = Span.new(trace, Span.new_span_id()) span = trace.span(parent_span=parent_span) self.assertIsInstance(span, Span) self.assertEqual(trace.spans, [span]) self.assertIs(span.parent_span_id, parent_span.span_id)
def test_context_manager_raises_DuplicateSpanEntryError(self): span = Span.new(self.trace, Span.new_span_id(), name='bob') with span as s: try: with s: pass except DuplicateSpanEntryError as e: self.assertIs(e.span, span) else: assert False
def test_init(self): span_id = Span.new_span_id() span = Span.new(self.trace, span_id) self.assertIs(span.trace, self.trace) self.assertEqual(span.span_id, span_id) self.assertIsNone(span.parent_span_id) self.assertEqual(span.name, '') self.assertIsNone(span.start_time) self.assertIsNone(span.end_time) self.assertEqual(span.span_kind, SpanKind.unspecified) self.assertEqual(len(span.labels), 0) self.assertEqual(span.project_id, self.project_id) self.assertEqual(span.sdk, self.sdk)
def test_context_manager(self): parent_span_id = Span.new_span_id() span = Span.new(self.trace, parent_span_id, name='parent') self.assertIsNone(span.start_time) self.assertIsNone(span.end_time) with span as s: self.assertIs(span, s) start_time = span.start_time self.assertIsNotNone(start_time) self.assertIsNone(span.end_time) self.assertIsNotNone(span.start_time) self.assertEqual(span.start_time, start_time) self.assertIsNotNone(span.end_time)
def test_export(self): parent_span_id = Span.new_span_id() e_labels = {'a': '1', 'b': '2', 'c': 'None'} span_kind = SpanKind.server start_time = datetime.datetime(2017, 1, 20) end_time = datetime.datetime(2017, 1, 23) e_start_time = datetime_to_timestamp(start_time) e_end_time = datetime_to_timestamp(end_time) span_id = Span.new_span_id() span = Span.new( self.trace, span_id, parent_span_id=parent_span_id, name='child', span_kind=span_kind, start_time=start_time, end_time=end_time, labels=e_labels, ) for data in [span.export(), json.loads(span.json)]: self.assertIsInstance(data, {}.__class__) self.assertSetEqual( set(data.keys()), { 'spanId', 'kind', 'name', 'startTime', 'endTime', 'parentSpanId', 'labels' }, ) self.assertEqual(data['spanId'], str(span_id)) self.assertEqual(data['kind'], span_kind.value) self.assertEqual(data['name'], 'child') self.assertEqual(data['startTime'], e_start_time) self.assertEqual(data['endTime'], e_end_time) self.assertEqual(data['parentSpanId'], str(parent_span_id)) self.assertEqual(data['labels'], e_labels)
def test_iadd_raises_ValueError(self): span_id = Span.new_span_id() span = Span.new(self.trace, span_id) self.assertRaises(TypeError, operator.iadd, span, 1)
def test_irshift_raises(self): span_id = Span.new_span_id() span_a = Span.new(self.trace, span_id) self.assertRaises(TypeError, operator.irshift, span_a, 1)