Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Push data to statsd in middleware

  • Loading branch information...
commit 28d2b107b6d4a7d9789519eeed8ad5bf722eec75 1 parent 16907d4
@jeremy jeremy authored
Showing with 33 additions and 18 deletions.
  1. +33 −18 lib/trashed/rack.rb
View
51 lib/trashed/rack.rb
@@ -1,31 +1,46 @@
module Trashed
module Rack
class MeasureResourceUsage
- def initialize(app, options = nil)
- @app, @options = app, options
-
- if @options
- @logger = options[:logger]
- @debug = options[:debug]
- end
+ def initialize(app, options = {})
+ @app = app
+ @debug, @logger, @statsd, @sample_rate = options.values_at(:debug, :logger, :statsd, :sample_rate)
+ @sample_rate ||= 0.1
end
def call(env)
response = nil
+ instrument { response = @app.call(env) }
+ response
+ end
- used = env['trashed.used'] = ResourceUsage.instrument { response = @app.call(env) }
- snap = env['trashed.snapshot'] = ResourceUsage.gauge
+ def instrument(&block)
+ change = env['trashed.change'] = ResourceUsage.instrument(&block)
+ usage = env['trashed.usage'] = ResourceUsage.gauge
+ record change, usage
+ end
- if @logger
- if @debug
- @logger.debug "Used: #{used.to_yaml}"
- @logger.debug "Snapshot: #{snap.to_yaml}"
- else
- @logger.info "Rack handled in #{used['Time.wall']}ms (GC runs: #{used['GC.count']})"
- end
- end
+ def record(change, usage)
+ record_logger change, usage if @logger
+ record_statsd change, usage if @statsd
+ end
- response
+ def record_logger(change, usage)
+ @logger.info "Rack handled in %dms (GC runs: %d)" % change.values_at('Time.wall', 'GC.count')
+ record_debug_logger change, usage if @debug
+ end
+
+ def record_debug_logger(change, usage)
+ @logger.debug "Changes: #{change.to_yaml}"
+ @logger.debug "Usage: #{usage.to_yaml}"
+ end
+
+ def record_statsd(change, usage)
+ record_statsd_timing change
+ record_statsd_timing usage
+ end
+
+ def record_statsd_timing(data)
+ data.each { |name, value| @statsd.timing "Performance.#{name}", value, @sample_rate }
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.