columbo 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -13,7 +13,12 @@ If your application includes a rackup file
13
13
  or uses *Rack::Builder* to construct the application pipeline,
14
14
  simply require and use as follows:
15
15
 
16
- require 'columbo/capture'
16
+ require 'columbo/capture', {
17
+ capture: true,
18
+ bench: false,
19
+ mongo_uri: 'mongodb://user:password@mongodb_host:port/database',
20
+ logger: 'log/columbo.log'
21
+ }
17
22
  use Columbo::Capture
18
23
  run app
19
24
 
@@ -27,10 +32,11 @@ In order to use, include the following in a Rails application
27
32
  *config/application.rb* file:
28
33
 
29
34
  require 'columbo/capture'
30
- config.middleware.insert_before ActionDispatch::ShowExceptions, Columbo::Capture, {
35
+ config.middleware.insert 0, Columbo::Capture, {
31
36
  capture: Rails.env.production?,
32
- bench: true,
33
- mongo_uri: 'mongodb://user:password@mongodb_host:port/database'
37
+ bench: false,
38
+ mongo_uri: 'mongodb://user:password@mongodb_host:port/database',
39
+ logger: 'log/columbo.log'
34
40
  }
35
41
 
36
42
  Check the Rack configuration:
@@ -6,15 +6,16 @@ module Columbo
6
6
  class Capture
7
7
  include Rack::Utils
8
8
 
9
- FORMAT = %{[Columbo #{Columbo::VERSION}] %s - [%s] %s "%s%s %s"\n}
9
+ FORMAT = %{[Columbo #{Columbo::VERSION}] [%s] %s - %s "%s%s %s\n}
10
10
 
11
11
  def initialize(app, opts={})
12
12
  @app = app
13
- @capture = opts[:capture] || false
14
- @bench = (opts[:capture] && opts[:bench]) || false
15
- @logger = opts[:logger]
13
+ @capture = opts[:capture]
14
+ @bench = opts[:capture] && opts[:bench]
16
15
  @mongo_uri = opts[:mongo_uri]
17
16
 
17
+ Columbo.logger = opts[:logger] if opts[:logger]
18
+
18
19
  raise ArgumentError, 'mongo URI missing.' if @mongo_uri.nil?
19
20
 
20
21
  @inspector = Columbo::Inspector.new @mongo_uri
@@ -38,14 +39,23 @@ module Columbo
38
39
  headers['content-type'] &&
39
40
  headers['content-type'].include?("text/html")
40
41
 
41
- Thread.new { @inspector.investigate env, status, headers, response, start_processing, stop_processing if @capture }
42
+ Thread.abort_on_exception = true
43
+
44
+ Thread.new do
45
+ begin
46
+ @inspector.investigate env, status, headers, response, start_processing, stop_processing
47
+ rescue Exception => e
48
+ log_error env, e
49
+ end
50
+ end if @capture
42
51
 
43
52
  end
44
53
 
45
54
  if @bench
46
55
  stop = Time.now
47
- log(env, (stop-start).seconds)
48
- headers['Columbo'] = "version #{Columbo::VERSION}, time #{(stop-start).seconds}s"
56
+ duration = ((stop-start).seconds * 1000).round(3)
57
+ log(env, "Time: #{duration}ms")
58
+ headers['Columbo'] = "version #{Columbo::VERSION}, time #{duration}ms"
49
59
  end
50
60
 
51
61
  [status, headers, response]
@@ -53,19 +63,28 @@ module Columbo
53
63
 
54
64
  private
55
65
 
56
- def log(env, time)
66
+ def log(env, message)
57
67
  now = Time.now
58
- logger = @logger || env['rack.errors']
68
+
69
+ logger = Columbo.logger || env['rack.errors']
59
70
 
60
71
  logger.write FORMAT % [
61
- "Time: #{time}s",
62
- now.strftime("%d-%b-%Y %H:%M:%S"),
63
- env["REQUEST_METHOD"],
64
- env["PATH_INFO"],
65
- env["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"],
66
- env["HTTP_VERSION"]
72
+ now.strftime('%d-%b-%Y %H:%M:%S'),
73
+ message,
74
+ env['REQUEST_METHOD'],
75
+ env['PATH_INFO'],
76
+ env['QUERY_STRING'].empty? ? '' : '?' + env['QUERY_STRING'],
77
+ env['HTTP_VERSION']
67
78
  ]
68
79
  end
69
80
 
81
+ def log_error(env, exception)
82
+ begin
83
+ logger = Columbo.logger || env['rack.errors']
84
+ log env, "Error: " + exception.message
85
+ logger.write "#{exception.backtrace.join("\n")}\n"
86
+ end
87
+ end
88
+
70
89
  end
71
90
  end
@@ -22,8 +22,6 @@ module Columbo
22
22
  # Get request headers
23
23
  request_headers = {}
24
24
  request.env.each { |key, value| request_headers[key.sub(/^HTTP_/, '').downcase] = value if key.start_with? 'HTTP_'}
25
- # Convert MIME types into String
26
- formats = request.env['action_dispatch.request.formats'].collect {|format| format.to_s}
27
25
  data = {
28
26
  request: {
29
27
  params: request.params,
@@ -40,7 +38,6 @@ module Columbo
40
38
  http: request.env['SERVER_PROTOCOL'],
41
39
  session: request.env['rack.session'],
42
40
  cookie: request.env['rack.request.cookie_hash'],
43
- formats: formats,
44
41
  path_parameters: request.env['action_dispatch.request.path_parameters'],
45
42
  headers: request_headers
46
43
  },
@@ -55,7 +52,6 @@ module Columbo
55
52
  time: stop-start
56
53
  }
57
54
  # Insert data in MongoDB
58
- # TODO: error logging
59
55
  client.insert sanitize(data)
60
56
  end
61
57
 
@@ -0,0 +1,25 @@
1
+ require 'logger'
2
+
3
+ module Columbo
4
+
5
+ class LogWriter < Logger
6
+ def initialize(log = STDOUT)
7
+ super(log)
8
+ self.level = Logger::INFO
9
+ self.formatter = Simple.new
10
+ self
11
+ end
12
+
13
+ def write(message)
14
+ add Logger::INFO, message
15
+ end
16
+
17
+ class Simple < Logger::Formatter
18
+ # Provide a call() method that returns the formatted message.
19
+ def call(severity, time, program_name, message)
20
+ "#{message}"
21
+ end
22
+ end
23
+
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module Columbo
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
data/lib/columbo.rb CHANGED
@@ -1,7 +1,16 @@
1
1
  require 'columbo/version'
2
2
  require 'columbo/db_client'
3
3
  require 'columbo/inspector'
4
+ require 'columbo/log_writer'
4
5
 
5
6
  module Columbo
6
7
  MONGO_COLLECTION = "hits".freeze
8
+
9
+ def self.logger
10
+ @logger
11
+ end
12
+
13
+ def self.logger=(log = STDOUT)
14
+ @logger = Columbo::LogWriter.new log
15
+ end
7
16
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: columbo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-26 00:00:00.000000000 Z
12
+ date: 2013-03-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -103,6 +103,7 @@ files:
103
103
  - lib/columbo/capture.rb
104
104
  - lib/columbo/db_client.rb
105
105
  - lib/columbo/inspector.rb
106
+ - lib/columbo/log_writer.rb
106
107
  - lib/columbo/version.rb
107
108
  - lib/columbo/web.rb
108
109
  - lib/columbo.rb