Class: NewRelic::TransactionSample::Segment
- Inherits:
-
Object
- Object
- NewRelic::TransactionSample::Segment
show all
- Defined in:
- lib/new_relic/transaction_sample/segment.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(timestamp, metric_name, segment_id) ⇒ Segment
Returns a new instance of Segment.
13
14
15
16
17
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 13
def initialize(timestamp, metric_name, segment_id)
@entry_timestamp = timestamp
@metric_name = metric_name || '<unknown>'
@segment_id = segment_id || object_id
end
|
Instance Attribute Details
#entry_timestamp ⇒ Object
Returns the value of attribute entry_timestamp.
5
6
7
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 5
def entry_timestamp
@entry_timestamp
end
|
#exit_timestamp ⇒ Object
The exit timestamp will be relative except for the outermost sample which will have a timestamp.
8
9
10
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 8
def exit_timestamp
@exit_timestamp
end
|
#metric_name ⇒ Object
Returns the value of attribute metric_name.
10
11
12
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 10
def metric_name
@metric_name
end
|
#parent_segment ⇒ Object
Returns the value of attribute parent_segment.
9
10
11
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 9
def parent_segment
@parent_segment
end
|
#segment_id ⇒ Object
Returns the value of attribute segment_id.
11
12
13
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 11
def segment_id
@segment_id
end
|
Instance Method Details
#[](key) ⇒ Object
136
137
138
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 136
def [](key)
params[key]
end
|
#[]=(key, value) ⇒ Object
130
131
132
133
134
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 130
def []=(key, value)
params[key] = value
end
|
#add_called_segment(s) ⇒ Object
25
26
27
28
29
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 25
def add_called_segment(s)
@called_segments ||= []
@called_segments << s
s.parent_segment = self
end
|
#called_segments ⇒ Object
81
82
83
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 81
def called_segments
@called_segments || []
end
|
#called_segments=(segments) ⇒ Object
193
194
195
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 193
def called_segments=(segments)
@called_segments = segments
end
|
#count_segments ⇒ Object
101
102
103
104
105
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 101
def count_segments
count = 1
called_segments.each { | seg | count += seg.count_segments }
count
end
|
#duration ⇒ Object
return the total duration of this segment
86
87
88
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 86
def duration
(@exit_timestamp - @entry_timestamp).to_f
end
|
#each_segment(&block) ⇒ Object
call the provided block for this segment and each of the called segments
146
147
148
149
150
151
152
153
154
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 146
def each_segment(&block)
block.call self
if @called_segments
@called_segments.each do |segment|
segment.each_segment(&block)
end
end
end
|
#each_segment_with_nest_tracking(&block) ⇒ Object
call the provided block for this segment and each of the called segments while keeping track of nested segments
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 158
def each_segment_with_nest_tracking(&block)
summary = block.call self
summary.current_nest_count += 1 if summary
if @called_segments
@called_segments.each do |segment|
segment.each_segment_with_nest_tracking(&block)
end
end
summary.current_nest_count -= 1 if summary
end
|
#end_trace(timestamp) ⇒ Object
sets the final timestamp on a segment to indicate the exit point of the segment
21
22
23
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 21
def end_trace(timestamp)
@exit_timestamp = timestamp
end
|
#exclusive_duration ⇒ Object
return the duration of this segment without including the time in the called segments
92
93
94
95
96
97
98
99
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 92
def exclusive_duration
d = duration
called_segments.each do |segment|
d -= segment.duration
end
d
end
|
#explain_sql ⇒ Object
180
181
182
183
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 180
def explain_sql
NewRelic::Agent::Database.explain_sql(params[:sql],
params[:connection_config])
end
|
#find_segment(id) ⇒ Object
171
172
173
174
175
176
177
178
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 171
def find_segment(id)
return self if @segment_id == id
called_segments.each do |segment|
found = segment.find_segment(id)
return found if found
end
nil
end
|
#params ⇒ Object
140
141
142
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 140
def params
@params ||= {}
end
|
#params=(p) ⇒ Object
189
190
191
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 189
def params=(p)
@params = p
end
|
#path_string ⇒ Object
49
50
51
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 49
def path_string
"#{metric_name}[#{called_segments.collect {|segment| segment.path_string }.join('')}]"
end
|
#to_debug_str(depth) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 60
def to_debug_str(depth)
tab = " " * depth
s = tab.clone
s << ">> #{'%3i ms' % (@entry_timestamp*1000)} [#{self.class.name.split("::").last}] #{metric_name} \n"
unless params.empty?
params.each do |k,v|
s << "#{tab} -#{'%-16s' % k}: #{v.to_s[0..80]}\n"
end
end
called_segments.each do |cs|
s << cs.to_debug_str(depth + 1)
end
s << tab + "<< "
s << case @exit_timestamp
when nil then ' n/a'
when Numeric then '%3i ms' % (@exit_timestamp*1000)
else @exit_timestamp.to_s
end
s << " #{metric_name}\n"
end
|
#to_json(options = {}) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 35
def to_json(options={})
hash = {
:entry_timestamp => @entry_timestamp,
:exit_timestamp => @exit_timestamp,
:metric_name => @metric_name,
:segment_id => @segment_id,
}
hash[:called_segments] = called_segments if !called_segments.empty?
hash[:params] = @params if @params && !@params.empty?
hash.to_json
end
|
#to_s ⇒ Object
31
32
33
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 31
def to_s
to_debug_str(0)
end
|
#to_s_compact ⇒ Object
52
53
54
55
56
57
58
59
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 52
def to_s_compact
str = ""
str << metric_name
if called_segments.any?
str << "{#{called_segments.map { | cs | cs.to_s_compact }.join(",")}}"
end
str
end
|
#truncate(max) ⇒ Object
Walk through the tree and truncate the segments in a depth-first manner
109
110
111
112
113
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 109
def truncate(max)
return 1 unless @called_segments
total, self.called_segments = truncate_each_child(max - 1)
total+1
end
|
#truncate_each_child(max) ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/new_relic/transaction_sample/segment.rb', line 115
def truncate_each_child(max)
total = 0
accumulator = []
called_segments.each { | s |
if total == max
true
else
total += s.truncate(max - total)
accumulator << s
end
}
total
[total, accumulator]
end
|