Class: AppPerfRpm::Span

Inherits:
Object
  • Object
show all
Defined in:
lib/app_perf_rpm/span.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSpan

Returns a new instance of Span.



42
43
44
45
46
# File 'lib/app_perf_rpm/span.rb', line 42

def initialize
  self.children = []
  self.type = "web"
  self.options = {}
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



3
4
5
# File 'lib/app_perf_rpm/span.rb', line 3

def action
  @action
end

#backtraceObject

Returns the value of attribute backtrace.



3
4
5
# File 'lib/app_perf_rpm/span.rb', line 3

def backtrace
  @backtrace
end

#childrenObject

Returns the value of attribute children.



3
4
5
# File 'lib/app_perf_rpm/span.rb', line 3

def children
  @children
end

#controllerObject

Returns the value of attribute controller.



3
4
5
# File 'lib/app_perf_rpm/span.rb', line 3

def controller
  @controller
end

#domainObject

Returns the value of attribute domain.



3
4
5
# File 'lib/app_perf_rpm/span.rb', line 3

def domain
  @domain
end

#ended_atObject

Returns the value of attribute ended_at.



3
4
5
# File 'lib/app_perf_rpm/span.rb', line 3

def ended_at
  @ended_at
end

#layerObject

Returns the value of attribute layer.



3
4
5
# File 'lib/app_perf_rpm/span.rb', line 3

def layer
  @layer
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/app_perf_rpm/span.rb', line 3

def options
  @options
end

#sourceObject

Returns the value of attribute source.



3
4
5
# File 'lib/app_perf_rpm/span.rb', line 3

def source
  @source
end

#started_atObject

Returns the value of attribute started_at.



3
4
5
# File 'lib/app_perf_rpm/span.rb', line 3

def started_at
  @started_at
end

#trace_idObject

Returns the value of attribute trace_id.



3
4
5
# File 'lib/app_perf_rpm/span.rb', line 3

def trace_id
  @trace_id
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/app_perf_rpm/span.rb', line 3

def type
  @type
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/app_perf_rpm/span.rb', line 3

def url
  @url
end

Class Method Details

.arrange(spans) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/app_perf_rpm/span.rb', line 17

def self.arrange(spans)
  spans.sort! { |a, b| (a.ended_at <=> b.ended_at) }

  null_span = Span.new
  controller = (spans.find {|s| s.controller } || null_span).controller
  action = (spans.find {|s| s.action } || null_span).action
  domain = (spans.find {|s| s.domain } || null_span).domain
  url = (spans.find {|s| s.url } || null_span).url

  while span = spans.shift
    span.controller ||= controller
    span.action ||= action
    span.domain ||= domain
    span.url ||= url

    if parent = spans.find { |n| n.parent_of?(span) }
      parent.children << span
    elsif spans.empty?
      root = span
    end
  end

  root
end

Instance Method Details

#base_optionsObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/app_perf_rpm/span.rb', line 77

def base_options
  opts = {}
  opts["domain"] = domain
  opts["controller"] = controller
  opts["action"] =  action
  opts["url"] = url
  opts["type"] = type
  #opts["backtrace"] = ::AppPerfRpm::Backtrace.backtrace
  opts["source"] = ::AppPerfRpm::Backtrace.source_extract
  opts.delete_if { |k, v| v.nil? }
end

#child_of?(span) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/app_perf_rpm/span.rb', line 61

def child_of?(span)
  span.parent_of?(self)
end

#durationObject



48
49
50
# File 'lib/app_perf_rpm/span.rb', line 48

def duration
  @duration ||= (ended_at - started_at) * 1000.0
end

#exclusive_durationObject



52
53
54
# File 'lib/app_perf_rpm/span.rb', line 52

def exclusive_duration
  @exclusive_duration ||= duration - children.inject(0.0) { |sum, child| sum + child.duration }
end

#parent_of?(span) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/app_perf_rpm/span.rb', line 56

def parent_of?(span)
  start = (started_at - span.started_at) * 1000.0
  start <= 0 && (start + duration >= span.duration)
end

#to_aObject



93
94
95
96
97
98
99
100
101
# File 'lib/app_perf_rpm/span.rb', line 93

def to_a
  [
    layer,
    trace_id,
    started_at,
    duration,
    base_options.merge(options)
  ]
end

#to_sObject



89
90
91
# File 'lib/app_perf_rpm/span.rb', line 89

def to_s
  "#{layer}:#{trace_id}:#{started_at}:#{exclusive_duration}"
end

#to_spansObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/app_perf_rpm/span.rb', line 65

def to_spans
  span = self.dup
  span.exclusive_duration
  span.children = []

  if children.size > 0
    return [span] + children.map(&:to_spans)
  else
    return [span]
  end
end