Class: Contrast::Utils::ThreadTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/contrast/utils/thread_tracker.rb

Overview

ThreadTracker allows tracking of singleton objects across threads. It acts on Thread#[] as that call is fiber-local where as Thread#thread_variables is not.

Instance Method Summary collapse

Instance Method Details

#currentContrast::Agent::RequestContext

Retrieve the Thread#[] RequestContext



46
47
48
# File 'lib/contrast/utils/thread_tracker.rb', line 46

def current
  get(:current_context)
end

#delete(key) ⇒ Object

Remove the given key from the current Thread#[] by setting it to nil.

Parameters:

  • key (Object)

    key of the value to delete



29
30
31
# File 'lib/contrast/utils/thread_tracker.rb', line 29

def delete key
  Thread.current[key] = nil
end

#get(key, default = nil) ⇒ Object

Get the given key to given value in Thread#[] or return default.

Parameters:

  • key (Object)

    key used to reference the value

  • default (Object) (defaults to: nil)

    value to return if not present in Thread#[]

Returns:



14
15
16
# File 'lib/contrast/utils/thread_tracker.rb', line 14

def get key, default = nil
  Thread.current[key] || default
end

#lifespan(context) ⇒ Object

Wrap the block given with a RequestContext by setting it beforehand and deleting it after.

Parameters:



36
37
38
39
40
41
# File 'lib/contrast/utils/thread_tracker.rb', line 36

def lifespan context
  set(:current_context, context)
  response = yield(context)
  delete(:current_context)
  response
end

#set(key, value) ⇒ Object

Set the given key to given value in Thread#[].

Parameters:

  • key (Object)

    key used to reference the value

  • value (Object)

    value to store



22
23
24
# File 'lib/contrast/utils/thread_tracker.rb', line 22

def set key, value
  Thread.current[key] = value
end

#update_current_context(context) ⇒ Object

Set the Thread#[] context to the one provided.

Parameters:



53
54
55
# File 'lib/contrast/utils/thread_tracker.rb', line 53

def update_current_context context
  set(:current_context, context)
end