Class: TraceId

Inherits:
Object
  • Object
show all
Defined in:
lib/trace_id.rb,
lib/trace_id/method.rb,
lib/trace_id/version.rb,
lib/trace_id/from_request.rb,
lib/trace_id/sidekiq_client_middleware.rb,
lib/trace_id/sidekiq_server_middleware.rb

Defined Under Namespace

Modules: Method Classes: FromRequest, SidekiqClientMiddleware, SidekiqServerMiddleware

Constant Summary collapse

TRACE_ID =
"__trace_id"
VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.getObject

Get the current trace id, or nil if one has not been set. You are more likely to want get_or_initialize, which will initialize one for the rest of the thread if one hasn’t been set.



15
16
17
# File 'lib/trace_id.rb', line 15

def self.get
  Thread.current.thread_variable_get(TRACE_ID)
end

.get_or_initializeObject

Get the current trace id, initializing it if there isn’t one set.



31
32
33
34
35
36
37
38
# File 'lib/trace_id.rb', line 31

def self.get_or_initialize
  trace_id = self.get
  if trace_id.to_s.strip.length == 0
    trace_id = self.new_trace_id_value
    self.set(trace_id)
  end
  self.get
end

.reset!(&block) ⇒ Object

Resets the current traced. The block, if given, is called with the old trace id if you want to log it before it becomes irrelevant.



22
23
24
25
26
27
28
# File 'lib/trace_id.rb', line 22

def self.reset!(&block)
  original = self.get
  self.set(self.new_trace_id_value)
  if !block.nil?
    block.(original)
  end
end

.set(new_trace_id) ⇒ Object

Set the new trace id. Note, you might want reset! if you simply want a new value. This method is useful only when you have externalized a trace id you wish to restore, such us in a sidekiq job



43
44
45
# File 'lib/trace_id.rb', line 43

def self.set(new_trace_id)
  Thread.current.thread_variable_set(TRACE_ID, new_trace_id)
end