Module: Perfgate::Instrumentation::SqlActivity
- Defined in:
- lib/perfgate/instrumentation/sql_activity.rb
Overview
SQL query count and cumulative duration during a Perfgate.measure
block (spec sections 13.2-13.3), collected via an
ActiveSupport::Notifications subscription scoped to exactly that
block. Schema queries, transaction control statements, and cached
query hits are excluded as configurable noise (13.2); raw SQL text
is never stored, only counts and a cumulative duration.
Constant Summary collapse
- NOISE_EVENT_NAMES =
%w[SCHEMA TRANSACTION].freeze
Class Method Summary collapse
- .ensure_active_support! ⇒ Object
- .finish(started) ⇒ Object
- .noise?(payload) ⇒ Boolean
- .record(state, event) ⇒ Object
- .start ⇒ Object
Class Method Details
.ensure_active_support! ⇒ Object
42 43 44 45 46 47 |
# File 'lib/perfgate/instrumentation/sql_activity.rb', line 42 def ensure_active_support! return if defined?(::ActiveSupport::Notifications) raise Perfgate::Error, "sql_count/sql_duration instrumentation requires ActiveSupport::Notifications to be loaded" end |
.finish(started) ⇒ Object
27 28 29 30 31 |
# File 'lib/perfgate/instrumentation/sql_activity.rb', line 27 def finish(started) ::ActiveSupport::Notifications.unsubscribe(started.fetch(:subscriber)) state = started.fetch(:state) { "sql_count" => state[:count], "sql_duration_ns" => state[:duration_ns] } end |
.noise?(payload) ⇒ Boolean
38 39 40 |
# File 'lib/perfgate/instrumentation/sql_activity.rb', line 38 def noise?(payload) NOISE_EVENT_NAMES.include?(payload[:name].to_s) || payload[:cached] end |
.record(state, event) ⇒ Object
33 34 35 36 |
# File 'lib/perfgate/instrumentation/sql_activity.rb', line 33 def record(state, event) state[:count] += 1 state[:duration_ns] += (event.duration * 1_000_000).round end |
.start ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/perfgate/instrumentation/sql_activity.rb', line 16 def start ensure_active_support! state = { count: 0, duration_ns: 0 } subscriber = ::ActiveSupport::Notifications.subscribe("sql.active_record") do |*args| event = ::ActiveSupport::Notifications::Event.new(*args) record(state, event) unless noise?(event.payload) end { subscriber: subscriber, state: state } end |