talentbox-newrelic-sequel 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .idea
2
+ .DS_Store
3
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/README.markdown ADDED
@@ -0,0 +1,36 @@
1
+ Newrelic Sequel
2
+ ===============
3
+ **Sequel instrumentation for Newrelic.**
4
+
5
+ Warning
6
+ -------
7
+
8
+ This gem is a fork of [newrelic-sequel](https://github.com/realestate-com-au/newrelic-sequel).
9
+
10
+ Usage
11
+ -----
12
+
13
+ 1. Install gem
14
+
15
+ >gem install talentbox-newrelic-sequel
16
+
17
+ 2. Config dependencies in application.rb
18
+
19
+ >config.gem "newrelic_rpm"
20
+
21
+ Dependencies
22
+ ------------
23
+
24
+ 1. newrelic_rpm > 3.0
25
+ 2. sequel > 3.22
26
+
27
+ License
28
+ -------
29
+
30
+ Copyright (C) 2012 REA Group Ltd.
31
+
32
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
33
+
34
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
35
+
36
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ Bundler::GemHelper.install_tasks
8
+
@@ -0,0 +1 @@
1
+ require 'newrelic_sequel/sequel'
@@ -0,0 +1,135 @@
1
+ require 'new_relic/agent/method_tracer'
2
+
3
+ DependencyDetection.defer do
4
+ depends_on do
5
+ defined?(::Sequel)
6
+ end
7
+
8
+ depends_on do
9
+ begin
10
+ (Sequel::MAJOR == 3 && Sequel::MINOR >= 22)
11
+ rescue Exception => e
12
+ false
13
+ end
14
+ end
15
+
16
+ executes do
17
+ ::Sequel::Model::ClassMethods.class_eval do
18
+
19
+ add_method_tracer :[], 'ActiveRecord/#{self.name}/find'
20
+
21
+ add_method_tracer :all, 'ActiveRecord/#{self.name}/find'
22
+ add_method_tracer :each, 'ActiveRecord/#{self.name}/find'
23
+ add_method_tracer :create, 'ActiveRecord/#{self.name}/create'
24
+ add_method_tracer :insert, 'ActiveRecord/#{self.name}/create'
25
+ add_method_tracer :insert_multiple, 'ActiveRecord/#{self.name}/create'
26
+ add_method_tracer :import, 'ActiveRecord/#{self.name}/create'
27
+ add_method_tracer :update, 'ActiveRecord/#{self.name}/update'
28
+ add_method_tracer :delete, 'ActiveRecord/#{self.name}/delete'
29
+
30
+ end
31
+
32
+ ::Sequel::Model::InstanceMethods.class_eval do
33
+
34
+ add_method_tracer :_insert, 'ActiveRecord/#{self.class.name[/[^:]*$/]}/create'
35
+ add_method_tracer :_update, 'ActiveRecord/#{self.class.name[/[^:]*$/]}/update'
36
+ add_method_tracer :_delete, 'ActiveRecord/#{self.class.name[/[^:]*$/]}/destroy'
37
+
38
+ end
39
+
40
+ ::Sequel::Dataset.class_eval do
41
+
42
+ add_method_tracer :first, 'ActiveRecord/#{self.respond_to?(:model) ? self.model.name : "Dataset"}/first'
43
+ add_method_tracer :find_all, 'ActiveRecord/#{self.respond_to?(:model) ? self.model.name : "Dataset"}/find_all'
44
+ add_method_tracer :execute, 'ActiveRecord/#{self.respond_to?(:model) ? self.model.name : "Dataset"}/find'
45
+ add_method_tracer :execute_insert, 'ActiveRecord/#{self.respond_to?(:model) ? self.model.name : "Dataset"}/create'
46
+ add_method_tracer :execute_dui, 'ActiveRecord/#{self.respond_to?(:model) ? self.model.name : "Dataset"}/update'
47
+ add_method_tracer :execute_ddl, 'ActiveRecord/#{self.respond_to?(:model) ? self.model.name : "Dataset"}/all'
48
+
49
+ end
50
+
51
+ ::Sequel::Database.class_eval do
52
+
53
+ add_method_tracer :execute, 'ActiveRecord/Database/find'
54
+ add_method_tracer :execute_insert, 'ActiveRecord/Database/create'
55
+ add_method_tracer :execute_dui, 'ActiveRecord/Database/update'
56
+ add_method_tracer :execute_ddl, 'ActiveRecord/Database/all'
57
+
58
+ end
59
+
60
+ end
61
+ end
62
+
63
+
64
+ module NewRelic
65
+ module Agent
66
+ module Instrumentation
67
+ module SequelDurationRecorder
68
+ def self.record(duration, sql)
69
+ return unless NewRelic::Agent.is_execution_traced?
70
+ return unless operation = extract_operation_from_sql(sql)
71
+ NewRelic::Agent.instance.transaction_sampler.notice_sql(sql, nil, duration)
72
+
73
+ metrics = ["ActiveRecord/#{operation}", 'ActiveRecord/all']
74
+ metrics.each do |metric|
75
+ NewRelic::Agent.instance.stats_engine.get_stats_no_scope(metric).trace_call(duration)
76
+ end
77
+ end
78
+
79
+ def self.extract_operation_from_sql(sql)
80
+ case sql
81
+ when /^\s*select/i then
82
+ 'find'
83
+ when /^\s*(update|insert)/i then
84
+ 'save'
85
+ when /^\s*delete/i then
86
+ 'destroy'
87
+ when /^\s*with (?:recursive)?/i then
88
+ # Recursive queries for Postgresql
89
+ # Syntax is: WITH [RECURSIVE]
90
+ # The results can be used to select/update/delete rows
91
+ # we're always tracking it as select here, because finding what the
92
+ # "real" action is, probably require a full SQL parser.
93
+ 'find'
94
+ else
95
+ nil
96
+ end
97
+ end
98
+ end
99
+
100
+ module SequelInstrumentation
101
+ def self.included(klass)
102
+ klass.class_eval do
103
+ alias_method :log_duration_without_newrelic_instrumentation, :log_duration
104
+ alias_method :log_duration, :log_duration_with_newrelic_instrumentation
105
+ end
106
+ end
107
+
108
+ def log_duration_with_newrelic_instrumentation(duration, sql)
109
+ SequelDurationRecorder.record(duration, sql)
110
+ ensure
111
+ log_duration_without_newrelic_instrumentation(duration, sql)
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
117
+
118
+ DependencyDetection.defer do
119
+ depends_on do
120
+ defined?(::Sequel) && defined?(::Sequel::Database)
121
+ end
122
+
123
+ executes do
124
+ if defined?(SequelRails)
125
+ ActiveSupport::Notifications.subscribe("sql.sequel") do |*args|
126
+ event = ActiveSupport::Notifications::Event.new(*args)
127
+ ::NewRelic::Agent::Instrumentation::SequelDurationRecorder.record(event.duration, event.payload[:sql])
128
+ end
129
+ else
130
+ ::Sequel::Database.class_eval do
131
+ include ::NewRelic::Agent::Instrumentation::SequelInstrumentation
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,3 @@
1
+ class NewRelicSequel
2
+ VERSION = '0.0.6'
3
+ end
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ require "newrelic_sequel/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "talentbox-newrelic-sequel"
8
+ s.version = NewRelicSequel::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+ s.authors = ["REA Group", "Metrilio S.A."]
11
+ s.email = [
12
+ "yong.fu@rea-group.com",
13
+ "wei.guangcheng@rea-group.com",
14
+ "jonathan.tron@metrilio.com"
15
+ ]
16
+ s.homepage = "http://github.com/TalentBox/newrelic-sequel"
17
+ s.summary = "Sequel instrumentation for Newrelic."
18
+ s.description = "Sequel instrumentation for Newrelic with sequel-rails and PostgreSQL CTE support"
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+ s.extra_rdoc_files = ["README.markdown"]
24
+ s.rdoc_options = ["--charset=UTF-8"]
25
+
26
+ s.add_runtime_dependency "sequel", "> 3.22"
27
+ s.add_runtime_dependency "newrelic_rpm", "~> 3.0"
28
+
29
+ s.add_development_dependency "rake"
30
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: talentbox-newrelic-sequel
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.6
6
+ platform: ruby
7
+ authors:
8
+ - REA Group
9
+ - Metrilio S.A.
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-01-19 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ! '>'
19
+ - !ruby/object:Gem::Version
20
+ version: '3.22'
21
+ none: false
22
+ prerelease: false
23
+ name: sequel
24
+ requirement: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ! '>'
27
+ - !ruby/object:Gem::Version
28
+ version: '3.22'
29
+ none: false
30
+ type: :runtime
31
+ - !ruby/object:Gem::Dependency
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ~>
35
+ - !ruby/object:Gem::Version
36
+ version: '3.0'
37
+ none: false
38
+ prerelease: false
39
+ name: newrelic_rpm
40
+ requirement: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: '3.0'
45
+ none: false
46
+ type: :runtime
47
+ - !ruby/object:Gem::Dependency
48
+ version_requirements: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ none: false
54
+ prerelease: false
55
+ name: rake
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ none: false
62
+ type: :development
63
+ description: Sequel instrumentation for Newrelic with sequel-rails and PostgreSQL
64
+ CTE support
65
+ email:
66
+ - yong.fu@rea-group.com
67
+ - wei.guangcheng@rea-group.com
68
+ - jonathan.tron@metrilio.com
69
+ executables: []
70
+ extensions: []
71
+ extra_rdoc_files:
72
+ - README.markdown
73
+ files:
74
+ - .gitignore
75
+ - Gemfile
76
+ - README.markdown
77
+ - Rakefile
78
+ - lib/newrelic-sequel.rb
79
+ - lib/newrelic_sequel/sequel.rb
80
+ - lib/newrelic_sequel/version.rb
81
+ - talentbox-newrelic-sequel.gemspec
82
+ homepage: http://github.com/TalentBox/newrelic-sequel
83
+ licenses: []
84
+ post_install_message:
85
+ rdoc_options:
86
+ - --charset=UTF-8
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ segments:
95
+ - 0
96
+ hash: -4563494043796374376
97
+ none: false
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ segments:
104
+ - 0
105
+ hash: -4563494043796374376
106
+ none: false
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 1.8.24
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: Sequel instrumentation for Newrelic.
113
+ test_files: []