Class: Synchrolog::Middleware

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



6
7
8
# File 'lib/synchrolog/middleware.rb', line 6

def initialize app
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/synchrolog/middleware.rb', line 10

def call env
  request = ActionDispatch::Request.new(env)
  if request.original_fullpath == "/synchrolog-time"
    [ 200, {'Content-Type' => 'application/json'}, [{ time: Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%3NZ") }.to_json] ]
  else
    if request.cookie_jar['synchrolog_anonymous_id'].nil?
      request.cookie_jar['synchrolog_anonymous_id'] = SecureRandom.hex
    end
    anonymous_id = request.cookie_jar['synchrolog_anonymous_id']
    user_id = request.cookie_jar['synchrolog_user_id']
    SYNCHROLOG.logger.tagged("synchrolog_anonymous_id:#{anonymous_id}", "synchrolog_user_id:#{user_id}") do
      begin
        response = @app.call(env)
      rescue Exception => exception
        SYNCHROLOG.exception_logger.capture(response, exception, env, anonymous_id, user_id)
        raise
      end
      exception = env['rack.exception'] || env['action_dispatch.exception']
      SYNCHROLOG.exception_logger.capture(response, exception, env, anonymous_id, user_id) if exception
      response
    end
  end
end