def test_set_annotation_with_attributes(self): pb_span = trace_pb2.Span() pb_event = pb_span.time_events.time_event.add() annotation = time_event_module.Annotation( description="hi there", attributes=attributes_module.Attributes( attributes={ 'test_str_key': 'test_str_value', 'test_int_key': 1, 'test_bool_key': False, 'test_double_key': 567.89 })) utils.set_proto_annotation(pb_event.annotation, annotation) self.assertEqual(pb_event.annotation.description.value, "hi there") self.assertEqual(len(pb_event.annotation.attributes.attribute_map), 3) self.assertEqual( pb_event.annotation.attributes.attribute_map['test_str_key'], trace_pb2.AttributeValue(string_value=trace_pb2.TruncatableString( value='test_str_value'))) self.assertEqual( pb_event.annotation.attributes.attribute_map['test_int_key'], trace_pb2.AttributeValue(int_value=1)) self.assertEqual( pb_event.annotation.attributes.attribute_map['test_bool_key'], trace_pb2.AttributeValue(bool_value=False)) self.assertEqual( pb_event.annotation.attributes.attribute_map['test_double_key'], trace_pb2.AttributeValue(double_value=567.89))
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 translate_to_trace_proto(span_data): """Translates the opencensus spans to ocagent proto spans. :type span_data: :class:`~opencensus.trace.span_data.SpanData` :param span_data: SpanData tuples to convert to protobuf spans :rtype: :class:`~opencensus.proto.trace.Span` :returns: Protobuf format span. """ if not span_data: return None pb_span = trace_pb2.Span( name=trace_pb2.TruncatableString(value=span_data.name), kind=span_data.span_kind, trace_id=hex_str_to_bytes_str(span_data.context.trace_id), span_id=hex_str_to_bytes_str(span_data.span_id), parent_span_id=hex_str_to_bytes_str(span_data.parent_span_id) if span_data.parent_span_id is not None else None, start_time=proto_ts_from_datetime_str(span_data.start_time), end_time=proto_ts_from_datetime_str(span_data.end_time), status=trace_pb2.Status(code=span_data.status.code, message=span_data.status.message) if span_data.status is not None else None, same_process_as_parent_span=BoolValue( value=span_data.same_process_as_parent_span) if span_data.same_process_as_parent_span is not None else None, child_span_count=UInt32Value(value=span_data.child_span_count) if span_data.child_span_count is not None else None) # attributes if span_data.attributes is not None: for attribute_key, attribute_value \ in span_data.attributes.items(): add_proto_attribute_value(pb_span.attributes, attribute_key, attribute_value) # time events if span_data.time_events is not None: for span_data_event in span_data.time_events: if span_data_event.message_event is not None: pb_event = pb_span.time_events.time_event.add() pb_event.time.FromJsonString(span_data_event.timestamp) set_proto_message_event(pb_event.message_event, span_data_event.message_event) elif span_data_event.annotation is not None: pb_event = pb_span.time_events.time_event.add() pb_event.time.FromJsonString(span_data_event.timestamp) set_proto_annotation(pb_event.annotation, span_data_event.annotation) # links if span_data.links is not None: for link in span_data.links: pb_link = pb_span.links.link.add( trace_id=hex_str_to_bytes_str(link.trace_id), span_id=hex_str_to_bytes_str(link.span_id), type=link.type) if link.attributes is not None and \ link.attributes.attributes is not None: for attribute_key, attribute_value \ in link.attributes.attributes.items(): add_proto_attribute_value(pb_link.attributes, attribute_key, attribute_value) # tracestate if span_data.context.tracestate is not None: for (key, value) in span_data.context.tracestate.items(): pb_span.tracestate.entries.add(key=key, value=value) return pb_span
def test_translate_time_events(self): annotation0_ts = datetime.utcnow() + timedelta(seconds=-10) annotation1_ts = datetime.utcnow() + timedelta(seconds=-9) message0_ts = datetime.utcnow() + timedelta(seconds=-8) message1_ts = datetime.utcnow() + timedelta(seconds=-7) message2_ts = datetime.utcnow() + timedelta(seconds=-6) span_data = span_data_module.SpanData( context=span_context_module.SpanContext( trace_id='6e0c63257de34c92bf9efcd03927272e'), span_id='6e0c63257de34c92', time_events=[ time_event_module.TimeEvent( timestamp=annotation0_ts, annotation=time_event_module.Annotation( description="hi there0", attributes=attributes_module.Attributes( attributes={ 'test_str_key': 'test_str_value', 'test_int_key': 1, 'test_bool_key': False, 'test_double_key': 567.89 }))), time_event_module.TimeEvent( timestamp=annotation1_ts, annotation=time_event_module.Annotation( description="hi there1")), time_event_module.TimeEvent( timestamp=message0_ts, message_event=time_event_module.MessageEvent( id=0, type=time_event_module.Type.SENT, uncompressed_size_bytes=10, compressed_size_bytes=1)), time_event_module.TimeEvent( timestamp=message1_ts, message_event=time_event_module.MessageEvent( id=1, type=time_event_module.Type.RECEIVED, uncompressed_size_bytes=20, compressed_size_bytes=2)), time_event_module.TimeEvent( timestamp=message2_ts, message_event=time_event_module.MessageEvent( id=2, type=time_event_module.Type.TYPE_UNSPECIFIED, uncompressed_size_bytes=30, compressed_size_bytes=3)) ], 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), 5) event0 = pb_span.time_events.time_event[0] event1 = pb_span.time_events.time_event[1] event2 = pb_span.time_events.time_event[2] event3 = pb_span.time_events.time_event[3] event4 = pb_span.time_events.time_event[4] self.assertEqual(event0.time.ToDatetime(), annotation0_ts) self.assertEqual(event1.time.ToDatetime(), annotation1_ts) self.assertEqual(event2.time.ToDatetime(), message0_ts) self.assertEqual(event3.time.ToDatetime(), message1_ts) self.assertEqual(event4.time.ToDatetime(), message2_ts) self.assertEqual(event0.annotation.description.value, "hi there0") self.assertEqual(event1.annotation.description.value, "hi there1") self.assertEqual(len(event0.annotation.attributes.attribute_map), 3) self.assertEqual(len(event1.annotation.attributes.attribute_map), 0) self.assertEqual( event0.annotation.attributes.attribute_map['test_str_key'], trace_pb2.AttributeValue(string_value=trace_pb2.TruncatableString( value='test_str_value'))) self.assertEqual( event0.annotation.attributes.attribute_map['test_int_key'], trace_pb2.AttributeValue(int_value=1)) self.assertEqual( event0.annotation.attributes.attribute_map['test_bool_key'], trace_pb2.AttributeValue(bool_value=False)) self.assertEqual( event0.annotation.attributes.attribute_map['test_double_key'], trace_pb2.AttributeValue(double_value=567.89)) self.assertEqual(event2.message_event.id, 0) self.assertEqual(event3.message_event.id, 1) self.assertEqual(event4.message_event.id, 2) self.assertEqual(event2.message_event.uncompressed_size, 10) self.assertEqual(event3.message_event.uncompressed_size, 20) self.assertEqual(event4.message_event.uncompressed_size, 30) self.assertEqual(event2.message_event.compressed_size, 1) self.assertEqual(event3.message_event.compressed_size, 2) self.assertEqual(event4.message_event.compressed_size, 3) self.assertEqual(event2.message_event.type, 1) self.assertEqual(event3.message_event.type, 2) self.assertEqual(event4.message_event.type, 0)
def test_translate_links(self): hex_encoder = codecs.getencoder('hex') span_data = span_data_module.SpanData( context=span_context_module.SpanContext( trace_id='6e0c63257de34c92bf9efcd03927272e'), span_id='6e0c63257de34c92', links=[ link_module.Link(trace_id='0e0c63257de34c92bf9efcd03927272e', span_id='0e0c63257de34c92', type=link_module.Type.TYPE_UNSPECIFIED, attributes=attributes_module.Attributes( attributes={ 'test_str_key': 'test_str_value', 'test_int_key': 1, 'test_bool_key': False, 'test_double_key': 567.89 })), link_module.Link(trace_id='1e0c63257de34c92bf9efcd03927272e', span_id='1e0c63257de34c92', type=link_module.Type.CHILD_LINKED_SPAN), link_module.Link(trace_id='2e0c63257de34c92bf9efcd03927272e', span_id='2e0c63257de34c92', type=link_module.Type.PARENT_LINKED_SPAN) ], 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, time_events=None) pb_span = utils.translate_to_trace_proto(span_data) self.assertEqual(len(pb_span.links.link), 3) self.assertEqual( hex_encoder(pb_span.links.link[0].trace_id)[0], b'0e0c63257de34c92bf9efcd03927272e') self.assertEqual( hex_encoder(pb_span.links.link[1].trace_id)[0], b'1e0c63257de34c92bf9efcd03927272e') self.assertEqual( hex_encoder(pb_span.links.link[2].trace_id)[0], b'2e0c63257de34c92bf9efcd03927272e') self.assertEqual( hex_encoder(pb_span.links.link[0].span_id)[0], b'0e0c63257de34c92') self.assertEqual( hex_encoder(pb_span.links.link[1].span_id)[0], b'1e0c63257de34c92') self.assertEqual( hex_encoder(pb_span.links.link[2].span_id)[0], b'2e0c63257de34c92') self.assertEqual(pb_span.links.link[0].type, 0) self.assertEqual(pb_span.links.link[1].type, 1) self.assertEqual(pb_span.links.link[2].type, 2) self.assertEqual(len(pb_span.links.link[0].attributes.attribute_map), 3) self.assertEqual(len(pb_span.links.link[1].attributes.attribute_map), 0) self.assertEqual(len(pb_span.links.link[2].attributes.attribute_map), 0) self.assertEqual( pb_span.links.link[0].attributes.attribute_map['test_str_key'], trace_pb2.AttributeValue(string_value=trace_pb2.TruncatableString( value='test_str_value'))) self.assertEqual( pb_span.links.link[0].attributes.attribute_map['test_int_key'], trace_pb2.AttributeValue(int_value=1)) self.assertEqual( pb_span.links.link[0].attributes.attribute_map['test_bool_key'], trace_pb2.AttributeValue(bool_value=False)) self.assertEqual( pb_span.links.link[0].attributes.attribute_map['test_double_key'], trace_pb2.AttributeValue(double_key=567.89))