Sha256: d27212b5fd058904747120cf611943f241530aefc72e3cae013ab783e6f83a4d

Contents?: true

Size: 1.55 KB

Versions: 5

Compression:

Stored size: 1.55 KB

Contents

module Timber
  module Probes
    # Reponsible for automatically adding the HTTP context for applications that use `Rack`.
    class RackHTTPContext < Probe
      class Middleware
        def initialize(app)
          @app = app
        end

        def call(env)
          request = ::Rack::Request.new(env)
          context = Contexts::HTTP.new(
            method: request.request_method,
            path: request.path,
            remote_addr: request.ip,
            request_id: request_id(env)
          )
          CurrentContext.instance.with(context) do
            @app.call(env)
          end
        end

        private
          def request_id(env)
            env["X-Request-ID"] ||
              env["X-Request-Id"] ||
              env["action_dispatch.request_id"]
          end
      end

      attr_reader :middleware, :insert_before

      def initialize(middleware, insert_before)
        if middleware.nil?
          raise RequirementNotMetError.new("The middleware class attribute is not set. " +
            "We need a middleware to insert the probe.")
        end
        @middleware = middleware
        @insert_before = insert_before
      end

      def insert!
        var_name = :"@_timber_rack_http_inserted"
        return true if middleware.instance_variable_defined?(var_name) && middleware.instance_variable_get(var_name) == true
        # Rails uses a proxy :/, so we need to do this instance variable hack
        middleware.instance_variable_set(var_name, true)
        middleware.insert_before insert_before, Middleware
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
timber-1.0.3 lib/timber/probes/rack_http_context.rb
timberio-1.0.3 lib/timber/probes/rack_http_context.rb
timberio-1.0.2 lib/timber/probes/rack_http_context.rb
timberio-1.0.1 lib/timber/probes/rack_http_context.rb
timberio-1.0.0 lib/timber/probes/rack_http_context.rb