rack-stats 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bc7935a3039650c4d289b6be87f66adf885aeddd
4
+ data.tar.gz: fee55ac0e408533efb715f04b5ec762b0e5caeed
5
+ SHA512:
6
+ metadata.gz: d476473c31f7f136ed1c27a740f9542a64ee9697f692e123bf1bedee8d24bdcb59b31f98b50cdcb08fe779026c22e5492120267bb11d00def4baf0008b541c6e
7
+ data.tar.gz: 05b0ecae37c2ad5ea7bb9d7b6ee68b64351ccac0c22a63e6366fed72662306405dbdc0313dcd7120ce550b0e1857ca93ce120f99cca3e4423d35c368da928d15
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-stats.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Alexis Montagne
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,29 @@
1
+ # Rack::Stats
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rack-stats'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rack-stats
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/rack-stats/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/example/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'sinatra'
4
+ gem 'rack-stats', path: '..'
data/example/config.ru ADDED
@@ -0,0 +1,12 @@
1
+ require 'sinatra'
2
+ require 'rack/stats'
3
+
4
+ use Rack::Stats
5
+
6
+ get '/' do
7
+ [200, 'foo']
8
+ end
9
+
10
+ get '/foo' do
11
+ [201, 'bar']
12
+ end
@@ -0,0 +1,33 @@
1
+ require 'statsd'
2
+ require 'rack'
3
+ require 'rack/stats/timer'
4
+ require 'rack/stats/stat'
5
+
6
+ module Rack
7
+ class Stats
8
+ class Runtime
9
+ def initialize(statsd, app, env, stats)
10
+ @batch = Statsd::Batch.new statsd
11
+ @app, @env, @timer = app, env, Timer.new
12
+ @stats = stats.map do |stat|
13
+ klass = stat[:type] == :increment ? Stat::Increment : Stat::Timing
14
+ klass.new stat[:name], stat[:value], stat[:condition]
15
+ end
16
+ end
17
+
18
+ def execute
19
+ r = @timer.time { @app.call(@env) }
20
+ flush_data @timer.duration, Rack::Response.new(r.last, r.first, r[1])
21
+ r
22
+ end
23
+
24
+ def flush_data(duration, response)
25
+ @stats.each do |stat|
26
+ stat.execute @batch, Rack::Request.new(@env), duration, response
27
+ end
28
+
29
+ @batch.flush
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,37 @@
1
+ module Rack
2
+ class Stats
3
+ module Stat
4
+ class Base
5
+ def initialize(compute_name, compute_value = nil, condition = nil)
6
+ @compute_name, @compute_value, @condition = \
7
+ compute_name, compute_value, condition
8
+ end
9
+
10
+ def execute(batch, request, duration, response)
11
+ if @condition.nil? || @condition.call(request, duration, response)
12
+ send_data(batch, request, duration, response)
13
+ end
14
+ end
15
+ end
16
+
17
+ class Timing < Base
18
+ def send_data(batch, request, duration, response)
19
+ batch.send(
20
+ :timing,
21
+ [@compute_name.call(request, duration, response)].compact.join('.'),
22
+ @compute_value.call(request, duration, response)
23
+ )
24
+ end
25
+ end
26
+
27
+ class Increment < Base
28
+ def send_data(batch, request, duration, response)
29
+ batch.send(
30
+ :increment,
31
+ [@compute_name.call(request, duration, response)].compact.join('.')
32
+ )
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,23 @@
1
+ module Rack
2
+ class Stats
3
+ class Duration
4
+ def initialize(ns)
5
+ @ns = ns
6
+ end
7
+
8
+ def ms; @ns / 1_000_000; end
9
+ def s; ms / 1_000; end
10
+ end
11
+
12
+ class Timer
13
+ attr_reader :duration
14
+
15
+ def time(&blk)
16
+ t0 = Time.now.nsec
17
+ r = blk.call
18
+ @duration = Duration.new(Time.now.nsec - t0)
19
+ r
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class Stats
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
data/lib/rack/stats.rb ADDED
@@ -0,0 +1,46 @@
1
+ require 'rack/stats/version'
2
+ require 'rack/stats/runtime'
3
+
4
+ module Rack
5
+ class Stats
6
+ DEFAULT_STATS = [
7
+ {
8
+ name: -> (*_args) { 'request_duration_global' },
9
+ value: -> (_req, d, _resp) { d.ms },
10
+ type: :timing
11
+ },
12
+ {
13
+ name: lambda do |req, _d, _resp|
14
+ [
15
+ req.path.eql?('/') ? 'index' : req.path.gsub(/\//, '-'),
16
+ req.request_method.downcase,
17
+ 'request_duration'
18
+ ]
19
+ end,
20
+ value: -> (_req, d, _resp) { d.ms },
21
+ type: :timing
22
+ },
23
+ {
24
+ name: -> (*_args) { 'request_number' },
25
+ type: :increment
26
+ },
27
+ {
28
+ name: lambda do |_req, _d, resp|
29
+ ['request_number_status', "#{resp.status / 100}XX"]
30
+ end,
31
+ type: :increment
32
+ }
33
+ ]
34
+
35
+ def initialize(app, args = {})
36
+ @app = app
37
+ @namespace = args.fetch(:namespace, ENV['RACK_STATS_NAMESPACE'])
38
+ @statsd = Statsd.new(*args.fetch(:statsd, '127.0.0.1:8125').split(':'))
39
+ @stats = args.fetch(:stats, DEFAULT_STATS)
40
+ end
41
+
42
+ def call(env)
43
+ Runtime.new(@statsd, @app, env, @stats).execute
44
+ end
45
+ end
46
+ 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 'rack/stats/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-stats"
8
+ spec.version = Rack::Stats::VERSION
9
+ spec.authors = ["Alexis Montagne"]
10
+ spec.email = ["alexis.montagne@gmail.com"]
11
+ spec.summary = %q{Customisable rack middleware to instrument to instrument your application}
12
+ spec.description = %q{Customisable rack middleware to instrument to instrument your application}
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", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_runtime_dependency "statsd-ruby"
25
+ spec.add_runtime_dependency "rack"
26
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::Stats do
4
+ let(:middleware) { Rack::Stats.new(app, args) }
5
+ let(:app) { -> (e) { [200, { 'Content-Length' => 3 }, ['foo']] } }
6
+ let(:args) { { stats: [] } }
7
+ let(:env) { double }
8
+ let(:inc) { { name: -> (*_args) { 'foo' }, type: :increment } }
9
+ let(:tim) do
10
+ { name: -> (*_args) { 'foo' }, value: -> (*_args) { 42 }, type: :timing }
11
+ end
12
+
13
+ after { middleware.call(env) }
14
+
15
+ subject { Statsd::Batch }
16
+
17
+ describe 'no stats' do
18
+ it { expect_any_instance_of(subject).to_not receive(:timing) }
19
+ it { expect_any_instance_of(subject).to_not receive(:increment) }
20
+ end
21
+
22
+ describe 'increment metric' do
23
+ let(:args) { { stats: [inc] } }
24
+
25
+ it { expect_any_instance_of(subject).to_not receive(:timing) }
26
+ it do
27
+ expect_any_instance_of(subject).to receive(:increment).with('foo').once
28
+ end
29
+ end
30
+
31
+ describe 'timing metric' do
32
+ let(:args) { { stats: [tim] } }
33
+
34
+ it { expect_any_instance_of(subject).to_not receive(:increment) }
35
+ it do
36
+ expect_any_instance_of(subject).to receive(:timing).with('foo', 42).once
37
+ end
38
+ end
39
+
40
+ describe 'both types at the same time' do
41
+ let(:args) { { stats: [tim, inc] } }
42
+
43
+ it do
44
+ expect_any_instance_of(subject).to receive(:increment).with('foo').once
45
+ end
46
+ it do
47
+ expect_any_instance_of(subject).to receive(:timing).with('foo', 42).once
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,90 @@
1
+ require 'rack/stats'
2
+ # This file was generated by the `rspec --init` command. Conventionally, all
3
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
4
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
5
+ # file to always be loaded, without a need to explicitly require it in any files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need it.
13
+ #
14
+ # The `.rspec` file also contains a few flags that are not defaults but that
15
+ # users commonly want.
16
+ #
17
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
18
+ RSpec.configure do |config|
19
+ # rspec-expectations config goes here. You can use an alternate
20
+ # assertion/expectation library such as wrong or the stdlib/minitest
21
+ # assertions if you prefer.
22
+ config.expect_with :rspec do |expectations|
23
+ # This option will default to `true` in RSpec 4. It makes the `description`
24
+ # and `failure_message` of custom matchers include text for helper methods
25
+ # defined using `chain`, e.g.:
26
+ # be_bigger_than(2).and_smaller_than(4).description
27
+ # # => "be bigger than 2 and smaller than 4"
28
+ # ...rather than:
29
+ # # => "be bigger than 2"
30
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
31
+ end
32
+
33
+ # rspec-mocks config goes here. You can use an alternate test double
34
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
35
+ config.mock_with :rspec do |mocks|
36
+ # Prevents you from mocking or stubbing a method that does not exist on
37
+ # a real object. This is generally recommended, and will default to
38
+ # `true` in RSpec 4.
39
+ mocks.verify_partial_doubles = true
40
+ end
41
+
42
+ # The settings below are suggested to provide a good initial experience
43
+ # with RSpec, but feel free to customize to your heart's content.
44
+ =begin
45
+ # These two settings work together to allow you to limit a spec run
46
+ # to individual examples or groups you care about by tagging them with
47
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
48
+ # get run.
49
+ config.filter_run :focus
50
+ config.run_all_when_everything_filtered = true
51
+
52
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
53
+ # For more details, see:
54
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
55
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
56
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
57
+ config.disable_monkey_patching!
58
+
59
+ # This setting enables warnings. It's recommended, but in some cases may
60
+ # be too noisy due to issues in dependencies.
61
+ config.warnings = true
62
+
63
+ # Many RSpec users commonly either run the entire suite or an individual
64
+ # file, and it's useful to allow more verbose output when running an
65
+ # individual spec file.
66
+ if config.files_to_run.one?
67
+ # Use the documentation formatter for detailed output,
68
+ # unless a formatter has already been configured
69
+ # (e.g. via a command-line flag).
70
+ config.default_formatter = 'doc'
71
+ end
72
+
73
+ # Print the 10 slowest examples and example groups at the
74
+ # end of the spec run, to help surface which specs are running
75
+ # particularly slow.
76
+ config.profile_examples = 10
77
+
78
+ # Run specs in random order to surface order dependencies. If you find an
79
+ # order dependency and want to debug it, you can fix the order by providing
80
+ # the seed, which is printed after each run.
81
+ # --seed 1234
82
+ config.order = :random
83
+
84
+ # Seed global randomization in this process using the `--seed` CLI option.
85
+ # Setting this allows you to use `--seed` to deterministically reproduce
86
+ # test failures related to randomization by passing the same `--seed` value
87
+ # as the one that triggered the failure.
88
+ Kernel.srand config.seed
89
+ =end
90
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-stats
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexis Montagne
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-14 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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: statsd-ruby
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Customisable rack middleware to instrument to instrument your application
84
+ email:
85
+ - alexis.montagne@gmail.com
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
+ - example/Gemfile
97
+ - example/config.ru
98
+ - lib/rack/stats.rb
99
+ - lib/rack/stats/runtime.rb
100
+ - lib/rack/stats/stat.rb
101
+ - lib/rack/stats/timer.rb
102
+ - lib/rack/stats/version.rb
103
+ - rack-stats.gemspec
104
+ - spec/acceptance/global_spec.rb
105
+ - spec/spec_helper.rb
106
+ homepage: ''
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.2.2
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Customisable rack middleware to instrument to instrument your application
130
+ test_files:
131
+ - spec/acceptance/global_spec.rb
132
+ - spec/spec_helper.rb