Class: Metrics::Integration::WEBrick

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Includes:
Logging
Defined in:
lib/ruby-metrics/integration/webrick.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, #logger

Constructor Details

#initialize(server, agent) ⇒ WEBrick

Returns a new instance of WEBrick.



35
36
37
# File 'lib/ruby-metrics/integration/webrick.rb', line 35

def initialize(server, agent)
  @agent = agent
end

Class Method Details

.start(options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby-metrics/integration/webrick.rb', line 17

def self.start(options = {})
  connection_options = {:Port => options.delete(:port) || options.delete(:Port) || 8001}
  agent = options.delete(:agent) || Agent.new
  
  logger.debug "Creating Metrics daemon thread."
  @thread = Thread.new do
    begin
      server = ::WEBrick::HTTPServer.new(connection_options)
      server.mount "/stats", self, agent
      server.start
    rescue Exception => e
      logger.error "Error in thread: %s: %s\n\t%s" % [e.class.to_s,
                                                      e.message,
                                                      e.backtrace.join("\n\t")]
    end
  end
end

Instance Method Details

#do_GET(request, response) ⇒ Object



39
40
41
42
43
# File 'lib/ruby-metrics/integration/webrick.rb', line 39

def do_GET(request, response)
  response.status           = 200
  response['Content-Type']  = 'application/json'
  response.body             = @agent.to_json
end