class OneApm::TransactionSample

the number of segments that need to exist before we roll them up into one segment with multiple executions

Attributes

finished[RW]
force_persist[RW]
guid[RW]
params[RW]
prepared[W]
profile[RW]
root_segment[RW]
sample_id[R]
start_time[RW]
synthetics_resource_id[RW]
threshold[RW]
xray_session_id[RW]

Public Class Methods

new(time = Time.now.to_f, sample_id = nil) click to toggle source
# File lib/one_apm/transaction/transaction_sample.rb, line 25
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"
  @prepared = false
end

Public Instance Methods

count_segments() click to toggle source
# File lib/one_apm/transaction/transaction_sample.rb, line 38
def count_segments
  @segment_count
end
create_segment(relative_timestamp, metric_name=nil) click to toggle source

relative_timestamp is seconds since the start of the transaction

# File lib/one_apm/transaction/transaction_sample.rb, line 105
def create_segment(relative_timestamp, metric_name=nil)
  raise TypeError.new("Frozen Transaction Sample") if finished
  @params[:segment_count] += 1
  @segment_count += 1
  OneApm::TransactionSample::Segment.new(relative_timestamp, metric_name)
end
duration() click to toggle source
# File lib/one_apm/transaction/transaction_sample.rb, line 112
def duration
  root_segment.duration
end
each_segment(&block) click to toggle source

Iterates recursively over each segment in the entire transaction sample tree

# File lib/one_apm/transaction/transaction_sample.rb, line 118
def each_segment(&block)
  @root_segment.each_segment(&block)
end
each_segment_with_nest_tracking(&block) click to toggle source

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

# File lib/one_apm/transaction/transaction_sample.rb, line 124
def each_segment_with_nest_tracking(&block)
  @root_segment.each_segment_with_nest_tracking(&block)
end
ensure_segment_count_set(count) click to toggle source

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

# File lib/one_apm/transaction/transaction_sample.rb, line 44
def ensure_segment_count_set(count)
  params[:segment_count] ||= count
end
find_segment(id) click to toggle source

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

# File lib/one_apm/transaction/transaction_sample.rb, line 134
def find_segment(id)
  @root_segment.find_segment(id)
end
forced?() click to toggle source
# File lib/one_apm/transaction/transaction_sample.rb, line 100
def forced?
  !!@force_persist || !int_or_nil(xray_session_id).nil?
end
params=(params) click to toggle source
# File lib/one_apm/transaction/transaction_sample.rb, line 175
def params=(params)
  @params = params
end
path_string() click to toggle source
# File lib/one_apm/transaction/transaction_sample.rb, line 88
def path_string
  @root_segment.path_string
end
prepare_to_send!() click to toggle source

Return a new transaction sample that can be sent to the OneApm service.

# File lib/one_apm/transaction/transaction_sample.rb, line 161
def prepare_to_send!
  return self if @prepared

  if Agent::Database.should_record_sql?
    collect_explain_plans!
    prepare_sql_for_transmission!
  else
    strip_sql!
  end

  @prepared = true
  self
end
prepared?() click to toggle source
# File lib/one_apm/transaction/transaction_sample.rb, line 34
def prepared?
  @prepared
end
set_custom_param(name, value) click to toggle source
# File lib/one_apm/transaction/transaction_sample.rb, line 53
def set_custom_param(name, value)
  @params[:custom_params] ||= {}
  @params[:custom_params][name] = value
end
timestamp() click to toggle source

offset from start of app

# File lib/one_apm/transaction/transaction_sample.rb, line 49
def timestamp
  @start_time - @@start_time.to_f
end
to_array() click to toggle source
# File lib/one_apm/transaction/transaction_sample.rb, line 60
def to_array
  [ OneApm::Helper.time_to_millis(@start_time),
    @params[:request_params],
    @params[:custom_params],
    @root_segment.to_array]
end
to_collector_array(encoder) click to toggle source
# File lib/one_apm/transaction/transaction_sample.rb, line 67
def to_collector_array(encoder)
  # trace_tree = OneApm::Helper.obfuscator.obfuscate(OneApm::JSONWrapper.dump([self.to_array]))
  trace_tree = encoder.encode(self.to_array)
  trace_array = [ Helper.time_to_millis(@start_time),
    Helper.time_to_millis(duration),
    string(transaction_name),
    string(@params[:uri]),
    trace_tree,
    string(@guid),
    nil,
    forced?,
    trip_guid
  ] 
 return trace_array unless int_or_nil(xray_session_id)
 trace_array + [ int_or_nil(xray_session_id) ]
end
to_s() click to toggle source
# File lib/one_apm/transaction/transaction_sample.rb, line 138
def to_s
  s = "Transaction Sample collected at #{Time.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_compact() click to toggle source
# File lib/one_apm/transaction/transaction_sample.rb, line 128
def to_s_compact
  @root_segment.to_s_compact
end
transaction_name() click to toggle source
# File lib/one_apm/transaction/transaction_sample.rb, line 92
def transaction_name
  @params[:path]
end
transaction_name=(new_name) click to toggle source
# File lib/one_apm/transaction/transaction_sample.rb, line 96
def transaction_name=(new_name)
  @params[:path] = new_name
end
trip_guid() click to toggle source
# File lib/one_apm/transaction/transaction_sample.rb, line 84
def trip_guid
  string(params[:custom_params][:'bw.trip_id'] || @guid) rescue string(@guid)
end

Private Instance Methods

collect_explain_plans!() click to toggle source
# File lib/one_apm/transaction/transaction_sample.rb, line 187
def collect_explain_plans!
  return unless Agent::Database.should_collect_explain_plans?
  threshold = OneApm::Manager.config[:'transaction_tracer.explain_threshold']
  each_segment do |segment|
    if segment[:sql] && segment.duration > threshold
      segment[:explain_plan] = segment.explain_sql
    end
  end
end
prepare_sql_for_transmission!() click to toggle source
# File lib/one_apm/transaction/transaction_sample.rb, line 197
def prepare_sql_for_transmission!
  strategy = Agent::Database.record_sql_method
  each_segment do |segment|
    if segment[:sql]
      segment[:sql] = case strategy
      when :raw
        segment[:sql].to_s
      when :obfuscated
        Agent::Database.obfuscate_sql(segment[:sql]).to_s
      end
    end
  end
end
strip_sql!() click to toggle source
# File lib/one_apm/transaction/transaction_sample.rb, line 181
def strip_sql!
  each_segment do |segment|
    segment.params.delete(:sql)
  end
end