Class: PgCanary::Detector

Inherits:
Object
  • Object
show all
Includes:
PgQuerySupport
Defined in:
lib/pg_canary/detector.rb

Overview

Parses an event's SQL into a QueryContext, runs every enabled rule against it, and filters ignored tables. Each returned Detection carries the query's fingerprint, so a consumer that sees the same query and rule repeatedly (e.g. an N+1 loop within one request) can collapse them — see Middleware.

Constant Summary

Constants included from PgQuerySupport

PgQuerySupport::COMPARISON_OPS

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Detector

Swallows the configuration whole at construction: pg_canary treats it as fixed after boot (set in an initializer).



16
17
18
19
20
# File 'lib/pg_canary/detector.rb', line 16

def initialize(config)
  @config = config
  @rules = Rules::Base.all.map(&:new).select { |rule| rule.enabled?(config) }
  @backtrace_cleaner = build_backtrace_cleaner(config.app_root)
end

Instance Method Details

#call(payload) ⇒ Object

=> [Detection] the detections that should be notified.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pg_canary/detector.rb', line 23

def call(payload)
  query = build_query(payload)
  return [] unless query

  location = nil
  @rules.flat_map do |rule|
    detections = check_rule(rule, query)
    next [] if detections.empty?

    location ||= source_location
    detections.each do |d|
      d.location = location
      d.fingerprint = query.fingerprint
    end
  end
end