Sha256: 5386dd9d4f805f66ed1b49fa825d95d1433b8c6a923c37972063f9d56be49be6

Contents?: true

Size: 792 Bytes

Versions: 1

Compression:

Stored size: 792 Bytes

Contents

require 'semlogr'
require 'securerandom'

module Semlogr
  module Rack
    class RequestCorrelator
      def initialize(app, opts = {})
        @app = app
        @id_header = opts[:id_header] || 'X-Correlation-Id'
        @id_generator = opts[:id_generator] || -> { SecureRandom.uuid }
      end

      def call(env)
        id_header = env_header_name(@id_header)
        correlation_id = env[id_header] || @id_generator.call
        status, headers, body =
          Semlogr::LogContext.push_property(correlation_id: correlation_id) do
            @app.call(env)
          end

        headers[@id_header] = correlation_id

        [status, headers, body]
      end

      private

      def env_header_name(header)
        'HTTP_' + header.tr('-', '_').upcase
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
semlogr-rack-0.1.3 lib/semlogr/rack/request_correlator.rb