log_parser_notifier 0.0.5 → 0.0.6

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: 95449b6a9352c49727c2c98767ae726617704bec
4
- data.tar.gz: 505d4e48911465e389179ffc76ab43ce29f95f2a
3
+ metadata.gz: 25e36655772d7de5a40f8243990da5d7c29e12a7
4
+ data.tar.gz: a5c8fb09ca8fd942526cb5a257ce62362bf34b67
5
5
  SHA512:
6
- metadata.gz: 9750ed89f239f2f4ad67372f9aaecbc07271c87a8e17df2bdc78e2370c8bcbc1eebf0e917849b67ed31e05d3edcfc0f021de036747f61e50d3c73a529a229e43
7
- data.tar.gz: 7369783bce6d5c012ee6d804fc598a7e46ad3c9da424fc938b0b1b0dd8d5e41963a9ce91ccd8ee2ea080623b0f7191659307d642e818047fdbff352d3a5c530b
6
+ metadata.gz: cc09fc1488a2bd4b266f5c60df9fa34d7a1412dbbb94e92e7e1e6aa6fff26de29522b90acd385acc601804860c225bfff2e793c03032b315074a64328323bd80
7
+ data.tar.gz: cf332ab19457b39caea0a5e79d3092fd28531c13e43d18a48d60f629ce4135f9ae2660de76e92783978f1ff16d61f17fa573325b7ff5c29de9cce799c2e593c5
@@ -9,6 +9,7 @@ end
9
9
  opts = Slop.parse! do |o|
10
10
  o.banner = 'usage: lib_parser_notifier [options] file_name'
11
11
  o.on :c, :check_interval=, 'check interval (in minutes)', default: 5, as: Integer
12
+ o.on :t, :slow_request_threshold=, 'How long does a request need to create an event? (in ms)', default: 500, as: Integer
12
13
  o.on :verbose, 'enable verbose mode'
13
14
  # o.integer '--check_interval', 'check interval (in minutes)', default: 5
14
15
  # o.bool '-v', '--verbose', 'enable verbose mode'
@@ -32,9 +33,7 @@ end
32
33
 
33
34
  require File.join(File.dirname(__FILE__), '..', 'lib', 'log_parser_notifier')
34
35
 
35
- puts ARGV.inspect
36
-
37
- request_notifier = LogParserNotifier::RailsRequestNotifier.new(ARGV.first)
36
+ request_notifier = LogParserNotifier::RailsRequestNotifier.new(ARGV.first, opts[:slow_request_threshold])
38
37
 
39
38
  while true
40
39
  LogParserNotifier.logger.debug('waking up and checking log')
@@ -1,9 +1,12 @@
1
1
  require 'statsd'
2
+ require 'time'
2
3
 
3
4
  module LogParserNotifier
4
5
  class RailsRequestNotifier
5
- def initialize(log_file)
6
+ def initialize(log_file, slow_request_threshold = 500)
6
7
  @log_reader = LogReader.new log_file
8
+ @slow_request_threshold = slow_request_threshold
9
+ puts @slow_request_threshold
7
10
  end
8
11
 
9
12
  def parser
@@ -20,13 +23,44 @@ module LogParserNotifier
20
23
 
21
24
  LogParserNotifier.logger.debug("processing request for #{request[:controller]}##{request[:action]} with status: #{request[:status]} it is complete: #{request.completed?}")
22
25
  if request.completed? && request[:status] >= 200 && request[:status] < 400
23
-
24
26
  statsd.increment 'rails.requests', tags: tags
25
27
  statsd.timing 'rails.load_time', request[:duration] * 1000, tags: tags
28
+
29
+ if request[:duration] * 1000 >= @slow_request_threshold
30
+ log = request.lines.collect { |line_hash| RailsRequestNotifier.render_line(line_hash) }.join("\n")
31
+ statsd.event(
32
+ "#{request[:controller]}##{request[:action]} #{request[:method]} slow request #{request[:duration]} sec",
33
+ log,
34
+ {
35
+ date_happened: Time.parse(request[:timestamp].to_s).to_i,
36
+ aggregation_key: 'slow_request_rails',
37
+ source_type_name: 'rails_log',
38
+ alert_type: 'warning',
39
+ tags: tags
40
+ }
41
+ )
42
+ end
26
43
  else
27
44
  statsd.increment 'rails.failed_requests', tags: tags
28
45
  end
29
46
  end
30
47
  end
48
+
49
+ def self.render_line(line_hash)
50
+ case line_hash[:line_type]
51
+ when :started
52
+ %Q(Started #{line_hash[:method]} "#{line_hash[:path]}" for #{line_hash[:ip]} at #{Time.parse(line_hash[:timestamp].to_s)})
53
+ when :processing
54
+ %Q(Processing by #{line_hash[:controller]}##{line_hash[:action]} as #{line_hash[:format]})
55
+ when :parameters
56
+ %Q(Parameters: #{line_hash[:params].inspect})
57
+ when :rendered
58
+ " Rendered #{line_hash[:rendered_file]} (#{line_hash[:partial_duration]}ms)"
59
+ when :completed
60
+ "Completed #{line_hash[:status]} OK in #{(line_hash[:duration] * 1000).to_i}ms (Views: #{line_hash[:view] * 1000}ms | ActiveRecord: #{line_hash[:db] * 1000}ms)"
61
+ else
62
+ line_hash.inspect
63
+ end
64
+ end
31
65
  end
32
66
  end
@@ -1,3 +1,3 @@
1
1
  module LogParserNotifier
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
@@ -26,4 +26,5 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency 'bundler', '~> 1.7'
27
27
  spec.add_development_dependency 'rake', '~> 10.0'
28
28
  spec.add_development_dependency 'rspec', '~> 3.3.0'
29
+ spec.add_development_dependency 'pry'
29
30
  end
@@ -8,10 +8,9 @@ module LogParserNotifier
8
8
  RailsRequestNotifier.new(@logs)
9
9
  end
10
10
 
11
- after { rails_request_notifier.parse_and_trigger_notifications }
12
-
13
11
  describe 'successful requests' do
14
12
  before { @logs = RailsLogRequests.normal_request_1 }
13
+ after { rails_request_notifier.parse_and_trigger_notifications }
15
14
 
16
15
  it 'should record the number of successful requests' do
17
16
  expect_any_instance_of(Statsd).to receive(:increment).with('rails.requests', tags: %w(controller:DashboardController action:show con_act:DashboardController#show))
@@ -24,10 +23,31 @@ module LogParserNotifier
24
23
  it 'should not record the number of failed requests' do
25
24
  expect_any_instance_of(Statsd).to_not receive(:increment).with('rails.failed_requests', anything)
26
25
  end
26
+
27
+ it 'should not create an event for short requests' do
28
+ expect_any_instance_of(Statsd).to_not receive('event')
29
+ end
30
+
31
+ describe 'slow requests' do
32
+ before { @logs = RailsLogRequests.slow_request }
33
+
34
+ it 'should create an event for the slow requests' do
35
+ expect_any_instance_of(Statsd).to receive('event').with(
36
+ 'DocumentsController#new GET slow request 0.8 sec',
37
+ "Started GET \"/documents/new?template_id=30000&state=AK\" for 123.193.169.96 at 2015-09-17 03:19:58 +0800\nProcessing by DocumentsController#new as HTML\nParameters: {:template_id=>\"30000\", :state=>\"AK\"}\n Rendered documents/_preview.html.erb (0.0ms)\n Rendered shared/_contact_number.erb (0.0ms)\n Rendered shared/_disclaimer.html.erb (0.001ms)\nCompleted 200 OK in 800ms (Views: 34.0ms | ActiveRecord: 4.0ms)",
38
+ date_happened: 1442431198,
39
+ aggregation_key: 'slow_request_rails',
40
+ source_type_name: 'rails_log',
41
+ alert_type: 'warning',
42
+ tags: %w(controller:DocumentsController action:new con_act:DocumentsController#new)
43
+ )
44
+ end
45
+ end
27
46
  end
28
47
 
29
48
  describe 'failed request' do
30
49
  before { @logs = RailsLogRequests.failed_request }
50
+ after { rails_request_notifier.parse_and_trigger_notifications }
31
51
 
32
52
  it 'should record the number of failed requests' do
33
53
  expect_any_instance_of(Statsd).to receive(:increment).with('rails.failed_requests', tags: %w(controller:DocumentsController action:preview con_act:DocumentsController#preview))
@@ -47,6 +67,41 @@ module LogParserNotifier
47
67
  @logs = RailsLogRequests.partial_request
48
68
  expect_any_instance_of(Statsd).to_not receive(:increment).with('rails.requests', tags: %w(controller:DashboardController action:index con_act:DashboardController#index))
49
69
  expect_any_instance_of(Statsd).to_not receive(:timing).with('rails.load_time', anything, tags: %w(controller:DashboardController action:index con_act:DashboardController#index))
70
+
71
+ rails_request_notifier.parse_and_trigger_notifications
72
+ end
73
+ end
74
+
75
+ describe '.render_line' do
76
+ subject do
77
+ RailsRequestNotifier.render_line @line_hash
78
+ end
79
+ it 'should render start line' do
80
+ @line_hash = { method: 'GET', path: '/documents/new?template_id=30000&state=AK', ip: '123.193.169.96', timestamp: 20_150_917_031_958, line_type: :started, lineno: 1, source: nil, compound: [] }
81
+ expect(subject).to eq 'Started GET "/documents/new?template_id=30000&state=AK" for 123.193.169.96 at 2015-09-17 03:19:58 +0800'
82
+ end
83
+
84
+ it 'should render the process line' do
85
+ @line_hash = { controller: 'DocumentsController', action: 'new', format: 'HTML', line_type: :processing, lineno: 2, source: nil, compound: [] }
86
+ expect(subject).to eq 'Processing by DocumentsController#new as HTML'
87
+ end
88
+
89
+ it 'should render the params line' do
90
+ @line_hash = { params: { template_id: '30000', state: 'AK' }, line_type: :parameters, lineno: 3, source: nil, compound: [] }
91
+
92
+ expect(subject).to eq 'Parameters: {:template_id=>"30000", :state=>"AK"}'
93
+ end
94
+
95
+ it 'should render the render line' do
96
+ @line_hash = { rendered_file: 'documents/_preview.html.erb', partial_duration: 0.0, line_type: :rendered, lineno: 4, source: nil, compound: [:partial_duration] }
97
+
98
+ expect(subject).to eq ' Rendered documents/_preview.html.erb (0.0ms)'
99
+ end
100
+
101
+ it 'should render the completed line' do
102
+ @line_hash = { status: 200, duration: 0.8, view: 0.034, db: 0.004, line_type: :completed, lineno: 7, source: nil, compound: [] }
103
+
104
+ expect(subject).to eq 'Completed 200 OK in 800ms (Views: 34.0ms | ActiveRecord: 4.0ms)'
50
105
  end
51
106
  end
52
107
  end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'bundler/setup'
2
2
  require 'logger'
3
+ require 'pry'
3
4
 
4
5
  Bundler.setup
5
6
 
@@ -19,6 +19,17 @@ I, [2015-08-24T07:36:43.695000 #19020] INFO -- : Completed 200 OK in 91ms (View
19
19
  )
20
20
  end
21
21
 
22
+ def self.slow_request
23
+ StringIO.new %Q(I, [2015-09-17T03:19:58.989000 #12393] INFO -- : Started GET "/documents/new?template_id=30000&state=AK" for 123.193.169.96 at 2015-09-17 03:19:58 +0000
24
+ I, [2015-09-17T03:19:58.994000 #12393] INFO -- : Processing by DocumentsController#new as HTML
25
+ I, [2015-09-17T03:19:58.994000 #12393] INFO -- : Parameters: {"template_id"=>"30000", "state"=>"AK"}
26
+ I, [2015-08-24T07:36:43.688000 #19020] INFO -- : Rendered documents/_preview.html.erb (0.0ms)
27
+ I, [2015-08-24T07:36:43.692000 #19020] INFO -- : Rendered shared/_contact_number.erb (0.0ms)
28
+ I, [2015-08-24T07:36:43.694000 #19020] INFO -- : Rendered shared/_disclaimer.html.erb (1.0ms)
29
+ I, [2015-08-24T07:36:43.695000 #19020] INFO -- : Completed 200 OK in 800ms (Views: 34.0ms | ActiveRecord: 4.0ms)
30
+ )
31
+ end
32
+
22
33
  def self.failed_request
23
34
  StringIO.new %Q(I, [2015-08-24T07:35:11.541000 #19020] INFO -- : Started GET "/documents/9293/preview/?page=0" for 50.31.29.217 at 2015-08-24 07:35:11 +0000
24
35
  I, [2015-08-24T07:35:11.549000 #19020] INFO -- : Processing by DocumentsController#preview as JSON
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: log_parser_notifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Moore
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: 3.3.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  description: Write a longer description. Optional.
98
112
  email:
99
113
  - joshsmoore@gmail.com