pghero_logs 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +66 -0
- data/Rakefile +2 -0
- data/bin/pghero_logs +4 -0
- data/lib/pghero_logs.rb +102 -0
- data/lib/pghero_logs/version.rb +3 -0
- data/pghero_logs.gemspec +26 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 737e31fc4c3ab01eeb5ba8e881283a98518334bc
|
4
|
+
data.tar.gz: 66220410a913aa2788e9d6f25dcf19d73b81ed22
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2eeeb1110a97f6b176f1ad42f1a6372a311f0c77752a6d927e72df19fa560197652778f581d8ce2892a108c8328339228bc3e163fee25a043eea2750a73ddd60
|
7
|
+
data.tar.gz: 204f704794d2632e47f7fe89aea94a77685b014b9cb8d8b3d7422b6b9ec1ba7394cfcf9897ab35c6ee0efd8282ccfce372b71fc994cc1f1a778ec4076ec23ab3
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Andrew Kane
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# PgHero Logs
|
2
|
+
|
3
|
+
Slow query log parser for Postgres
|
4
|
+
|
5
|
+
```sh
|
6
|
+
Total Avg Count Query
|
7
|
+
(min) (ms)
|
8
|
+
20 4381 283 SELECT DISTINCT "orders"."id" AS t0_r0, "orders"
|
9
|
+
7 3574 120 SELECT "visits".* FROM "visits" WHERE ("visits".
|
10
|
+
4 12621 20 SELECT DISTINCT "order_deliveries"."id" AS t0_r0
|
11
|
+
```
|
12
|
+
|
13
|
+
## Install
|
14
|
+
|
15
|
+
```sh
|
16
|
+
gem install pghero_logs
|
17
|
+
```
|
18
|
+
|
19
|
+
It can take 10 minutes or more to compile the [query parser](https://pganalyze.com/blog/parse-postgresql-queries-in-ruby.html) :clock2:
|
20
|
+
|
21
|
+
Tell Postgres to log slow queries in `postgresql.conf`
|
22
|
+
|
23
|
+
```conf
|
24
|
+
log_min_duration_statement = 20 # ms
|
25
|
+
```
|
26
|
+
|
27
|
+
Analyze the logs
|
28
|
+
|
29
|
+
```sh
|
30
|
+
cat /usr/local/var/postgres/server.log | pghero_logs
|
31
|
+
```
|
32
|
+
|
33
|
+
## Amazon RDS
|
34
|
+
|
35
|
+
First, download the logs. Create an IAM user with the policy below
|
36
|
+
|
37
|
+
```sh
|
38
|
+
{
|
39
|
+
"Statement": [
|
40
|
+
{
|
41
|
+
"Sid": "Stmt1410669817271",
|
42
|
+
"Action": [
|
43
|
+
"rds:DescribeDBLogFiles",
|
44
|
+
"rds:DownloadDBLogFilePortion"
|
45
|
+
],
|
46
|
+
"Effect": "Allow",
|
47
|
+
"Resource": "*"
|
48
|
+
}
|
49
|
+
]
|
50
|
+
}
|
51
|
+
```
|
52
|
+
|
53
|
+
And run
|
54
|
+
|
55
|
+
```sh
|
56
|
+
export AWS_ACCESS_KEY_ID=test123
|
57
|
+
export AWS_SECRET_ACCESS_KEY=secret123
|
58
|
+
export AWS_DB_INSTANCE_IDENTIFIER=production
|
59
|
+
pghero_logs download
|
60
|
+
```
|
61
|
+
|
62
|
+
Once logs are downloaded, run
|
63
|
+
|
64
|
+
```sh
|
65
|
+
cat logs/postgresql.log.* | pghero_logs
|
66
|
+
```
|
data/Rakefile
ADDED
data/bin/pghero_logs
ADDED
data/lib/pghero_logs.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
require "pghero_logs/version"
|
2
|
+
require "pg_query"
|
3
|
+
require "aws-sdk"
|
4
|
+
|
5
|
+
module PgHeroLogs
|
6
|
+
class << self
|
7
|
+
REGEX = /duration: (\d+\.\d+) ms execute <unnamed>: (.+)?/i
|
8
|
+
|
9
|
+
def run(command)
|
10
|
+
case command
|
11
|
+
when nil
|
12
|
+
parse
|
13
|
+
when "download"
|
14
|
+
download
|
15
|
+
else
|
16
|
+
abort "Unknown command: #{command}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
|
22
|
+
def download
|
23
|
+
Dir.mkdir("logs") if !Dir.exists?("logs")
|
24
|
+
|
25
|
+
db_instance_identifier = ENV["AWS_DB_INSTANCE_IDENTIFIER"]
|
26
|
+
rds = AWS::RDS.new(access_key_id: ENV["AWS_ACCESS_KEY_ID"], secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"])
|
27
|
+
resp = rds.client.describe_db_log_files(db_instance_identifier: db_instance_identifier)
|
28
|
+
files = resp[:describe_db_log_files].map{|f| f[:log_file_name] }
|
29
|
+
files.each do |log_file_name|
|
30
|
+
local_file_name = log_file_name.sub("error", "logs")
|
31
|
+
if File.exists?(local_file_name)
|
32
|
+
puts "EXISTS #{local_file_name}"
|
33
|
+
else
|
34
|
+
data = ""
|
35
|
+
marker = nil
|
36
|
+
begin
|
37
|
+
options = {
|
38
|
+
db_instance_identifier: db_instance_identifier,
|
39
|
+
log_file_name: log_file_name
|
40
|
+
}
|
41
|
+
options.merge!(marker: marker) if marker
|
42
|
+
resp = rds.client.download_db_log_file_portion(options)
|
43
|
+
data << resp[:log_file_data].to_s
|
44
|
+
end while resp[:additional_data_pending] && (marker = resp[:marker])
|
45
|
+
File.open(local_file_name, "w") { |file| file.write(data) }
|
46
|
+
puts "DOWNLOADED #{local_file_name}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def parse
|
52
|
+
active_entry = ""
|
53
|
+
$stdin.each_line do |line|
|
54
|
+
if line.include?(": ")
|
55
|
+
if active_entry
|
56
|
+
parse_entry(active_entry)
|
57
|
+
end
|
58
|
+
active_entry = ""
|
59
|
+
end
|
60
|
+
active_entry << line
|
61
|
+
end
|
62
|
+
parse_entry(active_entry)
|
63
|
+
|
64
|
+
queries = self.queries.sort_by{|q, i| -i[:total_time] }[0...20]
|
65
|
+
|
66
|
+
puts "Slowest Queries\n\n"
|
67
|
+
puts "Total Avg Count Query"
|
68
|
+
puts "(min) (ms)"
|
69
|
+
queries.each do |query, info|
|
70
|
+
puts "%5d %5d %5d %s" % [info[:total_time] / 60000, info[:total_time] / info[:count], info[:count], query[0...60]]
|
71
|
+
end
|
72
|
+
|
73
|
+
puts "\nFull Queries\n\n"
|
74
|
+
queries.each_with_index do |(query, info), i|
|
75
|
+
puts "#{i + 1}. #{info[:sample]}"
|
76
|
+
puts
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def parse_entry(active_entry)
|
81
|
+
if (matches = active_entry.match(REGEX))
|
82
|
+
begin
|
83
|
+
query = PgQuery.normalize(squish(matches[2].gsub(/\/\*.+/, ""))).gsub(/\?(, \?)+/, "?")
|
84
|
+
queries[query][:count] += 1
|
85
|
+
queries[query][:total_time] += matches[1].to_f
|
86
|
+
queries[query][:sample] = squish(matches[2])
|
87
|
+
rescue PgQuery::ParseError
|
88
|
+
# do nothing
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def queries
|
94
|
+
@queries ||= Hash.new {|hash, key| hash[key] = {count: 0, total_time: 0} }
|
95
|
+
end
|
96
|
+
|
97
|
+
def squish(str)
|
98
|
+
str.gsub(/\A[[:space:]]+/, '').gsub(/[[:space:]]+\z/, '').gsub(/[[:space:]]+/, ' ')
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
end
|
data/pghero_logs.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'pghero_logs/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "pghero_logs"
|
8
|
+
spec.version = PgheroLogs::VERSION
|
9
|
+
spec.authors = ["Andrew Kane"]
|
10
|
+
spec.email = ["andrew@chartkick.com"]
|
11
|
+
spec.summary = %q{Slow query log parser for Postgres}
|
12
|
+
spec.description = %q{Slow query log parser for Postgres}
|
13
|
+
spec.homepage = "https://github.com/ankane/pghero_logs"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "pg_query"
|
22
|
+
spec.add_dependency "aws-sdk"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pghero_logs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Kane
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pg_query
|
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: aws-sdk
|
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: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: Slow query log parser for Postgres
|
70
|
+
email:
|
71
|
+
- andrew@chartkick.com
|
72
|
+
executables:
|
73
|
+
- pghero_logs
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/pghero_logs
|
83
|
+
- lib/pghero_logs.rb
|
84
|
+
- lib/pghero_logs/version.rb
|
85
|
+
- pghero_logs.gemspec
|
86
|
+
homepage: https://github.com/ankane/pghero_logs
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.2.2
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Slow query log parser for Postgres
|
110
|
+
test_files: []
|
111
|
+
has_rdoc:
|