Sha256: 9d3c364a63a0f7612a5c5e122e0a59f5b33797465a6868fc02d3aa0acffe21bb

Contents?: true

Size: 1.34 KB

Versions: 12

Compression:

Stored size: 1.34 KB

Contents

require File.join(File.dirname(__FILE__), '..', '..', 'ruby-metrics')

require 'webrick'

module Metrics
  
  class Agent
    def start(options = {})
      Integration::WEBrick.start(options.merge(:agent => self))
    end
  end
  
  module Integration
    class WEBrick < ::WEBrick::HTTPServlet::AbstractServlet
      include Logging
      
      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
      
      def initialize(server, agent)
        @agent = agent
      end
      
      def do_GET(request, response)
        response.status           = 200
        response['Content-Type']  = 'application/json'
        response.body             = @agent.to_json
      end
      
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
ruby-metrics-0.9.4 lib/ruby-metrics/integration/webrick.rb
ruby-metrics-0.9.3 lib/ruby-metrics/integration/webrick.rb
ruby-metrics-0.9.2 lib/ruby-metrics/integration/webrick.rb
ruby-metrics-0.9.1 lib/ruby-metrics/integration/webrick.rb
ruby-metrics-0.9.0 lib/ruby-metrics/integration/webrick.rb
yam-ruby-metrics-0.8.9 lib/ruby-metrics/integration/webrick.rb
yam-ruby-metrics-0.8.8 lib/ruby-metrics/integration/webrick.rb
yam-ruby-metrics-0.8.7 lib/ruby-metrics/integration/webrick.rb
yam-ruby-metrics-0.8.6 lib/ruby-metrics/integration/webrick.rb
ruby-metrics-0.8.6 lib/ruby-metrics/integration/webrick.rb
ruby-metrics-0.8.5 lib/ruby-metrics/integration/webrick.rb
ruby-metrics-0.8.0 lib/ruby-metrics/integration/webrick.rb