Sha256: cca90b95037fd6105fc0c08f7298d8f779bda035ca629ea437513b4ace463d11

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require 'rack'
require 'request_store'
require 'securerandom'

module Marlowe
  # Marlowe correlation id middleware. Including this into your
  # middleware stack will add a correlation id header as an incoming
  # request, and save that id in a request session variable.

  # Name of the default header to look for and put the correlation id in.
  CORRELATION_HEADER = 'Correlation-Id'.freeze

  class Middleware
    # Sets the the rack application to +app+
    def initialize(app, opts={})
      @app = app
      @correlation_header = format_http_header(opts[:correlation_header] || Marlowe::CORRELATION_HEADER)
    end

    # Stores the incoming correlation id from the +env+ hash. If the correlation
    # id has not been sent, a new UUID is generated and the +env+ is modified.
    def call(env)
      env[@correlation_header] ||= SecureRandom.uuid
      RequestStore.store[:correlation_id] = env[@correlation_header]

      @status, @headers, @response = @app.call(env)
      [@status, @headers, @response]
    end

    private

    def format_http_header(header)
      ("HTTP_" + header.gsub(/-/, '_').upcase).freeze
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
marlowe-1.0.3 lib/marlowe/middleware.rb