Sha256: 30bdb9a74fd41d42bbda1ef6b008a8d1f7059076128cab3d9ae0d4c4e96b6354

Contents?: true

Size: 1.01 KB

Versions: 17

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

require "securerandom"

##
# This code is inspired by request_store gem by Steve Klabnik:
#
# https://github.com/steveklabnik/request_store/
#
# See LICENSE.txt in the current directory for the license.
module PlainApm
  module Extensions
    module Context
      class Rack
        def initialize(app)
          @app = app
        end

        def call(env)
          Context.trace_id = trace_id(env)

          status, headers, body = @app.call(env)

          body = if defined?(::Rack::BodyProxy)
            ::Rack::BodyProxy.new(body) do
              Context.clear!
            end
          else
            body
          end

          processed = true

          [status, headers, body]
        ensure
          Context.clear! if !processed
        end

        private

        def trace_id(env)
          request_id(env) || SecureRandom.uuid
        end

        def request_id(env)
          env["action_dispatch.request_id"] || env["HTTP_X_REQUEST_ID"]
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
plain_apm-0.10.2 lib/plain_apm/extensions/context/rack.rb
plain_apm-0.10.0 lib/plain_apm/extensions/context/rack.rb
plain_apm-0.9.8 lib/plain_apm/extensions/context/rack.rb
plain_apm-0.9.7 lib/plain_apm/extensions/context/rack.rb
plain_apm-0.9.6 lib/plain_apm/extensions/context/rack.rb
plain_apm-0.9.5 lib/plain_apm/extensions/context/rack.rb
plain_apm-0.9.4 lib/plain_apm/extensions/context/rack.rb
plain_apm-0.9.3 lib/plain_apm/extensions/context/rack.rb
plain_apm-0.9.2 lib/plain_apm/extensions/context/rack.rb
plain_apm-0.9.1 lib/plain_apm/extensions/context/rack.rb
plain_apm-0.9.0 lib/plain_apm/extensions/context/rack.rb
plain_apm-0.8.8 lib/plain_apm/extensions/context/rack.rb
plain_apm-0.8.7 lib/plain_apm/extensions/context/rack.rb
plain_apm-0.8.6 lib/plain_apm/extensions/context/rack.rb
plain_apm-0.8.5 lib/plain_apm/extensions/context/rack.rb
plain_apm-0.8.4 lib/plain_apm/extensions/context/rack.rb
plain_apm-0.8.3 lib/plain_apm/extensions/context/rack.rb