query_track 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5c122b692e3d0759318d5b4dc02d57a4d9b97435916c1fb4bfe19db40033bb22
4
+ data.tar.gz: 72cd1a23053496a135b6a1414e92409a1ad581bad29fb06c2c4429cec4baf3d2
5
+ SHA512:
6
+ metadata.gz: a98d3ea20b02490fa1b0da6bc1b41db364c7889f6e79191570af221ac2e4ff9fcf20228de2dc9f5e8e4d11cd96881271962c7a8feaadc522f1fcdc8b0ecad381
7
+ data.tar.gz: 463aa697653af58b1a0272699f1cdfd71f1d12543fe38dbd7129f7a0ccaa85406af301f792d1568ae95351333ecbe0dc5ecd289739cc0f6badfd18964389f8f1
@@ -0,0 +1,25 @@
1
+ version: 2
2
+ jobs:
3
+ build:
4
+ docker:
5
+ - image: circleci/ruby:2.5-node
6
+
7
+ steps:
8
+ - checkout
9
+
10
+ - restore_cache:
11
+ keys:
12
+ - query-track-bundle-v2-{{ checksum "query_track.gemspec" }}
13
+
14
+ - run:
15
+ name: Bundle Install
16
+ command: bundle check || bundle install
17
+
18
+ - save_cache:
19
+ key: query-track-bundle-v2-{{ checksum "query_track.gemspec" }}
20
+ paths:
21
+ - vendor/bundle
22
+
23
+ - run:
24
+ name: Run Rspec
25
+ command: bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,21 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ DisplayStyleGuide: true
4
+ StyleGuideCopsOnly: true
5
+ TargetRubyVersion: 2.5
6
+
7
+ Metrics/LineLength:
8
+ Max: 120
9
+
10
+ Documentation:
11
+ Enabled: false
12
+
13
+ FrozenStringLiteralComment:
14
+ Enabled: false
15
+
16
+ Style/FileName:
17
+ Enabled: false
18
+
19
+ Metrics/ModuleLength:
20
+ Exclude:
21
+ - spec/**/*_spec.rb
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # v0.0.1 2019-07-20
2
+
3
+ Initial release with event processing, slack notifications and duration limit. ([kirillshevch](https://github.com/kirillshevch/query_track/pull/1))
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in query_track.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,74 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ query_track (0.0.1)
5
+ activesupport
6
+ dry-configurable
7
+ slack_hook
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (5.2.3)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 0.7, < 2)
15
+ minitest (~> 5.1)
16
+ tzinfo (~> 1.1)
17
+ ast (2.4.0)
18
+ byebug (11.0.1)
19
+ concurrent-ruby (1.1.5)
20
+ diff-lcs (1.3)
21
+ dry-configurable (0.8.3)
22
+ concurrent-ruby (~> 1.0)
23
+ dry-core (~> 0.4, >= 0.4.7)
24
+ dry-core (0.4.8)
25
+ concurrent-ruby (~> 1.0)
26
+ i18n (1.6.0)
27
+ concurrent-ruby (~> 1.0)
28
+ jaro_winkler (1.5.2)
29
+ minitest (5.11.3)
30
+ parallel (1.17.0)
31
+ parser (2.6.3.0)
32
+ ast (~> 2.4.0)
33
+ rainbow (3.0.0)
34
+ rake (12.3.2)
35
+ rspec (3.8.0)
36
+ rspec-core (~> 3.8.0)
37
+ rspec-expectations (~> 3.8.0)
38
+ rspec-mocks (~> 3.8.0)
39
+ rspec-core (3.8.0)
40
+ rspec-support (~> 3.8.0)
41
+ rspec-expectations (3.8.4)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.8.0)
44
+ rspec-mocks (3.8.0)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.8.0)
47
+ rspec-support (3.8.2)
48
+ rubocop (0.71.0)
49
+ jaro_winkler (~> 1.5.1)
50
+ parallel (~> 1.10)
51
+ parser (>= 2.6)
52
+ rainbow (>= 2.2.2, < 4.0)
53
+ ruby-progressbar (~> 1.7)
54
+ unicode-display_width (>= 1.4.0, < 1.7)
55
+ ruby-progressbar (1.10.1)
56
+ slack_hook (0.1.2)
57
+ thread_safe (0.3.6)
58
+ tzinfo (1.2.5)
59
+ thread_safe (~> 0.1)
60
+ unicode-display_width (1.6.0)
61
+
62
+ PLATFORMS
63
+ ruby
64
+
65
+ DEPENDENCIES
66
+ bundler (~> 1.17)
67
+ byebug
68
+ query_track!
69
+ rake
70
+ rspec
71
+ rubocop
72
+
73
+ BUNDLED WITH
74
+ 1.17.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Kirill Shevchenko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # QueryTrack
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/query_track.svg)](https://badge.fury.io/rb/query_track)
4
+ [![CircleCI](https://circleci.com/gh/kirillshevch/query_track.svg?style=svg)](https://circleci.com/gh/kirillshevch/query_track)
5
+
6
+ Tool for finding time-consuming database queries for ActiveRecord-based Rails Apps. Provides Slack notifications for with backtrace, raw SQL, time duration, etc.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'query_track'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install query_track
23
+
24
+ ## Usage
25
+
26
+ Specify SQL duration query limit:
27
+
28
+ ```ruby
29
+ QueryTrack::Settings.configure do |config|
30
+ config.duration = 0.5
31
+ end
32
+ ```
33
+
34
+ To receive notifications about slow queries into Slack, you need to install [incoming-webhooks](https://reflow-files.slack.com/apps/A0F7XDUAZ-incoming-webhooks) and put link into config file:
35
+
36
+ ```ruby
37
+ QueryTrack::Settings.configure do |config|
38
+ config.duration = 0.5
39
+ config.notifications.slack = 'https://hooks.slack.com/services/T0000000/B0000000/C0000000'
40
+ end
41
+ ```
42
+
43
+ # <img src='https://github.com/kirillshevch/query_track/blob/master/slack.jpg' alt='Incoming Hook Example' />
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kirillshevch/query_track.
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'query_track'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,17 @@
1
+ module QueryTrack
2
+ class EventProcessor
3
+ attr_reader :event
4
+
5
+ def initialize(event)
6
+ @event = event
7
+ end
8
+
9
+ def call
10
+ return unless QueryTrack::Settings.config.duration
11
+
12
+ if event.duration > QueryTrack::Settings.config.duration
13
+ QueryTrack::Notifications::Slack.new(event.payload[:sql], event.duration).call
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,58 @@
1
+ module QueryTrack
2
+ module Notifications
3
+ class Slack
4
+ attr_reader :code, :duration, :webhook_url
5
+
6
+ def initialize(code, duration)
7
+ @code = code.strip
8
+ @duration = duration
9
+ @webhook_url = QueryTrack::Settings.config.notifications.slack
10
+ end
11
+
12
+ def call
13
+ slack_hook = SlackHook::Incoming.new(webhook_url)
14
+
15
+ trace = caller.select { |v| v =~ %r{app/} }[0]
16
+
17
+ payload = { blocks: blocks(trace) }
18
+
19
+ slack_hook.post(payload)
20
+ end
21
+
22
+ private
23
+
24
+ def blocks(trace)
25
+ [
26
+ {
27
+ "type": 'divider'
28
+ },
29
+ {
30
+ "type": 'section',
31
+ "text": {
32
+ "type": 'mrkdwn',
33
+ "text": "```#{code}```"
34
+ }
35
+ },
36
+ {
37
+ "type": 'context',
38
+ "elements": [
39
+ {
40
+ "type": 'mrkdwn',
41
+ "text": "Duration: *#{duration}s* "
42
+ }
43
+ ]
44
+ },
45
+ {
46
+ "type": 'context',
47
+ "elements": [
48
+ {
49
+ "type": 'mrkdwn',
50
+ "text": "#{trace}"
51
+ }
52
+ ]
53
+ }
54
+ ]
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,11 @@
1
+ module QueryTrack
2
+ class Settings
3
+ extend ::Dry::Configurable
4
+
5
+ setting :duration
6
+
7
+ setting :notifications do
8
+ setting :slack, ''
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module QueryTrack
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,13 @@
1
+ require 'dry-configurable'
2
+ require 'active_support/notifications'
3
+ require 'slack_hook'
4
+ require 'query_track/version'
5
+ require 'query_track/settings'
6
+ require 'query_track/notifications/slack'
7
+ require 'query_track/event_processor'
8
+ require 'subscriber'
9
+
10
+ module QueryTrack
11
+ class Error < StandardError; end
12
+ # Your code goes here...
13
+ end
data/lib/subscriber.rb ADDED
@@ -0,0 +1,5 @@
1
+ ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
2
+ event = ActiveSupport::Notifications::Event.new(*args)
3
+
4
+ QueryTrack::EventProcessor.new(event).call
5
+ end
@@ -0,0 +1,34 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'query_track/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'query_track'
7
+ spec.version = QueryTrack::VERSION
8
+ spec.authors = ['Kirill Shevchenko']
9
+ spec.email = ['hello@kirillshevch.com']
10
+
11
+ spec.summary = 'Finding time-consuming database queries for ActiveRecord-based Rails Apps'
12
+ spec.description = spec.summary
13
+ spec.homepage = 'https://github.com/kirillshevch/query_track'
14
+ spec.license = 'MIT'
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'activesupport'
26
+ spec.add_dependency 'slack_hook'
27
+ spec.add_dependency 'dry-configurable'
28
+
29
+ spec.add_development_dependency 'bundler', '~> 1.17'
30
+ spec.add_development_dependency 'rubocop'
31
+ spec.add_development_dependency 'rake'
32
+ spec.add_development_dependency 'rspec'
33
+ spec.add_development_dependency 'byebug'
34
+ end
data/slack.jpg ADDED
Binary file
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: query_track
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kirill Shevchenko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-07-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: slack_hook
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: dry-configurable
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.17'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.17'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: byebug
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Finding time-consuming database queries for ActiveRecord-based Rails Apps
126
+ email:
127
+ - hello@kirillshevch.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".circleci/config.yml"
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".rubocop.yml"
136
+ - CHANGELOG.md
137
+ - Gemfile
138
+ - Gemfile.lock
139
+ - LICENSE.txt
140
+ - README.md
141
+ - Rakefile
142
+ - bin/console
143
+ - bin/setup
144
+ - lib/query_track.rb
145
+ - lib/query_track/event_processor.rb
146
+ - lib/query_track/notifications/slack.rb
147
+ - lib/query_track/settings.rb
148
+ - lib/query_track/version.rb
149
+ - lib/subscriber.rb
150
+ - query_track.gemspec
151
+ - slack.jpg
152
+ homepage: https://github.com/kirillshevch/query_track
153
+ licenses:
154
+ - MIT
155
+ metadata: {}
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubygems_version: 3.0.3
172
+ signing_key:
173
+ specification_version: 4
174
+ summary: Finding time-consuming database queries for ActiveRecord-based Rails Apps
175
+ test_files: []