Module: TraceView::API::Util

Included in:
TraceView::API
Defined in:
lib/traceview/api/util.rb

Overview

General utility methods for the gem

Constant Summary collapse

BACKTRACE_CUTOFF =
200

Instance Method Summary collapse

Instance Method Details

#backtrace(_ignore = 1) ⇒ Object

Internal: Get the current backtrace.

ignore - Number of frames to ignore at the end of the backtrace. Use

when you know how many layers deep in oboe the call is being
made.

Returns a string with each frame of the backtrace separated by ‘rn’.

FIXME: ignore is not currently used (see BACKTRACE_CUTOFF)



32
33
34
# File 'lib/traceview/api/util.rb', line 32

def backtrace(_ignore = 1)
  trim_backtrace(Kernel.caller).join("\r\n")
end

#blacklisted?(addr_port) ⇒ Boolean

Internal: Check if a host is blacklisted from tracing

addr_port - the addr_port from Net::HTTP although this method can be used from any component in reality

Returns a boolean on blacklisted state

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/traceview/api/util.rb', line 60

def blacklisted?(addr_port)
  return false unless TraceView::Config.blacklist

  # Ensure that the blacklist is an array
  unless TraceView::Config.blacklist.is_a?(Array)
    val = TraceView::Config[:blacklist]
    TraceView::Config[:blacklist] = [val.to_s]
  end

  TraceView::Config.blacklist.each do |h|
    return true if addr_port.to_s.match(h.to_s)
  end

  false
end

#get_class_name(klass) ⇒ Object

Internal: Determine a string to report representing klass

args - an instance of a Class, a Class or a Module

Returns a string representation of klass



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/traceview/api/util.rb', line 98

def get_class_name(klass)
  kv = {}

  if klass.to_s =~ /::/
    klass.class.to_s.rpartition('::').last
  else
    if klass.is_a?(Class) && klass.is_a?(Module)
      # Class
      kv['Class'] = klass.to_s

    elsif (!klass.is_a?(Class) && !klass.is_a?(Module))
      # Class instance
      kv['Class'] = klass.class.to_s

    else
      # Module
      kv['Module'] = klass.to_s
    end
  end
  kv
end

#pps(*args) ⇒ Object

Internal: Pretty print a list of arguments for reporting

args - the list of arguments to work on

Returns a pretty string representation of arguments



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/traceview/api/util.rb', line 81

def pps(*args)
  old_out = $stdout
  begin
    s = StringIO.new
    $stdout = s
    pp(*args)
  ensure
    $stdout = old_out
  end
  s.string
end

#trim_backtrace(backtrace) ⇒ Object

Internal: Trim a backtrace to a manageable size

backtrace - the backtrace (an array of stack frames/from Kernel.caller)

Returns a trimmed backtrace



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/traceview/api/util.rb', line 41

def trim_backtrace(backtrace)
  return backtrace unless backtrace.is_a?(Array)

  length = backtrace.size
  if length > BACKTRACE_CUTOFF
    # Trim backtraces by getting the first 180 and last 20 lines
    trimmed = backtrace[0, 180] + ['...[snip]...'] + backtrace[length - 20, 20]
  else
    trimmed = backtrace
  end
  trimmed
end

#valid_key?(key) ⇒ Boolean

Internal: Check whether the provided key is reserved or not. Reserved keys are either keys that are handled by liboboe calls or the traceview gem.

key - the key to check.

Return a boolean indicating whether or not key is reserved.

Returns:

  • (Boolean)


19
20
21
# File 'lib/traceview/api/util.rb', line 19

def valid_key?(key)
  !%w(Label Layer Edge Timestamp Timestamp_u).include? key.to_s
end