class OpenCensus::Trace::SpanBuilder::PieceBuilder

Internal class that builds pieces of a span, honoring limits.

@private

Constants

MAX_INT

Maximum value of int64 @private

MIN_INT

Minimum value of int64 @private

Public Class Methods

new(max_attributes: nil, max_stack_frames: nil, max_annotations: nil, max_message_events: nil, max_links: nil, max_string_length: nil) click to toggle source

Initializer for PieceBuilder @private

# File lib/opencensus/trace/span_builder.rb, line 484
def initialize max_attributes: nil,
               max_stack_frames: nil,
               max_annotations: nil,
               max_message_events: nil,
               max_links: nil,
               max_string_length: nil
  config = OpenCensus::Trace.config
  @max_attributes = max_attributes || config.default_max_attributes
  @max_stack_frames =
    max_stack_frames || config.default_max_stack_frames
  @max_annotations = max_annotations || config.default_max_annotations
  @max_message_events =
    max_message_events || config.default_max_message_events
  @max_links = max_links || config.default_max_links
  @max_string_length =
    max_string_length || config.default_max_string_length
end

Public Instance Methods

convert_annotations(raw_annotations) click to toggle source

Build a canonical annotations list, truncating if necessary @private

# File lib/opencensus/trace/span_builder.rb, line 543
def convert_annotations raw_annotations
  result = []
  raw_annotations.each do |ann|
    break if @max_annotations != 0 && result.size >= @max_annotations
    attrs = convert_attributes ann.attributes
    dropped_attributes_count = ann.attributes.size - attrs.size
    result <<
      OpenCensus::Trace::Annotation.new(
        truncatable_string(ann.description),
        attributes: attrs,
        dropped_attributes_count: dropped_attributes_count,
        time: ann.time
      )
  end
  result
end
convert_attributes(attrs) click to toggle source

Build a canonical attributes hash, truncating if necessary @private

# File lib/opencensus/trace/span_builder.rb, line 506
def convert_attributes attrs
  result = {}
  attrs.each do |k, v|
    break if @max_attributes != 0 && result.size >= @max_attributes
    result[k.to_s] =
      case v
      when Integer
        if v >= MIN_INT && v <= MAX_INT
          v
        else
          truncatable_string v.to_s
        end
      when true, false, TruncatableString, Float
        v
      else
        truncatable_string v.to_s
      end
  end
  result
end
convert_message_events(raw_message_events) click to toggle source

Build a canonical message list, truncating if necessary @private

# File lib/opencensus/trace/span_builder.rb, line 564
def convert_message_events raw_message_events
  result = []
  raw_message_events.each do |evt|
    break if @max_message_events != 0 &&
             result.size >= @max_message_events
    result <<
      OpenCensus::Trace::MessageEvent.new(
        evt.type,
        evt.id,
        evt.uncompressed_size,
        compressed_size: evt.compressed_size,
        time: evt.time
      )
  end
  result
end
convert_status(status_code, status_message) click to toggle source

Build a canonical status object @private

# File lib/opencensus/trace/span_builder.rb, line 607
def convert_status status_code, status_message
  return nil unless status_code || status_message
  Status.new status_code.to_i, status_message.to_s
end
truncatable_string(str) click to toggle source

Build a truncatable string @private

# File lib/opencensus/trace/span_builder.rb, line 616
def truncatable_string str
  return str if str.is_a? TruncatableString
  orig_str = str.encode Encoding::UTF_8,
                        invalid: :replace,
                        undef: :replace
  if @max_string_length != 0 && @max_string_length < str.bytesize
    str = truncate_str orig_str, @max_string_length
    truncated_bytes = orig_str.bytesize - str.bytesize
    TruncatableString.new str, truncated_byte_count: truncated_bytes
  else
    TruncatableString.new orig_str
  end
end
truncate_stack_trace(raw_trace) click to toggle source

Build a canonical stack trace, truncating if necessary @private

# File lib/opencensus/trace/span_builder.rb, line 531
def truncate_stack_trace raw_trace
  if @max_stack_frames.zero? || raw_trace.size <= @max_stack_frames
    raw_trace
  else
    raw_trace[0, @max_stack_frames]
  end
end

Private Instance Methods

truncate_str(str, target_bytes) click to toggle source
# File lib/opencensus/trace/span_builder.rb, line 632
def truncate_str str, target_bytes
  tstr = str.dup
  tstr.force_encoding Encoding::ASCII_8BIT
  tstr.slice! target_bytes..-1
  tstr.force_encoding Encoding::UTF_8
  until tstr.valid_encoding?
    tstr.force_encoding Encoding::ASCII_8BIT
    tstr.slice!(-1..-1)
    tstr.force_encoding Encoding::UTF_8
  end
  tstr
end