Class: OpenCensus::Trace::SpanBuilder::PieceBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/opencensus/trace/span_builder.rb

Overview

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

Constant Summary collapse

MIN_INT =

Minimum value of int64

-0x10000000000000000
MAX_INT =

Maximum value of int64

0xffffffffffffffff

Instance Method Summary collapse

Constructor Details

#initialize(max_attributes: nil, max_stack_frames: nil, max_annotations: nil, max_message_events: nil, max_links: nil, max_string_length: nil) ⇒ PieceBuilder

Initializer for PieceBuilder



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/opencensus/trace/span_builder.rb', line 443

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

Instance Method Details

#convert_annotations(raw_annotations) ⇒ Object

Build a canonical annotations list, truncating if necessary



502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/opencensus/trace/span_builder.rb', line 502

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) ⇒ Object

Build a canonical attributes hash, truncating if necessary



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/opencensus/trace/span_builder.rb', line 465

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
        v
      else
        truncatable_string v.to_s
      end
  end
  result
end

Build a canonical links list, truncating if necessary



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/opencensus/trace/span_builder.rb', line 544

def convert_links raw_links
  result = []
  raw_links.each do |lnk|
    break if @max_links != 0 && result.size >= @max_links
    attrs = convert_attributes lnk.attributes
    dropped_attributes_count = lnk.attributes.size - attrs.size
    result <<
      OpenCensus::Trace::Link.new(
        lnk.trace_id,
        lnk.span_id,
        type: lnk.type,
        attributes: attrs,
        dropped_attributes_count: dropped_attributes_count
      )
  end
  result
end

#convert_message_events(raw_message_events) ⇒ Object

Build a canonical message list, truncating if necessary



523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/opencensus/trace/span_builder.rb', line 523

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) ⇒ Object

Build a canonical status object



566
567
568
569
# File 'lib/opencensus/trace/span_builder.rb', line 566

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) ⇒ Object

Build a truncatable string



575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/opencensus/trace/span_builder.rb', line 575

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) ⇒ Object

Build a canonical stack trace, truncating if necessary



490
491
492
493
494
495
496
# File 'lib/opencensus/trace/span_builder.rb', line 490

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