Sha256: 64b6bb2efa0e6265a2c1294d899b1240d3f6fbe7824b2282d5e73e86dbe7ad7b

Contents?: true

Size: 950 Bytes

Versions: 21

Compression:

Stored size: 950 Bytes

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 Middleware
        def initialize(app)
          @app = app
        end

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

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

          body = Rack::BodyProxy.new(body) do
            Context.clear!
          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

21 entries across 21 versions & 1 rubygems

Version Path
plain_apm-0.2.8 lib/plain_apm/extensions/context/middleware.rb