mysql2-metrics 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 +52 -0
- data/Rakefile +2 -0
- data/example/Gemfile +6 -0
- data/example/Gemfile.lock +28 -0
- data/example/app.rb +5 -0
- data/lib/mysql2-metrics.rb +1 -0
- data/lib/mysql2/metrics.rb +52 -0
- data/lib/mysql2/metrics/ltsv_formatter.rb +15 -0
- data/mysql2-metrics.gemspec +19 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 705c86d9ef9365f07cd87ac327caeebd6280f6f5
|
4
|
+
data.tar.gz: ed7db4027679cf407857bdcc28cb3d6693e7446f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a918ea7d95ba0a62982105508cf54dfde7895301d00ef114f8a49e75632bd938311af0e3669e8d71fa0a3fa7eb15ae9d88646d186d0a0e8c96a7a5d7a85bbfd2
|
7
|
+
data.tar.gz: 53b3950c5f65189e88124454f2fa53acdcb856286c4365a8e7a9c2e7f6ec952e9f564d5f438e6661bf0f172a053637700dfead3d4bd12268fbf73c32bf032692
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 sonots
|
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,52 @@
|
|
1
|
+
# Mysql2::Metrics
|
2
|
+
|
3
|
+
Instrument mysql2 queries.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'mysql2-metrics'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install mysql2-metrics
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Just require `mysql2/metrics` in addition to `mysql2`, ans use mysql2 as usual.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'mysql2'
|
27
|
+
require 'mysql2-metrics'
|
28
|
+
|
29
|
+
client = Mysql2::Client.new(:host => "localhost", :username => "root", :database => "test")
|
30
|
+
results = client.query("CREATE TABLE IF NOT EXISTS test (id INT)")
|
31
|
+
```
|
32
|
+
|
33
|
+
This will output a measured time to STDOUT as:
|
34
|
+
|
35
|
+
```
|
36
|
+
time:2014-11-02T01:38:25+09:00 query:CREATE TABLE IF NOT EXISTS test (id INT) elapsed:0.000299591
|
37
|
+
```
|
38
|
+
|
39
|
+
where `elapsed` is the elapsed time in second.
|
40
|
+
|
41
|
+
## ToDo
|
42
|
+
|
43
|
+
* write tests
|
44
|
+
* output to a file path
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
1. Fork it ( https://github.com/sonots/mysql2-metrics/fork )
|
49
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
51
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
52
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/example/Gemfile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../
|
3
|
+
specs:
|
4
|
+
mysql2-metrics (0.0.1)
|
5
|
+
mysql2
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
coderay (1.1.0)
|
11
|
+
method_source (0.8.2)
|
12
|
+
mysql2 (0.3.16)
|
13
|
+
pry (0.10.1)
|
14
|
+
coderay (~> 1.1.0)
|
15
|
+
method_source (~> 0.8.1)
|
16
|
+
slop (~> 3.4)
|
17
|
+
pry-nav (0.2.4)
|
18
|
+
pry (>= 0.9.10, < 0.11.0)
|
19
|
+
slop (3.6.0)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
mysql2
|
26
|
+
mysql2-metrics!
|
27
|
+
pry
|
28
|
+
pry-nav
|
data/example/app.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'mysql2/metrics'
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'mysql2'
|
2
|
+
require 'mysql2/metrics/ltsv_formatter'
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
module Mysql2
|
6
|
+
class Metrics
|
7
|
+
def self.logger=(logger)
|
8
|
+
@logger = logger
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.logger
|
12
|
+
@logger ||= Logger.new($stdout).tap {|log|
|
13
|
+
log.formatter = ::Mysql2::Metrics::LtsvFormatter.new
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.add_tracer(method)
|
18
|
+
klass = ::Mysql2::Client
|
19
|
+
unless klass.method_defined?("#{method}_without_metrics")
|
20
|
+
klass.__send__(:alias_method, "#{method}_without_metrics", method)
|
21
|
+
klass.__send__(:define_method, "#{method}_with_metrics") do |*args|
|
22
|
+
query = args.first
|
23
|
+
::Mysql2::Metrics.measure_time(query) do
|
24
|
+
send("#{method}_without_metrics", *args)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
klass.__send__(:alias_method, method, "#{method}_with_metrics")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.measure_time(query, &block)
|
32
|
+
start = Time.now
|
33
|
+
ret = yield
|
34
|
+
elapsed = Time.now - start
|
35
|
+
logger = Mysql2::Metrics.logger
|
36
|
+
logger.info({ query: query, elapsed: elapsed.to_f })
|
37
|
+
ret
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# open class
|
43
|
+
module Mysql2
|
44
|
+
class Client
|
45
|
+
# mysql2-cs-bind
|
46
|
+
if method_defined?(:xquery)
|
47
|
+
::Mysql2::Metrics.add_tracer :xquery
|
48
|
+
else
|
49
|
+
::Mysql2::Metrics.add_tracer :query
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Mysql2
|
2
|
+
class Metrics
|
3
|
+
class LtsvFormatter
|
4
|
+
def call(severity, datetime, progname, msg)
|
5
|
+
"time:#{datetime.iso8601}\t#{format_msg(msg)}\n"
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def format_msg(msg)
|
11
|
+
msg.map {|k, v| "#{k}:#{v}" }.join("\t")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "mysql2-metrics"
|
3
|
+
spec.version = "0.0.1"
|
4
|
+
spec.authors = ["sonots"]
|
5
|
+
spec.email = ["sonots@gmail.com"]
|
6
|
+
spec.summary = %q{Instrument mysql2 queries}
|
7
|
+
spec.description = %q{Instrument mysql2 queries}
|
8
|
+
spec.homepage = "https://github.com/sonots/mysql2-metrics"
|
9
|
+
spec.license = "MIT"
|
10
|
+
|
11
|
+
spec.files = `git ls-files -z`.split("\x0")
|
12
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
13
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
14
|
+
spec.require_paths = ["lib"]
|
15
|
+
|
16
|
+
spec.add_runtime_dependency "mysql2"
|
17
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
18
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mysql2-metrics
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- sonots
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mysql2
|
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: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: Instrument mysql2 queries
|
56
|
+
email:
|
57
|
+
- sonots@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- example/Gemfile
|
68
|
+
- example/Gemfile.lock
|
69
|
+
- example/app.rb
|
70
|
+
- lib/mysql2-metrics.rb
|
71
|
+
- lib/mysql2/metrics.rb
|
72
|
+
- lib/mysql2/metrics/ltsv_formatter.rb
|
73
|
+
- mysql2-metrics.gemspec
|
74
|
+
homepage: https://github.com/sonots/mysql2-metrics
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.2.2
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Instrument mysql2 queries
|
98
|
+
test_files: []
|