Class: NewRelic::TransactionSample

Inherits:
Object
  • Object
show all
Includes:
Coerce, TransactionAnalysis
Defined in:
lib/new_relic/transaction_sample.rb,
lib/new_relic/transaction_sample/segment.rb,
lib/new_relic/transaction_sample/fake_segment.rb,
lib/new_relic/transaction_sample/summary_segment.rb,
lib/new_relic/transaction_sample/composite_segment.rb

Defined Under Namespace

Classes: CompositeSegment, FakeSegment, Segment, SummarySegment

Constant Summary collapse

@@start_time =
Time.now

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Coerce

#float, #int, #log_failure, #string

Methods included from TransactionAnalysis

#breakdown_data, #database_time, #render_time, #sql_segments

Constructor Details

#initialize(time = Time.now.to_f, sample_id = nil) ⇒ TransactionSample

Returns a new instance of TransactionSample.



22
23
24
25
26
27
28
29
30
31
# File 'lib/new_relic/transaction_sample.rb', line 22

def initialize(time = Time.now.to_f, sample_id = nil)
  @sample_id = sample_id || object_id
  @start_time = time
  @params = { :segment_count => -1, :request_params => {} }
  @segment_count = -1
  @root_segment = create_segment 0.0, "ROOT"

  @guid = generate_guid
  NewRelic::Agent::TransactionInfo.get.guid = @guid
end

Instance Attribute Details

#force_persistObject

Returns the value of attribute force_persist.



14
15
16
# File 'lib/new_relic/transaction_sample.rb', line 14

def force_persist
  @force_persist
end

#guidObject

Returns the value of attribute guid.



14
15
16
# File 'lib/new_relic/transaction_sample.rb', line 14

def guid
  @guid
end

#paramsObject

Returns the value of attribute params.



14
15
16
# File 'lib/new_relic/transaction_sample.rb', line 14

def params
  @params
end

#profileObject

Returns the value of attribute profile.



14
15
16
# File 'lib/new_relic/transaction_sample.rb', line 14

def profile
  @profile
end

#root_segmentObject

Returns the value of attribute root_segment.



14
15
16
# File 'lib/new_relic/transaction_sample.rb', line 14

def root_segment
  @root_segment
end

#sample_idObject (readonly)

Returns the value of attribute sample_id.



16
17
18
# File 'lib/new_relic/transaction_sample.rb', line 16

def sample_id
  @sample_id
end

#thresholdObject

Returns the value of attribute threshold.



14
15
16
# File 'lib/new_relic/transaction_sample.rb', line 14

def threshold
  @threshold
end

Instance Method Details

#count_segmentsObject



33
34
35
# File 'lib/new_relic/transaction_sample.rb', line 33

def count_segments
  @segment_count
end

#create_segment(relative_timestamp, metric_name, segment_id = nil) ⇒ Object

Raises:

  • (TypeError)


90
91
92
93
94
95
# File 'lib/new_relic/transaction_sample.rb', line 90

def create_segment(relative_timestamp, metric_name, segment_id = nil)
  raise TypeError.new("Frozen Transaction Sample") if frozen?
  @params[:segment_count] += 1
  @segment_count += 1
  NewRelic::TransactionSample::Segment.new(relative_timestamp, metric_name, segment_id)
end

#durationObject



97
98
99
# File 'lib/new_relic/transaction_sample.rb', line 97

def duration
  root_segment.duration
end

#each_segment(&block) ⇒ Object

Iterates recursively over each segment in the entire transaction sample tree



103
104
105
# File 'lib/new_relic/transaction_sample.rb', line 103

def each_segment(&block)
  @root_segment.each_segment(&block)
end

#each_segment_with_nest_tracking(&block) ⇒ Object

Iterates recursively over each segment in the entire transaction sample tree while keeping track of nested segments



109
110
111
# File 'lib/new_relic/transaction_sample.rb', line 109

def each_segment_with_nest_tracking(&block)
  @root_segment.each_segment_with_nest_tracking(&block)
end

#ensure_segment_count_set(count) ⇒ Object

makes sure that the parameter cache for segment count is set to the correct value



48
49
50
# File 'lib/new_relic/transaction_sample.rb', line 48

def ensure_segment_count_set(count)
  params[:segment_count] ||= count
end

#find_segment(id) ⇒ Object

Searches the tree recursively for the segment with the given id. note that this is an internal id, not an ActiveRecord id



119
120
121
# File 'lib/new_relic/transaction_sample.rb', line 119

def find_segment(id)
  @root_segment.find_segment(id)
end

#omit_segments_with(regex) ⇒ Object

return a new transaction sample that treats segments with the given regular expression in their name as if they were never called at all. This allows us to strip out segments from traces captured in development environment that would not normally show up in production (like Rails/Application Code Loading)



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/new_relic/transaction_sample.rb', line 150

def omit_segments_with(regex)
  regex = Regexp.new(regex)

  sample = TransactionSample.new(@start_time, sample_id)

  sample.params = params.dup
  sample.params[:segment_count] = 0

  delta = build_segment_with_omissions(sample, 0.0, @root_segment, sample.root_segment, regex)
  sample.root_segment.end_trace(@root_segment.exit_timestamp - delta)
  sample.profile = self.profile
  sample
end

#path_stringObject



86
87
88
# File 'lib/new_relic/transaction_sample.rb', line 86

def path_string
  @root_segment.path_string
end

#prepare_to_send(options = {}) ⇒ Object

Return a new transaction sample that can be sent to the New Relic service. This involves potentially one or more of the following options

:explain_sql : run EXPLAIN on all queries whose response times equal the value for this key
    (for example :explain_sql => 2.0 would explain everything over 2 seconds.  0.0 would explain everything.)
:keep_backtraces : keep backtraces, significantly increasing size of trace (off by default)
:record_sql => [ :raw | :obfuscated] : copy over the sql, obfuscating if necessary


172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/new_relic/transaction_sample.rb', line 172

def prepare_to_send(options={})
  sample = TransactionSample.new(@start_time, sample_id)

  sample.params.merge! self.params
  sample.guid = self.guid
  sample.force_persist = self.force_persist if self.force_persist

  build_segment_for_transfer(sample, @root_segment, sample.root_segment, options)

  sample.root_segment.end_trace(@root_segment.exit_timestamp)
  sample
end

#start_timeObject



82
83
84
# File 'lib/new_relic/transaction_sample.rb', line 82

def start_time
  Time.at(@start_time)
end

#timestampObject

offset from start of app



53
54
55
# File 'lib/new_relic/transaction_sample.rb', line 53

def timestamp
  @start_time - @@start_time.to_f
end

#to_arrayObject



63
64
65
66
67
68
# File 'lib/new_relic/transaction_sample.rb', line 63

def to_array
  [ float(@start_time),
    @params[:request_params],
    @params[:custom_params],
    @root_segment.to_array ]
end

#to_collector_array(encoder) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/new_relic/transaction_sample.rb', line 70

def to_collector_array(encoder)
  trace_tree = encoder.encode(self.to_array)
  [ Helper.time_to_millis(@start_time),
    Helper.time_to_millis(duration),
    string(@params[:path]),
    string(@params[:uri]),
    trace_tree,
    string(@guid),
    nil,
    !!@force_persist ]
end

#to_jsonObject



57
58
59
# File 'lib/new_relic/transaction_sample.rb', line 57

def to_json
  JSON.dump(self.to_array)
end

#to_sObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/new_relic/transaction_sample.rb', line 123

def to_s
  s = "Transaction Sample collected at #{start_time}\n"
  s << "  {\n"
  s << "  Path: #{params[:path]} \n"

  params.each do |k,v|
    next if k == :path
    s << "  #{k}: " <<
    case v
      when Enumerable then v.map(&:to_s).sort.join("; ")
      when String then v
      when Float then '%6.3s' % v
      when Fixnum then v.to_s
      when nil then ''
    else
      raise "unexpected value type for #{k}: '#{v}' (#{v.class})"
    end << "\n"
  end
  s << "  }\n\n"
  s <<  @root_segment.to_debug_str(0)
end

#to_s_compactObject



113
114
115
# File 'lib/new_relic/transaction_sample.rb', line 113

def to_s_compact
  @root_segment.to_s_compact
end

#truncate(max) ⇒ Object

Truncates the transaction sample to a maximum length determined by the passed-in parameter. Operates recursively on the entire tree of transaction segments in a depth-first manner



40
41
42
43
44
# File 'lib/new_relic/transaction_sample.rb', line 40

def truncate(max)
  return if @segment_count < max
  @root_segment.truncate(max + 1)
  @segment_count = max
end