resque-logstash 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +42 -0
- data/Rakefile +6 -0
- data/lib/resque/logstash/config.rb +17 -0
- data/lib/resque/logstash/transport/redis.rb +23 -0
- data/lib/resque/logstash/version.rb +7 -0
- data/lib/resque/logstash.rb +47 -0
- data/resque-logstash.gemspec +26 -0
- data/spec/logstash_spec.rb +91 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/transport/redis_spec.rb +22 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3b18485e48d33c757334ae4e942731d81d8aee89
|
4
|
+
data.tar.gz: 39750b6d4041405197fe1c13dc0cb9de91bb679c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0cee284bd671941f94e7d5bade7d0c064a7336550f013925ce029e1923b08807f4eee0899f391f2c0e1658c7a5721357d57eace9cf3c8ac7b4b00bc9dbe7ce5e
|
7
|
+
data.tar.gz: 6a0840047311870353ad81c89d88efe03dc111f36f4e41a40bc48a6da4d506486e7eff907e1aeff1f7ddb0f6c9bd0566436b0e402ef4af8f53237aa57d6f1a6f
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Eugene Pimenov
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Resque::Logstash
|
2
|
+
|
3
|
+
Logs duration of a resque job in logstash. At the moment only redis
|
4
|
+
transport is supported.
|
5
|
+
|
6
|
+
Depends on Resque 1.24
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'resque-logstash'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install resque-logstash
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
Resque::Plugins::Logstash.transport = Resque::Plugins::Logstash::Transport::Redis.new('localhost', 6379)
|
26
|
+
|
27
|
+
class SomeJob
|
28
|
+
extend Resque::Plugins::Logstash
|
29
|
+
|
30
|
+
def self.perform
|
31
|
+
# do the heavy lifting here
|
32
|
+
end
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
1. Fork it
|
39
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
40
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
41
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
42
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
module Resque::Plugins
|
2
|
+
module Logstash
|
3
|
+
class Config
|
4
|
+
attr_accessor :transport, :disabled, :tags
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@transport = Transport::Redis.new('localhost', 6379)
|
8
|
+
@disabled = false
|
9
|
+
@tags = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def disabled?
|
13
|
+
disabled
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Resque::Plugins
|
2
|
+
module Logstash
|
3
|
+
module Transport
|
4
|
+
class Redis
|
5
|
+
def initialize(host, port, key = 'logstash')
|
6
|
+
@host = host
|
7
|
+
@port = port
|
8
|
+
@key = key
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_accessor :host, :port, :key
|
12
|
+
|
13
|
+
def redis
|
14
|
+
@redis ||= ::Redis.new(host: host, port: port)
|
15
|
+
end
|
16
|
+
|
17
|
+
def push(value)
|
18
|
+
redis.rpush @key, value.to_json
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "resque"
|
2
|
+
require "logstash/event"
|
3
|
+
require 'forwardable'
|
4
|
+
|
5
|
+
require "resque/logstash/version"
|
6
|
+
require 'resque/logstash/transport/redis'
|
7
|
+
require 'resque/logstash/config'
|
8
|
+
|
9
|
+
|
10
|
+
module Resque::Plugins
|
11
|
+
module Logstash
|
12
|
+
class << self
|
13
|
+
extend Forwardable
|
14
|
+
|
15
|
+
# support old API
|
16
|
+
def_delegators :config, :transport, :transport=, :tags, :tags=
|
17
|
+
|
18
|
+
def configure
|
19
|
+
yield config
|
20
|
+
end
|
21
|
+
|
22
|
+
def config
|
23
|
+
@config ||= Config.new
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def around_perform_logstash_measure(*args)
|
28
|
+
started_at = Time.now
|
29
|
+
yield
|
30
|
+
ensure
|
31
|
+
logstash_push_duration Time.now - started_at, args
|
32
|
+
end
|
33
|
+
|
34
|
+
def logstash_push_duration(duration, args)
|
35
|
+
return if Logstash.config.disabled?
|
36
|
+
Logstash.config.transport.push logstash_create_event(duration, args)
|
37
|
+
end
|
38
|
+
|
39
|
+
def logstash_create_event(duration, args)
|
40
|
+
LogStash::Event.new "message" => "Job #{self.name} finished in #{duration}s",
|
41
|
+
"job" => self.name,
|
42
|
+
"duration" => duration,
|
43
|
+
"job_arguments" => args.map { |a| a.to_s },
|
44
|
+
"tags" => Logstash.config.tags
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'resque/logstash/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "resque-logstash"
|
8
|
+
spec.version = Resque::Plugins::Logstash::VERSION
|
9
|
+
spec.authors = ["Eugene Pimenov"]
|
10
|
+
spec.email = ["eugene@libc.st"]
|
11
|
+
spec.description = %q{Measure duration of a resque job and log it in the logstash}
|
12
|
+
spec.summary = %q{A really simple logstash logger for resque}
|
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_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_dependency 'resque', '~> 1.24'
|
25
|
+
spec.add_dependency 'logstash-event', '~> 1.2'
|
26
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Resque::Plugins::Logstash do
|
4
|
+
class JobLike
|
5
|
+
extend Resque::Plugins::Logstash
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:job) { JobLike }
|
9
|
+
|
10
|
+
before do
|
11
|
+
Resque::Plugins::Logstash.transport = double(:push => nil)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'complient with resque plugin policy' do
|
15
|
+
expect { Resque::Plugin.lint(Resque::Plugins::Logstash) }.not_to raise_error
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#around_perform_logstash_measure' do
|
19
|
+
it 'calls logstash_push_time with the duration' do
|
20
|
+
expect(job).to receive(:logstash_push_duration).with(be_within(0.01).of(0.3), [])
|
21
|
+
|
22
|
+
job.around_perform_logstash_measure { sleep 0.3 }
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'logs job arguments' do
|
26
|
+
expect(job).to receive(:logstash_push_duration).with(kind_of(Numeric), [:test, "blah"])
|
27
|
+
expect { job.around_perform_logstash_measure(:test, "blah") {} }.not_to raise_error
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '#logstash_create_event' do
|
32
|
+
let(:event) { job.logstash_create_event 0.3, [:arg1, "arg2"] }
|
33
|
+
|
34
|
+
it 'puts classname as the job field' do
|
35
|
+
expect(event.fields['job']).to eq('JobLike')
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'puts duration in the field' do
|
39
|
+
expect(event.fields['duration']).to eq(0.3)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'provides a nice message' do
|
43
|
+
expect(event['message']).to eq("Job JobLike finished in 0.3s")
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'adds tags' do
|
47
|
+
Resque::Plugins::Logstash.tags = %w{tag1 tag2}
|
48
|
+
|
49
|
+
expect(event.tags).to include(*%w{tag1 tag2})
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'adds job arguments as Strings' do
|
53
|
+
expect(event.fields['job_arguments']).to eq(%w{arg1 arg2})
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe '#logstash_push_duration' do
|
58
|
+
it 'calls push on @transport' do
|
59
|
+
Resque::Plugins::Logstash.transport = double
|
60
|
+
expect(Resque::Plugins::Logstash.transport).to receive(:push)
|
61
|
+
|
62
|
+
job.logstash_push_duration(0.3, [])
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'does not push if disabled' do
|
66
|
+
Resque::Plugins::Logstash.configure { |c| c.disabled = true }
|
67
|
+
|
68
|
+
expect(Resque::Plugins::Logstash.transport).not_to receive(:push)
|
69
|
+
job.logstash_push_duration(0.3, [])
|
70
|
+
|
71
|
+
Resque::Plugins::Logstash.configure { |c| c.disabled = false }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe '#configure' do
|
76
|
+
it 'yields' do
|
77
|
+
yielded = false
|
78
|
+
Resque::Plugins::Logstash.configure { yielded = true }
|
79
|
+
expect(yielded).to be_true
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'yield config object' do
|
83
|
+
Resque::Plugins::Logstash.configure do |c|
|
84
|
+
%w{transport tags disabled}.each do |method|
|
85
|
+
expect(c).to respond_to("#{method}=")
|
86
|
+
expect(c).to respond_to("#{method}")
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'resque/logstash'
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Resque::Plugins::Logstash::Transport::Redis do
|
4
|
+
let(:transport) { Resque::Plugins::Logstash::Transport::Redis.new('localhost', 6379) }
|
5
|
+
|
6
|
+
describe '#initialize' do
|
7
|
+
it 'accepts host, port and the key' do
|
8
|
+
transport = Resque::Plugins::Logstash::Transport::Redis.new('host', 42, 'key')
|
9
|
+
expect(transport.host).to eq('host')
|
10
|
+
expect(transport.port).to eq(42)
|
11
|
+
expect(transport.key).to eq('key')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#push' do
|
16
|
+
it 'calls rpush on redis' do
|
17
|
+
expect(transport.redis).to receive(:rpush).with('logstash', '{"a":1}')
|
18
|
+
|
19
|
+
transport.push(a: 1)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resque-logstash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eugene Pimenov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
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: rspec
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: resque
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.24'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.24'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: logstash-event
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.2'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.2'
|
83
|
+
description: Measure duration of a resque job and log it in the logstash
|
84
|
+
email:
|
85
|
+
- eugene@libc.st
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- .rspec
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- lib/resque/logstash.rb
|
97
|
+
- lib/resque/logstash/config.rb
|
98
|
+
- lib/resque/logstash/transport/redis.rb
|
99
|
+
- lib/resque/logstash/version.rb
|
100
|
+
- resque-logstash.gemspec
|
101
|
+
- spec/logstash_spec.rb
|
102
|
+
- spec/spec_helper.rb
|
103
|
+
- spec/transport/redis_spec.rb
|
104
|
+
homepage: ''
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - '>='
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.0.3
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: A really simple logstash logger for resque
|
128
|
+
test_files:
|
129
|
+
- spec/logstash_spec.rb
|
130
|
+
- spec/spec_helper.rb
|
131
|
+
- spec/transport/redis_spec.rb
|