stashing 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f7d0081607df191265c2e884922516a8a9fcb227
4
+ data.tar.gz: a128c62947067509b5e39d83f6ee7890cd2b6157
5
+ SHA512:
6
+ metadata.gz: 7c10eea784bbcde5962ac762370d8a317ed9414209dc0ae3c138840cf231f213da83d4e6a462ebe5c7d3c687183a787e1d388f1daff9b91c313261510590b4ea
7
+ data.tar.gz: 0b2d8c7414407563ba7e5243a9f33463b5d0c4bd5eda489a1c81c999db0723c84f0016739c61321ef4b1e6cfb9b93a938374b89ec5b404d1ec1cb96493595180
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - jruby-19mode # JRuby in 1.9 mode
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,103 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ stashing (0.1.1)
5
+ logstasher
6
+ request_store
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionmailer (4.0.0)
12
+ actionpack (= 4.0.0)
13
+ mail (~> 2.5.3)
14
+ actionpack (4.0.0)
15
+ activesupport (= 4.0.0)
16
+ builder (~> 3.1.0)
17
+ erubis (~> 2.7.0)
18
+ rack (~> 1.5.2)
19
+ rack-test (~> 0.6.2)
20
+ activemodel (4.0.0)
21
+ activesupport (= 4.0.0)
22
+ builder (~> 3.1.0)
23
+ activerecord (4.0.0)
24
+ activemodel (= 4.0.0)
25
+ activerecord-deprecated_finders (~> 1.0.2)
26
+ activesupport (= 4.0.0)
27
+ arel (~> 4.0.0)
28
+ activerecord-deprecated_finders (1.0.3)
29
+ activesupport (4.0.0)
30
+ i18n (~> 0.6, >= 0.6.4)
31
+ minitest (~> 4.2)
32
+ multi_json (~> 1.3)
33
+ thread_safe (~> 0.1)
34
+ tzinfo (~> 0.3.37)
35
+ arel (4.0.1)
36
+ atomic (1.1.14)
37
+ builder (3.1.4)
38
+ diff-lcs (1.2.5)
39
+ erubis (2.7.0)
40
+ hike (1.2.3)
41
+ i18n (0.6.5)
42
+ logstash-event (1.1.5)
43
+ logstasher (0.3.0)
44
+ logstash-event (~> 1.1.0)
45
+ mail (2.5.4)
46
+ mime-types (~> 1.16)
47
+ treetop (~> 1.4.8)
48
+ mime-types (1.25)
49
+ minitest (4.7.5)
50
+ multi_json (1.8.2)
51
+ polyglot (0.3.3)
52
+ rack (1.5.2)
53
+ rack-test (0.6.2)
54
+ rack (>= 1.0)
55
+ rails (4.0.0)
56
+ actionmailer (= 4.0.0)
57
+ actionpack (= 4.0.0)
58
+ activerecord (= 4.0.0)
59
+ activesupport (= 4.0.0)
60
+ bundler (>= 1.3.0, < 2.0)
61
+ railties (= 4.0.0)
62
+ sprockets-rails (~> 2.0.0)
63
+ railties (4.0.0)
64
+ actionpack (= 4.0.0)
65
+ activesupport (= 4.0.0)
66
+ rake (>= 0.8.7)
67
+ thor (>= 0.18.1, < 2.0)
68
+ rake (10.1.0)
69
+ request_store (1.0.5)
70
+ rspec (2.14.1)
71
+ rspec-core (~> 2.14.0)
72
+ rspec-expectations (~> 2.14.0)
73
+ rspec-mocks (~> 2.14.0)
74
+ rspec-core (2.14.7)
75
+ rspec-expectations (2.14.3)
76
+ diff-lcs (>= 1.1.3, < 2.0)
77
+ rspec-mocks (2.14.4)
78
+ sprockets (2.10.0)
79
+ hike (~> 1.2)
80
+ multi_json (~> 1.0)
81
+ rack (~> 1.0)
82
+ tilt (~> 1.1, != 1.3.0)
83
+ sprockets-rails (2.0.1)
84
+ actionpack (>= 3.0)
85
+ activesupport (>= 3.0)
86
+ sprockets (~> 2.8)
87
+ thor (0.18.1)
88
+ thread_safe (0.1.3)
89
+ atomic
90
+ tilt (1.4.1)
91
+ treetop (1.4.15)
92
+ polyglot
93
+ polyglot (>= 0.3.1)
94
+ tzinfo (0.3.38)
95
+
96
+ PLATFORMS
97
+ ruby
98
+
99
+ DEPENDENCIES
100
+ bundler (>= 1.0.0)
101
+ rails (>= 3.0)
102
+ rspec
103
+ stashing!
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Stashing
2
+ [![Build Status](https://travis-ci.org/elhu/stashing.png)](https://travis-ci.org/elhu/stashing) [![Code Climate](https://codeclimate.com/github/elhu/stashing.png)](https://codeclimate.com/github/elhu/stashing)
3
+ ### Wrapped-up [Logstasher](https://github.com/shadabahmed/logstasher) for easy ActiveSupport::Notifications logging
4
+
5
+ LogStasher is awesome. It does one thing, and does it well.
6
+ While you can add information to Logstasher very easily, it is harder to add information that changes over time, for example, the number of SQL queries performed during a request.
7
+
8
+ ## Installation
9
+ In your Gemfile:
10
+
11
+ ```ruby
12
+ gem 'stashing'
13
+ ```
14
+
15
+ ### Configure your `<environment>.rb` (for example: `production.rb`)
16
+
17
+ ```ruby
18
+ # The options are the same as LogStasher's
19
+ config.stashing.enabled = true
20
+ config.stashing.suppress_app_log = false
21
+
22
+ # Set this to true if you want to enable caching instrumentation
23
+ config.stashing.enable_cache_instrumentation = true
24
+ ```
25
+
26
+ ## Enter Stashing
27
+ Stashing is a wrapper around LogStasher. It means that any option you set will be forwarded to LogStasher, Stashing takes care of everything!
28
+
29
+ Stashing is simply here to help you log metrics based on [ActiveSupport::Notifications](http://guides.rubyonrails.org/active_support_instrumentation.html), by tracking them and adding them to the final payload.
30
+
31
+ For example, if you want to log the number of SQL queries performed during a request, could can do:
32
+
33
+ ```ruby
34
+ # config/initializers/stashing.rb
35
+ Stashing.watch('sql.active_record') do |name, start, finish, id, payload, stash|
36
+ duration = (finish - start) * 1000
37
+ stash[:queries] = stash[:queries].to_i.succ
38
+ end
39
+ ```
40
+
41
+ `Stashing.watch` takes the notification's name in argument, and a block, called every time the notification is triggered, with the following arguments:
42
+
43
+ * `name`: The notification's name
44
+ * `start`: Beginning date for the event
45
+ * `finish`: End date for the event
46
+ * `id`: ID of the event
47
+ * `payload`: Event's payload
48
+ * `stash`: A thread-safe hash where you can store the data you want to log. This is reset after each request.
49
+
50
+ If you want to share a `stash` between several events, you can assign them to an `event_group`:
51
+
52
+ ``` ruby
53
+ # config/initializers/stashing.rb
54
+ Stashing.watch('cache_fetch_hit.active_support', event_group: 'cache') do |*args, stash|
55
+ stash[:fetch_hit] = stash[:fetch_hit].to_i.succ
56
+ end
57
+
58
+ Stashing.watch('cache_generate.active_support', event_group: 'cache') do |*args, stash|
59
+ stash[:generate] = stash[:generate].to_i.succ
60
+ end
61
+ ```
62
+
63
+ See a complete [example](https://github.com/elhu/stashing/blob/master/examples/initializer.rb).
64
+
65
+ Whatever you put in your `stash`, you'll get back in your log, with either the `event_group` or the event name as the key:
66
+
67
+ ```json
68
+ {"@source":"unknown","@tags":["request"],"@fields":{"method":"GET","path":"/login","format":"html","controller":"session","action":"credential_requestor","status":200,"duration":1265.1,"view":1087.07,"db":89.96,"sql.active_record":{"queries":35,"slowest_query":14.312999999999999},"ip":"127.0.0.1","route":"session#credential_requestor","parameters":"service=http://example.com/login\n","user_id":null,"cas_id":null,"session_id":"e309097c16d4c4bd2ca1474b316e6406","request_id":"6dffb5ea-8075-4e38-b2ba-bb5f5264421a","cache":{"fetch_hit":3}},"@timestamp":"2013-11-21T14:35:08.210091+00:00"}
69
+ ```
70
+
71
+ ## Copyright
72
+
73
+ Copyright &copy; 2013 Vincent Boisard, released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ desc 'Default: run specs.'
4
+ task :default => :spec
5
+
6
+ # Defining spec task for running spec
7
+ desc "Run specs"
8
+ RSpec::Core::RakeTask.new('spec') do |spec|
9
+ # Pattern filr for spec files to run. This is default btw.
10
+ spec.pattern = "./spec/**/*_spec.rb"
11
+ end
@@ -0,0 +1,23 @@
1
+ if Stashing.enabled
2
+ Stashing.add_custom_fields do |fields|
3
+ fields[:user_id] = current_user.id
4
+ fields[:session_id] = session[:session_id]
5
+ fields[:request_id] = request.uuid
6
+ end
7
+
8
+ Stashing.watch('sql.active_record') do |name, start, finish, id, payload, stash|
9
+ duration = (finish - start) * 1000
10
+ stash[:queries] = stash[:queries].to_i.succ
11
+ if stash[:slowest_query].to_i < duration
12
+ stash[:slowest_query] = duration
13
+ end
14
+ end
15
+
16
+ Stashing.watch('cache_fetch_hit.active_support', event_group: 'cache') do |*args, stash|
17
+ stash[:fetch_hit] = stash[:fetch_hit].to_i.succ
18
+ end
19
+
20
+ Stashing.watch('cache_generate.active_support', event_group: 'cache') do |*args, stash|
21
+ stash[:generate] = stash[:generate].to_i.succ
22
+ end
23
+ end
data/lib/stashing.rb ADDED
@@ -0,0 +1,40 @@
1
+ require 'request_store'
2
+ require 'forwardable'
3
+
4
+ class Stashing
5
+ STORE_KEY = :stashing_data
6
+
7
+ class << self
8
+ attr_accessor :enable_cache_instrumentation
9
+
10
+ # Send all the dirty work to LogStasher
11
+ extend Forwardable
12
+ def_delegators :LogStasher, :enabled, :add_custom_fields, :custom_fields=, :custom_fields
13
+ end
14
+
15
+ def self.stash
16
+ if RequestStore.store[STORE_KEY].nil?
17
+ # Get each event_store it's own private Hash instance.
18
+ RequestStore.store[STORE_KEY] = Hash.new { |hash, key| hash[key] = {} }
19
+ end
20
+ RequestStore.store[STORE_KEY]
21
+ end
22
+
23
+ def self.watch(event, opts = {}, &block)
24
+ event_group = opts[:event_group] || event
25
+ ActiveSupport::Notifications.subscribe(event) do |*args|
26
+ # It's necessary to add the custom_fields at runtime, otherwise LogStasher overrides them.
27
+ Stashing.custom_fields << event_group unless Stashing.custom_fields.include? event_group
28
+
29
+ # Calling the processing block with the Notification args, plus the event_stash
30
+ block.call(*args, stash[event_group])
31
+ end
32
+ end
33
+ end
34
+
35
+ # LogStasher must be loaded AFTER Stashing's Railtie
36
+ if defined?(Rails)
37
+ require 'stashing/railtie'
38
+ else
39
+ require 'logstasher'
40
+ end
@@ -0,0 +1,26 @@
1
+ require 'active_support/concern'
2
+
3
+ class Stashing
4
+ module ControllerExt
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ before_filter :enable_cache_instrumentation
9
+ end
10
+
11
+ protected
12
+ def enable_cache_instrumentation
13
+ # Cache instrumentation is disabled by default in Rails
14
+ Rails.cache.class.instrument = true if Stashing.enable_cache_instrumentation
15
+ end
16
+
17
+ def append_info_to_payload(payload)
18
+ super
19
+ # At the end of the request (process_action.action_controller), add the store to the payload
20
+ Stashing.stash.each do |key, value|
21
+ payload[key] = value
22
+ end
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,23 @@
1
+ require 'rails/railtie'
2
+ require 'stashing/controller_ext'
3
+
4
+ class Stashing
5
+ class Railtie < Rails::Railtie
6
+ config.stashing = ActiveSupport::OrderedOptions.new
7
+ config.stashing.enabled = false
8
+ config.stashing.enable_cache_instrumentation = false
9
+
10
+ initializer :stashing do |app|
11
+ # Extending ActionController with the controller extension
12
+ ActiveSupport.on_load(:action_controller) { include Stashing::ControllerExt }
13
+
14
+ # Pass the configuration over to logstasher
15
+ app.config.logstasher = app.config.stashing
16
+
17
+ # Should we enable cache instrumentation?
18
+ Stashing.enable_cache_instrumentation = app.config.stashing.enable_cache_instrumentation
19
+ end
20
+ end
21
+ end
22
+
23
+ require 'logstasher'
@@ -0,0 +1,3 @@
1
+ class Stashing
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
2
+
3
+ require 'active_support'
4
+ require 'stashing'
@@ -0,0 +1,21 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
2
+
3
+ describe 'Stashing' do
4
+ describe '#store' do
5
+ it "returns a new Hash for each key" do
6
+ expect(Stashing.stash['a'].object_id).to_not be(Stashing.stash['b'].object_id)
7
+ end
8
+
9
+ it "returns the same stash if called several time with the same key" do
10
+ expect(Stashing.stash['a'].object_id).to be(Stashing.stash['a'].object_id)
11
+ end
12
+ end
13
+
14
+ describe "#watch" do
15
+ it "subscribes to the required event" do
16
+ ActiveSupport::Notifications = double("Notifications")
17
+ ActiveSupport::Notifications.should_receive(:subscribe).with('event_name')
18
+ Stashing.watch('event_name')
19
+ end
20
+ end
21
+ end
data/stashing.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "stashing/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "stashing"
7
+ s.version = Stashing::VERSION
8
+ s.authors = ["Vincent Boisard"]
9
+ s.email = ["boisard.v@gmail.com"]
10
+ s.homepage = "https://github.com/elhu/stashing"
11
+ s.summary = %q{Logstash wrapper for easy ActiveSupport::Notifications logging}
12
+ s.description = s.summary
13
+ s.licenses = 'MIT'
14
+
15
+ s.rubyforge_project = "stashing"
16
+
17
+ s.files = `git ls-files`.split("\n") - [".gitignore"]
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_runtime_dependency "logstasher"
23
+ s.add_runtime_dependency "request_store"
24
+
25
+ # specify any dependencies here; for example:
26
+ s.add_development_dependency "rspec"
27
+ s.add_development_dependency("bundler", [">= 1.0.0"])
28
+ s.add_development_dependency("rails", [">= 3.0"])
29
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stashing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Vincent Boisard
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logstasher
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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: request_store
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 1.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Logstash wrapper for easy ActiveSupport::Notifications logging
84
+ email:
85
+ - boisard.v@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".travis.yml"
91
+ - Gemfile
92
+ - Gemfile.lock
93
+ - README.md
94
+ - Rakefile
95
+ - examples/initializer.rb
96
+ - lib/stashing.rb
97
+ - lib/stashing/controller_ext.rb
98
+ - lib/stashing/railtie.rb
99
+ - lib/stashing/version.rb
100
+ - spec/spec_helper.rb
101
+ - spec/stashing/stashing_spec.rb
102
+ - stashing.gemspec
103
+ homepage: https://github.com/elhu/stashing
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project: stashing
123
+ rubygems_version: 2.1.5
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Logstash wrapper for easy ActiveSupport::Notifications logging
127
+ test_files:
128
+ - spec/spec_helper.rb
129
+ - spec/stashing/stashing_spec.rb
130
+ has_rdoc: