frankenstein 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 43e6658a05dd6f1406af38841a88b1c4a8f0e0c8
4
- data.tar.gz: 66a2d42cee1f6e7a5d9685cd54aa24520e9efcba
3
+ metadata.gz: b078bfb45f77f51b4bd6e5b28aa3f2e7e5607a21
4
+ data.tar.gz: 78da8f3428955ece59e4d10bbb04a05fbdc23609
5
5
  SHA512:
6
- metadata.gz: 0f24f97eabdfb426390ef6a5474ec8cf74db408523c3802a56ab2691cfb92a4968e40fdb724138f6166b3464b9cf885c6bd1d4c6cd2f882f66cc54b4f0763603
7
- data.tar.gz: d7b6a0fd0811305b2960ad5c4557dd1a8fcf9236ac29588da6684af2c4acdfdd7cc5cfd607978842ee8ae0aa52d4200b67b9c1fbb55d7e905838091929ed11b9
6
+ metadata.gz: 802b00805c5c4366f720835590eeec6afbc4735118c88c2224ae477ac80a8a3d075b4353318e351b8041cf921969ae66d539542872186167196d89612297bbf2
7
+ data.tar.gz: beae09c8b7d81e80c8f856b93ed217d54a565e4b2b9925949a2539235720c11beb9b7043152d25c313704936691c6601c64ee8fbe84960899a2ba88a1992831f
@@ -0,0 +1,56 @@
1
+ require 'logger'
2
+
3
+ module Frankenstein
4
+ class Server
5
+ # Wrap a logger in behaviour that suits WEBrick's ideosyncratic style
6
+ #
7
+ # There are a few things in the way that WEBrick interacts with the standard
8
+ # Logger class which don't suit modern logging practices:
9
+ #
10
+ # * It doesn't set a `progname` when logging, which makes it much harder to
11
+ # separate out WEBrick logs from other parts of your system.
12
+ # * Logging calls use the direct-pass approach to passing in strings, which
13
+ # degrades performance.
14
+ # * Access logging doesn't log with a priority, making it impossible to
15
+ # selectively log or not log access logs.
16
+ # * Access logging doesn't respect the logger formatting, making it much harder
17
+ # to parse logs than it should need to be.
18
+ #
19
+ # This class is an attempt to capture all that aberrant behaviour and redirect
20
+ # it into more socially-acceptable forms:
21
+ #
22
+ # * All of the "priority" methods (`warn`, `debug`, etc) are wrapped to inject
23
+ # `progname`.
24
+ # * Access logging (or, more precisely, all calls to `#<<`) are intercepted
25
+ # and logged via the standard (format-respecting) calls, with the specified priority.
26
+ #
27
+ # This will *hopefully* provide a more palatable WEBrick logging experience.
28
+ #
29
+ class WEBrickLogger
30
+ # @param logger [Logger] the *actual* Logger you want to send all of the log
31
+ # messages to.
32
+ # @param progname [#to_s] the `progname` you want to pass to all log messages
33
+ # that come through this wrapper.
34
+ # @param access_log_priority [Fixnum] the priority at which to log access log messages.
35
+ # Any of the `Logger::*` priority constants will work Just Fine.
36
+ #
37
+ def initialize(logger:, progname: "WEBrick", access_log_priority: Logger::DEBUG)
38
+ @logger, @progname, @priority = logger, progname, access_log_priority
39
+ end
40
+
41
+ %i{debug error fatal info warn}.each do |sev|
42
+ define_method(sev) do |msg, &blk|
43
+ if blk
44
+ @logger.__send__(sev, @progname, &blk)
45
+ else
46
+ @logger.__send__(sev, @progname) { msg }
47
+ end
48
+ end
49
+ end
50
+
51
+ def <<(msg)
52
+ @logger.add(@priority, msg, @progname)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -8,6 +8,7 @@ require 'rack/handler/webrick'
8
8
  require 'rack/deflater'
9
9
 
10
10
  require 'frankenstein/error'
11
+ require 'frankenstein/server/webrick_logger'
11
12
 
12
13
  module Frankenstein
13
14
  # A straightforward Prometheus metrics server.
@@ -23,7 +24,7 @@ module Frankenstein
23
24
  # stats_server.run # We are now serving stats on port 8080!
24
25
  # counter = stats_server.registry.counter(:seconds_count, "Number of seconds")
25
26
  # # Give the counter something to count
26
- # loop { counter.increment({}) }
27
+ # loop { sleep 1; counter.increment({}) }
27
28
  #
28
29
  # Now if you hit http://localhost:8080/metrics you should see a counter
29
30
  # gradually going up, along with stats about the Frankenstein HTTP server
@@ -65,7 +66,7 @@ module Frankenstein
65
66
  @op_cv = ConditionVariable.new
66
67
  end
67
68
 
68
- # Start the server instance running.
69
+ # Start the server instance running in a separate thread.
69
70
  #
70
71
  # This method returns once the server is just about ready to start serving
71
72
  # requests.
@@ -76,11 +77,22 @@ module Frankenstein
76
77
 
77
78
  @server_thread = Thread.new do
78
79
  @op_mutex.synchronize do
79
- @server = WEBrick::HTTPServer.new(Logger: @logger, BindAddress: nil, Port: @port)
80
- @server.mount "/", Rack::Handler::WEBrick, app
81
- @op_cv.signal
80
+ begin
81
+ wrapped_logger = Frankenstein::Server::WEBrickLogger.new(logger: @logger)
82
+ @server = WEBrick::HTTPServer.new(Logger: wrapped_logger, BindAddress: nil, Port: @port, AccessLog: [[wrapped_logger, WEBrick::AccessLog::COMMON_LOG_FORMAT]])
83
+ @server.mount "/", Rack::Handler::WEBrick, app
84
+ rescue => ex
85
+ @logger.fatal("Frankenstein::Server#run") { (["Exception while trying to create WEBrick::HTTPServer: #{ex.message} (#{ex.class})"] + ex.backtrace).join("\n ") }
86
+ ensure
87
+ @op_cv.signal
88
+ end
89
+ end
90
+
91
+ begin
92
+ @server.start if @server
93
+ rescue => ex
94
+ @logger.fatal("Frankenstein::Server#run") { (["Exception while running WEBrick::HTTPServer: #{ex.message} (#{ex.class})"] + ex.backtrace).join("\n ") }
82
95
  end
83
- @server.start
84
96
  end
85
97
  end
86
98
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frankenstein
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Palmer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-14 00:00:00.000000000 Z
11
+ date: 2018-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prometheus-client
@@ -229,6 +229,7 @@ files:
229
229
  - lib/frankenstein/error.rb
230
230
  - lib/frankenstein/request.rb
231
231
  - lib/frankenstein/server.rb
232
+ - lib/frankenstein/server/webrick_logger.rb
232
233
  homepage: https://github.com/discourse/frankenstein
233
234
  licenses: []
234
235
  metadata: {}
@@ -248,7 +249,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
248
249
  version: '0'
249
250
  requirements: []
250
251
  rubyforge_project:
251
- rubygems_version: 2.5.2
252
+ rubygems_version: 2.6.13
252
253
  signing_key:
253
254
  specification_version: 4
254
255
  summary: or, the Modern Prometheus