Class: PgCanary::Subscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_canary/subscriber.rb

Overview

Subscribes to sql.active_record, filters events down to likely SELECT statements, and hands them to the Detector. Stateless per event, so everything lives at class level.

Constant Summary collapse

IGNORED_NAMES =

Event names that never carry application SELECTs. "SCHEMA" also covers the catalog queries ActiveRecord's schema cache runs on our behalf.

%w[SCHEMA TRANSACTION].freeze
SELECT_PREFIX =

Cheap pre-filter before paying for a real parse.

%r{\A\s*(?:/\*.*?\*/\s*)*(?:SELECT|WITH)\b}im

Class Method Summary collapse

Class Method Details

.subscribe!Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/pg_canary/subscriber.rb', line 15

def subscribe!
  @subscribe ||= ActiveSupport::Notifications.subscribe("sql.active_record") do |*, payload|
    next unless analysis_target?(payload)

    detections = detector.call(payload)
    Middleware.collect(detections) if detections.any?
  rescue StandardError => e
    PgCanary.internal_error(e)
  end
end