benji_node 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/README.textile +8 -0
- data/Rakefile +2 -0
- data/benji_node.gemspec +24 -0
- data/lib/benji_node/middleware.rb +22 -0
- data/lib/benji_node/parser.rb +90 -0
- data/lib/benji_node/railtie.rb +23 -0
- data/lib/benji_node/version.rb +3 -0
- data/lib/benji_node/writer.rb +48 -0
- data/lib/benji_node.rb +12 -0
- metadata +107 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.textile
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
h2. Installing the node to collect data for the app
|
2
|
+
|
3
|
+
Just add gem **'benji_node'** to your Rails 3 Gemfile and run bundle install as usual.
|
4
|
+
|
5
|
+
h2. Changing the connection settings for the redis server
|
6
|
+
|
7
|
+
You need to have a running redis server and the redis gem installed. **In the vendors/plugin/benji_node.rb** you can
|
8
|
+
change the port and host for the redis server you want to use.
|
data/Rakefile
ADDED
data/benji_node.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "benji_node/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "benji_node"
|
7
|
+
s.version = BenjiNode::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Thomas Fankhauser"]
|
10
|
+
s.email = ["tommylefunk@googlemail.com"]
|
11
|
+
s.homepage = "http://www.southdesign.de"
|
12
|
+
s.summary = %q{A node that collects data for the benji profiler for rails}
|
13
|
+
s.description = %q{This node collects the data for the benji profiler for rails that you can clone from https://southdesign@github.com/southdesign/benji.git}
|
14
|
+
|
15
|
+
s.rubyforge_project = "benji_node"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
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_dependency 'rails', '>= 3.0.0'
|
23
|
+
s.add_dependency 'redis'
|
24
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module BenjiNode
|
2
|
+
class Middleware
|
3
|
+
|
4
|
+
def initialize(app)
|
5
|
+
@app = app
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
response = notifications.instrument "rack.request",
|
10
|
+
:path => env["PATH_INFO"],
|
11
|
+
:method => env["REQUEST_METHOD"],
|
12
|
+
:instrumenter_id => notifications.instrumenter.id do
|
13
|
+
@app.call(env)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
def notifications
|
19
|
+
ActiveSupport::Notifications
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module BenjiNode
|
2
|
+
module Parser
|
3
|
+
|
4
|
+
CATEGORIES = {
|
5
|
+
:none => "none",
|
6
|
+
:request => "request",
|
7
|
+
:view => "view",
|
8
|
+
:view_partial => "view_partial",
|
9
|
+
:controller => "controller",
|
10
|
+
:database => "database"
|
11
|
+
}
|
12
|
+
|
13
|
+
def self.parse event
|
14
|
+
mapping = self.select_mapping event
|
15
|
+
self.send mapping, event
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def self.select_mapping event
|
20
|
+
method = case event.name
|
21
|
+
when "rack.request" then return "parse_request"
|
22
|
+
when "sql.active_record" then return "parse_sql"
|
23
|
+
when "process_action.action_controller" then return "parse_controller"
|
24
|
+
when "render_template.action_view" then return "parse_view_template"
|
25
|
+
when "render_partial.action_view" then return "parse_view_partial"
|
26
|
+
else "parse_unknown"
|
27
|
+
end
|
28
|
+
method.to_sym
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.parse_unknown event
|
32
|
+
nil
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.parse_request event
|
36
|
+
{
|
37
|
+
:name => "Request - #{event.name}",
|
38
|
+
:category => CATEGORIES[:request],
|
39
|
+
:time => event.time,
|
40
|
+
:duration => event.duration,
|
41
|
+
:path => event.payload[:path],
|
42
|
+
:method => event.payload[:method],
|
43
|
+
:body => nil
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.parse_sql event
|
48
|
+
{
|
49
|
+
:name => "Database - #{event.payload[:name]}",
|
50
|
+
:category => CATEGORIES[:database],
|
51
|
+
:time => event.time,
|
52
|
+
:duration => event.duration,
|
53
|
+
:body => event.payload[:sql]
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.parse_controller event
|
58
|
+
{
|
59
|
+
:name => "Controller - Process Action",
|
60
|
+
:category => CATEGORIES[:controller],
|
61
|
+
:time => event.time,
|
62
|
+
:duration => event.duration,
|
63
|
+
:body => "#{event.payload[:controller]}##{event.payload[:action]}",
|
64
|
+
:view_runtime => event.payload[:view_runtime],
|
65
|
+
:db_runtime => event.payload[:db_runtime]
|
66
|
+
}
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.parse_view_template event
|
70
|
+
{
|
71
|
+
:name => "View - Render Template",
|
72
|
+
:category => CATEGORIES[:view],
|
73
|
+
:time => event.time,
|
74
|
+
:duration => event.duration,
|
75
|
+
:body => event.payload[:identifier]
|
76
|
+
}
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.parse_view_partial event
|
80
|
+
{
|
81
|
+
:name => "View - Render Partial",
|
82
|
+
:category => CATEGORIES[:view_partial],
|
83
|
+
:time => event.time,
|
84
|
+
:duration => event.duration,
|
85
|
+
:body => event.payload[:identifier]
|
86
|
+
}
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module BenjiNode
|
2
|
+
class Railtie < Rails::Engine
|
3
|
+
|
4
|
+
# Default Configuration
|
5
|
+
config.benji = ActiveSupport::OrderedOptions.new
|
6
|
+
config.benji.poolsize = 1000
|
7
|
+
|
8
|
+
# Add Middleware
|
9
|
+
initializer "benji.add_middleware" do |app|
|
10
|
+
app.config.middleware.use BenjiNode::Middleware
|
11
|
+
end
|
12
|
+
|
13
|
+
# Initialize Subscription
|
14
|
+
writer = BenjiNode::Writer.new
|
15
|
+
initializer "benji.start_subscriber" do |app|
|
16
|
+
ActiveSupport::Notifications.subscribe /[^!]$/ do |*args|
|
17
|
+
event = ActiveSupport::Notifications::Event.new(*args)
|
18
|
+
writer.write(event)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module BenjiNode
|
2
|
+
class Writer
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@request_id = unique_id
|
6
|
+
end
|
7
|
+
|
8
|
+
def write event
|
9
|
+
data = BenjiNode::Parser.parse(event)
|
10
|
+
return unless data
|
11
|
+
|
12
|
+
check_for_new_request!(event, data)
|
13
|
+
check_for_event!(event, data)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def check_for_new_request! event, data
|
19
|
+
return unless is_new_request?(event)
|
20
|
+
|
21
|
+
# Create new _requests key
|
22
|
+
json = ActiveSupport::JSON.encode data
|
23
|
+
BenjiNode::Redis.hset "benji_requests", @request_id, json
|
24
|
+
|
25
|
+
@request_id = unique_id
|
26
|
+
end
|
27
|
+
|
28
|
+
def check_for_event! event, data
|
29
|
+
return if is_new_request?(event)
|
30
|
+
|
31
|
+
# Append to _events key
|
32
|
+
events = ActiveSupport::JSON.decode(BenjiNode::Redis.hget("benji_events", @request_id)) rescue []
|
33
|
+
events << data
|
34
|
+
json = ActiveSupport::JSON.encode events
|
35
|
+
BenjiNode::Redis.hset "benji_events", @request_id, json
|
36
|
+
end
|
37
|
+
|
38
|
+
def unique_id
|
39
|
+
BenjiNode::Redis.incr "benji_count"
|
40
|
+
"benji_#{BenjiNode::Redis.get "benji_count"}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def is_new_request? event
|
44
|
+
event.name == "rack.request"
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
data/lib/benji_node.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'redis'
|
2
|
+
require 'active_support/secure_random'
|
3
|
+
|
4
|
+
require 'benji_node/parser'
|
5
|
+
require 'benji_node/writer'
|
6
|
+
require 'benji_node/middleware'
|
7
|
+
|
8
|
+
module BenjiNode
|
9
|
+
Redis = Redis.new :host => "http://localhost", :port => 6379
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'benji_node/railtie'
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: benji_node
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Thomas Fankhauser
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-06 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rails
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 3
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 3.0.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: redis
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
type: :runtime
|
50
|
+
version_requirements: *id002
|
51
|
+
description: This node collects the data for the benji profiler for rails that you can clone from https://southdesign@github.com/southdesign/benji.git
|
52
|
+
email:
|
53
|
+
- tommylefunk@googlemail.com
|
54
|
+
executables: []
|
55
|
+
|
56
|
+
extensions: []
|
57
|
+
|
58
|
+
extra_rdoc_files: []
|
59
|
+
|
60
|
+
files:
|
61
|
+
- .gitignore
|
62
|
+
- Gemfile
|
63
|
+
- README.textile
|
64
|
+
- Rakefile
|
65
|
+
- benji_node.gemspec
|
66
|
+
- lib/benji_node.rb
|
67
|
+
- lib/benji_node/middleware.rb
|
68
|
+
- lib/benji_node/parser.rb
|
69
|
+
- lib/benji_node/railtie.rb
|
70
|
+
- lib/benji_node/version.rb
|
71
|
+
- lib/benji_node/writer.rb
|
72
|
+
has_rdoc: true
|
73
|
+
homepage: http://www.southdesign.de
|
74
|
+
licenses: []
|
75
|
+
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
hash: 3
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
hash: 3
|
96
|
+
segments:
|
97
|
+
- 0
|
98
|
+
version: "0"
|
99
|
+
requirements: []
|
100
|
+
|
101
|
+
rubyforge_project: benji_node
|
102
|
+
rubygems_version: 1.4.1
|
103
|
+
signing_key:
|
104
|
+
specification_version: 3
|
105
|
+
summary: A node that collects data for the benji profiler for rails
|
106
|
+
test_files: []
|
107
|
+
|