Class: UsageTracker::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/usage_tracker/middleware.rb

Constant Summary collapse

@@host =
'localhost'
@@port =
5985
@@backend =
`hostname`.strip
@@logger =
UsageTracker::Log.new
@@headers =
[
  # "REMOTE_ADDR",
  "REQUEST_METHOD",
  "PATH_INFO",
  "REQUEST_URI",
  "SERVER_PROTOCOL",
  #"HTTP_VERSION",
  "HTTP_HOST",
  "HTTP_USER_AGENT",
  "HTTP_ACCEPT",
  "HTTP_ACCEPT_LANGUAGE",
  "HTTP_X_FORWARDED_FOR",
  "HTTP_X_FORWARDED_PROTO",
  #"HTTP_ACCEPT_LANGUAGE",
  #"HTTP_ACCEPT_ENCODING",
  #"HTTP_ACCEPT_CHARSET",
  #"HTTP_KEEP_ALIVE",
  "HTTP_CONNECTION",
  #"HTTP_COOKIE",
  #"HTTP_CACHE_CONTROL",
  #"SERVER_NAME",
  #"SERVER_PORT",
  "QUERY_STRING"
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



43
44
45
46
47
48
# File 'lib/usage_tracker/middleware.rb', line 43

def initialize(app, options={})
  @@host    = options[:host]     if options.keys.include?(:host) 
  @@port    = options[:port]     if options.keys.include?(:port) 
  @@backend = options[:backend]  if options.keys.include?(:backend)
  @app      = app
end

Class Method Details

.development?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/usage_tracker/middleware.rb', line 84

def development? 
  defined?(Rails) && Rails.env.development? 
end

.track(data) ⇒ Object

Writes the given ‘data` to the reactor, using the UDP protocol. Times out after 1 second. If a write error occurs, data is lost.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/usage_tracker/middleware.rb', line 92

def track(data)
  Timeout.timeout(1) do

    @@logger.debug("Sending to #{@@host}:#{@@port} : #{data.to_json}") if development? 

    UDPSocket.open do |sock|
      sock.connect(@@host, @@port.to_i)
      sock.write_nonblock(data << "\n")
    end
  end

rescue Timeout::Error, Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EINTR
  @@logger.error "Cannot track data: #{$!.message}"
end

Instance Method Details

#call(env) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/usage_tracker/middleware.rb', line 50

def call(env)
  req_start = Time.now.to_f
  response  = @app.call env
  req_end   = Time.now.to_f

  begin
    data = {
      :user_id   => env['rack.session'][:user_id],
      :remote_ip => env['action_dispatch.remote_ip'].to_s, 
      :duration  => ((req_end - req_start) * 1000).to_i,
      :backend   => @@backend,
      :xhr       => env['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest',
      :context   => env[Context.key],
      :env       => {},
      :status    => response[0] # response contains [status, headers, body]
    }

    @@headers.each {|key| data[:env][key.downcase] = env[key] unless env[key].blank?}

    self.class.track(data.to_json)

  rescue
    raise unless response # Error in the application, raise it up
    # Error in usage tracker itself
    @@logger.error($!.message)
    @@logger.error($!.backtrace.join("\n"))
    
  end

  return response
end