class OpenCensus::Trace::Exporters::Stackdriver::Converter

An object that converts OpenCensus span data objects to Stackdriver Trace V2 protos.

You should use one converter instance to convert the spans for a single export request, because the converter will keep track of and omit duplicate stack traces. Use a new converter instance for the next request.

@private

Constants

AGENT_KEY

@private Attribute key for Stackdriver trace agent

AGENT_VALUE

@private Attribute value for Stackdriver trace agent

ATTRIBUTE_NAME_MAPPING

@private Mapping for certain well-known attributes defined in github.com/census-instrumentation/opencensus-specs/blob/master/trace/HTTP.md

TraceProtos

@private Alias for the V2 Cloudtrace protos namespace

Public Class Methods

new(project_id) click to toggle source

Create a converter

@param [String] project_id Google project ID

# File lib/opencensus/trace/exporters/stackdriver/converter.rb, line 75
def initialize project_id
  @project_id = project_id
  @stack_trace_hash_ids = {}
end

Public Instance Methods

convert_annotation(annotation) click to toggle source

Convert an annotation object

@param [OpenCensus::Trace::Annotation] annotation The annotation

object to convert

@return

[Google::Cloud::Trace::V2::Span::TimeEvent::Annotation]
The generated proto
# File lib/opencensus/trace/exporters/stackdriver/converter.rb, line 273
def convert_annotation annotation
  annotation_proto = TraceProtos::Span::TimeEvent::Annotation.new \
    description: convert_truncatable_string(annotation.description),
    attributes:
      convert_attributes(annotation.attributes,
                         annotation.dropped_attributes_count)
  TraceProtos::Span::TimeEvent.new \
    time: convert_time(annotation.time),
    annotation: annotation_proto
end
convert_attribute_name(name) click to toggle source

Map OpenCensus attribute name to Stackdriver Trace name.

@param [String] name OpenCensus attribute name @return [String] Corresponding Stackdriver Trace attribute.

# File lib/opencensus/trace/exporters/stackdriver/converter.rb, line 194
def convert_attribute_name name
  ATTRIBUTE_NAME_MAPPING[name] || name
end
convert_attribute_value(obj) click to toggle source

Convert a value that can be used for an attribute.

@param [OpenCensus::Trace::TruncatableString, Integer, boolean]

obj Object to convert

@return [Google::Cloud::Trace::V2::AttributeValue] The

generated proto
# File lib/opencensus/trace/exporters/stackdriver/converter.rb, line 176
def convert_attribute_value obj
  case obj
  when OpenCensus::Trace::TruncatableString
    TraceProtos::AttributeValue.new \
      string_value: convert_truncatable_string(obj)
  when Integer
    TraceProtos::AttributeValue.new int_value: obj
  when true, false
    TraceProtos::AttributeValue.new bool_value: obj
  end
end
convert_attributes(attributes, dropped_attributes_count, include_agent_attribute: false) click to toggle source

Convert an attributes hash

@param [Hash] attributes The map of attribute values to convert @param [Integer] dropped_attributes_count Number of dropped @param [Boolean] include_agent_attribute Include the `g.co/agent`

attribute in the result. Default is false.

@return [Google::Cloud::Trace::V2::Attributes] The

generated proto
# File lib/opencensus/trace/exporters/stackdriver/converter.rb, line 208
def convert_attributes attributes, dropped_attributes_count,
                       include_agent_attribute: false
  attribute_map = {}
  if include_agent_attribute
    attribute_map[AGENT_KEY] = convert_attribute_value AGENT_VALUE
  end
  attributes.each do |k, v|
    attribute_map[convert_attribute_name k] =
      convert_attribute_value v
  end
  TraceProtos::Span::Attributes.new \
    attribute_map: attribute_map,
    dropped_attributes_count: dropped_attributes_count
end
convert_message_event(message_event) click to toggle source

Convert a message event object

@param [OpenCensus::Trace::MessageEvent] message_event The message

event object to convert

@return

[Google::Cloud::Trace::V2::Span::TimeEvent::MessageEvent]
The generated proto
# File lib/opencensus/trace/exporters/stackdriver/converter.rb, line 293
def convert_message_event message_event
  message_event_proto =
    TraceProtos::Span::TimeEvent::MessageEvent.new \
      type: message_event.type,
      id: message_event.id,
      uncompressed_size_bytes: message_event.uncompressed_size,
      compressed_size_bytes: message_event.compressed_size
  TraceProtos::Span::TimeEvent.new \
    time: convert_time(message_event.time),
    message_event: message_event_proto
end
convert_optional_bool(value) click to toggle source

Convert a nullable boolean object

@param [boolean, nil] value The value to convert, or nil if absent @return [Google::Protobuf::BoolValue, nil] Generated proto, or nil

# File lib/opencensus/trace/exporters/stackdriver/converter.rb, line 386
def convert_optional_bool value
  return nil if value.nil?

  Google::Protobuf::BoolValue.new value: value
end
convert_optional_int32(value) click to toggle source

Convert a nullable int32 object

@param [Integer, nil] value The value to convert, or nil if absent @return [Google::Protobuf::Int32Value, nil] Generated proto, or nil

# File lib/opencensus/trace/exporters/stackdriver/converter.rb, line 398
def convert_optional_int32 value
  return nil if value.nil?

  Google::Protobuf::Int32Value.new value: value
end
convert_optional_status(status) click to toggle source

Convert a nullable status object

@param [OpenCensus::Trace::Status, nil] status The status object to

convert, or nil if absent

@return [Google::Rpc::Status, nil] The generated proto, or nil

# File lib/opencensus/trace/exporters/stackdriver/converter.rb, line 374
def convert_optional_status status
  return nil if status.nil?

  Google::Rpc::Status.new code: status.code, message: status.message
end
convert_span(obj) click to toggle source

Convert a span object.

@param [OpenCensus::Trace::Span] obj OpenCensus span object @return [Google::Cloud::Trace::V2::Span] The generated

proto
# File lib/opencensus/trace/exporters/stackdriver/converter.rb, line 89
def convert_span obj
  TraceProtos::Span.new \
    name: make_resource_name(@project_id, obj.trace_id, obj.span_id),
    span_id: obj.span_id,
    parent_span_id: obj.parent_span_id || "",
    display_name: convert_truncatable_string(obj.name),
    start_time: convert_time(obj.start_time),
    end_time: convert_time(obj.end_time),
    attributes:
      convert_attributes(obj.attributes,
                         obj.dropped_attributes_count,
                         include_agent_attribute: true),
    stack_trace:
      convert_stack_trace(obj.stack_trace, obj.dropped_frames_count,
                          obj.stack_trace_hash_id),
    time_events:
      convert_time_events(obj.time_events,
                          obj.dropped_annotations_count,
                          obj.dropped_message_events_count),
    links: convert_links(obj.links, obj.dropped_links_count),
    status: convert_optional_status(obj.status),
    same_process_as_parent_span:
      convert_optional_bool(obj.same_process_as_parent_span),
    child_span_count: convert_optional_int32(obj.child_span_count)
end
convert_stack_frame(frame) click to toggle source

Convert a single stack frame as a Thread::Backtrace::Location

@param [Thread::Backtrace::Location] frame The backtrace element to

convert

@return [Google::Cloud::Trace::V2::StackTrace::StackFrame]

The generated proto
# File lib/opencensus/trace/exporters/stackdriver/converter.rb, line 231
def convert_stack_frame frame
  TraceProtos::StackTrace::StackFrame.new \
    function_name: make_truncatable_string(frame.label),
    file_name: make_truncatable_string(frame.path),
    line_number: frame.lineno
end
convert_stack_trace(backtrace, dropped_frames_count, stack_trace_hash_id) click to toggle source

Convert a full backtrace.

@param [Array<Thread::Backtrace::Location>] backtrace The backtrace

element array to convert

@param [Integer] dropped_frames_count Frames that were dropped @param [Integer] stack_trace_hash_id Hash of the data @return [Google::Cloud::Trace::V2::StackTrace] The

generated proto
# File lib/opencensus/trace/exporters/stackdriver/converter.rb, line 248
def convert_stack_trace backtrace, dropped_frames_count,
                        stack_trace_hash_id
  if @stack_trace_hash_ids[stack_trace_hash_id]
    return TraceProtos::StackTrace.new \
      stack_trace_hash_id: stack_trace_hash_id
  end
  @stack_trace_hash_ids[stack_trace_hash_id] = true
  frame_protos = backtrace.map { |frame| convert_stack_frame(frame) }
  frames_proto = TraceProtos::StackTrace::StackFrames.new \
    frame: frame_protos,
    dropped_frames_count: dropped_frames_count
  TraceProtos::StackTrace.new \
    stack_frames: frames_proto,
    stack_trace_hash_id: stack_trace_hash_id
end
convert_time(time) click to toggle source

Convert a time object.

@param [Time] time Ruby Time object @return [Google::Protobuf::Timestamp] The generated proto

# File lib/opencensus/trace/exporters/stackdriver/converter.rb, line 162
def convert_time time
  proto = Google::Protobuf::Timestamp.new
  proto.from_time(time)
  proto
end
convert_time_events(time_events, dropped_annotations_count, dropped_message_events_count) click to toggle source

Convert a list of time event objects

@param [Array<OpenCensus::Trace::TimeEvent>] time_events The time

event objects to convert

@param [Integer] dropped_annotations_count Number of dropped

annotations

@param [Integer] dropped_message_events_count Number of dropped

message events

@return [Google::Cloud::Trace::V2::Span::TimeEvents] The

generated proto
# File lib/opencensus/trace/exporters/stackdriver/converter.rb, line 317
def convert_time_events time_events, dropped_annotations_count,
                        dropped_message_events_count
  time_event_protos = time_events.map do |time_event|
    case time_event
    when OpenCensus::Trace::Annotation
      convert_annotation time_event
    when OpenCensus::Trace::MessageEvent
      convert_message_event time_event
    else
      nil
    end
  end.compact
  TraceProtos::Span::TimeEvents.new \
    time_event: time_event_protos,
    dropped_annotations_count: dropped_annotations_count,
    dropped_message_events_count: dropped_message_events_count
end
convert_truncatable_string(obj) click to toggle source

Convert a truncatable string object.

@param [OpenCensus::Trace::TruncatableString] obj OpenCensus

truncatable string object

@return [Google::Cloud::Trace::V2::TruncatableString] The

generated proto
# File lib/opencensus/trace/exporters/stackdriver/converter.rb, line 152
def convert_truncatable_string obj
  make_truncatable_string obj.value, obj.truncated_byte_count
end
make_resource_name(project_id, trace_id, span_id) click to toggle source

Make a span resource name.

@param [String] project_id The project ID @param [String] trace_id The project ID @param [String] span_id The project ID @return [String] The resource na,e

# File lib/opencensus/trace/exporters/stackdriver/converter.rb, line 125
def make_resource_name project_id, trace_id, span_id
  "projects/#{project_id}/traces/#{trace_id}/spans/#{span_id}"
end
make_truncatable_string(str, truncated_byte_count = 0) click to toggle source

Create a truncatable string proto.

@param [String] str The string @param [Integer] truncated_byte_count The number of bytes omitted.

Defaults to 0

@return [Google::Cloud::Trace::V2::TruncatableString] The

generated proto
# File lib/opencensus/trace/exporters/stackdriver/converter.rb, line 138
def make_truncatable_string str, truncated_byte_count = 0
  TraceProtos::TruncatableString.new \
    value: str,
    truncated_byte_count: truncated_byte_count
end