resque_log_error_parser 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 01983891b93d909cccf6c77177a2a3b4cbf042f8
4
+ data.tar.gz: 4d798bdd4a5ca70ab0d0e8d36556c286dd6015f2
5
+ SHA512:
6
+ metadata.gz: 7096493461a534a1d626f1311336ce0a6894927d74353f0e3e939eb02f375ae8939d22f2a30ddb30f09fc03af8bb1475543fd6ffb76fa89757cf68b14d78902a
7
+ data.tar.gz: da05e6fd1edccf410696ae8b526431ef838b53fee57c018d7f258db3b8af03841bd4db44c8df11eec657a2b8fd05410b2e68ea1f3df0a12632a26a952d78ed69
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # 1.0.0
2
+
3
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "pry"
data/Gemfile.lock ADDED
@@ -0,0 +1,56 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ resque_log_error_parser (0.0.0)
5
+ resque
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ coderay (1.0.9)
11
+ diff-lcs (1.2.4)
12
+ method_source (0.8.2)
13
+ mono_logger (1.1.0)
14
+ multi_json (1.8.1)
15
+ pry (0.9.12.2)
16
+ coderay (~> 1.0.5)
17
+ method_source (~> 0.8)
18
+ slop (~> 3.4)
19
+ rack (1.5.2)
20
+ rack-protection (1.5.0)
21
+ rack
22
+ redis (3.0.5)
23
+ redis-namespace (1.3.1)
24
+ redis (~> 3.0.0)
25
+ resque (1.25.1)
26
+ mono_logger (~> 1.0)
27
+ multi_json (~> 1.0)
28
+ redis-namespace (~> 1.2)
29
+ sinatra (>= 0.9.2)
30
+ vegas (~> 0.1.2)
31
+ rspec (2.14.1)
32
+ rspec-core (~> 2.14.0)
33
+ rspec-expectations (~> 2.14.0)
34
+ rspec-mocks (~> 2.14.0)
35
+ rspec-core (2.14.5)
36
+ rspec-expectations (2.14.3)
37
+ diff-lcs (>= 1.1.3, < 2.0)
38
+ rspec-mocks (2.14.3)
39
+ sinatra (1.4.3)
40
+ rack (~> 1.4)
41
+ rack-protection (~> 1.4)
42
+ tilt (~> 1.3, >= 1.3.4)
43
+ slop (3.4.6)
44
+ tilt (1.4.1)
45
+ timecop (0.6.1)
46
+ vegas (0.1.11)
47
+ rack (>= 1.0.0)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ pry
54
+ resque_log_error_parser!
55
+ rspec
56
+ timecop
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 G5
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # Resque Log File Error Parser
2
+
3
+ Resque worker that parses log files given filters, and executes a callback of your choice.
4
+
5
+ ## Install
6
+
7
+ gem "resque_log_file_error_parser"
8
+
9
+ ## Setup:
10
+
11
+ ResqueLogFileErrorParser.queue = :queue_the_worker_should_listen_to
12
+ ResqueLogFileErrorParser.on_error do |line_in_log_with_error|
13
+ # You can email yourself, or report to PagerDuty like:
14
+ p = Pagerduty.new("key here")
15
+ p.trigger("Error found: #{line_in_log_with_error}")
16
+ end
17
+
18
+ # If your logs are located in a place other than "log/*.log", then you need to override the log_paths:
19
+ ResqueLogFileErrorParser.log_paths = ["some/other/*.log"]
20
+
21
+ ## Scheduling
22
+
23
+ ResqueLogFileErrorParser is intended to be run periodically (e.g. every hour) using `resque_scheduler` or `cron`.
24
+
25
+ ## Processing log entries
26
+
27
+ Right now, the worker will only process log lines that match the following format (assuming the job was executed on Oct 7, 2013, between 6pm and 7pm):
28
+
29
+ E, [2013-10-07T18:01:04.194981 #28727] ERROR -- : Error line today
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = FileList['spec/**/*_spec.rb']
7
+ end
@@ -0,0 +1,19 @@
1
+ module ResqueLogErrorParser
2
+ class Lines < Array
3
+
4
+ def filter_by(*filters)
5
+ filters = [filters].flatten
6
+ filtered_lines = self.dup
7
+ filters.each do |filter|
8
+ filtered_lines = filtered_lines.grep(/#{filter}/)
9
+ end
10
+ self.class.new(filtered_lines)
11
+ end
12
+
13
+ def strip_newlines
14
+ stripped_lines = self.map { |line| line.gsub("\n", "") }
15
+ self.class.new(stripped_lines)
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module ResqueLogErrorParser
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,43 @@
1
+ module ResqueLogErrorParser
2
+ class Worker
3
+
4
+ def self.queue
5
+ if ResqueLogErrorParser.queue.nil?
6
+ raise ArgumentError, "you must set ResqueLogErrorParser.queue"
7
+ end
8
+
9
+ ResqueLogErrorParser.queue
10
+ end
11
+
12
+ def self.perform
13
+ self.log_files.each do |file|
14
+ self.lines_for(file).each do |line|
15
+ ResqueLogErrorParser.error_handler.call(line)
16
+ end
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def self.log_files
23
+ ResqueLogErrorParser.log_paths.map do |log_path|
24
+ Dir.glob(log_path)
25
+ end.flatten
26
+ end
27
+
28
+ def self.lines_for(file)
29
+ lines = Lines.new(File.open(file).lines.to_a)
30
+ lines.filter_by(self.filters).strip_newlines
31
+ end
32
+
33
+ def self.filters
34
+ [self.time_filter_regex, ResqueLogErrorParser.line_filter_keys].flatten
35
+ end
36
+
37
+ def self.time_filter_regex
38
+ time_filter_str = Time.now.strftime(ResqueLogErrorParser.line_date_format)
39
+ /#{time_filter_str}/
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,46 @@
1
+ module ResqueLogErrorParser
2
+ DEFAULT_LOG_PATHS = ["log/*.log"]
3
+ DEFAULT_LINE_DATE_FORMAT = '%Y-%m-%dT%H'
4
+ DEFAULT_LINE_FILTER_KEYS = ["ERROR"]
5
+
6
+ def self.queue=(q)
7
+ @queue = q
8
+ end
9
+
10
+ def self.queue
11
+ @queue
12
+ end
13
+
14
+ def self.log_paths
15
+ return [@log_paths].flatten unless @log_paths.nil?
16
+ return DEFAULT_LOG_PATHS
17
+ end
18
+
19
+ def self.log_paths=(paths)
20
+ @log_paths = paths
21
+ end
22
+
23
+ def self.on_error(&block)
24
+ @on_error = block
25
+ end
26
+
27
+ def self.error_handler
28
+ @on_error
29
+ end
30
+
31
+ def self.line_date_format
32
+ DEFAULT_LINE_DATE_FORMAT
33
+ end
34
+
35
+ def self.line_filter_keys
36
+ DEFAULT_LINE_FILTER_KEYS
37
+ end
38
+
39
+ def self.restore_defaults!
40
+ self.log_paths = DEFAULT_LOG_PATHS
41
+ end
42
+
43
+ end
44
+
45
+ require "resque_log_error_parser/worker"
46
+ require "resque_log_error_parser/lines"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "resque_log_error_parser/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "resque_log_error_parser"
8
+ spec.version = ResqueLogErrorParser::VERSION
9
+ spec.authors = ["G5 Search Marketing"]
10
+ spec.email = ["nestor@aelogica.com", "ramon@aelogica.com"]
11
+ spec.description = %q{Resque worker that parses log files given filters, and executes a callback of your choice.}
12
+ spec.summary = %q{Resque worker that parses log files given filters, and executes a callback of your choice.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "resque"
22
+ spec.add_development_dependency "rspec"
23
+ spec.add_development_dependency "timecop"
24
+ end
@@ -0,0 +1,3 @@
1
+ E, [2013-10-05T17:01:04.194981 #28727] ERROR -- : Error line yesterday
2
+ E, [2013-10-06T17:01:04.194981 #28727] ERROR -- : Error line today
3
+ D, [2013-10-06T17:01:04.423205 #28727] DEBUG -- : No error
@@ -0,0 +1,45 @@
1
+ require "spec_helper"
2
+
3
+ describe ResqueLogErrorParser::Lines do
4
+
5
+ describe "#filter_by" do
6
+ let(:lines) do
7
+ described_class.new(["hello", "world", "helloworld"])
8
+ end
9
+
10
+ it "returns a Lines instance filtered by the given strings" do
11
+ filtered_lines = lines.filter_by("llo", /orld/)
12
+ expect(filtered_lines.count).to eq(1)
13
+ expect(filtered_lines).to include("helloworld")
14
+ end
15
+
16
+ it "returns an instance of Lines" do
17
+ filtered_lines = lines.filter_by(["llo"])
18
+ expect(filtered_lines).to be_kind_of(described_class)
19
+ end
20
+
21
+ context "argument is an array" do
22
+ it "filters the lines given the array elements" do
23
+ filtered_lines = lines.filter_by(["ell", /hello/])
24
+ expect(filtered_lines.count).to eq(2)
25
+ expect(filtered_lines).to include("hello")
26
+ expect(filtered_lines).to include("helloworld")
27
+ end
28
+ end
29
+
30
+ context "there are no filters" do
31
+ it "returns the original" do
32
+ filtered_lines = lines.filter_by([])
33
+ expect(filtered_lines).to eq(lines)
34
+ end
35
+ end
36
+ end
37
+
38
+ describe "#strip_newlines" do
39
+ let(:lines) { described_class.new(["hello\n"]) }
40
+ subject { lines.strip_newlines }
41
+ it { should eq(["hello"]) }
42
+ it { should be_kind_of(described_class) }
43
+ end
44
+
45
+ end
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+
3
+ describe ResqueLogErrorParser::Worker do
4
+
5
+ describe ".queue" do
6
+ it "refers to the queue set in ResqueLogErrorParser" do
7
+ ResqueLogErrorParser.queue = :q
8
+ expect(described_class.queue).to eq(:q)
9
+ ResqueLogErrorParser.queue = :r
10
+ expect(described_class.queue).to eq(:r)
11
+ end
12
+
13
+ context "when queue has not been set on ResqueLogErrorParser" do
14
+ it "raises an error" do
15
+ ResqueLogErrorParser.queue = nil
16
+ expect { described_class.queue }.to raise_error(ArgumentError, "you must set ResqueLogErrorParser.queue")
17
+ end
18
+ end
19
+ end
20
+
21
+ describe ".perform" do
22
+ it "should call the error_handler block for each matching line in the logs for the given hour" do
23
+ Timecop.travel(2013, 10, 6, 17)
24
+
25
+ ResqueLogErrorParser.log_paths = "#{SPEC_DIR}/fixtures/log/*.log"
26
+ lines = []
27
+ ResqueLogErrorParser.on_error do |line|
28
+ lines << line
29
+ end
30
+ described_class.perform
31
+ expect(lines.count).to eq(1)
32
+ expect(lines).to include("E, [2013-10-06T17:01:04.194981 #28727] ERROR -- : Error line today")
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,62 @@
1
+ require "spec_helper"
2
+
3
+ describe ResqueLogErrorParser do
4
+
5
+ describe ".queue=" do
6
+ it "sets the queue on the module" do
7
+ described_class.queue = :a
8
+ expect(described_class.queue).to eq(:a)
9
+ described_class.queue = :b
10
+ expect(described_class.queue).to eq(:b)
11
+ end
12
+ end
13
+
14
+ describe ".log_paths" do
15
+ it "defaults to ['log/*.log']" do
16
+ expect(described_class.log_paths).to eq(['log/*.log'])
17
+ end
18
+
19
+ context "when log_paths was set to nil" do
20
+ it "return the default log paths" do
21
+ described_class.log_paths = nil
22
+ expect(described_class.log_paths).to eq(["log/*.log"])
23
+ end
24
+ end
25
+ end
26
+
27
+ describe ".log_paths=" do
28
+ it "sets the log_paths" do
29
+ described_class.log_paths = ["SOMETHING"]
30
+ expect(described_class.log_paths).to eq(["SOMETHING"])
31
+ end
32
+
33
+ context "a non-enumerable was passed" do
34
+ it "should wrap with an array" do
35
+ described_class.log_paths = "SOMETHING"
36
+ expect(described_class.log_paths).to eq(["SOMETHING"])
37
+ end
38
+ end
39
+ end
40
+
41
+ describe ".on_error" do
42
+ let(:block) { lambda { |msg| msg } }
43
+
44
+ it "sets the callback function" do
45
+ described_class.on_error(&block)
46
+ expect(described_class.error_handler).to eq(block)
47
+ end
48
+ end
49
+
50
+ describe ".line_date_format" do
51
+ it "defaults to '%Y-%m-%dT%H'" do
52
+ expect(described_class.line_date_format).to eq('%Y-%m-%dT%H')
53
+ end
54
+ end
55
+
56
+ describe ".line_filter_keys" do
57
+ it "defaults to ['ERROR']" do
58
+ expect(described_class.line_filter_keys).to eq(['ERROR'])
59
+ end
60
+ end
61
+
62
+ end
@@ -0,0 +1,14 @@
1
+ SPEC_DIR = File.expand_path(File.dirname(__FILE__))
2
+
3
+ require "bundler/setup"
4
+ Bundler.require(:test, :development, :defaults)
5
+
6
+ require "resque_log_error_parser"
7
+
8
+ RSpec.configure do |config|
9
+ config.before(:each) do
10
+ ResqueLogErrorParser.restore_defaults!
11
+ end
12
+
13
+ config.order = 'random'
14
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque_log_error_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - G5 Search Marketing
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: resque
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: timecop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Resque worker that parses log files given filters, and executes a callback
56
+ of your choice.
57
+ email:
58
+ - nestor@aelogica.com
59
+ - ramon@aelogica.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - CHANGELOG.md
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE
68
+ - README.md
69
+ - Rakefile
70
+ - lib/resque_log_error_parser.rb
71
+ - lib/resque_log_error_parser/lines.rb
72
+ - lib/resque_log_error_parser/version.rb
73
+ - lib/resque_log_error_parser/worker.rb
74
+ - resque_log_error_parser.gemspec
75
+ - spec/fixtures/log/test.log
76
+ - spec/resque_log_error_parser/lines_spec.rb
77
+ - spec/resque_log_error_parser/worker_spec.rb
78
+ - spec/resque_log_error_parser_spec.rb
79
+ - spec/spec_helper.rb
80
+ homepage: ''
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.0.3
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Resque worker that parses log files given filters, and executes a callback
104
+ of your choice.
105
+ test_files:
106
+ - spec/fixtures/log/test.log
107
+ - spec/resque_log_error_parser/lines_spec.rb
108
+ - spec/resque_log_error_parser/worker_spec.rb
109
+ - spec/resque_log_error_parser_spec.rb
110
+ - spec/spec_helper.rb