ganeshan 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 376a01f404d1f85b76e56c668faabc6a6fa67b6b4f16535bf00fa90c33534800
4
+ data.tar.gz: 26eaf6346c7a3dacbf917a495cfd0193d2c3ff3488345ba37eb63243d540e665
5
+ SHA512:
6
+ metadata.gz: c709b5a4b2db67451e36ed5a87b4405fea582b5d3016bb56a3a5dffb63b760568cefedd59652cecc55dd1c38ca6efbbf5bc82f424aedade75f96c566dec855dd
7
+ data.tar.gz: 5d5b3b3f970b488a81d18ea8255c83dfce902b959883a2c8ec58c53befd24c18f954ec53d796fc8dd95fa70885c0931a07af90b1adc30338b5d98666a808f460
Binary file
@@ -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
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at mkawasaki@sbihg.co.jp. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ganesh.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,58 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ganeshan (0.1.0)
5
+ activerecord
6
+ coderay
7
+ pg
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activemodel (6.0.3.2)
13
+ activesupport (= 6.0.3.2)
14
+ activerecord (6.0.3.2)
15
+ activemodel (= 6.0.3.2)
16
+ activesupport (= 6.0.3.2)
17
+ activesupport (6.0.3.2)
18
+ concurrent-ruby (~> 1.0, >= 1.0.2)
19
+ i18n (>= 0.7, < 2)
20
+ minitest (~> 5.1)
21
+ tzinfo (~> 1.1)
22
+ zeitwerk (~> 2.2, >= 2.2.2)
23
+ coderay (1.1.3)
24
+ concurrent-ruby (1.1.7)
25
+ diff-lcs (1.4.4)
26
+ i18n (1.8.5)
27
+ concurrent-ruby (~> 1.0)
28
+ minitest (5.14.1)
29
+ pg (1.2.3)
30
+ rake (12.3.3)
31
+ rspec (3.9.0)
32
+ rspec-core (~> 3.9.0)
33
+ rspec-expectations (~> 3.9.0)
34
+ rspec-mocks (~> 3.9.0)
35
+ rspec-core (3.9.2)
36
+ rspec-support (~> 3.9.3)
37
+ rspec-expectations (3.9.2)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.9.0)
40
+ rspec-mocks (3.9.1)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.9.0)
43
+ rspec-support (3.9.3)
44
+ thread_safe (0.3.6)
45
+ tzinfo (1.2.7)
46
+ thread_safe (~> 0.1)
47
+ zeitwerk (2.4.0)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ ganeshan!
54
+ rake (~> 12.0)
55
+ rspec (~> 3.0)
56
+
57
+ BUNDLED WITH
58
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Masumi Kawasaki
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.
@@ -0,0 +1,152 @@
1
+ # Ganeshan
2
+
3
+ Ganeshan is a gem to find slow query with EXPAIN.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ganeshan'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ganeshan
20
+
21
+ ## Usage
22
+
23
+ You can enable it with config/environments/development.rb
24
+ ```rb
25
+ Ganeshan.enabled = true
26
+ ```
27
+
28
+ Or You can use it without rails, but with active_record.
29
+
30
+ ```rb
31
+ #!/usr/bin/env ruby
32
+
33
+ require "bundler/inline"
34
+
35
+ gemfile(true) do
36
+ source "https://rubygems.org"
37
+ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
38
+
39
+ gem "ganeshan"
40
+ end
41
+
42
+
43
+ require 'active_record'
44
+ require 'ganeshan'
45
+
46
+ ActiveRecord::Base.establish_connection(
47
+ host: '127.0.0.1',
48
+ adapter: 'postgresql',
49
+ username: 'hoge',
50
+ password: 'password',
51
+ database: 'ganeshan'
52
+ )
53
+
54
+ Ganeshan.enabled = true
55
+
56
+ ActiveRecord::Schema.define do
57
+ create_table :products, force: true do |t|
58
+ t.text :name
59
+ end
60
+ end
61
+
62
+ class Product < ActiveRecord::Base
63
+ end
64
+
65
+ p Product.all.to_a.count
66
+ p Product.all
67
+ p Product.limit(1)
68
+ p Product.where(id: 1).limit(10)
69
+ ```
70
+
71
+ then, output is like this.
72
+
73
+ ```rb
74
+ Ganeshan INFO 2020-08-14 02:30:34 +0900 {
75
+ "sql": "SELECT \"products\".* FROM \"products\"",
76
+ "explain": [
77
+ {
78
+ "line": 1,
79
+ "QUERY PLAN": "Seq Scan on products (cost=0.00..22.00 rows=1200 width=40)"
80
+ }
81
+ ]
82
+ }
83
+ 2
84
+
85
+ Ganeshan INFO 2020-08-14 02:30:34 +0900 {
86
+ "sql": "SELECT \"products\".* FROM \"products\" LIMIT $1",
87
+ "explain": [
88
+ {
89
+ "line": 1,
90
+ "QUERY PLAN": "Limit (cost=0.00..0.20 rows=11 width=40)"
91
+ },
92
+ {
93
+ "line": 2,
94
+ "QUERY PLAN": " -> Seq Scan on products (cost=0.00..22.00 rows=1200 width=40)"
95
+ }
96
+ ]
97
+ }
98
+ #<ActiveRecord::Relation [#<Product id: 2, name: "test2">, #<Product id: 1, name: "test1">]>
99
+
100
+ Ganeshan INFO 2020-08-14 02:30:34 +0900 {
101
+ "sql": "SELECT \"products\".* FROM \"products\" LIMIT $1",
102
+ "explain": [
103
+ {
104
+ "line": 1,
105
+ "QUERY PLAN": "Limit (cost=0.00..0.02 rows=1 width=40)"
106
+ },
107
+ {
108
+ "line": 2,
109
+ "QUERY PLAN": " -> Seq Scan on products (cost=0.00..22.00 rows=1200 width=40)"
110
+ }
111
+ ]
112
+ }
113
+ #<ActiveRecord::Relation [#<Product id: 2, name: "test2">]>
114
+
115
+ Ganeshan INFO 2020-08-14 02:30:34 +0900 {
116
+ "sql": "SELECT \"products\".* FROM \"products\" WHERE \"products\".\"id\" = $1 LIMIT $2",
117
+ "explain": [
118
+ {
119
+ "line": 1,
120
+ "QUERY PLAN": "Limit (cost=0.15..8.17 rows=1 width=40)"
121
+ },
122
+ {
123
+ "line": 2,
124
+ "QUERY PLAN": " -> Index Scan using products_pkey on products (cost=0.15..8.17 rows=1 width=40)"
125
+ },
126
+ {
127
+ "line": 3,
128
+ "QUERY PLAN": " Index Cond: (id = '1'::bigint)"
129
+ }
130
+ ]
131
+ }
132
+ #<ActiveRecord::Relation [#<Product id: 1, name: "test1">]>
133
+
134
+ ```
135
+ ## Development
136
+
137
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
138
+
139
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
140
+
141
+ ## Contributing
142
+
143
+ Bug reports and pull requests are welcome on GitHub at https://github.com/geeknees/ganeshan. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/geeknees/ganeshan/blob/master/CODE_OF_CONDUCT.md).
144
+
145
+
146
+ ## License
147
+
148
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
149
+
150
+ ## Code of Conduct
151
+
152
+ Everyone interacting in the Ganeshan project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/geeknees/ganeshan/blob/master/CODE_OF_CONDUCT.md).
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ganesh"
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__)
@@ -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,31 @@
1
+ require_relative 'lib/ganeshan/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "ganeshan"
5
+ spec.version = Ganeshan::VERSION
6
+ spec.authors = ["Masumi Kawasaki"]
7
+ spec.email = ["mkawasaki@sbihg.co.jp"]
8
+
9
+ spec.summary = %q{show EXPLAIN log with postgresql.}
10
+ spec.description = %q{show EXPLAIN log with postgresql to help in detecting slow queries.}
11
+ spec.homepage = "https://github.com/geeknees/ganeshan"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/geeknees/ganeshan"
17
+ spec.metadata["changelog_uri"] = "https://github.com/geeknees/ganeshan"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency 'activerecord'
29
+ spec.add_dependency 'coderay'
30
+ spec.add_dependency 'pg'
31
+ end
Binary file
@@ -0,0 +1,15 @@
1
+ require 'logger'
2
+ require 'active_support'
3
+ require 'coderay'
4
+ require 'pg'
5
+
6
+ require 'ganeshan/config'
7
+ require 'ganeshan/error'
8
+ require 'ganeshan/explainer'
9
+ require 'ganeshan/json_logger'
10
+ require 'ganeshan/version'
11
+
12
+ ActiveSupport.on_load :active_record do
13
+ require 'active_record/connection_adapters/postgresql_adapter'
14
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend Ganeshan::Explainer
15
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ganeshan
4
+ @enabled = (/\A(1|true)\z/i =~ ENV['GANESH_ENABLED'])
5
+
6
+ @logger = Logger.new(ENV['GANESH_LOG'] || $stdout).tap do |logger|
7
+ logger.formatter = lambda do |severity, datetime, _progname, msg|
8
+ "\n#{self}\t#{severity}\t#{datetime}\t#{msg}\n"
9
+ end
10
+ end
11
+
12
+ class << self
13
+ attr_accessor :enabled,
14
+ :logger,
15
+ :connection
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ module Ganeshan
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,45 @@
1
+ module Ganeshan
2
+ module Explainer
3
+ def exec_query(*args)
4
+ p args
5
+ _with_explain(sql: args.first, binds: args[2]) do
6
+ super
7
+ end
8
+ end
9
+
10
+ private
11
+
12
+ def _with_explain(sql:, binds:)
13
+ begin
14
+ if Ganeshan.enabled && sql =~ /\A\s*SELECT\b/i
15
+ conn = Ganeshan.connection || raw_connection
16
+
17
+ type_casted_binds = if binds
18
+ type_casted_binds(binds)
19
+ else
20
+ []
21
+ end
22
+
23
+ if type_casted_binds.empty?
24
+ exp = conn.query("EXPLAIN #{sql}").to_a
25
+ else
26
+ exp = conn.query("EXPLAIN #{sql}", type_casted_binds).to_a
27
+ end
28
+
29
+ _validate_explain(sql: sql, exp: exp)
30
+ end
31
+ rescue StandardError => e
32
+ Ganeshan.logger.error("#{e}\n\t#{e.backtrace.join("\n\t")}")
33
+ end
34
+
35
+ yield
36
+ end
37
+
38
+ def _validate_explain(sql:, exp:)
39
+ Ganeshan::JsonLogger.log(
40
+ sql: sql,
41
+ explain: exp,
42
+ )
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,15 @@
1
+ module Ganeshan
2
+ module JsonLogger
3
+ module_function
4
+
5
+ def log(sql:, explain:)
6
+ h = {
7
+ sql: sql,
8
+ explain: explain.each_with_index.map { |r, i| { line: i + 1 }.merge(r) },
9
+ }
10
+
11
+ line = CodeRay.scan(JSON.pretty_generate(h), :json).terminal
12
+ Ganeshan.logger.info(line)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Ganeshan
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ganeshan
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Masumi Kawasaki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-08-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
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: coderay
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: pg
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
+ description: show EXPLAIN log with postgresql to help in detecting slow queries.
56
+ email:
57
+ - mkawasaki@sbihg.co.jp
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".DS_Store"
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - ganeshan.gemspec
75
+ - lib/.DS_Store
76
+ - lib/ganeshan.rb
77
+ - lib/ganeshan/config.rb
78
+ - lib/ganeshan/error.rb
79
+ - lib/ganeshan/explainer.rb
80
+ - lib/ganeshan/json_logger.rb
81
+ - lib/ganeshan/version.rb
82
+ homepage: https://github.com/geeknees/ganeshan
83
+ licenses:
84
+ - MIT
85
+ metadata:
86
+ homepage_uri: https://github.com/geeknees/ganeshan
87
+ source_code_uri: https://github.com/geeknees/ganeshan
88
+ changelog_uri: https://github.com/geeknees/ganeshan
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: 2.3.0
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubygems_version: 3.1.2
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: show EXPLAIN log with postgresql.
108
+ test_files: []