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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e00f8a3beffa73912bccaa929bbfd50fbfc89775
4
- data.tar.gz: 6f3bc08197a8975d66bf970a74769d7ba74d2506
3
+ metadata.gz: 778f409d79d4181a1b2db1212d74eb0c1e99e6e8
4
+ data.tar.gz: 46d73670e6b9dfc1d21ade794382a86f0506adc6
5
5
  SHA512:
6
- metadata.gz: d36ef682fb0278c9e8b1e54e051cf01df1f0c499a437889d204e339552aae977dd1398724f9dca174a98f830f0bc1ec3793f0be25d1f942e172fe7c3f090b7aa
7
- data.tar.gz: db4e5a11691f10d8bb9e123d59502de8bf85280b61809d660e54f632325678e23c49897b3bdebfa7dd6862f42f4bc17a73a5145f98903830fb0bae22a96cbc31
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
- DELETE_SQL_REGEX = /^DELETE.+FROM\s+"([^"]+)"/
9
- INSERT_SQL_REGEX = /^INSERT INTO\s+"([^"]+)"/
10
- SELECT_SQL_REGEX = /^SELECT.+FROM\s+"([^"]+)"/
11
- UPDATE_SQL_REGEX = /^UPDATE\s+"([^"]+)"/
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 = case payload[:sql]
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
@@ -1,3 +1,3 @@
1
1
  module NSA
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nsa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - BJ Neilsen