blob: 87e76257c246488c67af9aaba987326ff2d9d9f0 (
plain)
1
2
3
4
5
6
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# frozen_string_literal: true
module Gitlab
module Database
module Transaction
class Observer
INSTRUMENTED_STATEMENTS = %w[BEGIN SAVEPOINT ROLLBACK RELEASE].freeze
LONGEST_COMMAND_LENGTH = 'ROLLBACK TO SAVEPOINT'.length
START_COMMENT = '/*'
END_COMMENT = '*/'
def self.instrument_transactions(cmd, event)
connection = event.payload[:connection]
manager = connection&.transaction_manager
return unless manager.respond_to?(:transaction_context)
context = manager.transaction_context
return if context.nil?
if cmd.start_with?('BEGIN')
context.set_start_time
context.set_depth(0)
context.track_sql(event.payload[:sql])
context.initialize_external_http_tracking
elsif cmd.start_with?('SAVEPOINT', 'EXCEPTION')
context.set_depth(manager.open_transactions)
context.increment_savepoints
context.track_backtrace(caller)
elsif cmd.start_with?('ROLLBACK TO SAVEPOINT')
context.increment_rollbacks
elsif cmd.start_with?('RELEASE SAVEPOINT ')
context.increment_releases
end
end
def self.register!
ActiveSupport::Notifications.subscribe('sql.active_record') do |event|
sql = event.payload.dig(:sql).to_s
cmd = extract_sql_command(sql)
if cmd.start_with?(*INSTRUMENTED_STATEMENTS)
self.instrument_transactions(cmd, event)
end
end
end
def self.extract_sql_command(sql)
return sql unless sql.start_with?(START_COMMENT)
index = sql.index(END_COMMENT)
return sql unless index
# /* comment */ SELECT
#
# We offset using a position of the end comment + 1 character to
# accomodate a space between Marginalia comment and a SQL statement.
offset = index + END_COMMENT.length + 1
# Avoid duplicating the entire string. This isn't optimized to
# strip extra spaces, but we assume that this doesn't happen
# for performance reasons.
sql[offset..offset + LONGEST_COMMAND_LENGTH]
end
end
end
end
end
|