Class: NewRelic::Agent::SqlSampler
- Inherits:
-
Object
- Object
- NewRelic::Agent::SqlSampler
- Defined in:
- lib/new_relic/agent/sql_sampler.rb
Overview
This class contains the logic of recording slow SQL traces, which may represent multiple aggregated SQL queries.
A slow SQL trace consists of a collection of SQL instrumented SQL queries that all normalize to the same text. For example, the following two queries would be aggregated together into a single slow SQL trace:
SELECT * FROM table WHERE id=42
SELECT * FROM table WHERE id=1234
Each slow SQL trace keeps track of the number of times the same normalized query was seen, the min, max, and total time spent executing those queries, and an example backtrace from one of the aggregated queries.
Constant Summary collapse
- MAX_SAMPLES =
10- PRIORITY =
'priority'.freeze
Instance Attribute Summary collapse
- #disabled ⇒ Object readonly
-
#sql_traces ⇒ Object
readonly
this is for unit tests only.
Instance Method Summary collapse
- #distributed_trace_attributes(state) ⇒ Object
- #enabled? ⇒ Boolean
- #harvest! ⇒ Object
-
#has_room? ⇒ Boolean
this should always be called under the @samples_lock.
-
#initialize ⇒ SqlSampler
constructor
A new instance of SqlSampler.
- #merge!(sql_traces) ⇒ Object
- #notice_sql_statement(statement, metric_name, duration) ⇒ Object
-
#on_finishing_transaction(state, name) ⇒ Object
This is called when we are done with the transaction.
- #on_start_transaction(state, uri = nil) ⇒ Object
-
#remove_shortest_trace ⇒ Object
this should always be called under the @samples_lock.
- #reset! ⇒ Object
-
#save_slow_sql(transaction_sql_data) ⇒ Object
this should always be called under the @samples_lock.
-
#should_add_trace?(sql_item) ⇒ Boolean
this should always be called under the @samples_lock.
-
#tl_transaction_data ⇒ Object
only used for testing.
Constructor Details
#initialize ⇒ SqlSampler
Returns a new instance of SqlSampler.
32 33 34 35 36 37 38 39 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 32 def initialize @sql_traces = {} # This lock is used to synchronize access to @sql_traces # 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)
25 26 27 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 25 def disabled @disabled end |
#sql_traces ⇒ Object (readonly)
this is for unit tests only
28 29 30 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 28 def sql_traces @sql_traces end |
Instance Method Details
#distributed_trace_attributes(state) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 126 def distributed_trace_attributes(state) transaction = state.current_transaction params = nil if transaction&.distributed_tracer&.distributed_trace_payload params = {} payload = transaction.distributed_tracer.distributed_trace_payload DistributedTraceAttributes.copy_from_transaction(transaction, payload, params) params[PRIORITY] = transaction.priority end params end |
#enabled? ⇒ Boolean
41 42 43 44 45 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 41 def enabled? Agent.config[:'slow_sql.enabled'] && Agent.config[:'transaction_tracer.enabled'] && NewRelic::Agent::Database.should_record_sql?(:slow_sql) end |
#harvest! ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 165 def harvest! return NewRelic::EMPTY_ARRAY unless enabled? slowest = [] @samples_lock.synchronize do slowest = @sql_traces.values @sql_traces = {} end slowest.each { |trace| trace.prepare_to_send } slowest end |
#has_room? ⇒ Boolean
this should always be called under the @samples_lock
114 115 116 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 114 def has_room? @sql_traces.size < MAX_SAMPLES end |
#merge!(sql_traces) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 152 def merge!(sql_traces) @samples_lock.synchronize do sql_traces.each do |trace| existing_trace = @sql_traces[trace.sql] if existing_trace existing_trace.aggregate_trace(trace) else @sql_traces[trace.sql] = trace end end end end |
#notice_sql_statement(statement, metric_name, duration) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 138 def notice_sql_statement(statement, metric_name, duration) state ||= Tracer.state data = state.sql_sampler_transaction_data return unless data if state.is_sql_recorded? if duration > Agent.config[:'slow_sql.explain_threshold'] backtrace = caller.join("\n") params = distributed_trace_attributes(state) data.sql_data << SlowSql.new(statement, metric_name, duration, backtrace, params) end end end |
#on_finishing_transaction(state, name) ⇒ Object
This is called when we are done with the transaction.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 66 def on_finishing_transaction(state, name) return unless enabled? data = state.sql_sampler_transaction_data return unless data data.set_transaction_name(name) if data.sql_data.size > 0 @samples_lock.synchronize do ::NewRelic::Agent.logger.debug("Examining #{data.sql_data.size} slow transaction sql statement(s)") save_slow_sql(data) end end end |
#on_start_transaction(state, uri = nil) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 47 def on_start_transaction(state, uri = nil) return unless enabled? state.sql_sampler_transaction_data = TransactionSqlData.new if state.current_transaction guid = state.current_transaction.guid end if Agent.config[:'slow_sql.enabled'] && state.sql_sampler_transaction_data state.sql_sampler_transaction_data.set_transaction_info(uri, guid) end end |
#remove_shortest_trace ⇒ Object
this should always be called under the @samples_lock
119 120 121 122 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 119 def remove_shortest_trace shortest_key, _ = @sql_traces.min_by { |(_, trace)| trace.max_call_time } @sql_traces.delete(shortest_key) end |
#reset! ⇒ Object
177 178 179 180 181 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 177 def reset! @samples_lock.synchronize do @sql_traces = {} end end |
#save_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 96 97 98 99 100 101 102 103 104 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 82 def save_slow_sql(transaction_sql_data) path = transaction_sql_data.path uri = transaction_sql_data.uri 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, path, uri) else if has_room? sql_trace = SqlTrace.new(normalized_sql, sql_item, path, uri) elsif should_add_trace?(sql_item) remove_shortest_trace sql_trace = SqlTrace.new(normalized_sql, sql_item, path, uri) end if sql_trace @sql_traces[normalized_sql] = sql_trace end end end end |
#should_add_trace?(sql_item) ⇒ Boolean
this should always be called under the @samples_lock
107 108 109 110 111 |
# File 'lib/new_relic/agent/sql_sampler.rb', line 107 def should_add_trace?(sql_item) @sql_traces.any? do |(_, existing_trace)| existing_trace.max_call_time < sql_item.duration end end |