Class: NurseAndrea::QuerySubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/nurse_andrea/query_subscriber.rb

Constant Summary collapse

SKIP_PATTERN =
/\A(SHOW|SET|PRAGMA|SELECT.*schema_migrations|BEGIN|COMMIT|ROLLBACK|SAVEPOINT|RELEASE|SELECT version)/i
SKIP_CONNECTIONS =
%w[ClickhouseActiverecord Clickhouse].freeze
SLOW_THRESHOLD_MS =
100

Class Method Summary collapse

Class Method Details

.subscribe!Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/nurse_andrea/query_subscriber.rb', line 7

def self.subscribe!
  ActiveSupport::Notifications.subscribe("sql.active_record") do |name, start, finish, id, payload|
    next if payload[:name] == "SCHEMA"

    # Only instrument PostgreSQL queries — skip ClickHouse
    conn = payload[:connection]
    conn_name = conn.is_a?(Class) ? conn.name.to_s : conn.class.name.to_s
    next if SKIP_CONNECTIONS.any? { |c| conn_name.include?(c) }
    # Also skip by adapter name if available
    next if payload[:connection_id].to_s.include?("clickhouse")

    # Skip queries targeting ClickHouse tables
    sql_check = payload[:sql].to_s
    next if sql_check.match?(/\b(metric_points|log_entries|job_metrics|spans)\b/i)

    sql = payload[:sql].to_s.strip
    next if sql.empty?
    next if sql.match?(SKIP_PATTERN)

    duration_ms = ((finish - start) * 1000).round(2)
    timestamp   = finish.utc.iso8601(3)

    # Always ship slow queries
    if duration_ms >= SLOW_THRESHOLD_MS
      LogShipper.instance.enqueue(
        level:     "warn",
        message:   "SLOW QUERY took #{duration_ms.round}ms: #{sql.first(2000)}",
        timestamp: timestamp
      )
    end

    # Sample 10% of all queries for frequency tracking
    if rand < 0.1
      LogShipper.instance.enqueue(
        level:     "debug",
        message:   "#{duration_ms.round}ms: #{sql.first(2000)}",
        timestamp: timestamp
      )
    end
  end
end