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"
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) }
next if payload[:connection_id].to_s.include?("clickhouse")
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)
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
if rand < 0.1
LogShipper.instance.enqueue(
level: "debug",
message: "#{duration_ms.round}ms: #{sql.first(2000)}",
timestamp: timestamp
)
end
end
end
|