Sha256: f370e924267c87c7942d02490550babdde4234013b0ff65d5432a489a39e4a36

Contents?: true

Size: 1.53 KB

Versions: 30

Compression:

Stored size: 1.53 KB

Contents

require 'honeybadger/conversions'

module Honeybadger
  # @api private
  class ContextManager
    include Conversions

    def self.current
      Thread.current[:__hb_context_manager] ||= new
    end

    def initialize
      @mutex = Mutex.new
      _initialize
    end

    def clear!
      _initialize
    end

    # Internal helpers


    def set_context(hash, &block)
      local = block_given?
      @mutex.synchronize do
        @global_context ||= {}
        @local_context ||= []

        new_context = Context(hash)

        if local
          @local_context << new_context
        else
          @global_context.update(new_context)
        end
      end

      if local
        begin
          yield
        ensure
          @mutex.synchronize { @local_context&.pop }
        end
      end
    end

    def get_context
      @mutex.synchronize do
        return @global_context unless @local_context

        @global_context.merge(@local_context.inject({}, :merge))
      end
    end

    def set_rack_env(env)
      @mutex.synchronize { @rack_env = env }
    end

    def get_rack_env
      @mutex.synchronize { @rack_env }
    end

    def set_request_id(request_id)
      @mutex.synchronize { @request_id = request_id }
    end

    def get_request_id
      @mutex.synchronize { @request_id }
    end

    private

    attr_accessor :custom, :rack_env, :request_id

    def _initialize
      @mutex.synchronize do
        @global_context = nil
        @local_context = nil
        @rack_env = nil
        @request_id = nil
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
honeybadger-5.24.0 lib/honeybadger/context_manager.rb
honeybadger-5.23.0 lib/honeybadger/context_manager.rb
honeybadger-5.22.0 lib/honeybadger/context_manager.rb
honeybadger-5.21.0 lib/honeybadger/context_manager.rb
honeybadger-5.20.1 lib/honeybadger/context_manager.rb
honeybadger-5.20.0 lib/honeybadger/context_manager.rb
honeybadger-5.19.2 lib/honeybadger/context_manager.rb
honeybadger-5.19.1 lib/honeybadger/context_manager.rb
honeybadger-5.19.0 lib/honeybadger/context_manager.rb
honeybadger-5.18.0 lib/honeybadger/context_manager.rb
honeybadger-5.17.0 lib/honeybadger/context_manager.rb
honeybadger-5.16.0 lib/honeybadger/context_manager.rb
honeybadger-5.15.6 lib/honeybadger/context_manager.rb
honeybadger-5.15.5 lib/honeybadger/context_manager.rb
honeybadger-5.15.4 lib/honeybadger/context_manager.rb
honeybadger-5.15.3 lib/honeybadger/context_manager.rb
honeybadger-5.15.2 lib/honeybadger/context_manager.rb
honeybadger-5.15.1 lib/honeybadger/context_manager.rb
honeybadger-5.15.0 lib/honeybadger/context_manager.rb
honeybadger-5.14.2 lib/honeybadger/context_manager.rb