td-logger 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,4 +1,13 @@
1
1
 
2
+ == 2011-09-30 version 0.2.5
3
+
4
+ * Use HTTP_X_FORWARDED_FOR instead of REMOTE_ADDR if it's available
5
+ * Don't enable ActiveRecord extension unless defined?(ActiveRecord)
6
+ * Ignore OPTIONS method
7
+ * Use Symbol for keys of access log
8
+ * Removed defined?(RAILS_ENV) and defined?(RAILS_ROOT) to suppress wornings
9
+
10
+
2
11
  == 2011-09-13 version 0.2.4
3
12
 
4
13
  * Increase flush_interval from 10 sec to 5 mins gradually
data/README.rdoc CHANGED
@@ -20,7 +20,7 @@ And then add +config/treasure_data.yml+ file as following:
20
20
  access_log_table: access
21
21
  auto_create_table: true
22
22
 
23
- # logging via td-agent
23
+ # logging via td-agent (fluent)
24
24
  production:
25
25
  agent: "localhost:24224"
26
26
  tag: td.myapp
@@ -4,8 +4,8 @@ module Logger
4
4
  module Agent
5
5
 
6
6
  ACCESS_LOG_PARAM_ENV =
7
- if defined? Rails
8
- if Rails.respond_to?(:version) && Rails.version =~ /^3/
7
+ if defined? ::Rails
8
+ if ::Rails.respond_to?(:version) && ::Rails.version =~ /^3/
9
9
  # Rails 3
10
10
  'action_dispatch.request.path_parameters'
11
11
  else
@@ -18,54 +18,62 @@ module Agent
18
18
  end
19
19
 
20
20
  ACCESS_LOG_PRESET_PARAM_KEYS = {
21
- 'controller' => :controller,
22
- 'action' => :action,
23
- }
24
-
25
- ACCESS_LOG_PRESET_ENV_KEYS = {
26
- 'ip' => 'REMOTE_ADDR',
27
- 'method' => 'REQUEST_METHOD',
28
- 'uri' => 'REQUEST_URI',
29
- 'referer' => 'HTTP_REFERER',
30
- 'ua' => 'HTTP_USER_AGENT'
21
+ :controller => :controller,
22
+ :action => :action,
31
23
  }
32
24
 
33
25
  def self.enable_access_log(tag)
34
26
  Middleware.before do |env|
35
- data = {}
36
- Thread.current['td.access_log'] = data
37
- env['td.access_log'] = data
27
+ record = {}
28
+ Thread.current['td.access_log'] = record
29
+ env['td.access_log'] = record
38
30
  env['td.access_time'] = Time.now
39
31
  end
40
32
 
41
33
  Middleware.after do |env,result|
42
- data = env['td.access_log'] || {}
43
- access_time = env['td.access_time']
44
-
45
- # add 'elapsed' column
46
- if access_time
47
- elapsed = Time.now - access_time
48
- data['elapsed'] = elapsed
49
- # set 'time' column to access time
50
- data['time'] = access_time
34
+ req = env['action_dispatch.request']
35
+ if !req || !req.is_a?(Rack::Request)
36
+ req = Rack::Request.new(env)
51
37
  end
52
38
 
53
- ACCESS_LOG_PRESET_ENV_KEYS.each_pair {|key,val|
54
- data[key] ||= env[val] if env[val]
55
- }
39
+ # ignore OPTIONS request
40
+ if req.request_method != "OPTIONS"
41
+ record = env['td.access_log'] || {}
42
+ access_time = env['td.access_time']
43
+
44
+ # add 'elapsed' column
45
+ if access_time
46
+ elapsed = Time.now - access_time
47
+ record[:elapsed] = elapsed
48
+ # set 'time' column to access time
49
+ record[:time] = access_time
50
+ end
51
+
52
+ record[:method] ||= req.request_method
53
+ record[:ip] ||= (env['action_dispatch.remote_ip'] || req.ip).to_s
54
+ record[:uri] ||= env['REQUEST_URI'].to_s if env['REQUEST_URI']
55
+ record[:referer] ||= env['HTTP_REFERER'].to_s if env['HTTP_REFERER']
56
+ record[:ua] ||= env['HTTP_USER_AGENT'].to_s if env['HTTP_USER_AGENT']
56
57
 
57
- m = env[ACCESS_LOG_PARAM_ENV]
58
- ACCESS_LOG_PRESET_PARAM_KEYS.each_pair {|key,val|
59
- data[key] ||= m[val] if m[val]
60
- }
58
+ m = env[ACCESS_LOG_PARAM_ENV]
59
+ ACCESS_LOG_PRESET_PARAM_KEYS.each_pair {|key,val|
60
+ record[key] ||= m[val] if m[val]
61
+ }
61
62
 
62
- # result code
63
- data['status'] ||= result[0].to_i
63
+ # result code
64
+ record[:status] ||= result[0].to_i
64
65
 
65
- TreasureData.log(tag, data)
66
+ TreasureData.log(tag, record)
67
+ end
66
68
  end
67
69
  end
68
70
 
69
71
  end
70
72
  end
71
73
  end
74
+
75
+ module TreasureData
76
+ def self.access_log
77
+ Thread.current['td.access_log']
78
+ end
79
+ end
@@ -4,29 +4,35 @@ module Agent
4
4
  module Rails
5
5
 
6
6
  def self.init_controller
7
- ms = ControllerMethods
8
- modms = ControllerModuleMethods
9
- ActionController::Base.class_eval do
10
- include ms
11
- extend modms
12
- end
7
+ ActionController::Base.send(:include, ControllerExtension)
13
8
  end
14
9
 
15
- module ControllerMethods
16
- def td_access_log
17
- request.env['td.access_log'] ||= {}
10
+ module ControllerExtension
11
+ if defined?(ActiveSupport::Concern)
12
+ # Rails 2
13
+ extend ActiveSupport::Concern
14
+ else
15
+ def self.included(mod)
16
+ im = InstanceMethods
17
+ cm = ClassMethods
18
+ mod.class_eval do
19
+ include im
20
+ extend cm
21
+ end
22
+ end
23
+ end
24
+
25
+ module InstanceMethods
26
+ def td_access_log
27
+ request.env['td.access_log'] ||= {}
28
+ end
18
29
  end
19
- end
20
30
 
21
- module ControllerModuleMethods
31
+ module ClassMethods
32
+ end
22
33
  end
23
34
 
24
35
  end
25
36
  end
26
37
  end
27
-
28
- def self.access_log
29
- Thread.current['td.access_log']
30
- end
31
-
32
38
  end
@@ -4,75 +4,94 @@ module Agent
4
4
  module Rails
5
5
 
6
6
  def self.init_model
7
- ms = ModelMethods
8
- modms = ModelModuleMethods
9
- require 'active_record' unless defined?(ActiveRecord)
10
- ActiveRecord::Base.class_eval do
11
- include ms
12
- extend modms
7
+ unless defined?(::ActiveRecord)
8
+ # disable model extension if Rails > 3 and
9
+ # ActiveRecord is not loaded (other ORM is used)
10
+ if ::Rails.respond_to?(:version) && ::Rails.version =~ /^3/
11
+ return
12
+ end
13
+ require 'active_record'
13
14
  end
15
+ ::ActiveRecord::Base.send(:include, ModelExtension)
14
16
  end
15
17
 
16
- module ModelMethods
17
- end
18
+ module ModelExtension
19
+ if defined?(ActiveSupport::Concern)
20
+ # Rails 2
21
+ extend ActiveSupport::Concern
22
+ else
23
+ def self.included(mod)
24
+ im = InstanceMethods
25
+ cm = ClassMethods
26
+ mod.class_eval do
27
+ include im
28
+ extend cm
29
+ end
30
+ end
31
+ end
18
32
 
19
- module ModelModuleMethods
20
- def td_enable_model_tracer(tag, options={})
21
- only = nil
22
- except = nil
23
- static = {}
33
+ module InstanceMethods
34
+ end
24
35
 
25
- if o = options[:only]
26
- only = case o
27
- when Array
28
- o
29
- else
30
- [o]
31
- end.map {|e| e.to_s }
32
- end
36
+ module ClassMethods
37
+ def td_enable_model_tracer(tag, options={})
38
+ only = nil
39
+ except = nil
40
+ static = {}
33
41
 
34
- if o = options[:except]
35
- except = case o
36
- when Array
37
- o
38
- else
39
- [o]
40
- end.map {|e| e.to_s }
41
- end
42
+ if o = options[:only]
43
+ only = case o
44
+ when Array
45
+ o
46
+ else
47
+ [o]
48
+ end.map {|e| e.to_s }
49
+ end
42
50
 
43
- if o = options[:static]
44
- o.each_pair {|k,v|
45
- static[k.to_s] = v
46
- }
47
- end
51
+ if o = options[:except]
52
+ except = case o
53
+ when Array
54
+ o
55
+ else
56
+ [o]
57
+ end.map {|e| e.to_s }
58
+ end
48
59
 
49
- if defined?(after_commit)
50
- # Rails 3
51
- m = :after_commit
52
- else
53
- # Rails 2
54
- m = :after_save
55
- end
60
+ if o = options[:static]
61
+ o.each_pair {|k,v|
62
+ static[k.to_s] = v
63
+ }
64
+ end
65
+
66
+ if defined?(after_commit)
67
+ # Rails 3
68
+ m = :after_commit
69
+ else
70
+ # Rails 2
71
+ m = :after_save
72
+ end
56
73
 
57
- __send__(m) do |record|
58
- data = {}
59
- record.attribute_names.each {|name|
60
- name = name.to_s
61
- if (!only || only.include?(name)) && (!except || !except.include?(name))
62
- data[name] = record.read_attribute(name)
74
+ __send__(m) do |record|
75
+ data = {}
76
+ record.attribute_names.each {|name|
77
+ name = name.to_s
78
+ if (!only || only.include?(name)) && (!except || !except.include?(name))
79
+ data[name] = record.read_attribute(name)
80
+ end
81
+ }
82
+ static.each_pair {|k,v|
83
+ data[k] = v
84
+ }
85
+ if time = data['updated_at'] && time.is_a?(Time)
86
+ data['time'] = time.to_i
87
+ data.delete('updated_at')
63
88
  end
64
- }
65
- static.each_pair {|k,v|
66
- data[k] = v
67
- }
68
- if time = data['updated_at'] && time.is_a?(Time)
69
- data['time'] = time.to_i
70
- data.delete('updated_at')
89
+ TreasureData.log(tag, data)
71
90
  end
72
- TreasureData.log(tag, data)
73
91
  end
74
92
  end
75
93
  end
94
+
76
95
  end
77
96
  end
78
97
  end
@@ -1,11 +1,6 @@
1
1
  module TreasureData
2
2
  module Logger
3
3
  module Agent
4
- require 'td/logger/agent/middleware'
5
- require 'td/logger/agent/access_log'
6
- require 'td/logger/agent/rails/controller'
7
- require 'td/logger/agent/rails/model'
8
-
9
4
  module Rails
10
5
 
11
6
  CONFIG_PATH = 'config/treasure_data.yml'
@@ -28,22 +23,6 @@ production:
28
23
  test:
29
24
  EOF
30
25
 
31
- def self.rails_env
32
- if defined?(RAILS_ENV) && RAILS_ENV.to_s != ''
33
- RAILS_ENV.to_s
34
- else
35
- ::Rails.env
36
- end
37
- end
38
-
39
- def self.rails_root
40
- if defined?(RAILS_ROOT) && RAILS_ROOT.to_s != ''
41
- RAILS_ROOT.to_s
42
- else
43
- ::Rails.root
44
- end
45
- end
46
-
47
26
  class Config
48
27
  def initialize(conf)
49
28
  if agent = conf['agent']
@@ -97,14 +76,14 @@ EOF
97
76
  end
98
77
  return Config.new({
99
78
  'apikey' => apikey,
100
- 'database' => ENV['TREASURE_DATA_DB'] || "rails_#{rails_env}",
79
+ 'database' => ENV['TREASURE_DATA_DB'] || "rails_#{::Rails.env}",
101
80
  'access_log_table' => ENV['TREASURE_DATA_TABLE'] || 'web_access',
102
81
  'auto_create_table' => true
103
82
  })
104
83
  end
105
84
 
106
85
  begin
107
- src = File.read("#{rails_root}/#{CONFIG_PATH}")
86
+ src = File.read("#{::Rails.root}/#{CONFIG_PATH}")
108
87
  yaml = ERB.new(src).result
109
88
  env_conf = YAML.load(yaml)
110
89
  rescue
@@ -115,9 +94,9 @@ EOF
115
94
  return
116
95
  end
117
96
 
118
- conf = env_conf[rails_env]
97
+ conf = env_conf[::Rails.env]
119
98
  unless conf
120
- logger.warn "#{CONFIG_PATH} doesn't include setting for current environment (#{rails_env})."
99
+ logger.warn "#{CONFIG_PATH} doesn't include setting for current environment (#{::Rails.env})."
121
100
  logger.warn "Disabling Treasure Data logger."
122
101
  return
123
102
  end
@@ -132,6 +111,11 @@ EOF
132
111
  end
133
112
 
134
113
  def self.init(rails)
114
+ require 'td/logger/agent/middleware'
115
+ require 'td/logger/agent/access_log'
116
+ require 'td/logger/agent/rails/controller'
117
+ require 'td/logger/agent/rails/model'
118
+
135
119
  c = read_config(rails)
136
120
  return unless c
137
121
 
@@ -155,17 +139,17 @@ end
155
139
  end
156
140
  end
157
141
 
158
- if defined? Rails
159
- if Rails.respond_to?(:version) && Rails.version =~ /^3/
142
+ if defined? ::Rails
143
+ if ::Rails.respond_to?(:version) && ::Rails.version =~ /^3/
160
144
  module TreasureData
161
- class Railtie < Rails::Railtie
145
+ class Railtie < ::Rails::Railtie
162
146
  initializer "treasure_data_agent.start_plugin" do |app|
163
147
  TreasureData::Logger::Agent::Rails.init(app.config)
164
148
  end
165
149
  end
166
150
  end
167
151
  else
168
- TreasureData::Logger::Agent::Rails.init(Rails.configuration)
152
+ TreasureData::Logger::Agent::Rails.init(::Rails.configuration)
169
153
  end
170
154
  end
171
155
 
@@ -1,7 +1,7 @@
1
1
  module TreasureData
2
2
  module Logger
3
3
 
4
- VERSION = '0.2.4'
4
+ VERSION = '0.2.5'
5
5
 
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: td-logger
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 4
10
- version: 0.2.4
9
+ - 5
10
+ version: 0.2.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sadayuki Furuhashi
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-13 00:00:00 +09:00
18
+ date: 2011-09-30 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency