Sha256: 501be166bfd39197897bc5f32b652dd3b88d11c1c83d7ccef9b853f2d4119748

Contents?: true

Size: 1.66 KB

Versions: 6

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module Airbrake
  module Rack
    # Adds context (URL, User-Agent, framework version, controller and more).
    #
    # @since v5.7.0
    class ContextFilter
      # @return [Integer]
      attr_reader :weight

      def initialize
        @weight = 99
      end

      # @see Airbrake::FilterChain#refine
      def call(notice)
        return unless (request = notice.stash[:rack_request])

        context = notice[:context]

        context[:url] = request.url
        context[:userAgent] = request.user_agent

        add_ip(context, request)
        add_framework_version(context)

        controller = request.env['action_controller.instance']
        return unless controller

        context[:component] = controller.controller_name
        context[:action] = controller.action_name
      end

      private

      def add_framework_version(context)
        if context.key?(:versions)
          context[:versions].merge!(framework_version)
        else
          context[:versions] = framework_version
        end
      end

      def framework_version
        @framework_version ||=
          if defined?(::Rails) && ::Rails.respond_to?(:version)
            { 'rails' => ::Rails.version }
          elsif defined?(::Sinatra)
            { 'sinatra' => Sinatra::VERSION }
          else
            {
              'rack_version' => ::Rack.version,
              'rack_release' => ::Rack.release,
            }
          end
      end

      def add_ip(context, request)
        context[:userAddr] =
          if request.respond_to?(:remote_ip)
            request.remote_ip
          else
            request.ip
          end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
airbrake-13.0.4 lib/airbrake/rack/context_filter.rb
airbrake-13.0.3 lib/airbrake/rack/context_filter.rb
airbrake-13.0.2 lib/airbrake/rack/context_filter.rb
airbrake-13.0.1 lib/airbrake/rack/context_filter.rb
airbrake-13.0.0 lib/airbrake/rack/context_filter.rb
airbrake-12.0.0 lib/airbrake/rack/context_filter.rb