Sha256: 613421205b21159c6ffe19091a1cfe4bcb8d834d9b8524259ce25248b6a93d59

Contents?: true

Size: 1.4 KB

Versions: 13

Compression:

Stored size: 1.4 KB

Contents

require "logtail/config"
require "logtail/contexts/session"
require "logtail-rack/middleware"

module Logtail
  module Integrations
    module Rack
      # A Rack middleware that is responsible for adding the Session context
      # {Logtail::Contexts::Session}.
      class SessionContext < Middleware
        def call(env)
          id = get_session_id(env)
          if id
            context = Contexts::Session.new(id: id)
            CurrentContext.with(context) do
              @app.call(env)
            end
          else
            @app.call(env)
          end
        end

        private
          def get_session_id(env)
            if session = env['rack.session']
              if session.respond_to?(:id)
                Logtail::Config.instance.debug { "Rack env session detected, using id attribute" }
                session.id
              elsif session.respond_to?(:[])
                Logtail::Config.instance.debug { "Rack env session detected, using the session_id key" }
                session["session_id"]
              else
                Logtail::Config.instance.debug { "Rack env session detected but could not extract id" }
                nil
              end
            else
              Logtail::Config.instance.debug { "No session data could be detected, skipping" }

              nil
            end
          rescue Exception => e
            nil
          end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
logtail-rack-0.2.5 lib/logtail-rack/session_context.rb
logtail-rack-0.2.4 lib/logtail-rack/session_context.rb
logtail-rack-0.2.3 lib/logtail-rack/session_context.rb
logtail-rack-0.2.2 lib/logtail-rack/session_context.rb
logtail-rack-0.2.1 lib/logtail-rack/session_context.rb
logtail-rack-0.2.0 lib/logtail-rack/session_context.rb
logtail-rack-0.1.6 lib/logtail-rack/session_context.rb
logtail-rack-0.1.5 lib/logtail-rack/session_context.rb
logtail-rack-0.1.4 lib/logtail-rack/session_context.rb
logtail-rack-0.1.3 lib/logtail-rack/session_context.rb
logtail-rack-0.1.2 lib/logtail-rack/session_context.rb
logtail-rack-0.1.1 lib/logtail-rack/session_context.rb
logtail-rack-0.1.0 lib/logtail-rack/session_context.rb