wildsight 0.1.11 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 65d837ab02bd66fe49d9441f6d168519ce65bac7
4
- data.tar.gz: 4083aa040757c15a1d1f04a9badf31d5fa8b5d9d
3
+ metadata.gz: d0b043b6bdf567a187eaf195729978ab7b187fe1
4
+ data.tar.gz: 79421c3cba0177185caf0f8235512a7a93c0c162
5
5
  SHA512:
6
- metadata.gz: 170a7e6d6c8d6cd7390491be6e2ec57be87aeed92a7565fce8d504c26d506afc32e988aaa4e5b953d9dbc7842858543e494501ad123100e65f47a1b003abb3cc
7
- data.tar.gz: 11ccc7f2d32ff8f039fd1006349f1adc64e73ae564f38964d15b7e1a1b2af2dcc43dad45809fd3e4ce911f66d2afad44ee870c5d7e77b08e4955fbc1b6cbcf4a
6
+ metadata.gz: ffcaec0911490b358e86cd2a251fc71b25d7c9be9b2bc9f0ff9ea10aa34dea60e83b308721d374f4c27c892828ef16658e74be31ce305a866cda196ba3e4b5a8
7
+ data.tar.gz: 6d9e9cb8c1f5c19cc59912c35b156105a5fab5356ecb49ab8676276ccd689bf12da971fdb1e4552fe01a4785b0cd00b3ae61dbb55f37e5c7a2662e145c84e380
@@ -1,4 +1,5 @@
1
1
  require 'securerandom'
2
+ require 'pp'
2
3
 
3
4
  require 'wildsight/agent/tools'
4
5
  require 'wildsight/agent/config'
@@ -50,7 +51,7 @@ module Wildsight
50
51
  @contexts.delete(name)
51
52
  end
52
53
 
53
- def event(event)
54
+ def submit(event)
54
55
  @events << event
55
56
  if @events.size > @config['agent']['threshold']
56
57
  self.logger.log(:debug) { 'Uploading collected data' }
@@ -60,7 +61,7 @@ module Wildsight
60
61
 
61
62
  def upload
62
63
  payload, @events = @events, []
63
- @transport.send(payload)
64
+ @transport.send(payload) if payload != nil && payload.size > 0
64
65
  end
65
66
 
66
67
  private
@@ -0,0 +1,17 @@
1
+ module Wildsight
2
+ module Context
3
+
4
+ module ActiveRecord
5
+
6
+ def active_record_data
7
+ @data[:active_record] ||= []
8
+ end
9
+
10
+ def active_record_report(event)
11
+ active_record_data << event
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+ end
@@ -1,6 +1,8 @@
1
1
  require 'wildsight/context/rack'
2
+ require 'wildsight/context/rails'
2
3
  require 'wildsight/context/logger'
3
- require 'wildsight/context/sql'
4
+ require 'wildsight/context/exception'
5
+ require 'wildsight/context/active_record'
4
6
 
5
7
  module Wildsight
6
8
  module Context
@@ -11,9 +13,11 @@ module Wildsight
11
13
 
12
14
  class Context
13
15
 
14
- include Wildsight::Context::Rack
15
16
  include Wildsight::Context::Logs
16
- include Wildsight::Context::Sql
17
+ include Wildsight::Context::Exception
18
+ include Wildsight::Context::Rack
19
+ include Wildsight::Context::Rails
20
+ include Wildsight::Context::ActiveRecord
17
21
 
18
22
  attr_reader :name
19
23
 
@@ -23,8 +27,8 @@ module Wildsight
23
27
  @data = {}
24
28
  end
25
29
 
26
- def event(name, data)
27
- @agent.event({ :context => { :id => @name }, name => data })
30
+ def submit
31
+ @agent.submit({ :context => { :id => @name }, :payload => @data })
28
32
  end
29
33
 
30
34
  def unregister
@@ -39,18 +43,16 @@ module Wildsight
39
43
  thread.thread_variable_set('wildsight.context', nil)
40
44
  end
41
45
 
42
- def request
43
- @data[:http_request] ||= {}
46
+ def data
47
+ @data
44
48
  end
45
49
 
46
- def self.thread_context_id(thread=Thread.current)
47
- thread.thread_variable_get('wildsight.context')
50
+ def profiler
51
+ @profiler ||= ::Wildsight::Profiler::Profiler.new(self)
48
52
  end
49
53
 
50
- def extract_exception(exception)
51
- data = {:occurred => DateTime.now, :message => exception.message, :name => exception.class.name, :backtrace => exception.backtrace}
52
- data[:cause] = extract_exception(exception.cause) if exception.cause
53
- return data
54
+ def self.thread_context_id(thread=Thread.current)
55
+ thread.thread_variable_get('wildsight.context')
54
56
  end
55
57
 
56
58
  end
@@ -0,0 +1,24 @@
1
+ module Wildsight
2
+ module Context
3
+
4
+ module Exception
5
+
6
+ def exception_data
7
+ @data[:exceptions] = []
8
+ end
9
+
10
+ def exception_report(exception)
11
+ exception_data << exception_extract(exception)
12
+ end
13
+
14
+
15
+ def exception_extract(exception)
16
+ data = {:occurred => DateTime.now, :message => exception.message, :name => exception.class.name, :backtrace => exception.backtrace}
17
+ data[:cause] = extract_exception(exception.cause) if exception.cause
18
+ return data
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
@@ -5,8 +5,12 @@ module Wildsight
5
5
 
6
6
  module Logs
7
7
 
8
- def report_log(log)
9
- event(:log, log)
8
+ def log_data
9
+ @data[:logs] = []
10
+ end
11
+
12
+ def log_report(log)
13
+ log_data << log
10
14
  end
11
15
 
12
16
  end
@@ -28,7 +32,7 @@ module Wildsight
28
32
  }
29
33
  context = @context
30
34
  context = Wildsight::Context.detect unless context
31
- context.report_log(payload)
35
+ context.log_report(payload)
32
36
  return nil
33
37
  end
34
38
 
@@ -1,5 +1,3 @@
1
- require 'pp'
2
-
3
1
  module Wildsight
4
2
  module Context
5
3
 
@@ -7,57 +5,55 @@ module Wildsight
7
5
 
8
6
  RACK_ENV_KEY = 'wildsight.context'
9
7
 
10
- INCLUDE_KEYS = ['SCRIPT_NAME', 'QUERY_STRING', 'PATH_INFO', 'REMOTE_ADDR']
11
- EXCLUDE_KEYS = ['HTTP_COOKIE']
8
+ REQUEST_INCLUDE_KEYS = ['SCRIPT_NAME', 'QUERY_STRING', 'PATH_INFO', 'REMOTE_ADDR']
9
+ REQUEST_EXCLUDE_KEYS = ['HTTP_COOKIE']
10
+ RESPONSE_EXCLUDE_KEYS = ['Set-Cookie']
12
11
 
13
12
  def self.detect_context(env)
14
13
  env[RACK_ENV_KEY] || Wildsight::Context.detect
15
14
  end
16
15
 
17
- def report_rack_request(env)
18
- @started = DateTime.now
16
+ def rack_instrument_request(env)
19
17
  env[RACK_ENV_KEY] = self
18
+ end
19
+
20
+ def rack_data
21
+ @data[:rack] ||= {}
22
+ end
20
23
 
21
- self.request[:occurred] = DateTime.now
22
- self.request[:request_headers] = {}
23
- self.request[:metrics] ||= {}
24
+ def rack_report(env, response = nil)
25
+ env.delete(RACK_ENV_KEY)
24
26
 
25
- self.request[:request_target] = env['SCRIPT_NAME'].to_s + env['PATH_INFO'].to_s
27
+ rack_data[:request_target] ||= env['SCRIPT_NAME'].to_s + env['PATH_INFO'].to_s
26
28
 
27
29
  env.each_pair do |key,value|
28
30
  name = key.to_s
29
- self.request[:request_headers][key] = value if INCLUDE_KEYS.include?(name)
30
- self.request[:request_headers][key] = value if name.start_with?('HTTP_')
31
- self.request[:request_headers][key] = value if name.start_with?('REQUEST_')
32
- self.request[:request_headers][key] = value if name.start_with?('SERVER_')
31
+ rack_data[:request_headers] ||= {}
32
+ rack_data[:request_headers][key] = value if REQUEST_INCLUDE_KEYS.include?(name)
33
+ rack_data[:request_headers][key] = value if name.start_with?('HTTP_')
34
+ rack_data[:request_headers][key] = value if name.start_with?('REQUEST_')
35
+ rack_data[:request_headers][key] = value if name.start_with?('SERVER_')
33
36
  end
34
37
 
35
- EXCLUDE_KEYS.each { |key| self.request[:request_headers].delete(key) }
36
-
37
- if !self.request[:session_id] && !env['rack.session'].nil? && env['rack.session'].respond_to?(:id)
38
- self.request[:session_id] = env['rack.session'].id
38
+ if !rack_data[:session_id] && !env['rack.session'].nil? && env['rack.session'].respond_to?(:id)
39
+ rack_data[:session_id] ||= env['rack.session'].id
39
40
  end
40
- rescue => e
41
- @agent.logger.log(:error, e)
42
- end
43
41
 
44
- def report_rack_response(env, response = nil)
45
- @finished = DateTime.now
46
- env.delete(RACK_ENV_KEY)
42
+ REQUEST_EXCLUDE_KEYS.each { |key| rack_data[:request_headers].delete(key) }
47
43
 
48
44
  if response
49
- self.request[:response_code] = response[0]
50
- self.request[:response_headers] = response[1]
45
+ rack_data[:response_code] ||= response[0]
46
+ rack_data[:response_headers] ||= response[1].dup
47
+ RESPONSE_EXCLUDE_KEYS.each { |key| rack_data[:response_headers].delete(key) }
51
48
  end
52
49
 
53
- self.request[:metrics][:request] = (@finished.to_f - @started.to_f) * 1000
54
-
55
50
  exception = env['rack.exception'] || env['action_dispatch.exception'] || env['sinatra.error']
56
51
 
57
52
  if exception
58
- event(:exception, extract_exception(exception))
53
+ exception_report(exception)
59
54
  end
60
- event(:http_request, self.request)
55
+
56
+ self.submit
61
57
  rescue => e
62
58
  @agent.logger.log(:error, e)
63
59
  end
@@ -0,0 +1,31 @@
1
+ module Wildsight
2
+ module Context
3
+
4
+ module Rails
5
+
6
+ def rails_data
7
+ @data[:rails] ||= {}
8
+ end
9
+
10
+ def rails_extract(controller)
11
+ request = controller.request
12
+
13
+ rack_data[:request_target] = controller.class.name + '#' + request.params['action']
14
+
15
+ rack_data[:method] = request.method
16
+ rack_data[:path] = request.original_fullpath
17
+ rack_data[:params] = request.filtered_parameters
18
+
19
+ rack_data[:session_id] = request.session.id
20
+
21
+ rack_data[:url] = request.original_url
22
+ rack_data[:protocol] = request.protocol
23
+ rack_data[:host] = request.host_with_port
24
+
25
+ rack_data[:remote_ip] = request.remote_ip
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,33 @@
1
+ module Wildsight
2
+ module Profiler
3
+ class Profiler
4
+
5
+ def initialize(context)
6
+ @context = context
7
+ @context.data[:profiler] ||= {}
8
+ end
9
+
10
+ def duration(name, time = nil, options = {}, &block)
11
+ if block_given?
12
+ start = DateTime.now.to_f
13
+ result = block.call
14
+ stop = DateTime.now.to_f
15
+ item = { before: start, after: stop }
16
+ else
17
+ result = nil
18
+ item = { before: time[0], after: time[1] }
19
+ end
20
+ item[:duration] = (item[:after] - item[:before]) * 1000
21
+ durations[name] ||= { intervals: [], duration: 0.0 }
22
+ durations[name][:intervals] << item
23
+ durations[name][:duration] += item[:duration]
24
+ return result
25
+ end
26
+
27
+ def durations
28
+ @context.data[:profiler][:durations] ||= {}
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -10,13 +10,13 @@ module Wildsight
10
10
  def call(env)
11
11
  @context = Wildsight::Agent.default.context
12
12
  @context.bind_thread
13
- @context.report_rack_request(env)
13
+ @context.rack_instrument_request(env)
14
14
  response = @app.call(env)
15
15
  rescue Exception => exception
16
16
  env['rack.exception'] = exception
17
17
  raise exception
18
18
  ensure
19
- @context.report_rack_response(env, response)
19
+ @context.rack_report(env, response)
20
20
  @context.release_thread
21
21
  @context.unregister
22
22
  end
@@ -2,50 +2,95 @@ module Wildsight
2
2
  module Rails
3
3
  class Railtie < ::Rails::Railtie
4
4
 
5
- initializer('wildsight.middleware') do |app|
6
- app.config.middleware.insert_before('ActionDispatch::Executor', Wildsight::Rack::Middleware)
7
- app.config.middleware.insert_after('ActionDispatch::DebugExceptions', Wildsight::Rack::MiddlewareException)
8
- end
9
-
10
5
  initializer('wildsight.rails.logger') do |app|
11
6
  ::Rails.logger.extend(ActiveSupport::Logger.broadcast(Wildsight::Context::Logger.new))
12
7
  end
13
8
 
14
9
  initializer('wildsight.action_controller.extensions') do |app|
15
10
  ActiveSupport.on_load(:action_controller) do
16
- include Wildsight::Rails::ActionController
17
- end
18
- end
19
11
 
20
- initializer('wildsight.action_controller.metrics') do |app|
21
- ActiveSupport::Notifications.subscribe('process_action.action_controller') do |*args|
22
- request = args[4][:request]
12
+ ::Rails::Engine.class_eval do
23
13
 
24
- context = Wildsight::Context::Rack.detect_context(request.env)
14
+ alias_method :call_without_ws, :call
25
15
 
26
- context.request[:request_target] = args[4][:controller_class] + '#' + request.params['action']
16
+ def call(env)
17
+ context = Wildsight::Agent.default.context
18
+ context.bind_thread
19
+ context.rack_instrument_request(env)
27
20
 
28
- context.request[:metrics][:rails] = (args[2] - args[1]) * 1000
29
- context.request[:metrics][:view] = args[4][:view_runtime] || 0
21
+ response = context.profiler.duration(:middleware) { call_without_ws(env) }
22
+
23
+ return response
24
+ ensure
25
+ context.rack_report(env, response)
26
+ context.release_thread
27
+ context.unregister
28
+ end
30
29
 
31
- if context.request[:metrics][:database] == nil || context.request[:metrics][:database] == 0
32
- context.request[:metrics][:database] = args[4][:db_runtime] || 0
33
30
  end
34
31
 
35
- context.request[:method] = args[4][:method]
36
- context.request[:path] = args[4][:path]
37
- context.request[:params] = args[4][:params]
32
+ ::ActionDispatch::Routing::RouteSet.class_eval do
38
33
 
39
- context.request[:response_code] = args[4][:status]
34
+ alias_method :call_without_ws, :call
40
35
 
41
- context.request[:session_id] = request.session.id
42
- context.request[:session] = request.session.to_hash
36
+ def call(env)
37
+ context = Wildsight::Context::Rack.detect_context(env)
38
+ result = context.profiler.duration(:routing) { call_without_ws(env) }
39
+ return result
40
+ end
41
+ end
43
42
 
44
- context.request[:url] = request.original_url
45
- context.request[:protocol] = request.protocol
46
- context.request[:host] = request.host_with_port
43
+ ::AbstractController::Base.class_eval do
44
+
45
+ alias_method :process_without_ws, :process
46
+
47
+ def process(*args)
48
+ context = Wildsight::Context::Rack.detect_context(request.env)
49
+ result = context.profiler.duration(:rails) { process_without_ws(*args) }
50
+ return result
51
+ end
52
+
53
+ end
54
+
55
+ self.class_eval do
56
+
57
+ alias_method :process_action_without_ws, :process_action
58
+
59
+ def wildsight_context
60
+ @_wildsight_context ||= Wildsight::Context::Rack.detect_context(request.env)
61
+ end
62
+
63
+ def process_action(*args)
64
+ context = Wildsight::Context::Rack.detect_context(request.env)
65
+ context.rails_extract(self)
66
+ result = context.profiler.duration(:action) { process_action_without_ws(*args) }
67
+ return result
68
+ rescue Exception => e
69
+ request.env['rack.exception'] = e
70
+ raise e
71
+ end
72
+ end
73
+
74
+ include Wildsight::Rails::ActionController
75
+ end
76
+
77
+ ::AbstractController::Rendering.class_eval do
78
+
79
+ alias_method :render_without_ws, :render
80
+
81
+ def render(*args, &block)
82
+ context = Wildsight::Context::Rack.detect_context(request.env)
83
+ result = context.profiler.duration(:render) { render_without_ws(*args, &block) }
84
+ return result
85
+ end
86
+
87
+ end
88
+
89
+ end
90
+
91
+ initializer('wildsight.action_controller.metrics') do |app|
92
+ ActiveSupport::Notifications.subscribe('process_action.action_controller') do |*args|
47
93
 
48
- context.request[:remote_ip] = request.remote_ip
49
94
  end
50
95
  end
51
96
 
@@ -57,20 +102,21 @@ module Wildsight
57
102
 
58
103
  initializer('wildsight.active_record.metrics') do |app|
59
104
  ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
60
- event = {
61
- :name => args[4][:name],
62
- :statement => args[4][:statement_name],
63
- :binds => args[4][:binds],
64
- :query => args[4][:sql],
65
- :connection => args[4][:connection_id],
66
- :occurred => args[1],
67
- :duration => (args[2] - args[1]) * 1000
68
- }
69
105
  context = Wildsight::Context.detect
70
- context.request[:metrics] ||= {}
71
- context.request[:metrics][:database] ||= 0
72
- context.request[:metrics][:database] += (args[2] - args[1]) * 1000
73
- context.report_sql_query(event)
106
+ if context
107
+ context.profiler.duration(:database, [args[1].to_f, args[2].to_f])
108
+
109
+ event = {
110
+ :name => args[4][:name],
111
+ :statement => args[4][:statement_name],
112
+ :binds => args[4][:binds],
113
+ :query => args[4][:sql],
114
+ :connection => args[4][:connection_id],
115
+ :occurred => args[1],
116
+ :duration => (args[2] - args[1]) * 1000
117
+ }
118
+ context.active_record_report(event)
119
+ end
74
120
  end
75
121
 
76
122
  end
@@ -15,10 +15,8 @@ module Wildsight
15
15
 
16
16
  def send(payload)
17
17
  begin
18
- if payload != nil && payload.size > 0
19
- @agent.logger.log(:debug) { @connection.post(@uri, {'payload' => MultiJson.dump(payload)}).inspect }
20
- @agent.logger.log(:debug) { 'Uploaded' }
21
- end
18
+ data = { payload: MultiJson.dump(payload), client: 'ruby' }
19
+ @agent.logger.log(:debug) { @connection.post(@uri, data).inspect }
22
20
  rescue Exception => e
23
21
  @agent.logger.log(:error) { e }
24
22
  end
@@ -13,9 +13,8 @@ module Wildsight
13
13
 
14
14
  def send(payload)
15
15
  begin
16
- if payload != nil && payload.size > 0
17
- @agent.logger.log(:debug) { Net::HTTP.post_form(@uri, 'payload' => MultiJson.dump(payload)).inspect }
18
- end
16
+ data = { payload: MultiJson.dump(payload), client: 'ruby' }
17
+ @agent.logger.log(:debug) { Net::HTTP.post_form(@uri, data).inspect }
19
18
  rescue Exception => e
20
19
  @agent.logger.log(:error) { e }
21
20
  end
@@ -1,3 +1,3 @@
1
1
  module Wildsight
2
- VERSION = '0.1.11'
2
+ VERSION = '0.1.12'
3
3
  end
@@ -2,12 +2,12 @@ require 'wildsight/version'
2
2
 
3
3
  require 'wildsight/transport/transport'
4
4
  require 'wildsight/metrics/metrics'
5
+ require 'wildsight/profiler/profiler'
5
6
  require 'wildsight/context/context'
6
7
  require 'wildsight/agent/agent'
7
8
 
8
9
  if defined?(Rack)
9
10
  require 'wildsight/rack/middleware_context'
10
- require 'wildsight/rack/middleware_exception'
11
11
  require 'wildsight/rack/middleware'
12
12
  end
13
13
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wildsight
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marek Jelen
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-06-18 00:00:00.000000000 Z
12
+ date: 2016-06-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json
@@ -103,17 +103,19 @@ files:
103
103
  - lib/wildsight/agent/config.yml
104
104
  - lib/wildsight/agent/logger.rb
105
105
  - lib/wildsight/agent/tools.rb
106
+ - lib/wildsight/context/active_record.rb
106
107
  - lib/wildsight/context/context.rb
108
+ - lib/wildsight/context/exception.rb
107
109
  - lib/wildsight/context/logger.rb
108
110
  - lib/wildsight/context/rack.rb
109
- - lib/wildsight/context/sql.rb
111
+ - lib/wildsight/context/rails.rb
110
112
  - lib/wildsight/metrics/counter.rb
111
113
  - lib/wildsight/metrics/gauge.rb
112
114
  - lib/wildsight/metrics/metrics.rb
113
115
  - lib/wildsight/metrics/statistics.rb
116
+ - lib/wildsight/profiler/profiler.rb
114
117
  - lib/wildsight/rack/middleware.rb
115
118
  - lib/wildsight/rack/middleware_context.rb
116
- - lib/wildsight/rack/middleware_exception.rb
117
119
  - lib/wildsight/rails/action_controller.rb
118
120
  - lib/wildsight/rails/active_record.rb
119
121
  - lib/wildsight/rails/railtie.rb
@@ -143,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
145
  version: '0'
144
146
  requirements: []
145
147
  rubyforge_project:
146
- rubygems_version: 2.2.2
148
+ rubygems_version: 2.5.1
147
149
  signing_key:
148
150
  specification_version: 4
149
151
  summary: Multi language/framework/platform error and statistics monitor
@@ -1,13 +0,0 @@
1
- module Wildsight
2
- module Context
3
-
4
- module Sql
5
-
6
- def report_sql_query(event)
7
- event(:sql, event)
8
- end
9
-
10
- end
11
-
12
- end
13
- end
@@ -1,20 +0,0 @@
1
- module Wildsight
2
- module Rack
3
-
4
- class MiddlewareException
5
-
6
- def initialize(app)
7
- @app = app
8
- end
9
-
10
- def call(env)
11
- @app.call(env)
12
- rescue Exception => e
13
- env['rack.exception'] ||= e
14
- raise e
15
- end
16
-
17
- end
18
-
19
- end
20
- end