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.



52
53
54
# File 'lib/bitfab/span_context.rb', line 52

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



95
96
97
98
99
100
101
102
103
# File 'lib/bitfab/span_context.rb', line 95

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

#dropObject

Flag this trace to be dropped. Once flagged, spans that complete afterward are not uploaded at all, and the completion POST carries a top-level dropped: true so the server scrubs any payloads that already raced out and marks the trace dropped. Never raises into caller code.



109
110
111
112
113
114
# File 'lib/bitfab/span_context.rb', line 109

def drop
  trace_state = get_or_create_trace_state
  trace_state[:dropped] = true
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



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

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_name(name) ⇒ Object



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

def set_name(name)
  return unless name.is_a?(String) && !name.empty?

  get_or_create_trace_state[:name] = name
rescue
  nil
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



61
62
63
64
65
66
# File 'lib/bitfab/span_context.rb', line 61

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