Class: Bitfab::CurrentTrace

Inherits:
Object
  • Object
show all
Defined in:
lib/bitfab/span_context.rb

Overview

Handle to the current active trace, allowing trace-level context to be set.

Instance Method Summary collapse

Constructor Details

#initialize(trace_id) ⇒ CurrentTrace

Returns a new instance of CurrentTrace.



45
46
47
# File 'lib/bitfab/span_context.rb', line 45

def initialize(trace_id)
  @trace_id = trace_id
end

Instance Method Details

#add_context(context) ⇒ Object

Add a context entry to this trace. The entire hash is pushed as a single entry in the contexts array. Context entries are accumulated - multiple calls add to the list.

Parameters:

  • context (Hash)

    key-value pairs to add as a single context entry



80
81
82
83
84
85
86
87
88
# File 'lib/bitfab/span_context.rb', line 80

def add_context(context)
  return unless context.is_a?(Hash)

  trace_state = get_or_create_trace_state
  trace_state[:contexts] ||= []
  trace_state[:contexts] << context
rescue
  # Silently ignore - never crash the host app
end

#set_metadata(metadata) ⇒ Object

Set metadata for this trace. Metadata is stored in the raw trace data. Subsequent calls merge with existing metadata, with later values taking precedence.

Parameters:

  • metadata (Hash)

    key-value pairs to store as trace metadata



66
67
68
69
70
71
72
73
# File 'lib/bitfab/span_context.rb', line 66

def ()
  return unless .is_a?(Hash)

  trace_state = get_or_create_trace_state
  trace_state[:metadata] = (trace_state[:metadata] || {}).merge()
rescue
  # Silently ignore - never crash the host app
end

#set_session_id(session_id) ⇒ Object

Set the session ID for this trace. Session ID is used to group traces from the same user session. This is stored as a database column.

Parameters:

  • session_id (String)

    the session ID to set



54
55
56
57
58
59
# File 'lib/bitfab/span_context.rb', line 54

def set_session_id(session_id)
  trace_state = get_or_create_trace_state
  trace_state[:session_id] = session_id
rescue
  # Silently ignore - never crash the host app
end