def format_annotation_json(self): annotation_json = {'description': utils.get_truncatable_str(self.description)} if self.attributes is not None: annotation_json['attributes'] = self.attributes.\ format_attributes_json() return annotation_json
def format_span_json(span): """Helper to format a Span in JSON format. :type span: :class:`~opencensus.trace.span.Span` :param span: A Span to be transferred to JSON format. :rtype: dict :returns: Formatted Span. """ span_json = { 'displayName': utils.get_truncatable_str(span.name), 'spanId': span.span_id, 'startTime': span.start_time, 'endTime': span.end_time, 'childSpanCount': len(span._child_spans) } parent_span_id = None if span.parent_span is not None: parent_span_id = span.parent_span.span_id if parent_span_id is not None: span_json['parentSpanId'] = parent_span_id if span.attributes: span_json['attributes'] = attributes_module.Attributes( span.attributes).format_attributes_json() if span.stack_trace is not None: span_json['stackTrace'] = span.stack_trace.format_stack_trace_json() formatted_time_events = [] if span.annotations: formatted_time_events.extend({ 'time': aa.timestamp, 'annotation': aa.format_annotation_json() } for aa in span.annotations) if span.message_events: formatted_time_events.extend( { 'time': aa.timestamp, 'message_event': aa.format_message_event_json() } for aa in span.message_events) if formatted_time_events: span_json['timeEvents'] = {'timeEvent': formatted_time_events} if span.links: span_json['links'] = { 'link': [link.format_link_json() for link in span.links] } if span.status is not None: span_json['status'] = span.status.format_status_json() if span.same_process_as_parent_span is not None: span_json['sameProcessAsParentSpan'] = \ span.same_process_as_parent_span return span_json
def test_get_truncatable_str(self): str_to_convert = 'test string' truncatable_str = utils.get_truncatable_str(str_to_convert) expected_str = {'value': str_to_convert, 'truncated_byte_count': 0} self.assertEqual(expected_str, truncatable_str)
def _format_legacy_span_json(span_data): """ :param SpanData span_data: SpanData object to convert :rtype: dict :return: Dictionary representing the Span """ span_json = { 'displayName': utils.get_truncatable_str(span_data.name), 'spanId': span_data.span_id, 'startTime': span_data.start_time, 'endTime': span_data.end_time, 'childSpanCount': span_data.child_span_count, 'kind': span_data.span_kind } if span_data.parent_span_id is not None: span_json['parentSpanId'] = span_data.parent_span_id if span_data.attributes: span_json['attributes'] = attributes.Attributes( span_data.attributes).format_attributes_json() if span_data.stack_trace is not None: span_json['stackTrace'] = \ span_data.stack_trace.format_stack_trace_json() formatted_time_events = [] if span_data.annotations: formatted_time_events.extend({ 'time': aa.timestamp, 'annotation': aa.format_annotation_json() } for aa in span_data.annotations) if span_data.message_events: formatted_time_events.extend( { 'time': aa.timestamp, 'message_event': aa.format_message_event_json() } for aa in span_data.message_events) if formatted_time_events: span_json['timeEvents'] = {'timeEvent': formatted_time_events} if span_data.links: span_json['links'] = { 'link': [link.format_link_json() for link in span_data.links] } if span_data.status is not None: span_json['status'] = span_data.status.format_status_json() if span_data.same_process_as_parent_span is not None: span_json['sameProcessAsParentSpan'] = \ span_data.same_process_as_parent_span return span_json
def test_get_truncatable_str_length_exceeds(self): max_len = 5 patch = mock.patch('opencensus.common.utils.MAX_LENGTH', max_len) with patch: str_to_convert = 'length exceeded' truncatable_str = utils.get_truncatable_str(str_to_convert) expected_str = {'value': 'lengt', 'truncated_byte_count': 10} self.assertEqual(expected_str, truncatable_str)
def _format_attribute_value(value): if isinstance(value, bool): value_type = 'bool_value' elif isinstance(value, int): value_type = 'int_value' elif isinstance(value, six.string_types): value_type = 'string_value' value = utils.get_truncatable_str(value) elif isinstance(value, float): value_type = 'double_value' else: return None return {value_type: value}
def format_stack_frame_json(self): """Convert StackFrame object to json format.""" stack_frame_json = {} stack_frame_json['function_name'] = get_truncatable_str(self.func_name) stack_frame_json['original_function_name'] = get_truncatable_str( self.original_func_name) stack_frame_json['file_name'] = get_truncatable_str(self.file_name) stack_frame_json['line_number'] = self.line_num stack_frame_json['column_number'] = self.col_num stack_frame_json['load_module'] = { 'module': get_truncatable_str(self.load_module), 'build_id': get_truncatable_str(self.build_id), } stack_frame_json['source_version'] = get_truncatable_str( self.source_version) return stack_frame_json