def test_span_generator(self): hex_encoder = codecs.getencoder('hex') span_datas = [ span_data_module.SpanData( name="name0", context=span_context_module.SpanContext( trace_id='0e0c63257de34c92bf9efcd03927272e'), span_id='0e0c63257de34c92', parent_span_id=None, attributes=None, start_time=None, end_time=None, child_span_count=None, stack_trace=None, time_events=None, links=None, status=None, same_process_as_parent_span=None, span_kind=0), span_data_module.SpanData( name="name1", context=span_context_module.SpanContext( trace_id='1e0c63257de34c92bf9efcd03927272e'), span_id='1e0c63257de34c92', parent_span_id=None, attributes=None, start_time=None, end_time=None, child_span_count=None, stack_trace=None, time_events=None, links=None, status=None, same_process_as_parent_span=None, span_kind=0) ] exporter = TraceExporter(service_name=SERVICE_NAME, transport=MockTransport) export_requests = list(exporter.generate_span_requests(span_datas)) self.assertEqual(len(export_requests), 1) self.assertEqual(export_requests[0].node, exporter.node) self.assertEqual(len(export_requests[0].spans), 2) self.assertEqual(export_requests[0].spans[0].name.value, 'name0') self.assertEqual(export_requests[0].spans[1].name.value, 'name1') self.assertEqual( hex_encoder(export_requests[0].spans[0].trace_id)[0], b'0e0c63257de34c92bf9efcd03927272e') self.assertEqual( hex_encoder(export_requests[0].spans[0].span_id)[0], b'0e0c63257de34c92') self.assertEqual( hex_encoder(export_requests[0].spans[1].trace_id)[0], b'1e0c63257de34c92bf9efcd03927272e') self.assertEqual( hex_encoder(export_requests[0].spans[1].span_id)[0], b'1e0c63257de34c92')
def test_translate_message_event(self): ts = datetime.utcnow() + timedelta(seconds=-10) message_event = time_event_module.MessageEvent( timestamp=ts, id=0, type=time_event_module.Type.SENT, uncompressed_size_bytes=10, compressed_size_bytes=1) span_data = span_data_module.SpanData( context=span_context_module.SpanContext( trace_id='6e0c63257de34c92bf9efcd03927272e'), span_id='6e0c63257de34c92', annotations=None, message_events=[message_event], span_kind=span_module.SpanKind.SERVER, status=None, start_time=None, end_time=None, child_span_count=None, name=None, parent_span_id=None, attributes=None, same_process_as_parent_span=False, stack_trace=None, links=None) pb_span = utils.translate_to_trace_proto(span_data) self.assertEqual(len(pb_span.time_events.time_event), 1)
def test_emit(self): exporter = logging_exporter.LoggingExporter() logger = mock.Mock() exporter.logger = logger span_datas = [ span_data_module.SpanData( name='span', context=span_context.SpanContext(trace_id='1'), span_id='1111', parent_span_id=None, attributes=None, start_time=None, end_time=None, child_span_count=None, stack_trace=None, time_events=None, links=None, status=None, same_process_as_parent_span=None, span_kind=0, ) ] exporter.emit(span_datas) logger.info.assert_called_once_with( span_data_module.format_legacy_trace_json(span_datas))
def test_format_legacy_trace_json(self): trace_id = '2dd43a1d6b2549c6bc2a1a54c2fc0b05' span_data = span_data_module.SpanData( name='root', context=span_context.SpanContext(trace_id=trace_id, span_id='6e0c63257de34c92'), span_id='6e0c63257de34c92', parent_span_id='6e0c63257de34c93', attributes={'key1': 'value1'}, start_time=utils.to_iso_str(), end_time=utils.to_iso_str(), stack_trace=stack_trace.StackTrace(stack_trace_hash_id='111'), links=[link.Link('1111', span_id='6e0c63257de34c92')], status=status.Status(code=0, message='pok'), annotations=[ time_event.Annotation(timestamp=datetime.datetime(1970, 1, 1), description='description') ], message_events=[ time_event.MessageEvent( timestamp=datetime.datetime(1970, 1, 1), id=0, ) ], same_process_as_parent_span=False, child_span_count=0, span_kind=0, ) trace_json = span_data_module.format_legacy_trace_json([span_data]) self.assertEqual(trace_json.get('traceId'), trace_id) self.assertEqual(len(trace_json.get('spans')), 1)
def test_basic_span_translation(self): hex_encoder = codecs.getencoder('hex') span_data = span_data_module.SpanData( name="name", context=span_context_module.SpanContext( trace_id='6e0c63257de34c92bf9efcd03927272e'), span_id='6e0c63257de34c92', parent_span_id='6e0c63257de34c93', attributes={ 'test_str_key': 'test_str_value', 'test_int_key': 1, 'test_bool_key': False }, start_time='2017-08-15T18:02:26.071158Z', end_time='2017-08-15T18:02:36.071158Z', child_span_count=None, stack_trace=None, time_events=None, links=None, status=None, same_process_as_parent_span=None, span_kind=0) pb_span = utils.translate_to_trace_proto(span_data) self.assertEqual(pb_span.name.value, "name") self.assertEqual( hex_encoder(pb_span.trace_id)[0], b'6e0c63257de34c92bf9efcd03927272e') self.assertEqual(hex_encoder(pb_span.span_id)[0], b'6e0c63257de34c92') self.assertEqual( hex_encoder(pb_span.parent_span_id)[0], b'6e0c63257de34c93') self.assertEqual(pb_span.kind, 0) self.assertEqual(len(pb_span.attributes.attribute_map), 3) self.assertEqual( pb_span.attributes.attribute_map['test_str_key'], trace_pb2.AttributeValue(string_value=trace_pb2.TruncatableString( value='test_str_value'))) self.assertEqual(pb_span.attributes.attribute_map['test_int_key'], trace_pb2.AttributeValue(int_value=1)) self.assertEqual(pb_span.attributes.attribute_map['test_bool_key'], trace_pb2.AttributeValue(bool_value=False)) self.assertEqual(pb_span.start_time.ToJsonString(), '2017-08-15T18:02:26.071158Z') self.assertEqual(pb_span.end_time.ToJsonString(), '2017-08-15T18:02:36.071158Z') self.assertEqual(pb_span.child_span_count.value, 0) self.assertEqual(pb_span.same_process_as_parent_span.value, False) self.assertEqual(len(pb_span.time_events.time_event), 0) self.assertEqual(pb_span.status.code, 0) self.assertFalse(pb_span.status.message) self.assertEqual(len(pb_span.links.link), 0) self.assertEqual(len(pb_span.tracestate.entries), 0)
def test_emit(self, mr_mock): trace_id = '6e0c63257de34c92bf9efcd03927272e' span_datas = [ span_data_module.SpanData( name='span', context=span_context.SpanContext(trace_id=trace_id), span_id=None, parent_span_id=None, attributes=None, start_time=None, end_time=None, child_span_count=None, stack_trace=None, annotations=None, message_events=None, links=None, status=None, same_process_as_parent_span=None, span_kind=0, ) ] mock_dd_transport = mock.Mock() exporter = DatadogTraceExporter( options=Options(service="dd-unit-test"), transport=MockTransport) exporter._dd_transport = mock_dd_transport exporter.emit(span_datas) # mock_dd_transport.send_traces.assert_called_with(datadog_spans) self.assertTrue(mock_dd_transport.send_traces.called)
def test_translate_status(self): span_data = span_data_module.SpanData( context=span_context_module.SpanContext( trace_id='6e0c63257de34c92bf9efcd03927272e'), span_id='6e0c63257de34c92', status=status_module.Status(code=2, message='ERR', details='details'), span_kind=span_module.SpanKind.SERVER, start_time=None, end_time=None, child_span_count=None, name=None, parent_span_id=None, attributes=None, same_process_as_parent_span=False, stack_trace=None, annotations=None, message_events=None, links=None) pb_span = utils.translate_to_trace_proto(span_data) self.assertEqual(pb_span.status.code, 2) self.assertEqual(pb_span.status.message, 'ERR')
def test_constructor_default(self, mock_get_webapp2_header): from opencensus.trace import span_context from opencensus.trace.reporters import print_reporter from opencensus.trace.samplers import always_on from opencensus.trace.propagation import google_cloud_format from opencensus.trace.tracer import webapp2_tracer trace_id = '6e0c63257de34c92bf9efcd03927272e' test_span_context = span_context.SpanContext(trace_id=trace_id) patch = mock.patch.object( google_cloud_format.GoogleCloudFormatPropagator, 'from_header', return_value=test_span_context) mock_get_webapp2_header.return_value = 'test_webapp2_header' with patch: tracer = webapp2_tracer.WebApp2Tracer() assert isinstance(tracer.reporter, print_reporter.PrintReporter) assert isinstance(tracer.sampler, always_on.AlwaysOnSampler) assert isinstance(tracer.propagator, google_cloud_format.GoogleCloudFormatPropagator) assert isinstance(tracer.span_context, span_context.SpanContext) self.assertEqual(tracer.span_context, test_span_context)
def test_translate_tracestate(self): tracestate = tracestate_module.Tracestate() tracestate.append("k1", "v1") tracestate.append("k2", "v2") tracestate.append("k3", "v3") client_span_data = span_data_module.SpanData( context=span_context_module.SpanContext( trace_id='6e0c63257de34c92bf9efcd03927272e', tracestate=tracestate), span_id='6e0c63257de34c92', start_time='2017-08-15T18:02:26.071158Z', end_time='2017-08-15T18:02:36.071158Z', span_kind=span_module.SpanKind.CLIENT, same_process_as_parent_span=True, name=None, parent_span_id=None, attributes=None, child_span_count=None, stack_trace=None, time_events=None, links=None, status=None) pb_span = utils.translate_to_trace_proto(client_span_data) self.assertEqual(len(pb_span.tracestate.entries), 3) self.assertEqual(pb_span.tracestate.entries[0].key, "k1") self.assertEqual(pb_span.tracestate.entries[0].value, "v1") self.assertEqual(pb_span.tracestate.entries[1].key, "k2") self.assertEqual(pb_span.tracestate.entries[1].value, "v2") self.assertEqual(pb_span.tracestate.entries[2].key, "k3") self.assertEqual(pb_span.tracestate.entries[2].value, "v3")
def test_translate_annotation(self): ts = datetime.utcnow() + timedelta(seconds=-10) annotation = time_event_module.Annotation( timestamp=ts, description='description') span_data = span_data_module.SpanData( context=span_context_module.SpanContext( trace_id='6e0c63257de34c92bf9efcd03927272e'), span_id='6e0c63257de34c92', annotations=[annotation], message_events=None, span_kind=span_module.SpanKind.SERVER, status=None, start_time=None, end_time=None, child_span_count=None, name=None, parent_span_id=None, attributes=None, same_process_as_parent_span=False, stack_trace=None, links=None) pb_span = utils.translate_to_trace_proto(span_data) self.assertEqual(len(pb_span.time_events.time_event), 1)
def from_header(self, binary): """Generate a SpanContext object using the trace context header. The value of enabled parsed from header is int. Need to convert to bool. :type binary: bytes :param binary: Trace context header which was extracted from the request headers. :rtype: :class:`~opencensus.trace.span_context.SpanContext` :returns: SpanContext generated from the trace context header. """ # If no binary provided, generate a new SpanContext if binary is None: return span_context_module.SpanContext(from_header=False) # If cannot parse, return a new SpanContext and ignore the context # from binary. try: data = Header._make(struct.unpack(BINARY_FORMAT, binary)) except struct.error: logging.warning( 'Cannot parse the incoming binary data {}, ' 'wrong format. Total bytes length should be {}.'.format( binary, FORMAT_LENGTH ) ) return span_context_module.SpanContext(from_header=False) # data.trace_id is in bytes with length 16, hexlify it to hex bytes # with length 32, then decode it to hex string using utf-8. trace_id = str(binascii.hexlify(data.trace_id).decode(UTF8)) span_id = str(binascii.hexlify(data.span_id).decode(UTF8)) trace_options = TraceOptions(data.trace_option) span_context = span_context_module.SpanContext( trace_id=trace_id, span_id=span_id, trace_options=trace_options, from_header=True) return span_context
def test_constructor_explicit(self): from opencensus.trace import span_context from opencensus.trace import trace span_context = span_context.SpanContext() tracer = context_tracer.ContextTracer(span_context=span_context) self.assertIs(tracer.span_context, span_context) self.assertEqual(tracer.trace_id, span_context.trace_id) self.assertEqual(tracer._span_stack, []) self.assertEqual(tracer.root_span_id, span_context.span_id) assert isinstance(tracer.cur_trace, trace.Trace)
def test_constructor_explicit(self): from opencensus.trace import span_context span_context = span_context.SpanContext() exporter = mock.Mock() tracer = context_tracer.ContextTracer(exporter=exporter, span_context=span_context) self.assertIs(tracer.span_context, span_context) self.assertIs(tracer.exporter, exporter) self.assertEqual(tracer.trace_id, span_context.trace_id) self.assertEqual(tracer._spans_list, []) self.assertEqual(tracer.root_span_id, span_context.span_id)
def test_to_header(self): from opencensus.trace import span_context trace_id = '6e0c63257de34c92bf9efcd03927272e' span_id = 1234 span_context = span_context.SpanContext(trace_id=trace_id, span_id=span_id, enabled=True) propagator = google_cloud_format.GoogleCloudFormatPropagator() header = propagator.to_header(span_context) expected_header = '{}/{};o={}'.format(trace_id, span_id, 1) self.assertEqual(header, expected_header)
def test_span(self): from opencensus.trace import span_context span_id = 1234 span_name = 'test_span' span_context = span_context.SpanContext(span_id=span_id) tracer = context_tracer.ContextTracer(span_context=span_context) span = tracer.span(name=span_name) self.assertEqual(span.parent_span_id, span_id) self.assertEqual(span.name, span_name) self.assertEqual(len(tracer.cur_trace.spans), 1) self.assertEqual(len(tracer._span_stack), 1) self.assertEqual(span_context.span_id, span.span_id)
def test_to_header(self): from opencensus.trace import span_context from opencensus.trace import trace_options trace_id = '6e0c63257de34c92bf9efcd03927272e' span_id = '00f067aa0ba902b7' span_context = span_context.SpanContext( trace_id=trace_id, span_id=span_id, trace_options=trace_options.TraceOptions('1')) propagator = google_cloud_format.GoogleCloudFormatPropagator() header = propagator.to_header(span_context) expected_header = '{}/{};o={}'.format(trace_id, span_id, 1) self.assertEqual(header, expected_header)
def test_span(self, current_span_mock): from opencensus.trace import span_context span_id = '6e0c63257de34c92' span_name = 'test_span' mock_span = mock.Mock() mock_span.span_id = span_id span_context = span_context.SpanContext(span_id=span_id) tracer = context_tracer.ContextTracer(span_context=span_context) current_span_mock.return_value = mock_span span = tracer.span(name=span_name) self.assertEqual(span.parent_span.span_id, span_id) self.assertEqual(span.name, span_name) self.assertEqual(len(tracer._spans_list), 1) self.assertEqual(span_context.span_id, span.span_id)
def test_to_header(self): from opencensus.trace import span_context from opencensus.trace import trace_options trace_id = '6e0c63257de34c92bf9efcd03927272e' span_id_hex = '00f067aa0ba902b7' span_context = span_context.SpanContext( trace_id=trace_id, span_id=span_id_hex, trace_options=trace_options.TraceOptions('1')) propagator = trace_context_http_header_format.\ TraceContextPropagator() header = propagator.to_header(span_context) expected_header = '00-{}-{}-01'.format(trace_id, span_id_hex) self.assertEqual(header, expected_header)
def test_to_headers(self): from opencensus.trace import span_context from opencensus.trace import trace_options trace_id = '6e0c63257de34c92bf9efcd03927272e' span_id = '00f067aa0ba902b7' span_context = span_context.SpanContext( trace_id=trace_id, span_id=span_id, trace_options=trace_options.TraceOptions('1')) propagator = jaeger_format.JaegerFormatPropagator() headers = propagator.to_headers(span_context) expected_headers = { 'Uber-Trace-Id': '{}:{}::{:02x}'.format(trace_id, span_id, int(1)), } self.assertEqual(headers, expected_headers)
def test_to_headers_without_tracestate(self): from opencensus.trace import span_context from opencensus.trace import trace_options trace_id = '6e0c63257de34c92bf9efcd03927272e' span_id_hex = '00f067aa0ba902b7' span_context = span_context.SpanContext( trace_id=trace_id, span_id=span_id_hex, trace_options=trace_options.TraceOptions('1')) propagator = trace_context_http_header_format.\ TraceContextPropagator() headers = propagator.to_headers(span_context) self.assertTrue('traceparent' in headers) self.assertEqual(headers['traceparent'], '00-{}-{}-01'.format(trace_id, span_id_hex)) self.assertFalse('tracestate' in headers)
def test_to_headers_without_tracestate(self): from opencensus.trace import span_context from opencensus.trace import trace_options propagator = trace_context_http_header_format.\ TraceContextPropagator() trace_id = '12345678901234567890123456789012' span_id_hex = '1234567890123456' span_context = span_context.SpanContext( trace_id=trace_id, span_id=span_id_hex, trace_options=trace_options.TraceOptions('1')) headers = propagator.to_headers(span_context) self.assertTrue('traceparent' in headers) self.assertEqual(headers['traceparent'], '00-{}-{}-01'.format(trace_id, span_id_hex)) self.assertFalse('tracestate' in headers)
def test_constructor_explicit(self): from opencensus.trace import span_context from opencensus.trace.reporters import print_reporter from opencensus.trace.samplers import always_on from opencensus.trace.propagation import google_cloud_format from opencensus.trace.tracer import webapp2_tracer trace_id = '6e0c63257de34c92bf9efcd03927272e' span_context = span_context.SpanContext(trace_id=trace_id) sampler = always_on.AlwaysOnSampler() reporter = print_reporter.PrintReporter() propagator = google_cloud_format.GoogleCloudFormatPropagator() tracer = webapp2_tracer.WebApp2Tracer(span_context=span_context, sampler=sampler, reporter=reporter, propagator=propagator) self.assertEqual(tracer.span_context, span_context) self.assertEqual(tracer.sampler, sampler) self.assertEqual(tracer.reporter, reporter) self.assertEqual(tracer.propagator, propagator)
def test_compositeSampler_picksCorrect( self, _name: str, test_input: str, expected: int, mock_request: Mock ) -> None: # Arrange mock_1 = Mock() mock_1.should_sample.return_value = 1 mock_2 = Mock() mock_2.should_sample.return_value = 2 mock_3 = Mock() mock_3.should_sample.return_value = 3 composite = trace.CompositeSampler( { "/a/": mock_1, "/b": mock_2, }, mock_3, ) mock_request.path = test_input # Act / Assert self.assertEqual(composite.should_sample(span_context.SpanContext()), expected)
def test_translate_client_span_kind(self): client_span_data = span_data_module.SpanData( context=span_context_module.SpanContext( trace_id='6e0c63257de34c92bf9efcd03927272e'), span_id='6e0c63257de34c92', start_time='2017-08-15T18:02:26.071158Z', end_time='2017-08-15T18:02:36.071158Z', span_kind=span_module.SpanKind.CLIENT, same_process_as_parent_span=True, name=None, parent_span_id=None, attributes=None, child_span_count=None, stack_trace=None, time_events=None, links=None, status=None) pb_span = utils.translate_to_trace_proto(client_span_data) self.assertEqual(pb_span.kind, 2) self.assertEqual(pb_span.same_process_as_parent_span.value, True)
def test_translate_server_span_kindt(self): server_span_data = span_data_module.SpanData( context=span_context_module.SpanContext( trace_id='6e0c63257de34c92bf9efcd03927272e'), span_id='6e0c63257de34c92', span_kind=span_module.SpanKind.SERVER, child_span_count=1, start_time=None, end_time=None, name=None, parent_span_id=None, attributes=None, same_process_as_parent_span=False, stack_trace=None, time_events=None, links=None, status=None) pb_span = utils.translate_to_trace_proto(server_span_data) self.assertEqual(pb_span.kind, 1) self.assertEqual(pb_span.child_span_count.value, 1)
def test_span_emit_exception(self): client = mock.Mock() span_data = span_data_module.SpanData( name="name0", context=span_context_module.SpanContext( trace_id='0e0c63257de34c92bf9efcd03927272e'), span_id='0e0c63257de34c92', parent_span_id=None, start_time=None, end_time=None, attributes=None, child_span_count=None, stack_trace=None, time_events=None, links=None, status=None, same_process_as_parent_span=None, span_kind=0) exporter = TraceExporter( service_name=SERVICE_NAME, client=client, transport=MockTransport) client.Export.side_effect = grpc.RpcError() # does not throw: exporter.emit([span_data]) client.Export.return_value = iter([1]) exporter.emit([span_data]) self.assertEqual(len(client.Export.mock_calls), 2) actual_request = list(client.Export.call_args[0][0])[0] self.assertEqual(actual_request.node, exporter.node)
def test_emit(self, mr_mock): trace_id = '6e0c63257de34c92bf9efcd03927272e' span_datas = [ span_data_module.SpanData( name='span', context=span_context.SpanContext(trace_id=trace_id), span_id='1111', parent_span_id=None, attributes=None, start_time=None, end_time=None, child_span_count=None, stack_trace=None, time_events=None, links=None, status=None, same_process_as_parent_span=None, span_kind=0, ) ] stackdriver_spans = { 'spans': [{ 'status': None, 'childSpanCount': None, 'links': None, 'startTime': None, 'spanId': '1111', 'attributes': { 'attributeMap': { 'g.co/agent': { 'string_value': { 'truncated_byte_count': 0, 'value': 'opencensus-python [{}]'.format(__version__) } } } }, 'stackTrace': None, 'displayName': { 'truncated_byte_count': 0, 'value': 'span' }, 'name': 'projects/PROJECT/traces/{}/spans/1111'.format(trace_id), 'timeEvents': None, 'endTime': None, 'sameProcessAsParentSpan': None }] } client = mock.Mock() project_id = 'PROJECT' client.project = project_id exporter = trace_exporter.StackdriverExporter( client=client, project_id=project_id) exporter.emit(span_datas) name = 'projects/{}'.format(project_id) client.batch_write_spans.assert_called_with(name, stackdriver_spans) self.assertTrue(client.batch_write_spans.called)
def test_translate_to_zipkin_span_kind_none(self): trace_id = '6e0c63257de34c92bf9efcd03927272e' spans_ipv4 = [ span_data_module.SpanData( name='child_span', context=span_context.SpanContext(trace_id=trace_id), span_id='6e0c63257de34c92', parent_span_id='6e0c63257de34c93', attributes={'test_key': 'test_value'}, start_time='2017-08-15T18:02:26.071158Z', end_time='2017-08-15T18:02:36.071158Z', child_span_count=None, stack_trace=None, time_events=None, links=None, status=None, same_process_as_parent_span=None, span_kind=0, ), span_data_module.SpanData( name='child_span', context=span_context.SpanContext(trace_id=trace_id), span_id='6e0c63257de34c92', parent_span_id='6e0c63257de34c93', attributes={'test_key': 1}, start_time='2017-08-15T18:02:26.071158Z', end_time='2017-08-15T18:02:36.071158Z', child_span_count=None, stack_trace=None, time_events=None, links=None, status=None, same_process_as_parent_span=None, span_kind=None, ), ] trace_id = '6e0c63257de34c92bf9efcd03927272e' spans_ipv6 = [ span_data_module.SpanData( name='child_span', context=span_context.SpanContext(trace_id=trace_id), span_id='6e0c63257de34c92', parent_span_id=None, attributes={ 'test_key': False, 'test_key2': 'raw_value', # these tags are malformed and should be omitted 'test_key3': 0.1, }, start_time='2017-08-15T18:02:26.071158Z', end_time='2017-08-15T18:02:36.071158Z', child_span_count=None, stack_trace=None, time_events=None, links=None, status=None, same_process_as_parent_span=None, span_kind=1, ), ] ipv4 = '127.0.0.1' ipv6 = '2001:0db8:85a3:0000:0000:8a2e:0370:7334' local_endpoint_ipv4 = { 'serviceName': 'my_service', 'ipv4': ipv4, 'port': 9411, } local_endpoint_ipv6 = { 'serviceName': 'my_service', 'ipv6': ipv6, 'port': 9411, } expected_zipkin_spans_ipv4 = [ { 'traceId': '6e0c63257de34c92bf9efcd03927272e', 'id': '6e0c63257de34c92', 'parentId': '6e0c63257de34c93', 'name': 'child_span', 'timestamp': 1502820146071158, 'duration': 10000000, 'localEndpoint': local_endpoint_ipv4, 'tags': { 'test_key': 'test_value' }, }, { 'traceId': '6e0c63257de34c92bf9efcd03927272e', 'id': '6e0c63257de34c92', 'parentId': '6e0c63257de34c93', 'name': 'child_span', 'timestamp': 1502820146071158, 'duration': 10000000, 'localEndpoint': local_endpoint_ipv4, 'tags': { 'test_key': '1' }, }, ] expected_zipkin_spans_ipv6 = [ { 'traceId': '6e0c63257de34c92bf9efcd03927272e', 'id': '6e0c63257de34c92', 'name': 'child_span', 'timestamp': 1502820146071158, 'duration': 10000000, 'localEndpoint': local_endpoint_ipv6, 'tags': { 'test_key': 'False', 'test_key2': 'raw_value' }, 'kind': 'SERVER', }, ] # Test ipv4 local endpoint exporter_ipv4 = zipkin_exporter.ZipkinExporter( service_name='my_service', ipv4=ipv4) ipv4_trace = span_data_module.format_legacy_trace_json(spans_ipv4) zipkin_spans_ipv4 = exporter_ipv4.translate_to_zipkin( trace_id=trace_id, spans=ipv4_trace.get('spans')) self.assertEqual(zipkin_spans_ipv4, expected_zipkin_spans_ipv4) # Test ipv6 local endpoint exporter_ipv6 = zipkin_exporter.ZipkinExporter( service_name='my_service', ipv6=ipv6) ipv6_trace = span_data_module.format_legacy_trace_json(spans_ipv6) zipkin_spans_ipv6 = exporter_ipv6.translate_to_zipkin( trace_id=trace_id, spans=ipv6_trace.get('spans')) self.assertEqual(zipkin_spans_ipv6, expected_zipkin_spans_ipv6)
def test_translate_to_jaeger(self): self.maxDiff = None trace_id_high = '6e0c63257de34c92' trace_id_low = 'bf9efcd03927272e' trace_id = trace_id_high + trace_id_low span_id = '6e0c63257de34c92' parent_span_id = '1111111111111111' span_attributes = { 'key_bool': False, 'key_string': 'hello_world', 'key_int': 3 } annotation_attributes = { 'annotation_bool': True, 'annotation_string': 'annotation_test', 'key_float': .3 } link_attributes = {'key_bool': True} import datetime s = '2017-08-15T18:02:26.071158' time = datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%f') time_events = [ time_event.TimeEvent( timestamp=time, annotation=time_event.Annotation( description='First Annotation', attributes=attributes.Attributes(annotation_attributes))), time_event.TimeEvent(timestamp=time, message_event=time_event.MessageEvent( id='message-event-id', uncompressed_size_bytes=0, )), ] time_events2 = [ time_event.TimeEvent(timestamp=time, annotation=time_event.Annotation( description='First Annotation', attributes=None)), time_event.TimeEvent(timestamp=time, message_event=time_event.MessageEvent( id='message-event-id', uncompressed_size_bytes=0, )), ] links = [ link.Link(trace_id=trace_id, span_id=span_id, type=link.Type.CHILD_LINKED_SPAN, attributes=link_attributes), link.Link(trace_id=trace_id, span_id=span_id, type=link.Type.PARENT_LINKED_SPAN, attributes=link_attributes), link.Link(trace_id=trace_id, span_id=span_id, type=link.Type.TYPE_UNSPECIFIED, attributes=link_attributes) ] span_status = status.Status(code=200, message='success') start_time = '2017-08-15T18:02:26.071158Z' end_time = '2017-08-15T18:02:36.071158Z' span_datas = [ span_data.SpanData( name='test1', context=span_context.SpanContext(trace_id=trace_id), span_id=span_id, parent_span_id=parent_span_id, attributes=span_attributes, start_time=start_time, end_time=end_time, child_span_count=0, stack_trace=None, time_events=time_events, links=links, status=span_status, same_process_as_parent_span=None, span_kind=0, ), span_data.SpanData( name='test2', context=None, span_id=span_id, parent_span_id=None, attributes=None, start_time=start_time, end_time=end_time, child_span_count=None, stack_trace=None, time_events=time_events2, links=None, status=None, same_process_as_parent_span=None, span_kind=None, ), span_data.SpanData( name='test3', context=None, span_id=span_id, parent_span_id=None, attributes=None, start_time=start_time, end_time=end_time, child_span_count=None, stack_trace=None, time_events=None, links=None, status=None, same_process_as_parent_span=None, span_kind=None, ) ] exporter = jaeger_exporter.JaegerExporter() spans = exporter.translate_to_jaeger(span_datas) expected_spans = [ jaeger.Span( traceIdHigh=7929822056569588882, traceIdLow=-4638992594902767826, spanId=7929822056569588882, parentSpanId=1229782938247303441, operationName='test1', startTime=1502820146071158, duration=10000000, flags=1, tags=[ jaeger.Tag(key='key_bool', vType=jaeger.TagType.BOOL, vBool=False), jaeger.Tag(key='key_string', vType=jaeger.TagType.STRING, vStr='hello_world'), jaeger.Tag(key='key_int', vType=jaeger.TagType.LONG, vLong=3), jaeger.Tag(key='status.code', vType=jaeger.TagType.LONG, vLong=200), jaeger.Tag(key='status.message', vType=jaeger.TagType.STRING, vStr='success') ], references=[ jaeger.SpanRef(refType=jaeger.SpanRefType.CHILD_OF, traceIdHigh=7929822056569588882, traceIdLow=-4638992594902767826, spanId=7929822056569588882), jaeger.SpanRef(refType=jaeger.SpanRefType.FOLLOWS_FROM, traceIdHigh=7929822056569588882, traceIdLow=-4638992594902767826, spanId=7929822056569588882), jaeger.SpanRef(refType=None, traceIdHigh=7929822056569588882, traceIdLow=-4638992594902767826, spanId=7929822056569588882) ], logs=[ jaeger.Log(timestamp=1502820146071158, fields=[ jaeger.Tag(key='annotation_bool', vType=jaeger.TagType.BOOL, vBool=True), jaeger.Tag(key='annotation_string', vType=jaeger.TagType.STRING, vStr='annotation_test'), jaeger.Tag(key='message', vType=jaeger.TagType.STRING, vStr='First Annotation') ]) ]), jaeger.Span(operationName="test2", traceIdHigh=7929822056569588882, traceIdLow=-4638992594902767826, spanId=7929822056569588882, parentSpanId=0, startTime=1502820146071158, duration=10000000, logs=[ jaeger.Log(timestamp=1502820146071158, fields=[ jaeger.Tag( key='message', vType=jaeger.TagType.STRING, vStr='First Annotation') ]) ]), jaeger.Span(operationName="test3", traceIdHigh=7929822056569588882, traceIdLow=-4638992594902767826, spanId=7929822056569588882, parentSpanId=0, startTime=1502820146071158, duration=10000000, logs=[]) ] spans_json = [span.format_span_json() for span in spans] expected_spans_json = [ span.format_span_json() for span in expected_spans ] span = spans_json[0] expected_span = expected_spans_json[0] try: listsEqual = self.assertCountEqual except AttributeError: listsEqual = self.assertItemsEqual log = span.get('logs')[0] expected_log = expected_span.get('logs')[0] self.assertEqual(log.get('timestamp'), expected_log.get('timestamp')) listsEqual(log.get('fields'), expected_log.get('fields')) listsEqual(span.get('tags'), expected_span.get('tags')) listsEqual(span.get('references'), expected_span.get('references')) self.assertEqual(span.get('traceIdHigh'), expected_span.get('traceIdHigh')) self.assertEqual(span.get('traceIdLow'), expected_span.get('traceIdLow')) self.assertEqual(span.get('spanId'), expected_span.get('spanId')) self.assertEqual(span.get('parentSpanId'), expected_span.get('parentSpanId')) self.assertEqual(span.get('operationName'), expected_span.get('operationName')) self.assertEqual(span.get('startTime'), expected_span.get('startTime')) self.assertEqual(span.get('duration'), expected_span.get('duration')) self.assertEqual(span.get('flags'), expected_span.get('flags')) self.assertEqual(spans_json[1], expected_spans_json[1]) self.assertEqual(spans_json[2], expected_spans_json[2])
def test_translate_to_zipkin_with_annotations(self): trace_id = '6e0c63257de34c92bf9efcd03927272e' annotation_attributes = { 'annotation_bool': True, 'annotation_string': 'annotation_test', 'key_float': .3 } s = '2017-08-15T18:02:26.071158' time = datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%f') time_events = [ time_event.TimeEvent(timestamp=time, annotation=time_event.Annotation( description='First Annotation', attributes=annotation_attributes)), time_event.TimeEvent(timestamp=time, message_event=time_event.MessageEvent( id='message-event-id', uncompressed_size_bytes=0, )), ] spans_ipv4 = [ span_data_module.SpanData( name='child_span', context=span_context.SpanContext(trace_id=trace_id), span_id='6e0c63257de34c92', parent_span_id='6e0c63257de34c93', attributes={'test_key': 'test_value'}, start_time='2017-08-15T18:02:26.071158Z', end_time='2017-08-15T18:02:36.071158Z', child_span_count=None, stack_trace=None, time_events=time_events, links=None, status=None, same_process_as_parent_span=None, span_kind=0, ), span_data_module.SpanData( name='child_span', context=span_context.SpanContext(trace_id=trace_id), span_id='6e0c63257de34c92', parent_span_id='6e0c63257de34c93', attributes={'test_key': 1}, start_time='2017-08-15T18:02:26.071158Z', end_time='2017-08-15T18:02:36.071158Z', child_span_count=None, stack_trace=None, time_events=time_events, links=None, status=None, same_process_as_parent_span=None, span_kind=None, ), ] spans_ipv6 = [ span_data_module.SpanData( name='child_span', context=span_context.SpanContext(trace_id=trace_id), span_id='6e0c63257de34c92', parent_span_id=None, attributes={ 'test_key': False, 'test_key2': 'raw_value', 'test_key3': 0.1, }, start_time='2017-08-15T18:02:26.071158Z', end_time='2017-08-15T18:02:36.071158Z', child_span_count=None, stack_trace=None, time_events=time_events, links=None, status=None, same_process_as_parent_span=None, span_kind=1, ), ] ipv4 = '127.0.0.1' ipv6 = '2001:0db8:85a3:0000:0000:8a2e:0370:7334' local_endpoint_ipv4 = { 'serviceName': 'my_service', 'ipv4': ipv4, 'port': 9411, } local_endpoint_ipv6 = { 'serviceName': 'my_service', 'ipv6': ipv6, 'port': 9411, } expected_zipkin_spans_ipv4 = [ { 'traceId': '6e0c63257de34c92bf9efcd03927272e', 'id': '6e0c63257de34c92', 'parentId': '6e0c63257de34c93', 'name': 'child_span', 'timestamp': 1502820146071158, 'duration': 10000000, 'localEndpoint': local_endpoint_ipv4, 'tags': { 'test_key': 'test_value' }, 'annotations': [{ 'timestamp': 1502820146071158, 'value': 'First Annotation' }] }, { 'traceId': '6e0c63257de34c92bf9efcd03927272e', 'id': '6e0c63257de34c92', 'parentId': '6e0c63257de34c93', 'name': 'child_span', 'timestamp': 1502820146071158, 'duration': 10000000, 'localEndpoint': local_endpoint_ipv4, 'tags': { 'test_key': '1' }, 'annotations': [{ 'timestamp': 1502820146071158, 'value': 'First Annotation' }] }, ] expected_zipkin_spans_ipv6 = [ { 'traceId': '6e0c63257de34c92bf9efcd03927272e', 'id': '6e0c63257de34c92', 'name': 'child_span', 'timestamp': 1502820146071158, 'duration': 10000000, 'localEndpoint': local_endpoint_ipv6, 'tags': { 'test_key': 'False', 'test_key2': 'raw_value', 'test_key3': '0.1' }, 'kind': 'SERVER', 'annotations': [{ 'timestamp': 1502820146071158, 'value': 'First Annotation' }] }, ] # Test ipv4 local endpoint exporter_ipv4 = trace_exporter.ZipkinExporter( service_name='my_service', ipv4=ipv4) zipkin_spans_ipv4 = exporter_ipv4.translate_to_zipkin( span_datas=spans_ipv4) self.assertEqual(zipkin_spans_ipv4, expected_zipkin_spans_ipv4) # Test ipv6 local endpoint exporter_ipv6 = trace_exporter.ZipkinExporter( service_name='my_service', ipv6=ipv6) zipkin_spans_ipv6 = exporter_ipv6.translate_to_zipkin( span_datas=spans_ipv6) self.assertEqual(zipkin_spans_ipv6, expected_zipkin_spans_ipv6)