rack-latency 0.0.1
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/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +41 -0
- data/Rakefile +1 -0
- data/lib/rack/latency.rb +38 -0
- data/lib/rack/latency/rack_latency_railtie.rb +9 -0
- data/lib/rack/latency/reporter.rb +43 -0
- data/lib/rack/latency/version.rb +5 -0
- data/rack-latency.gemspec +23 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c13a816fa706319c820894a429de6eb770a85000
|
4
|
+
data.tar.gz: b65a68da691aa42a2ea36c0e2f70bdd7e4a21551
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e5221035a5f2afc4f68157c24556c2963fc7ef077344f6b3976ea48a556daa3f1f1eb1d1996dcbaae4184c6c3afda74283425680c5013e64f62f6fe040025b45
|
7
|
+
data.tar.gz: dffe20aba64ccdae7e219f0da740ef1f8dfcf42399e0fca6baab3bfb9d47635644475786f34437fc3d2a305261fb3e02b9b3cb028ba994ba3048cb099c90c322
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0-p353
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Chad Bailey
|
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,41 @@
|
|
1
|
+
# Rack::Latency
|
2
|
+
|
3
|
+
```
|
4
|
+
10:06:49 web.1 | at=info source=yahoo measure#response.size=9 measure#response.latency=130.86ms
|
5
|
+
10:06:53 web.1 | at=info source=google measure#response.size=11 measure#response.latency=60.65ms
|
6
|
+
10:06:53 web.1 | at=info source=yahoo measure#response.size=9 measure#response.latency=122.92ms
|
7
|
+
10:06:57 web.1 | at=info source=google measure#response.size=11 measure#response.latency=53.46ms
|
8
|
+
10:06:57 web.1 | at=info source=yahoo measure#response.size=9 measure#response.latency=129.75ms
|
9
|
+
10:07:01 web.1 | at=info source=google measure#response.size=11 measure#response.latency=58.56ms
|
10
|
+
```
|
11
|
+
|
12
|
+
This gem measures request latency to other hosts. It runs as a separate thread created by its own middleware. If you're running multiple apps that talk to each other, it's a good way to sniff out potential network problems.
|
13
|
+
|
14
|
+
On my system, the `head` measurement times are roughly equivalent to (maybe 10-20% faster than) `curl -I`.
|
15
|
+
|
16
|
+
## Installation and Configuration
|
17
|
+
|
18
|
+
Add `gem 'rack-latency'` to your app's Gemfile. Then create an initializer to specify your desired measurements:
|
19
|
+
|
20
|
+
```
|
21
|
+
# config/initializers/rack-latency.rb
|
22
|
+
Rack::Latency.configure do |measure|
|
23
|
+
# time a HEAD request for google.com.
|
24
|
+
measure.head "http://www.google.com/"
|
25
|
+
|
26
|
+
# time a GET request to a Yahoo IP, and rename the measurement to "yahoo".
|
27
|
+
measure.get "http://98.138.253.109/", name: "yahoo"
|
28
|
+
|
29
|
+
# set the delay time between measurement loops.
|
30
|
+
measure.wait ENV["RACK-LATENCY-WAIT"] || 4
|
31
|
+
end
|
32
|
+
|
33
|
+
```
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
1. Fork it ( http://github.com/<my-github-username>/rack-latency/fork )
|
38
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
39
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
40
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
41
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/rack/latency.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require "rack/latency/version"
|
2
|
+
require "rack/latency/reporter"
|
3
|
+
require "rack/latency/rack_latency_railtie"
|
4
|
+
|
5
|
+
module Rack
|
6
|
+
module Latency
|
7
|
+
|
8
|
+
def self.configure(&block)
|
9
|
+
yield self
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.measurements
|
13
|
+
@measurements ||= {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.wait(val)
|
17
|
+
@wait = val
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.get_wait
|
21
|
+
@wait || 2
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.head(url, opts = {})
|
25
|
+
add_measurement(url, :head, opts)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.get(url, opts = {})
|
29
|
+
add_measurement(url, :get, opts)
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.add_measurement(url, method, opts = {})
|
33
|
+
url = URI.parse(url)
|
34
|
+
url.path = "/" if url.path == ""
|
35
|
+
measurements[url] = opts.merge(method: method)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module Rack
|
5
|
+
module Latency
|
6
|
+
class Reporter
|
7
|
+
def initialize(app, logger = nil)
|
8
|
+
@app = app
|
9
|
+
@logger = logger || ::Logger.new($stdout)
|
10
|
+
|
11
|
+
interval = (Rack::Latency.get_wait).to_i
|
12
|
+
|
13
|
+
@logger.info "-> rack-latency starting in interval mode (#{interval}s)"
|
14
|
+
Thread.new { report(interval) }
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(env)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def report(interval)
|
23
|
+
loop do
|
24
|
+
Rack::Latency.measurements.each do |uri, opts|
|
25
|
+
begin
|
26
|
+
start_time = Time.now
|
27
|
+
@size = 0
|
28
|
+
Net::HTTP.start(uri.host, uri.port) do |http|
|
29
|
+
response = http.send(opts[:method], uri.path)
|
30
|
+
@size = response.length
|
31
|
+
end
|
32
|
+
end_time = Time.now
|
33
|
+
@logger.info "at=info source=#{opts[:name] || uri.host} measure#response.size=#{@size} measure#response.latency=#{"%.2f" % ((end_time - start_time)*1000)}ms"
|
34
|
+
rescue
|
35
|
+
@logger.info "at=error source=#{uri}"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
sleep(interval)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rack/latency/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rack-latency"
|
8
|
+
spec.version = Rack::Latency::VERSION
|
9
|
+
spec.authors = ["Chad Bailey"]
|
10
|
+
spec.email = ["chad@heroku.com"]
|
11
|
+
spec.summary = %q{Measure and report latency to external hosts.}
|
12
|
+
spec.description = %q{Measure and report latency to external hosts.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-latency
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chad Bailey
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-11 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: '0'
|
20
|
+
type: :development
|
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: 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
|
+
description: Measure and report latency to external hosts.
|
42
|
+
email:
|
43
|
+
- chad@heroku.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- .ruby-version
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/rack/latency.rb
|
55
|
+
- lib/rack/latency/rack_latency_railtie.rb
|
56
|
+
- lib/rack/latency/reporter.rb
|
57
|
+
- lib/rack/latency/version.rb
|
58
|
+
- rack-latency.gemspec
|
59
|
+
homepage: ''
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.0.14
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Measure and report latency to external hosts.
|
83
|
+
test_files: []
|