class Honeybadger::ContextManager

@api private

Attributes

custom[RW]
rack_env[RW]

Public Class Methods

current() click to toggle source
# File lib/honeybadger/context_manager.rb, line 8
def self.current
  Thread.current[:__hb_context_manager] ||= new
end
new() click to toggle source
# File lib/honeybadger/context_manager.rb, line 12
def initialize
  @mutex = Mutex.new
  _initialize
end

Public Instance Methods

clear!() click to toggle source
# File lib/honeybadger/context_manager.rb, line 17
def clear!
  _initialize
end
get_context() click to toggle source
# File lib/honeybadger/context_manager.rb, line 48
def get_context
  @mutex.synchronize do
    return @global_context unless @local_context

    @global_context.merge(@local_context.inject({}, :merge))
  end
end
get_rack_env() click to toggle source
# File lib/honeybadger/context_manager.rb, line 60
def get_rack_env
  @mutex.synchronize { @rack_env }
end
set_context(hash) { || ... } click to toggle source

Internal helpers

# File lib/honeybadger/context_manager.rb, line 24
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
set_rack_env(env) click to toggle source
# File lib/honeybadger/context_manager.rb, line 56
def set_rack_env(env)
  @mutex.synchronize { @rack_env = env }
end

Private Instance Methods

_initialize() click to toggle source
# File lib/honeybadger/context_manager.rb, line 68
def _initialize
  @mutex.synchronize do
    @global_context = nil
    @local_context = nil
    @rack_env = nil
  end
end