Permalink
Browse files

Set up basic config class; middleware -> tracker

  • Loading branch information...
1 parent acf5ee1 commit ca9f132b1b9aa7839b75b990827d1172bca59af8 @nextmat nextmat committed Jan 9, 2013
View
@@ -0,0 +1 @@
+require 'librato/rack'
View
@@ -1 +0,0 @@
-require 'librato/rails'
View
@@ -1,4 +1,9 @@
-module Librato::Rack
+require 'thread'
+require 'librato/metrics'
+
+module Librato
+ module Rack; end
end
-require 'librato/rack/middleware'
+require 'librato/rack/configuration'
+require 'librato/rack/version'
@@ -0,0 +1,29 @@
+module Librato::Rack
+ # Holds configuration for Librato::Rack middleware to use.
+ # Acquires some settings by default from environment variables,
+ # but this allows easy setting and overrides.
+ #
+ # @example
+ # config = Librato::Rack::Configuration.new
+ # config.user = 'mimo@librato.com'
+ # config.token = 'mytoken'
+ #
+ class Configuration
+ attr_accessor :user, :token, :api_endpoint, :prefix, :source,
+ :source_pids, :log_level, :flush_interval
+
+ def initialize(&block)
+ # set up defaults
+ self.api_endpoint = Librato::Metrics.api_endpoint
+ self.flush_interval = 60
+
+ # check environment
+ self.user = ENV['LIBRATO_USER']
+ self.token = ENV['LIBRATO_TOKEN']
+ self.prefix = ENV['LIBRATO_PREFIX']
+ self.source = ENV['LIBRATO_SOURCE']
+ self.log_level = ENV['LIBRATO_LOG_LEVEL'] || :info
+ end
+
+ end
+end
@@ -1,47 +0,0 @@
-class Librato::Rack::Middleware
- def initialize(app, metrics = Librato::Rails)
- @app, @metrics = app, metrics
- end
-
- def call(env)
- @metrics.check_worker
-
- header_metrics env
-
- time = Time.now
- response = @app.call(env)
- duration = (Time.now - time) * 1000.0
-
- request_metrics response.first, duration
-
- response
- end
-
- private
-
- def header_metrics(env)
- return unless env.keys.include?('HTTP_X_HEROKU_QUEUE_DEPTH')
-
- @metrics.group 'rack.heroku' do |group|
- group.measure 'queue.depth', env['HTTP_X_HEROKU_QUEUE_DEPTH'].to_f
- group.timing 'queue.wait_time', env['HTTP_X_HEROKU_QUEUE_WAIT_TIME'].to_f
- group.measure 'queue.dynos', env['HTTP_X_HEROKU_DYNOS_IN_USE'].to_f
- end
- end
-
- def request_metrics(status, duration)
- @metrics.group 'rack.request' do |group|
- group.increment 'total'
- group.timing 'time', duration
- group.increment 'slow' if duration > 200.0
-
- group.group 'status' do |s|
- s.increment status
- s.increment "#{status.to_s[0]}xx"
-
- s.timing "#{status}.time", duration
- s.timing "#{status.to_s[0]}xx.time", duration
- end
- end
- end
-end
@@ -0,0 +1,61 @@
+module Librato::Rack
+ # Middleware for rack applications. Installs tracking hearbeat for
+ # metric submission and tracks performance metrics.
+ #
+ # @example
+ # require 'rack'
+ # require 'librato-rack'
+ #
+ # app = Rack::Builder.app do
+ # use Librato::Rack::Tracker
+ # run lambda { |env| }
+ # end
+ #
+ class Tracker
+ def initialize(app, config = nil)
+ @app, @config = app, config
+ end
+
+ def call(env)
+ # @metrics.check_worker
+
+ header_metrics env
+
+ time = Time.now
+ response = @app.call(env)
+ duration = (Time.now - time) * 1000.0
+
+ request_metrics response.first, duration
+
+ response
+ end
+
+ private
+
+ def header_metrics(env)
+ return unless env.keys.include?('HTTP_X_HEROKU_QUEUE_DEPTH')
+
+ # @metrics.group 'rack.heroku' do |group|
+ # group.measure 'queue.depth', env['HTTP_X_HEROKU_QUEUE_DEPTH'].to_f
+ # group.timing 'queue.wait_time', env['HTTP_X_HEROKU_QUEUE_WAIT_TIME'].to_f
+ # group.measure 'queue.dynos', env['HTTP_X_HEROKU_DYNOS_IN_USE'].to_f
+ # end
+ end
+
+ def request_metrics(status, duration)
+ # @metrics.group 'rack.request' do |group|
+ # group.increment 'total'
+ # group.timing 'time', duration
+ # group.increment 'slow' if duration > 200.0
+ #
+ # group.group 'status' do |s|
+ # s.increment status
+ # s.increment "#{status.to_s[0]}xx"
+ #
+ # s.timing "#{status}.time", duration
+ # s.timing "#{status.to_s[0]}xx.time", duration
+ # end
+ # end
+ end
+ end
+end
@@ -1,5 +1,3 @@
-module Librato
- module Rack
- VERSION = "0.1.0"
- end
+module Librato::Rack
+ VERSION = "0.1.0"
end

0 comments on commit ca9f132

Please sign in to comment.