puff 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in puff.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Polydice, Inc.
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,49 @@
1
+ # Puff
2
+
3
+ Puff provides Redis integration and instrumentation for Rails.
4
+
5
+ ## Features
6
+
7
+ * Simple integration (Put your server info in config/puff.yml)
8
+ * High performance by default (hiredis C-based native driver)
9
+ * Rails instrumentation (Using ActiveSupport::Notifications)
10
+
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'puff'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install puff
25
+
26
+ ## Usage
27
+
28
+ After put puff in your Gemfile, use the generator to generate default config file.
29
+
30
+ ```
31
+ rails g puff:config
32
+ ```
33
+
34
+ To use Redis in your Rails apps, using the singleton method:
35
+
36
+ ```
37
+ irb(main):001:0> redis = Puff.redis
38
+ => #<Redis client v3.0.2 for redis://localhost:6379/0>
39
+ irb(main):002:0> redis.ping
40
+ => "PONG"
41
+ ```
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,16 @@
1
+ require 'rails/generators'
2
+
3
+ module Puff
4
+ module Generators
5
+ class ConfigGenerator < ::Rails::Generators::Base
6
+ desc "Creates a Puff configuration file at config/puff.yml"
7
+
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ def create_config_file
11
+ template 'puff.yml', File.join('config', "puff.yml")
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ Adefaults: &defaults
2
+ host: localhost
3
+ port: 6379
4
+ db: 0
5
+
6
+ development:
7
+ <<: *defaults
8
+
9
+ test:
10
+ <<: *defaults
11
+
12
+ production:
13
+ <<: *defaults
@@ -0,0 +1,14 @@
1
+ require 'puff/instrumentation/log_subscriber'
2
+ require 'puff/instrumentation/controller_runtime'
3
+
4
+ module Puff
5
+ class Engine < ::Rails::Engine
6
+ initializer "puff.Instrumentation" do
7
+ Puff::Instrumentation::LogSubscriber.attach_to(:redis)
8
+
9
+ ActiveSupport.on_load(:action_controller) do
10
+ include Puff::Instrumentation::ControllerRuntime
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,34 @@
1
+ module Puff
2
+ module Instrumentation
3
+ module ControllerRuntime
4
+ extend ActiveSupport::Concern
5
+
6
+ protected
7
+
8
+ attr_internal :redis_runtime_before_render
9
+ attr_internal :redis_runtime_during_render
10
+
11
+ def cleanup_view_runtime
12
+ self.redis_runtime_before_render = Puff::Instrumentation::LogSubscriber.reset_runtime
13
+ runtime = super
14
+ self.redis_runtime_during_render = Puff::Instrumentation::LogSubscriber.reset_runtime
15
+ runtime - redis_runtime_during_render
16
+ end
17
+
18
+ def append_info_to_payload(payload)
19
+ super
20
+ payload[:redis_runtime] = (redis_runtime_before_render || 0) +
21
+ (redis_runtime_during_render || 0) +
22
+ Puff::Instrumentation::LogSubscriber.reset_runtime
23
+ end
24
+
25
+ module ClassMethods
26
+ def log_process_action(payload)
27
+ messages, redis_runtime = super, payload[:redis_runtime]
28
+ messages << ("Redis: %.1fms" % redis_runtime.to_f) if redis_runtime
29
+ messages
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,40 @@
1
+ require 'puff/Instrumentation/redis'
2
+
3
+ module Puff
4
+ module Instrumentation
5
+ class LogSubscriber < ActiveSupport::LogSubscriber
6
+ def self.runtime=(value)
7
+ Thread.current["redis_runtime"] = value
8
+ end
9
+
10
+ def self.runtime
11
+ Thread.current["redis_runtime"] ||= 0
12
+ end
13
+
14
+ def self.reset_runtime
15
+ rt, self.runtime = runtime, 0
16
+ rt
17
+ end
18
+
19
+ def request(event)
20
+ self.class.runtime += event.duration
21
+ return unless logger.debug?
22
+
23
+ name = "%s (%.2fms)" % ["Redis", event.duration]
24
+ cmds = event.payload[:commands]
25
+
26
+ output = " #{color(name, RED, true)}"
27
+
28
+ cmds.each do |name, *args|
29
+ if args.present?
30
+ output << " #{name.to_s.upcase} #{args.join(" ")}"
31
+ else
32
+ output << " #{name.to_s.upcase}"
33
+ end
34
+ end
35
+
36
+ debug output
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,13 @@
1
+ # Monkey Patching for profit.
2
+
3
+ class Redis
4
+ class Client
5
+ alias :old_logging :logging
6
+
7
+ def logging(commands, &block)
8
+ ActiveSupport::Notifications.instrument('request.redis', commands: commands) do
9
+ return old_logging(commands, &block)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Puff
2
+ VERSION = "0.3.0"
3
+ end
data/lib/puff.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'redis'
2
+ require 'redis/connection/hiredis'
3
+
4
+ require "puff/version"
5
+ require 'puff/engine' if defined?(::Rails::Railtie)
6
+ require "puff/instrumentation/log_subscriber"
7
+
8
+ module Puff
9
+ # Generic error
10
+ class PuffError < RuntimeError; end
11
+
12
+ def self.config
13
+ @config ||= YAML.load(File.open("#{Rails.root}/config/puff.yml"))[Rails.env]
14
+ end
15
+
16
+ def self.config=(config)
17
+ @config = config
18
+ end
19
+
20
+ def self.redis
21
+ @redis ||= ::Redis.new(Puff.config)
22
+ end
23
+
24
+ def self.redis=(redis)
25
+ @redis = redis
26
+ end
27
+
28
+ end
data/puff.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'puff/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "puff"
8
+ gem.version = Puff::VERSION
9
+ gem.authors = ["Richard Lee"]
10
+ gem.email = ["rl@polydice.com"]
11
+ gem.description = %q{Puff provides Redis integration and instrumentation for Rails.}
12
+ gem.summary = %q{Elagant Redis solution for Rails.}
13
+ gem.homepage = "https://github.com/polydice/puff"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "railties", "~> 3.1"
21
+ gem.add_dependency "activesupport", "~> 3.2"
22
+ gem.add_dependency "redis", "~> 3.0"
23
+ gem.add_dependency "hiredis", "~> 0.4"
24
+
25
+ gem.add_development_dependency "rake", "~> 10.0"
26
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puff
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Richard Lee
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: railties
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.1'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '3.2'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3.2'
46
+ - !ruby/object:Gem::Dependency
47
+ name: redis
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: hiredis
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '0.4'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '0.4'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rake
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '10.0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '10.0'
94
+ description: Puff provides Redis integration and instrumentation for Rails.
95
+ email:
96
+ - rl@polydice.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - lib/generators/puff/config_generator.rb
107
+ - lib/generators/puff/templates/puff.yml
108
+ - lib/puff.rb
109
+ - lib/puff/engine.rb
110
+ - lib/puff/instrumentation/controller_runtime.rb
111
+ - lib/puff/instrumentation/log_subscriber.rb
112
+ - lib/puff/instrumentation/redis.rb
113
+ - lib/puff/version.rb
114
+ - puff.gemspec
115
+ homepage: https://github.com/polydice/puff
116
+ licenses: []
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ segments:
128
+ - 0
129
+ hash: 2726262251431638489
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ segments:
137
+ - 0
138
+ hash: 2726262251431638489
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 1.8.23
142
+ signing_key:
143
+ specification_version: 3
144
+ summary: Elagant Redis solution for Rails.
145
+ test_files: []