Class: NewRelic::Agent::TransactionSampleBuilder

Inherits:
Object
  • Object
show all
Includes:
CollectionHelper
Defined in:
lib/new_relic/agent/transaction_sample_builder.rb

Overview

a builder is created with every sampled transaction, to dynamically generate the sampled data. It is a thread-local object, and is not accessed by any other thread so no need for synchronization.

Constant Summary

Constants included from CollectionHelper

CollectionHelper::DEFAULT_ARRAY_TRUNCATION_SIZE, CollectionHelper::DEFAULT_TRUNCATION_SIZE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CollectionHelper

#normalize_params, #strip_nr_from_backtrace

Constructor Details

#initialize(time = Time.now) ⇒ TransactionSampleBuilder

Returns a new instance of TransactionSampleBuilder.



16
17
18
19
20
21
22
# File 'lib/new_relic/agent/transaction_sample_builder.rb', line 16

def initialize(time=Time.now)
  @sample = NewRelic::TransactionSample.new(time.to_f)
  @sample_start = time.to_f
  @current_segment = @sample.root_segment
  @segment_limit = NewRelic::Control.instance.fetch('transaction_tracer', {}) \
    .fetch('limit_segments', 4000)
end

Instance Attribute Details

#current_segmentObject (readonly)

Returns the value of attribute current_segment.



11
12
13
# File 'lib/new_relic/agent/transaction_sample_builder.rb', line 11

def current_segment
  @current_segment
end

#sampleObject (readonly)

Returns the value of attribute sample.



11
12
13
# File 'lib/new_relic/agent/transaction_sample_builder.rb', line 11

def sample
  @sample
end

#segment_limitObject

Returns the value of attribute segment_limit.



12
13
14
# File 'lib/new_relic/agent/transaction_sample_builder.rb', line 12

def segment_limit
  @segment_limit
end

Instance Method Details

#finish_trace(time) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/new_relic/agent/transaction_sample_builder.rb', line 57

def finish_trace(time)
  # This should never get called twice, but in a rare case that we can't reproduce in house it does.
  # log forensics and return gracefully
  if @sample.frozen?
    log = NewRelic::Control.instance.log
    log.error "Unexpected double-freeze of Transaction Trace Object: \n#{@sample.to_s}"
    return
  end
  @sample.root_segment.end_trace(time.to_f - @sample_start)
  @sample.params[:custom_params] = normalize_params(NewRelic::Agent::Instrumentation::MetricFrame.custom_parameters)
  
  @sample.force_persist = NewRelic::Agent::TransactionInfo.get.force_persist_sample?(sample)
  @sample.freeze
  @current_segment = nil
end

#freezeObject



85
86
87
# File 'lib/new_relic/agent/transaction_sample_builder.rb', line 85

def freeze
  @sample.freeze unless sample.frozen?
end

#ignore_transactionObject



32
33
34
# File 'lib/new_relic/agent/transaction_sample_builder.rb', line 32

def ignore_transaction
  @ignore = true
end

#ignored?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/new_relic/agent/transaction_sample_builder.rb', line 28

def ignored?
  @ignore || @sample.params[:path].nil?
end

#sample_idObject



24
25
26
# File 'lib/new_relic/agent/transaction_sample_builder.rb', line 24

def sample_id
  @sample.sample_id
end

#scope_depthObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/new_relic/agent/transaction_sample_builder.rb', line 73

def scope_depth
  depth = -1        # have to account for the root
  current = @current_segment

  while(current)
    depth += 1
    current = current.parent_segment
  end

  depth
end

#set_profile(profile) ⇒ Object



89
90
91
# File 'lib/new_relic/agent/transaction_sample_builder.rb', line 89

def set_profile(profile)
  @sample.profile = profile
end

#set_transaction_cpu_time(cpu_time) ⇒ Object



106
107
108
# File 'lib/new_relic/agent/transaction_sample_builder.rb', line 106

def set_transaction_cpu_time(cpu_time)
  @sample.params[:cpu_time] = cpu_time
end

#set_transaction_info(path, uri, params) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/new_relic/agent/transaction_sample_builder.rb', line 93

def set_transaction_info(path, uri, params)
  @sample.params[:path] = path

  if NewRelic::Control.instance.capture_params
    params = normalize_params params

    @sample.params[:request_params].merge!(params)
    @sample.params[:request_params].delete :controller
    @sample.params[:request_params].delete :action
  end
  @sample.params[:uri] ||= uri || params[:uri]
end

#trace_entry(metric_name, time) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/new_relic/agent/transaction_sample_builder.rb', line 36

def trace_entry(metric_name, time)
  if @sample.count_segments < @segment_limit
    segment = @sample.create_segment(time.to_f - @sample_start, metric_name)
    @current_segment.add_called_segment(segment)
    @current_segment = segment
    if @sample.count_segments == @segment_limit
      NewRelic::Control.instance.log.debug("Segment limit of #{@segment_limit} reached, ceasing collection.")
    end
    @current_segment
  end
end

#trace_exit(metric_name, time) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/new_relic/agent/transaction_sample_builder.rb', line 48

def trace_exit(metric_name, time)
  return unless @sample.count_segments < @segment_limit
  if metric_name != @current_segment.metric_name
    fail "unbalanced entry/exit: #{metric_name} != #{@current_segment.metric_name}"
  end
  @current_segment.end_trace(time.to_f - @sample_start)
  @current_segment = @current_segment.parent_segment
end