pg_decorator 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +47 -0
- data/Rakefile +1 -0
- data/lib/pg_decorator.rb +10 -0
- data/lib/pg_decorator/decorator.rb +74 -0
- data/lib/pg_decorator/injector.rb +13 -0
- data/lib/pg_decorator/version.rb +3 -0
- data/pg_decorator.gemspec +20 -0
- metadata +77 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Ervin Weber
|
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,47 @@
|
|
1
|
+
# PgDecorator
|
2
|
+
|
3
|
+
This gem is inspired by [marginalia by 37signals](https://github.com/37signals/marginalia) but instead of patching
|
4
|
+
Rails, patches [Pg gem](https://bitbucket.org/ged/ruby-pg/wiki/Home)
|
5
|
+
|
6
|
+
PgDecorator adds first line of caller (tries to detect place in *your* code by ignoring gems, ruby and rbenv lines) as
|
7
|
+
/* SQL comment */ so you could later identify "strange" or long queries in your PostgreSQL log directly.
|
8
|
+
|
9
|
+
Several reasons for creating gem:
|
10
|
+
|
11
|
+
* marginalia did not decorate some of PostgreSQL adapter queries
|
12
|
+
* no Rails 'dependency', you can use this gem if you use pg gem directly, or if you have non rails wrapper, such as [pg_helper](https://github.com/webervin/pg_helper)
|
13
|
+
* having aggressive connection pooler in front of database server may lead to loss of "set application_name = 'newappname';",
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
gem 'pg_decorator'
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install pg_decorator
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
in your ruby script add following:
|
32
|
+
|
33
|
+
require 'rubygems'
|
34
|
+
require 'pg_decorator'
|
35
|
+
PgDecorator::Injector.inject('AppToRuleWorld')
|
36
|
+
|
37
|
+
If you use Rails then you could create config/initializers/pg_decorator
|
38
|
+
|
39
|
+
PgDecorator::Injector.inject('AppToRuleWorld')
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
1. Fork it
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
47
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/pg_decorator.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
module PgDecorator
|
2
|
+
module Decorator
|
3
|
+
def self.included(instrumented_class)
|
4
|
+
instrumented_class.class_eval do
|
5
|
+
alias_method :async_exec_without_sql_decorator, :async_exec
|
6
|
+
alias_method :async_exec, :async_exec_with_sql_decorator
|
7
|
+
|
8
|
+
alias_method :async_query_without_sql_decorator, :async_query
|
9
|
+
alias_method :async_query, :async_query_with_sql_decorator
|
10
|
+
|
11
|
+
alias_method :exec_without_sql_decorator, :exec
|
12
|
+
alias_method :exec, :exec_with_sql_decorator
|
13
|
+
|
14
|
+
alias_method :query_without_sql_decorator, :query
|
15
|
+
alias_method :query, :query_with_sql_decorator
|
16
|
+
|
17
|
+
alias_method :prepare_without_sql_decorator, :prepare
|
18
|
+
alias_method :prepare, :prepare_with_sql_decorator
|
19
|
+
|
20
|
+
alias_method :send_prepare_without_sql_decorator, :send_prepare
|
21
|
+
alias_method :send_prepare, :send_prepare_with_sql_decorator
|
22
|
+
|
23
|
+
alias_method :send_query_without_sql_decorator, :send_query
|
24
|
+
alias_method :send_query, :send_query_with_sql_decorator
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def decorate_sql(sql)
|
29
|
+
last_line = caller.detect{|l| l !~ /\.?(rvm|gem|vendor|rbenv|pg_decorator)/ }
|
30
|
+
if last_line
|
31
|
+
if last_line.starts_with? Rails.root.to_s
|
32
|
+
last_line = last_line[Rails.root.to_s.length..-1]
|
33
|
+
end
|
34
|
+
"/* #{ escape_string(app_name) } #{escape_comment(last_line)} */\n#{sql}"
|
35
|
+
else
|
36
|
+
"/* #{ escape_string(app_name) } outside code tree */\n#{sql}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def escape_comment(str)
|
41
|
+
escape_string( str.gsub('*/','* /'))
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
def async_exec_with_sql_decorator(sql,*args, &block)
|
46
|
+
async_exec_without_sql_decorator(decorate_sql(sql), *args, &block)
|
47
|
+
end
|
48
|
+
|
49
|
+
def exec_with_sql_decorator(sql,*args, &block)
|
50
|
+
exec_without_sql_decorator(decorate_sql(sql), *args, &block)
|
51
|
+
end
|
52
|
+
|
53
|
+
def query_with_sql_decorator(sql, *args, &block)
|
54
|
+
query_without_sql_decorator(decorate_sql(sql), *args, &block)
|
55
|
+
end
|
56
|
+
|
57
|
+
def async_query_with_sql_decorator(sql, *args, &block)
|
58
|
+
async_query_without_sql_decorator(decorate_sql(sql), *args, &block)
|
59
|
+
end
|
60
|
+
|
61
|
+
def prepare_with_sql_decorator(stmt_name, sql, *args, &block)
|
62
|
+
prepare_without_sql_decorator(stmt_name, decorate_sql(sql), *args, &block)
|
63
|
+
end
|
64
|
+
|
65
|
+
def send_prepare_with_sql_decorator(stmt_name, sql, *args, &block)
|
66
|
+
send_prepare_without_sql_decorator(stmt_name, decorate_sql(sql), *args, &block)
|
67
|
+
end
|
68
|
+
|
69
|
+
def send_query_with_sql_decorator(sql, *args, &block)
|
70
|
+
send_query_without_sql_decorator(decorate_sql(sql), *args, &block)
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module PgDecorator
|
2
|
+
class Injector
|
3
|
+
def self.inject(custom_app_name='Ruby pg app')
|
4
|
+
require 'pg' unless defined?( PG )
|
5
|
+
require_relative 'decorator'
|
6
|
+
PG::Connection.module_eval do
|
7
|
+
define_method(:app_name) { custom_app_name }
|
8
|
+
include PgDecorator::Decorator
|
9
|
+
end
|
10
|
+
true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'pg_decorator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "pg_decorator"
|
8
|
+
gem.version = PgDecorator::VERSION
|
9
|
+
gem.authors = ["Ervin Weber"]
|
10
|
+
gem.email = ["webervin@gmail.com"]
|
11
|
+
gem.description = %q{adds caller method as SQL comment}
|
12
|
+
gem.summary = %q{Makes ruby caller visible in PostgreSQL log}
|
13
|
+
gem.homepage = 'https://github.com/webervin/pg_decorator'
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.add_development_dependency 'rake'
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pg_decorator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ervin Weber
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: adds caller method as SQL comment
|
31
|
+
email:
|
32
|
+
- webervin@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/pg_decorator.rb
|
43
|
+
- lib/pg_decorator/decorator.rb
|
44
|
+
- lib/pg_decorator/injector.rb
|
45
|
+
- lib/pg_decorator/version.rb
|
46
|
+
- pg_decorator.gemspec
|
47
|
+
homepage: https://github.com/webervin/pg_decorator
|
48
|
+
licenses: []
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
hash: -3810251013660995226
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
hash: -3810251013660995226
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.8.24
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Makes ruby caller visible in PostgreSQL log
|
77
|
+
test_files: []
|