Sha256: 2c84fba477dbae11b17f25ef20b1b71384922517608561ef34db31e3fe78d22d

Contents?: true

Size: 1.54 KB

Versions: 10

Compression:

Stored size: 1.54 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.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

10 entries across 10 versions & 1 rubygems

Version Path
timber-1.0.13 lib/timber/probes/rack_http_context.rb
timber-1.0.12 lib/timber/probes/rack_http_context.rb
timber-1.0.11 lib/timber/probes/rack_http_context.rb
timber-1.0.10 lib/timber/probes/rack_http_context.rb
timber-1.0.9 lib/timber/probes/rack_http_context.rb
timber-1.0.8 lib/timber/probes/rack_http_context.rb
timber-1.0.7 lib/timber/probes/rack_http_context.rb
timber-1.0.6 lib/timber/probes/rack_http_context.rb
timber-1.0.5 lib/timber/probes/rack_http_context.rb
timber-1.0.4 lib/timber/probes/rack_http_context.rb