site24x7_apminsight 1.4 → 1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,12 +17,13 @@ module ManageEngine
17
17
  @metrics.dup
18
18
  end
19
19
 
20
- def removeData key, strt_indx,end_indx
21
- if @metrics.has_key?(key)
22
- val = @metrics[key]
23
- val = val.drop(end_indx)
24
- @metrics[key]=val
25
- end
20
+ def removeData key
21
+ # if @metrics.has_key?(key)
22
+ # val = @metrics[key]
23
+ # val = val.drop(end_indx)
24
+ # @metrics[key]=val
25
+ # end
26
+ @metrics.delete(key)
26
27
  end
27
28
 
28
29
  end
@@ -1,5 +1,6 @@
1
1
  require "agent/am_objectholder"
2
2
  require "agent/server/worker/am_worker"
3
+ require "agent/server/instrument/environment"
3
4
 
4
5
  require 'socket'
5
6
 
@@ -12,7 +13,8 @@ module ManageEngine
12
13
 
13
14
  if !@obj.shutdown && @obj.agent_initialized
14
15
  @obj.log.info "Agent Initialization - DONE"
15
- @obj.instrumenter.doSubscribe
16
+ ManageEngine::Environment.new.detect_and_instrument
17
+
16
18
  doDispatcherActions
17
19
  doCollect
18
20
  puts "APM Insight Ruby Agent Started"
@@ -146,8 +146,11 @@ module ManageEngine
146
146
  response_action srCode
147
147
  end
148
148
  if data.has_key?(@obj.constants.custom_config_info)
149
- config_info = data[@obj.constants.custom_config_info]
150
- update_config config_info
149
+ config_info = data[@obj.constants.custom_config_info]
150
+ if data.has_key?(@obj.constants.agent_specific_info)
151
+ config_info = config_info.merge(data[@obj.constants.agent_specific_info])
152
+ end
153
+ update_config config_info
151
154
  end
152
155
  end
153
156
  return data
@@ -207,9 +210,6 @@ module ManageEngine
207
210
  end
208
211
 
209
212
  def unManage
210
- @obj.instrumenter.doUnSubscribe
211
- @obj.instrumenter =nil
212
- @obj.instrumenter = ManageEngine::APMInstrumenter.new
213
213
  uManage = Hash.new
214
214
  uManage["agent.id"]=@obj.config.instance_id
215
215
  uManage["agent.enabled"]=false
@@ -217,7 +217,6 @@ module ManageEngine
217
217
  end
218
218
 
219
219
  def manage
220
- @obj.instrumenter.doSubscribe
221
220
  uManage = Hash.new
222
221
  uManage["agent.id"]=@obj.config.instance_id
223
222
  uManage["agent.enabled"]=true
@@ -225,8 +224,6 @@ module ManageEngine
225
224
  end
226
225
 
227
226
  def deleteAgent
228
- @obj.instrumenter.doUnSubscribe
229
- @obj.instrumenter =nil
230
227
  uManage = Hash.new
231
228
  uManage["agent.id"]=@obj.config.instance_id
232
229
  uManage["agent.enabled"]=false
@@ -0,0 +1,50 @@
1
+ require 'agent/am_objectholder'
2
+ require 'agent/trackers/database_tracker'
3
+
4
+ module ManageEngine
5
+ module Instrumentation
6
+ class ActiveRecordSQL
7
+
8
+ def present?
9
+ defined?(::ActiveRecord::Base) && defined?(::ActiveSupport::Notifications)
10
+ end
11
+
12
+ def name
13
+ 'ActiveRecord'
14
+ end
15
+
16
+ def instrument
17
+ @obj = ManageEngine::APMObjectHolder.instance
18
+ @obj.log.info "Instrumenting ActiveRecord"
19
+
20
+ ActiveSupport::Notifications.subscribe('sql.active_record') do |name, start, finish, id, payload|
21
+ begin
22
+ if @obj.config.agent_enabled && @obj.config.sql_capture && payload[:name] != "SCHEMA" # Dropping internal schema related queries
23
+ tracker = Thread.current[:apminsight]
24
+ if tracker != nil
25
+ dbTracker = ManageEngine::Tracker::DatabaseTracker.new(payload[:name], start.to_f * 1000)
26
+ dbTracker.sql(payload[:sql])
27
+ dbTracker.params(payload[:binds])
28
+ dbTracker.finish(finish.to_f * 1000)
29
+
30
+ if dbTracker.duration >= @obj.config.sql_trace_t.to_f
31
+ dbTracker.sqlBacktrace(caller(10))
32
+ end
33
+
34
+ exception = payload[:exception_object]
35
+ if exception != nil
36
+ dbTracker.setError(exception)
37
+ end
38
+
39
+ tracker.addChild(dbTracker)
40
+ end
41
+ end
42
+ rescue Exception => e
43
+ @obj.log.logException("Error processing #{name} payload", e)
44
+ end
45
+ end #subscribe
46
+ end #def instrument
47
+
48
+ end #class ActiveRecordSQL
49
+ end
50
+ end
@@ -1,99 +1,109 @@
1
- require 'agent/am_objectholder'
2
- @obj = ManageEngine::APMObjectHolder.instance
3
- class Class
4
- alias old_new new
5
- def new(*args, &block)
6
- result =nil;
7
- begin
8
- if(block==nil || block=="")
9
- result = old_new(*args)
10
- elsif
11
- result = old_new(*args,&block)
12
- end
13
- rescue Exception=>exe
14
- raise exe
15
- result = self
16
- end
17
- me_apm_injector(self,result)
18
-
19
- return result
20
- end
21
- end
1
+ ##
2
+ ##
3
+ # Currently disabling this class, since it will be called for every class's instance creation (Class.new is aliased here)
4
+ # This can be used for custom instrumentation.
5
+ ##
6
+ ##
22
7
 
23
- def me_apm_injector(s,result)
24
- begin
25
- if(ManageEngine::APMObjectHolder.instance.config.include_packages.index(s.name)!=nil)
26
- ms =s.instance_methods(false)
27
- cms = s.methods(false)
28
- begin
29
- ms.each do |m|
30
- if( m.to_s.index("APMTEST"))
31
- return;
32
- end
33
- end
34
- cms.each do |m|
35
- if( m.to_s.index("APMTEST"))
36
- return;
37
- end
38
- end
39
- rescue Exception=>e
40
- return;
41
- end
42
- ManageEngine::APMObjectHolder.instance.log.debug "Injection Method : #{ms} "
43
- ManageEngine::APMObjectHolder.instance.log.debug "Injection Class Method : #{cms} "
44
- ms.each do |m|
45
- mn = m.to_s
46
- #ManageEngine::APMObjectHolder.instance.log.info "ManageEngine Monitor Method : #{s.name} # #{m.to_s}"
47
- omn = "APMTEST"+mn+"APMTEST"
48
- s.class_eval %{
49
- alias_method :#{omn}, :#{mn}
50
- def #{mn} *args, &block
51
- begin
52
- ActiveSupport::Notifications.instrument("apm.methodstart", {:method=>"#{mn}",:args=>args})
53
- res = #{omn} *args, &block
54
- ActiveSupport::Notifications.instrument("apm.methodend", {:method=>"#{mn}",:args=>args})
55
- return res
56
- rescue Exception => exe
57
- puts "error in calling method"
58
- raise exe
59
- ensure
60
- end
61
- end
62
- }
63
- end#do
64
- default_methods = Array.new
65
- default_methods.push("_helpers");
66
- default_methods.push("middleware_stack");
67
- default_methods.push("helpers_path");
68
- default_methods.push("_wrapper_options");
69
- cms.each do |m|
70
- if(default_methods.index(m.to_s)==nil)
71
- mn = m.to_s
72
- #ManageEngine::APMObjectHolder.instance.log.debug "ManageEngine Monitor Singleton Method : #{s.name} ---> #{m.to_s}"
73
- omn = "APMTEST"+mn+"APMTEST"
74
- s.instance_eval %{
75
- class << self
76
- alias_method :#{omn}, :#{mn}
77
- end
78
- def self.#{mn} *args, &block
79
- begin
80
- ActiveSupport::Notifications.instrument("apm.methodstart", {:method=>"#{mn}",:args=>args})
81
- res = #{omn} *args, &block
82
- ActiveSupport::Notifications.instrument("apm.methodend", {:method=>"#{mn}",:args=>args})
83
- return res
84
- rescue Exception=>exe
85
- puts "Instrument : error in calling class method"
86
- raise exe
87
- ensure
88
- end
89
- end
90
- }
91
- end
92
- end#do
93
- end#if
94
- rescue Exception=>e
95
- puts "Exception in instrument : #{e}"
96
- ensure
97
- end
98
- end
8
+
9
+
10
+
11
+ #require 'agent/am_objectholder'
12
+ #@obj = ManageEngine::APMObjectHolder.instance
13
+ #class Class
14
+ # alias old_new new
15
+ # def new(*args, &block)
16
+ # result =nil;
17
+ # begin
18
+ # if(block==nil || block=="")
19
+ # result = old_new(*args)
20
+ # elsif
21
+ # result = old_new(*args,&block)
22
+ # end
23
+ # rescue Exception=>exe
24
+ # raise exe
25
+ # result = self
26
+ # end
27
+ # me_apm_injector(self,result)
28
+ #
29
+ # return result
30
+ # end
31
+ # end
32
+ #
33
+ #def me_apm_injector(s,result)
34
+ # begin
35
+ # if(ManageEngine::APMObjectHolder.instance.config.include_packages.index(s.name)!=nil)
36
+ # ms =s.instance_methods(false)
37
+ # cms = s.methods(false)
38
+ # begin
39
+ # ms.each do |m|
40
+ # if( m.to_s.index("APMTEST"))
41
+ # return;
42
+ # end
43
+ # end
44
+ # cms.each do |m|
45
+ # if( m.to_s.index("APMTEST"))
46
+ # return;
47
+ # end
48
+ # end
49
+ # rescue Exception=>e
50
+ # return;
51
+ # end
52
+ # ManageEngine::APMObjectHolder.instance.log.debug "Injection Method : #{ms} "
53
+ # ManageEngine::APMObjectHolder.instance.log.debug "Injection Class Method : #{cms} "
54
+ # ms.each do |m|
55
+ # mn = m.to_s
56
+ # #ManageEngine::APMObjectHolder.instance.log.info "ManageEngine Monitor Method : #{s.name} # #{m.to_s}"
57
+ # omn = "APMTEST"+mn+"APMTEST"
58
+ # s.class_eval %{
59
+ # alias_method :#{omn}, :#{mn}
60
+ # def #{mn} *args, &block
61
+ # begin
62
+ # ActiveSupport::Notifications.instrument("apm.methodstart", {:method=>"#{mn}",:args=>args})
63
+ # res = #{omn} *args, &block
64
+ # ActiveSupport::Notifications.instrument("apm.methodend", {:method=>"#{mn}",:args=>args})
65
+ # return res
66
+ # rescue Exception => exe
67
+ # puts "error in calling method"
68
+ # raise exe
69
+ # ensure
70
+ # end
71
+ # end
72
+ # }
73
+ # end#do
74
+ # default_methods = Array.new
75
+ # default_methods.push("_helpers");
76
+ # default_methods.push("middleware_stack");
77
+ # default_methods.push("helpers_path");
78
+ # default_methods.push("_wrapper_options");
79
+ # cms.each do |m|
80
+ # if(default_methods.index(m.to_s)==nil)
81
+ # mn = m.to_s
82
+ # #ManageEngine::APMObjectHolder.instance.log.debug "ManageEngine Monitor Singleton Method : #{s.name} ---> #{m.to_s}"
83
+ # omn = "APMTEST"+mn+"APMTEST"
84
+ # s.instance_eval %{
85
+ # class << self
86
+ # alias_method :#{omn}, :#{mn}
87
+ # end
88
+ # def self.#{mn} *args, &block
89
+ # begin
90
+ # ActiveSupport::Notifications.instrument("apm.methodstart", {:method=>"#{mn}",:args=>args})
91
+ # res = #{omn} *args, &block
92
+ # ActiveSupport::Notifications.instrument("apm.methodend", {:method=>"#{mn}",:args=>args})
93
+ # return res
94
+ # rescue Exception=>exe
95
+ # puts "Instrument : error in calling class method"
96
+ # raise exe
97
+ # ensure
98
+ # end
99
+ # end
100
+ # }
101
+ # end
102
+ # end#do
103
+ # end#if
104
+ # rescue Exception=>e
105
+ # puts "Exception in instrument : #{e}"
106
+ # ensure
107
+ # end
108
+ #end
99
109
 
@@ -1,43 +1,55 @@
1
- require 'agent/am_objectholder'
2
- require 'socket'
3
- module ManageEngine
4
- class APMInstrumenter
5
- @t =nil;
6
- def initialize
7
- @obj=ManageEngine::APMObjectHolder.instance
8
- end
1
+ ##
2
+ ## This is Rails framework specific tracking mechanism, they are moved to multiple files for separate tracking
3
+ ##
9
4
 
10
- def doSubscribe
11
- @obj=ManageEngine::APMObjectHolder.instance
12
- @obj.log.debug "[ instrumenter ] [ Subscriber for Agent ]"
13
- @subscriber = ActiveSupport::Notifications.subscribe do |name, start, finish, id, payload|
14
- if(ManageEngine::APMObjectHolder.instance.config.agent_enabled)
15
- #rt = (finish-start).to_i
16
- ManageEngine::APMWorker.getInstance.start
17
- ManageEngine::APMObjectHolder.instance.log.debug "[ Notifications for Agent ] #{Thread.current} #{id} #{name} - #{payload[:path]}"
18
- #trace= caller;
19
- id = "#{Thread.current}"
20
- stats = Hash.new
21
- stats["name"] = name;
22
- stats["start"] = start.to_f * 1000;
23
- stats["end"] = finish.to_f * 1000;
24
- stats["id"] = id;
25
- stats["payload"] = payload;
26
- if (name=="sql.active_record" && (finish.to_f - start.to_f)>=(ManageEngine::APMObjectHolder.instance.config.sql_trace_t).to_f)
27
- stats["trace"] = caller(20); # Taking stacktrace of depth 20
28
- end
29
- stats["ctime"] =ManageEngine::APMObjectHolder.instance.util.currenttimemillis;
30
- ManageEngine::APMObjectHolder.instance.collector.updateTransaction(id,stats);
31
- else
32
- ActiveSupport::Notifications.unsubscribe @subscriber
33
- @obj.log.info "[ instrumenter ] [ RETURNING NO METRICS] "
34
- end
35
- end
36
- end
37
-
38
- def doUnSubscribe
39
- ActiveSupport::Notifications.unsubscribe @subscriber
40
- end
41
-
42
- end #class
43
- end#module
5
+ #require 'agent/am_objectholder'
6
+ #require 'socket'
7
+ #module ManageEngine
8
+ # class APMInstrumenter
9
+ # @t =nil;
10
+ # def initialize
11
+ # @obj=ManageEngine::APMObjectHolder.instance
12
+ # end
13
+ #
14
+ # def doSubscribe
15
+ # @obj=ManageEngine::APMObjectHolder.instance
16
+ # @obj.log.debug "[ instrumenter ] [ Subscriber for Agent ]"
17
+ # @subscriber = ActiveSupport::Notifications.subscribe do |name, start, finish, id, payload|
18
+ # if(ManageEngine::APMObjectHolder.instance.config.agent_enabled)
19
+ # #rt = (finish-start).to_i
20
+ # ManageEngine::APMWorker.getInstance.start
21
+ # ManageEngine::APMObjectHolder.instance.log.debug "[ Notifications for Agent ] #{Thread.current} #{id} #{name} - #{payload[:path]}"
22
+ # #trace= caller;
23
+ # #puts ">>> Threadlocal var : #{Thread.current[:apminsight]}"
24
+ # if name=="sql.active_record"
25
+ # #Thread.current[:apminsight] = "#{Thread.current[:apminsight]} + #{payload[:sql]}"
26
+ # if payload[:name] != "SCHEMA"
27
+ # @obj.log.debug ">>>>>>>> SQL: #{payload[:sql]}"
28
+ # end
29
+ # @obj.log.debug "~~~~~ SQL Payload: #{payload}"
30
+ # end
31
+ # id = "#{Thread.current}"
32
+ # stats = Hash.new
33
+ # stats["name"] = name;
34
+ # stats["start"] = start.to_f * 1000;
35
+ # stats["end"] = finish.to_f * 1000;
36
+ # stats["id"] = id;
37
+ # stats["payload"] = payload;
38
+ # if (name=="sql.active_record" && (finish.to_f - start.to_f)>=(ManageEngine::APMObjectHolder.instance.config.sql_trace_t).to_f)
39
+ # stats["trace"] = caller(20); # Taking stacktrace of depth 20
40
+ # end
41
+ # stats["ctime"] =ManageEngine::APMObjectHolder.instance.util.currenttimemillis;
42
+ # ManageEngine::APMObjectHolder.instance.collector.updateTransaction(id,stats);
43
+ # else
44
+ # ActiveSupport::Notifications.unsubscribe @subscriber
45
+ # @obj.log.info "[ instrumenter ] [ RETURNING NO METRICS] "
46
+ # end
47
+ # end
48
+ # end
49
+ #
50
+ # def doUnSubscribe
51
+ # ActiveSupport::Notifications.unsubscribe @subscriber
52
+ # end
53
+ #
54
+ #end #class
55
+ #end#module
@@ -0,0 +1,29 @@
1
+ require 'agent/server/instrument/rails'
2
+ require 'agent/server/instrument/sinatra'
3
+ require 'agent/server/instrument/active_record'
4
+
5
+ module ManageEngine
6
+ class Environment
7
+
8
+ SUPPORTED_FRAMEWORKS = [
9
+ ManageEngine::Instrumentation::RailsFramework.new,
10
+ ManageEngine::Instrumentation::SinatraFramework.new
11
+ ]
12
+
13
+ DATABASE_INTERCEPTORS = [
14
+ ManageEngine::Instrumentation::ActiveRecordSQL.new
15
+ ]
16
+
17
+ def detect_and_instrument
18
+ @framework ||= SUPPORTED_FRAMEWORKS.detect{ |framework| framework.present? }
19
+ @framework.instrument
20
+
21
+ DATABASE_INTERCEPTORS.each do |interceptor|
22
+ if (interceptor.present?)
23
+ interceptor.instrument
24
+ end
25
+ end
26
+ end
27
+
28
+ end
29
+ end