Class: NewRelic::Agent::SqlSampler

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/sql_sampler.rb

Defined Under Namespace

Modules: Shim

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSqlSampler

Returns a new instance of SqlSampler.



25
26
27
28
29
30
31
32
33
# File 'lib/new_relic/agent/sql_sampler.rb', line 25

def initialize
  @sql_traces = {}
  clear_transaction_data

  # This lock is used to synchronize access to the @last_sample
  # and related variables. It can become necessary on JRuby or
  # any 'honest-to-god'-multithreaded system
  @samples_lock = Mutex.new
end

Instance Attribute Details

#disabledObject (readonly)

Returns the value of attribute disabled.



20
21
22
# File 'lib/new_relic/agent/sql_sampler.rb', line 20

def disabled
  @disabled
end

#sql_tracesObject (readonly)

this is for unit tests only



23
24
25
# File 'lib/new_relic/agent/sql_sampler.rb', line 23

def sql_traces
  @sql_traces
end

Instance Method Details

#clear_transaction_dataObject



63
64
65
# File 'lib/new_relic/agent/sql_sampler.rb', line 63

def clear_transaction_data
  Thread.current[:new_relic_sql_data] = nil
end

#create_transaction_dataObject



55
56
57
# File 'lib/new_relic/agent/sql_sampler.rb', line 55

def create_transaction_data
  Thread.current[:new_relic_sql_data] = TransactionSqlData.new
end

#enabled?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/new_relic/agent/sql_sampler.rb', line 35

def enabled?
  Agent.config[:'slow_sql.enabled'] &&
    (Agent.config[:'slow_sql.record_sql'] == 'raw' ||
     Agent.config[:'slow_sql.record_sql'] == 'obfuscated') &&
    Agent.config[:'transaction_tracer.enabled']
end

#harvestObject



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/new_relic/agent/sql_sampler.rb', line 115

def harvest
  return [] if !Agent.config[:'slow_sql.enabled']
  result = []
  @samples_lock.synchronize do
    result = @sql_traces.values
    @sql_traces = {}
  end
  slowest = result.sort{|a,b| b.max_call_time <=> a.max_call_time}[0,10]
  slowest.each {|trace| trace.prepare_to_send }
  slowest
end

#harvest_slow_sql(transaction_sql_data) ⇒ Object

this should always be called under the @samples_lock



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/new_relic/agent/sql_sampler.rb', line 82

def harvest_slow_sql(transaction_sql_data)
  transaction_sql_data.sql_data.each do |sql_item|
    normalized_sql = sql_item.normalize
    sql_trace = @sql_traces[normalized_sql]
    if sql_trace
      sql_trace.aggregate(sql_item, transaction_sql_data.path,
                          transaction_sql_data.uri)
    else
      @sql_traces[normalized_sql] = SqlTrace.new(normalized_sql,
          sql_item, transaction_sql_data.path, transaction_sql_data.uri)
    end
  end

end

#merge(sql_traces) ⇒ Object



108
109
110
111
112
113
# File 'lib/new_relic/agent/sql_sampler.rb', line 108

def merge(sql_traces)
  @samples_lock.synchronize do
#FIXME we need to merge the sql_traces array back into the @sql_traces hash
#          @sql_traces.merge! sql_traces
  end
end

#notice_first_scope_push(time) ⇒ Object



51
52
53
# File 'lib/new_relic/agent/sql_sampler.rb', line 51

def notice_first_scope_push(time)
  create_transaction_data
end

#notice_scope_empty(time = Time.now) ⇒ Object

This is called when we are done with the transaction.



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/new_relic/agent/sql_sampler.rb', line 68

def notice_scope_empty(time=Time.now)
  data = transaction_data
  clear_transaction_data

  if data.sql_data.size > 0
    @samples_lock.synchronize do
      NewRelic::Agent.instance.log.debug "Harvesting #{data.sql_data.size} slow transaction sql statement(s)"
      #FIXME get tx name and uri
      harvest_slow_sql data
    end
  end
end

#notice_sql(sql, metric_name, config, duration) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/new_relic/agent/sql_sampler.rb', line 97

def notice_sql(sql, metric_name, config, duration)
  return unless transaction_data
  if NewRelic::Agent.is_sql_recorded?
    if duration > Agent.config[:'slow_sql.explain_threshold']
      backtrace = caller.join("\n")
      transaction_data.sql_data << SlowSql.new(sql, metric_name, config,
                                               duration, backtrace)
    end
  end
end

#notice_transaction(path, uri = nil, params = {}) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/new_relic/agent/sql_sampler.rb', line 42

def notice_transaction(path, uri=nil, params={})
  if NewRelic::Agent.instance.transaction_sampler.builder
    guid = NewRelic::Agent.instance.transaction_sampler.builder.sample.guid
  end
  if Agent.config[:'slow_sql.enabled'] && transaction_data
    transaction_data.set_transaction_info(path, uri, params, guid)
  end
end

#reset!Object

reset samples without rebooting the web server



128
129
# File 'lib/new_relic/agent/sql_sampler.rb', line 128

def reset!
end

#transaction_dataObject



59
60
61
# File 'lib/new_relic/agent/sql_sampler.rb', line 59

def transaction_data
  Thread.current[:new_relic_sql_data]
end