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



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/opencensus/trace/span_builder.rb', line 414

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



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/opencensus/trace/span_builder.rb', line 473

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



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/opencensus/trace/span_builder.rb', line 436

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



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# File 'lib/opencensus/trace/span_builder.rb', line 515

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



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'lib/opencensus/trace/span_builder.rb', line 494

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



537
538
539
540
# File 'lib/opencensus/trace/span_builder.rb', line 537

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



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

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



461
462
463
464
465
466
467
# File 'lib/opencensus/trace/span_builder.rb', line 461

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