nsa 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/nsa/collectors/active_record.rb +20 -12
- data/lib/nsa/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 778f409d79d4181a1b2db1212d74eb0c1e99e6e8
|
4
|
+
data.tar.gz: 46d73670e6b9dfc1d21ade794382a86f0506adc6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e6555f13345e3fa8e85fcb04173b628a2c784212ea684b00f4d0cb8e6598cda64eb5fbc74b38834e995312b4a3dda5cb82b03edd2276586726d5c61c3e5993be
|
7
|
+
data.tar.gz: 2d211ffbae3cb6ec597381b118b264fe7c4c04e23d2431fec1e76e9f06778fab61b4c33928c69841326d64ffb72c627e6cbe1be476ce4f9a0356093c04fbfff3
|
@@ -5,29 +5,37 @@ module NSA
|
|
5
5
|
module ActiveRecord
|
6
6
|
extend ::NSA::Statsd::Publisher
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
# Ordered by most common query type
|
9
|
+
MATCHERS = [
|
10
|
+
[ :select, /^\s*SELECT.+?FROM\s+"?([^".\s),]+)"?/im ],
|
11
|
+
[ :insert, /^\s*INSERT INTO\s+"?([^".\s]+)"?/im ],
|
12
|
+
[ :update, /^\s*UPDATE\s+"?([^".\s]+)"?/im ],
|
13
|
+
[ :delete, /^\s*DELETE.+FROM\s+"?([^".\s]+)"?/im ]
|
14
|
+
].freeze
|
15
|
+
|
16
|
+
EMPTY_MATCH_RESULT = []
|
12
17
|
|
13
18
|
def self.collect(key_prefix)
|
14
19
|
::ActiveSupport::Notifications.subscribe("sql.active_record") do |_, start, finish, _id, payload|
|
15
|
-
query_type, table_name =
|
16
|
-
when DELETE_SQL_REGEX then [ :delete, $1 ]
|
17
|
-
when INSERT_SQL_REGEX then [ :insert, $1 ]
|
18
|
-
when SELECT_SQL_REGEX then [ :select, $1 ]
|
19
|
-
when UPDATE_SQL_REGEX then [ :update, $1 ]
|
20
|
-
else nil
|
21
|
-
end
|
22
|
-
|
20
|
+
query_type, table_name = match_query(payload[:sql])
|
23
21
|
unless query_type.nil?
|
24
22
|
stat_name = "#{key_prefix}.tables.#{table_name}.queries.#{query_type}.duration"
|
25
23
|
duration_ms = (finish - start) * 1000
|
26
24
|
statsd_timing(stat_name, duration_ms)
|
27
25
|
end
|
28
26
|
end
|
27
|
+
end
|
29
28
|
|
29
|
+
def self.match_query(sql)
|
30
|
+
MATCHERS
|
31
|
+
.lazy
|
32
|
+
.map { |(type, regex)|
|
33
|
+
match = (sql.match(regex) || EMPTY_MATCH_RESULT)
|
34
|
+
[ type, match[1] ]
|
35
|
+
}
|
36
|
+
.detect { |(_, table_name)| ! table_name.nil? }
|
30
37
|
end
|
38
|
+
|
31
39
|
end
|
32
40
|
end
|
33
41
|
end
|
data/lib/nsa/version.rb
CHANGED