rack-statsd 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rack-statsd.rb +34 -8
- data/rack-statsd.gemspec +2 -2
- metadata +51 -27
data/lib/rack-statsd.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module RackStatsD
|
2
|
-
VERSION = "0.
|
2
|
+
VERSION = "0.2.0"
|
3
3
|
|
4
4
|
# Simple middleware to add a quick status URL for tools like Nagios.
|
5
5
|
class RequestStatus
|
@@ -7,6 +7,7 @@ module RackStatsD
|
|
7
7
|
GET = 'GET'.freeze
|
8
8
|
PATH_INFO = 'PATH_INFO'.freeze
|
9
9
|
STATUS_PATH = '/status'
|
10
|
+
HEADERS = {"Content-Type" => "text/plain"}.freeze
|
10
11
|
|
11
12
|
# Initializes the middleware.
|
12
13
|
#
|
@@ -39,8 +40,11 @@ module RackStatsD
|
|
39
40
|
def call(env)
|
40
41
|
if env[REQUEST_METHOD] == GET
|
41
42
|
if env[PATH_INFO] == @status_path
|
42
|
-
|
43
|
-
|
43
|
+
if @callback.respond_to?(:call)
|
44
|
+
return @callback.call
|
45
|
+
else
|
46
|
+
return [200, HEADERS, [@callback.to_s]]
|
47
|
+
end
|
44
48
|
end
|
45
49
|
end
|
46
50
|
|
@@ -63,13 +67,13 @@ module RackStatsD
|
|
63
67
|
# Returns nothing.
|
64
68
|
def initialize(app, options = {})
|
65
69
|
@app = app
|
66
|
-
@host = options[:host]
|
70
|
+
@host = options.key?(:host) ? options[:host] : `hostname -s`.chomp
|
67
71
|
@sha = options[:revision] || '<none>'
|
68
72
|
end
|
69
73
|
|
70
74
|
def call(env)
|
71
75
|
status, headers, body = @app.call(env)
|
72
|
-
headers['X-Node'] = @host
|
76
|
+
headers['X-Node'] = @host if @host
|
73
77
|
headers['X-Revision'] = @sha
|
74
78
|
[status, headers, body]
|
75
79
|
end
|
@@ -82,6 +86,9 @@ module RackStatsD
|
|
82
86
|
# NOTE This middleware is not thread safe. It should only be used when
|
83
87
|
# rack.multiprocess is true and rack.multithread is false.
|
84
88
|
class ProcessUtilization
|
89
|
+
REQUEST_METHOD = 'REQUEST_METHOD'.freeze
|
90
|
+
VALID_METHODS = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE'].freeze
|
91
|
+
|
85
92
|
# Initializes the middleware.
|
86
93
|
#
|
87
94
|
# app - The next Rack app in the pipeline.
|
@@ -210,7 +217,7 @@ module RackStatsD
|
|
210
217
|
|
211
218
|
# called immediately after a request to record statistics, update the
|
212
219
|
# procline, and dump information to the logfile
|
213
|
-
def record_request(status)
|
220
|
+
def record_request(status, env)
|
214
221
|
now = Time.now
|
215
222
|
diff = (now - @start)
|
216
223
|
@active_time += diff
|
@@ -220,6 +227,11 @@ module RackStatsD
|
|
220
227
|
|
221
228
|
if @stats
|
222
229
|
@stats.timing("#{@stats_prefix}.response_time", diff * 1000)
|
230
|
+
if VALID_METHODS.include?(env[REQUEST_METHOD])
|
231
|
+
stat = "#{@stats_prefix}.response_time.#{env[REQUEST_METHOD].downcase}"
|
232
|
+
@stats.timing(stat, diff * 1000)
|
233
|
+
end
|
234
|
+
|
223
235
|
if suffix = status_suffix(status)
|
224
236
|
@stats.increment "#{@stats_prefix}.status_code.#{status_suffix(status)}"
|
225
237
|
end
|
@@ -236,12 +248,26 @@ module RackStatsD
|
|
236
248
|
|
237
249
|
def status_suffix(status)
|
238
250
|
suffix = case status.to_i
|
251
|
+
when 200 then :ok
|
252
|
+
when 201 then :created
|
253
|
+
when 202 then :accepted
|
254
|
+
when 301 then :moved_permanently
|
255
|
+
when 302 then :found
|
256
|
+
when 303 then :see_other
|
257
|
+
when 304 then :not_modified
|
258
|
+
when 305 then :use_proxy
|
259
|
+
when 307 then :temporary_redirect
|
239
260
|
when 400 then :bad_request
|
240
261
|
when 401 then :unauthorized
|
262
|
+
when 402 then :payment_required
|
263
|
+
when 403 then :forbidden
|
241
264
|
when 404 then :missing
|
265
|
+
when 410 then :gone
|
242
266
|
when 422 then :invalid
|
243
|
-
when 503 then :node_down
|
244
267
|
when 500 then :error
|
268
|
+
when 502 then :bad_gateway
|
269
|
+
when 503 then :node_down
|
270
|
+
when 504 then :gateway_timeout
|
245
271
|
end
|
246
272
|
end
|
247
273
|
|
@@ -279,7 +305,7 @@ module RackStatsD
|
|
279
305
|
env.delete('HTTP_X_REQUEST_START')
|
280
306
|
|
281
307
|
status, headers, body = @app.call(env)
|
282
|
-
body = Body.new(body) { record_request(status) }
|
308
|
+
body = Body.new(body) { record_request(status, env) }
|
283
309
|
[status, headers, body]
|
284
310
|
end
|
285
311
|
end
|
data/rack-statsd.gemspec
CHANGED
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
|
|
13
13
|
## If your rubyforge_project name is different, then edit it and comment out
|
14
14
|
## the sub! line in the Rakefile
|
15
15
|
s.name = 'rack-statsd'
|
16
|
-
s.version = '0.
|
17
|
-
s.date = '
|
16
|
+
s.version = '0.2.0'
|
17
|
+
s.date = '2013-01-24'
|
18
18
|
s.rubyforge_project = 'rack-statsd'
|
19
19
|
|
20
20
|
## Make sure your summary is short. The description may be as long
|
metadata
CHANGED
@@ -1,61 +1,85 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-statsd
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Ryan Tomayko
|
9
14
|
- Rick Olson
|
10
15
|
autorequire:
|
11
16
|
bindir: bin
|
12
17
|
cert_chain: []
|
13
|
-
|
14
|
-
|
15
|
-
|
18
|
+
|
19
|
+
date: 2013-01-24 00:00:00 -07:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
16
23
|
name: rack-test
|
17
|
-
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
18
26
|
none: false
|
19
|
-
requirements:
|
20
|
-
- -
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 3
|
31
|
+
segments:
|
32
|
+
- 0
|
33
|
+
version: "0"
|
23
34
|
type: :development
|
24
|
-
|
25
|
-
version_requirements: *70288897702740
|
35
|
+
version_requirements: *id001
|
26
36
|
description: Tools for monitoring Rack apps in production.
|
27
37
|
email: technoweenie@gmail.com
|
28
38
|
executables: []
|
39
|
+
|
29
40
|
extensions: []
|
41
|
+
|
30
42
|
extra_rdoc_files: []
|
31
|
-
|
43
|
+
|
44
|
+
files:
|
32
45
|
- LICENSE
|
33
46
|
- README.md
|
34
47
|
- Rakefile
|
35
48
|
- lib/rack-statsd.rb
|
36
49
|
- rack-statsd.gemspec
|
50
|
+
has_rdoc: true
|
37
51
|
homepage: https://github.com/github/rack-statsd
|
38
52
|
licenses: []
|
53
|
+
|
39
54
|
post_install_message:
|
40
55
|
rdoc_options: []
|
41
|
-
|
56
|
+
|
57
|
+
require_paths:
|
42
58
|
- lib
|
43
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
60
|
none: false
|
45
|
-
requirements:
|
46
|
-
- -
|
47
|
-
- !ruby/object:Gem::Version
|
48
|
-
|
49
|
-
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
hash: 3
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
69
|
none: false
|
51
|
-
requirements:
|
52
|
-
- -
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 3
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
55
77
|
requirements: []
|
78
|
+
|
56
79
|
rubyforge_project: rack-statsd
|
57
|
-
rubygems_version: 1.
|
80
|
+
rubygems_version: 1.6.2
|
58
81
|
signing_key:
|
59
82
|
specification_version: 2
|
60
83
|
summary: Tools for monitoring Rack apps in production.
|
61
84
|
test_files: []
|
85
|
+
|