Class: NewRelic::Agent::SqlSampler
- Inherits:
-
Object
- Object
- NewRelic::Agent::SqlSampler
- Defined in:
- lib/new_relic/agent/sql_sampler.rb
Defined Under Namespace
Modules: Shim
Instance Attribute Summary collapse
-
#disabled ⇒ Object
readonly
Returns the value of attribute disabled.
-
#sql_traces ⇒ Object
readonly
this is for unit tests only.
Instance Method Summary collapse
- #clear_transaction_data ⇒ Object
- #create_transaction_data ⇒ Object
- #enabled? ⇒ Boolean
- #harvest ⇒ Object
-
#harvest_slow_sql(transaction_sql_data) ⇒ Object
this should always be called under the @samples_lock.
-
#initialize ⇒ SqlSampler
constructor
A new instance of SqlSampler.
- #merge(sql_traces) ⇒ Object
- #notice_first_scope_push(time) ⇒ Object
-
#notice_scope_empty(time = Time.now) ⇒ Object
This is called when we are done with the transaction.
- #notice_sql(sql, metric_name, config, duration) ⇒ Object
- #notice_transaction(path, uri = nil, params = {}) ⇒ Object
-
#reset! ⇒ Object
reset samples without rebooting the web server.
- #transaction_data ⇒ Object
Constructor Details
#initialize ⇒ SqlSampler
Returns a new instance of SqlSampler.
29 30 31 32 33 34 35 36 37 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 29 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
#disabled ⇒ Object (readonly)
Returns the value of attribute disabled.
24 25 26 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 24 def disabled @disabled end |
#sql_traces ⇒ Object (readonly)
this is for unit tests only
27 28 29 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 27 def sql_traces @sql_traces end |
Instance Method Details
#clear_transaction_data ⇒ Object
67 68 69 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 67 def clear_transaction_data Thread.current[:new_relic_sql_data] = nil end |
#create_transaction_data ⇒ Object
59 60 61 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 59 def create_transaction_data Thread.current[:new_relic_sql_data] = TransactionSqlData.new end |
#enabled? ⇒ Boolean
39 40 41 42 43 44 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 39 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 |
#harvest ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 120 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
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 86 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
113 114 115 116 117 118 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 113 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
55 56 57 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 55 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.
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 72 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.logger.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
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 101 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(TransactionSampler.(sql), metric_name, config, duration, backtrace) end end end |
#notice_transaction(path, uri = nil, params = {}) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 46 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
133 134 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 133 def reset! end |
#transaction_data ⇒ Object
63 64 65 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 63 def transaction_data Thread.current[:new_relic_sql_data] end |