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



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/opencensus/trace/span_builder.rb', line 397

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



456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/opencensus/trace/span_builder.rb', line 456

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



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/opencensus/trace/span_builder.rb', line 419

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



498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/opencensus/trace/span_builder.rb', line 498

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



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/opencensus/trace/span_builder.rb', line 477

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



520
521
522
523
# File 'lib/opencensus/trace/span_builder.rb', line 520

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



529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'lib/opencensus/trace/span_builder.rb', line 529

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



444
445
446
447
448
449
450
# File 'lib/opencensus/trace/span_builder.rb', line 444

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