rails_riemann_middleware 0.5.0
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.
- data/.gitignore +18 -0
- data/Gemfile +14 -0
- data/LICENSE +22 -0
- data/README.md +40 -0
- data/Rakefile +9 -0
- data/lib/rails_riemann_middleware.rb +30 -0
- data/lib/rails_riemann_middleware/duration.rb +32 -0
- data/lib/rails_riemann_middleware/event.rb +40 -0
- data/lib/rails_riemann_middleware/exception_notification.rb +36 -0
- data/lib/rails_riemann_middleware/version.rb +3 -0
- data/rails_riemann_middleware.gemspec +20 -0
- data/specs/duraction_spec.rb +44 -0
- data/specs/spec_helper.rb +7 -0
- metadata +70 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# -*- mode: Ruby; tab-width: 2; -*-
|
2
|
+
source 'https://rubygems.org'
|
3
|
+
|
4
|
+
group :test do
|
5
|
+
gem "minitest", "~> 4.6.2"
|
6
|
+
gem 'minitest-reporters', "~> 0.14.7"
|
7
|
+
gem 'minitest-metadata', "~> 0.2.0"
|
8
|
+
end
|
9
|
+
|
10
|
+
gem "rake"
|
11
|
+
gem "awesome_print"
|
12
|
+
|
13
|
+
# Specify your gem's dependencies in rails_riemann_middleware.gemspec
|
14
|
+
gemspec
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Richard Outten
|
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,40 @@
|
|
1
|
+
# RailsRiemannMiddleware
|
2
|
+
|
3
|
+
A middleware to add to your rails application that sends request
|
4
|
+
duration metrics and exception notifications to
|
5
|
+
[Riemann](http://riemann.io/).
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'rails_riemann_middleware'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install rails_riemann_middleware
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Add the following to your specific rails enviroment.
|
24
|
+
|
25
|
+
config.middleware.use(RailsRiemannMiddleware::Notifier,
|
26
|
+
:riemann_host => "riemann_host_name")
|
27
|
+
|
28
|
+
## Running Tests
|
29
|
+
|
30
|
+
To run the tests use:
|
31
|
+
|
32
|
+
$ bundle exec rake test
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
1. Fork it
|
37
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
38
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
39
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
40
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require "rails_riemann_middleware/version"
|
2
|
+
require "rails_riemann_middleware/event"
|
3
|
+
require "rails_riemann_middleware/duration"
|
4
|
+
require "rails_riemann_middleware/exception_notification"
|
5
|
+
|
6
|
+
module RailsRiemannMiddleware
|
7
|
+
|
8
|
+
class Notifier
|
9
|
+
attr_reader :event, :send_durations, :send_exceptions
|
10
|
+
|
11
|
+
def initialize(app, options = {})
|
12
|
+
@app, @options = app, options
|
13
|
+
@send_durations = options.fetch(:send_durations, true)
|
14
|
+
@send_exceptions = options.fetch(:send_exceptions, true)
|
15
|
+
@event = Event.new(options)
|
16
|
+
end
|
17
|
+
|
18
|
+
def call(env)
|
19
|
+
start_time = Time.now
|
20
|
+
@app.call(env)
|
21
|
+
rescue Exception => exception
|
22
|
+
ExceptionNotification.new(event, env, exception).send if send_exceptions
|
23
|
+
raise exception
|
24
|
+
ensure
|
25
|
+
Duration.new(event, env, start_time).send if send_durations
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'riemann/client'
|
2
|
+
|
3
|
+
module RailsRiemannMiddleware
|
4
|
+
|
5
|
+
class Duration
|
6
|
+
attr_reader :event, :env, :start_time
|
7
|
+
|
8
|
+
def initialize(event, env, start_time)
|
9
|
+
@event, @env, @start_time = event, env, start_time
|
10
|
+
end
|
11
|
+
|
12
|
+
def send
|
13
|
+
event << message
|
14
|
+
end
|
15
|
+
|
16
|
+
def message
|
17
|
+
end_time = Time.new
|
18
|
+
duration = (end_time - start_time)
|
19
|
+
msg = {
|
20
|
+
:host => env['HTTP_HOST'],
|
21
|
+
:service => "#{event.app_prefix} request duration".strip,
|
22
|
+
:state => 'info',
|
23
|
+
:metric => duration,
|
24
|
+
:tags => ["duration"]
|
25
|
+
}
|
26
|
+
# ap msg
|
27
|
+
msg
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'riemann/client'
|
2
|
+
|
3
|
+
module RailsRiemannMiddleware
|
4
|
+
|
5
|
+
class Event
|
6
|
+
attr_reader :client, :options, :reporting_host, :tags
|
7
|
+
|
8
|
+
def initialize(options)
|
9
|
+
@options = options
|
10
|
+
@client = create_riemann_client
|
11
|
+
@reporting_host = options[:reporting_host]
|
12
|
+
@tags = options.fetch(:tags, [])
|
13
|
+
@attributes = options.fetch(:attributes, {})
|
14
|
+
end
|
15
|
+
|
16
|
+
def <<(msg)
|
17
|
+
msg[:tags] += Array(tags)
|
18
|
+
client << {:time => time_for_client, :host => reporting_host}.merge(@attributes).merge(msg)
|
19
|
+
end
|
20
|
+
|
21
|
+
def app_prefix
|
22
|
+
options.fetch(:app_prefix, "")
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def time_for_client
|
28
|
+
Time.now.to_i - 10
|
29
|
+
end
|
30
|
+
|
31
|
+
def create_riemann_client
|
32
|
+
riemann_host = options.fetch(:riemann_host, "127.0.0.1")
|
33
|
+
riemann_port = options.fetch(:riemann_port, "5555")
|
34
|
+
|
35
|
+
Riemann::Client.new(:host => riemann_host, :port => riemann_port)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module RailsRiemannMiddleware
|
2
|
+
|
3
|
+
class ExceptionNotification
|
4
|
+
attr_reader :event, :env, :exception
|
5
|
+
|
6
|
+
def initialize(event, env, exception)
|
7
|
+
@event, @env, @exception = event, env, exception
|
8
|
+
end
|
9
|
+
|
10
|
+
def send
|
11
|
+
event << message
|
12
|
+
end
|
13
|
+
|
14
|
+
def message
|
15
|
+
msg = {
|
16
|
+
:host => env['HTTP_HOST'],
|
17
|
+
:service => "#{event.app_prefix} exception".strip,
|
18
|
+
:state => 'error',
|
19
|
+
:description => backtrace,
|
20
|
+
:tags => ["exception"]
|
21
|
+
}
|
22
|
+
# ap msg
|
23
|
+
msg
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def backtrace
|
29
|
+
e = "#{exception.to_s}\n"
|
30
|
+
e << exception.backtrace.join("\n")
|
31
|
+
e[0..8000]
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/rails_riemann_middleware/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Richard Outten"]
|
6
|
+
gem.email = ["outtenr@gmail.com"]
|
7
|
+
gem.description = %q{Rack middleware for sending data to riemann}
|
8
|
+
gem.summary = %q{Rack middleware for sending data to riemann}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "rails_riemann_middleware"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = RailsRiemannMiddleware::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency("riemann-client", "~> 0.2.0")
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
require 'minitest/mock'
|
3
|
+
require 'awesome_print'
|
4
|
+
require 'rails_riemann_middleware/duration'
|
5
|
+
|
6
|
+
describe RailsRiemannMiddleware::Duration do
|
7
|
+
|
8
|
+
before do
|
9
|
+
@start_time = Time.at(0)
|
10
|
+
@env = {}
|
11
|
+
@event = MiniTest::Mock.new
|
12
|
+
@event.expect :app_prefix, "TEST APP"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should set the duration to 100 second" do
|
16
|
+
d = RailsRiemannMiddleware::Duration.new(@event, @env, @start_time)
|
17
|
+
Time.stub :new, Time.at(100) do
|
18
|
+
d.message[:metric].must_equal 100
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should set the host to the value of HTTP_HOST" do
|
23
|
+
@env["HTTP_HOST"] = "my.test.host"
|
24
|
+
d = RailsRiemannMiddleware::Duration.new(@event, @env, @start_time)
|
25
|
+
d.message[:host].must_equal "my.test.host"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should add the hash to the message array" do
|
29
|
+
to_send = []
|
30
|
+
def to_send.app_prefix
|
31
|
+
"TEST APP"
|
32
|
+
end
|
33
|
+
d = RailsRiemannMiddleware::Duration.new(to_send, @env, @start_time)
|
34
|
+
result = d.send
|
35
|
+
result.size.must_equal 1
|
36
|
+
result.first[:service].must_match /^TEST APP/
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should have a state of 'info'" do
|
40
|
+
d = RailsRiemannMiddleware::Duration.new(@event, @env, @start_time)
|
41
|
+
d.message[:state].must_equal "info"
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_riemann_middleware
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Richard Outten
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-06-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: riemann-client
|
16
|
+
requirement: &70318980766960 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.2.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70318980766960
|
25
|
+
description: Rack middleware for sending data to riemann
|
26
|
+
email:
|
27
|
+
- outtenr@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- lib/rails_riemann_middleware.rb
|
38
|
+
- lib/rails_riemann_middleware/duration.rb
|
39
|
+
- lib/rails_riemann_middleware/event.rb
|
40
|
+
- lib/rails_riemann_middleware/exception_notification.rb
|
41
|
+
- lib/rails_riemann_middleware/version.rb
|
42
|
+
- rails_riemann_middleware.gemspec
|
43
|
+
- specs/duraction_spec.rb
|
44
|
+
- specs/spec_helper.rb
|
45
|
+
homepage: ''
|
46
|
+
licenses: []
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.11
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Rack middleware for sending data to riemann
|
69
|
+
test_files: []
|
70
|
+
has_rdoc:
|