class Rack::Monitor::OpenTSDB

Public Class Methods

new(app, key, options={}) click to toggle source
# File lib/rack/monitor/opentsdb.rb, line 12
def initialize(app, key, options={})
  @app = app
  @options = {
    :host            => 'localhost',
    :port            => 4242,
    :key             => key,
    # If somebody has a better idea than defining a regex and gsub
    # replacement tell me.
    :track_regex     => /^(\/[^\/]+).*?$/, # Regex to select trackable string
    :track_replace   => '\1',              # gsub pattern to select trackable string
    :report_interval => 60,
    :logger          => nil,
    :tags            => {}
  }.merge(options)
  @stats = Stats.new
  @reporter = Reporter.new(@stats, @options[:host], @options[:port], @options[:key], @options[:report_interval], @options[:tags], @options[:logger])
  @reporter.start
end

Public Instance Methods

call(env, options={}) click to toggle source
# File lib/rack/monitor/opentsdb.rb, line 31
def call(env, options={})
  track_path = env['REQUEST_METHOD'] + ' ' + env['PATH_INFO'].gsub(@options[:track_regex], @options[:track_replace])

  beginning_time = Time.now
  status, headers, body = @app.call(env)
  execution_time = (Time.now - beginning_time)

  @stats.synchronize do
    @stats[track_path] ||= {}
    @stats[track_path][status] ||= []
    @stats[track_path][status] << execution_time
  end

  [status, headers, body]
end