maiha-request_id 0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README +27 -4
  2. data/lib/request_id.rb +38 -11
  3. metadata +2 -2
data/README CHANGED
@@ -7,17 +7,40 @@ Rails plugin that adds a new method (Controller#request_id) to distinguish reque
7
7
  Methods
8
8
  =======
9
9
 
10
- ActionController::Base#request_id
10
+ * ActionController::Base#request_id
11
+
12
+ => "3000:1" # request_id_with_port is true
13
+ => "1" # request_id_with_port is false
14
+
15
+
16
+ Configurations
17
+ ==============
18
+
19
+ * ActionController::Base.request_id_with_port
20
+
21
+ Indicates whether request_id needs port number or not.
22
+ It is useful under clustered servers. This is true, by default.
11
23
 
12
24
 
13
25
  Log
14
26
  ===
15
27
 
28
+ * Request ID field is automatically added into log file
29
+
16
30
  Processing UserController#index (for 127.0.0.1 at 2008-11-16 21:30:00) [GET]
17
- Request ID: 1 (port:3000)
18
- Session ID: 2e324cf7ff9737194a07911cdbf323b0
19
- Parameters: {"action"=>"index", "controller"=>"user"}
31
+ Request ID: 3000:1
32
+
33
+ Processing UserController#index (for 127.0.0.1 at 2008-11-16 21:30:05) [GET]
34
+ Request ID: 3000:2
35
+
36
+
37
+ Note
38
+ ====
39
+
40
+ * request_id is reset when the server restarts
20
41
 
42
+ * backend_port is used as port number only on Mongrel server, otherwise frontend_port is used.
43
+ Anyone know how to detect backend port number on Lighttpd, WEBrick and Thin?
21
44
 
22
45
 
23
46
  Copyright (c) 2008 maiha@wota.jp, released under the MIT license
data/lib/request_id.rb CHANGED
@@ -1,17 +1,48 @@
1
1
  module RequestId
2
2
  def self.included(base)
3
- instance_methods = rails22?(base) ? Rails22 : Rails21
3
+ instance_methods = rails2x? ? Rails2x : Rails1x
4
4
  base.class_eval do
5
5
  cattr_accessor :max_request_id
6
+ cattr_accessor :request_id_with_port
7
+ self.request_id_with_port = true
6
8
  include instance_methods
7
9
  end
8
10
  end
9
11
 
10
- def self.rails22?(base)
11
- base.instance_methods.include?("log_processing_for_request_id")
12
+ def self.rails2x?
13
+ ActionController::Base.private_instance_methods.include?("log_processing_for_request_id")
12
14
  end
13
15
 
14
- module Rails21
16
+ def request_id
17
+ @request_id ||= new_request_id
18
+ end
19
+
20
+ private
21
+ def new_request_id
22
+ id = self.class.max_request_id = self.class.max_request_id.to_i + 1
23
+ if self.class.request_id_with_port
24
+ port = backend_port || request.port
25
+ "#{port}:#{id}"
26
+ else
27
+ id.to_s
28
+ end
29
+ end
30
+
31
+ def backend_port
32
+ case request.cgi.class.name
33
+ when "Mongrel::CGIWrapper"
34
+ request.cgi.handler.listener.port rescue "unknown_mongrel"
35
+ when "Rack::Adapter::Rails::CGIWrapper"
36
+ # not implemented yet
37
+ nil
38
+ else
39
+ nil
40
+ end
41
+ rescue => error
42
+ logger.error "[request_id] backend_port error: #{error}" if logger
43
+ end
44
+
45
+ module Rails1x
15
46
  def self.included(base)
16
47
  base.class_eval do
17
48
  alias_method_chain :log_processing, :request_id
@@ -19,21 +50,17 @@ module RequestId
19
50
  end
20
51
 
21
52
  private
22
- def request_id
23
- @request_id ||= (self.class.max_request_id = self.class.max_request_id.to_i + 1)
24
- end
25
-
26
53
  def log_processing_with_request_id
27
54
  if logger
28
55
  logger.info "\n\nProcessing #{controller_class_name}\##{action_name} (for #{request_origin}) [#{request.method.to_s.upcase}]"
29
- logger.info " Request ID: #{request_id} (port:#{request.port})"
56
+ logger.info " Request ID: #{request_id}"
30
57
  logger.info " Session ID: #{@_session.session_id}" if @_session and @_session.respond_to?(:session_id)
31
58
  logger.info " Parameters: #{respond_to?(:filter_parameters) ? filter_parameters(params).inspect : params.inspect}"
32
59
  end
33
60
  end
34
61
  end
35
62
 
36
- module Rails22
63
+ module Rails2x
37
64
  def self.included(base)
38
65
  base.class_eval do
39
66
  alias_method_chain :log_processing_for_request_id, :request_id
@@ -43,7 +70,7 @@ module RequestId
43
70
  private
44
71
  def log_processing_for_request_id_with_request_id
45
72
  log_processing_for_request_id_without_request_id
46
- logger.info " Request ID: #{request_id} (port:#{request.port})"
73
+ logger.info " Request ID: #{request_id}"
47
74
  end
48
75
  end
49
76
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maiha-request_id
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.1"
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - maiha
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-11-16 00:00:00 -08:00
12
+ date: 2009-02-16 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15