Module: Bitfab

Defined in:
lib/bitfab.rb,
lib/bitfab/client.rb,
lib/bitfab/replay.rb,
lib/bitfab/version.rb,
lib/bitfab/constants.rb,
lib/bitfab/serialize.rb,
lib/bitfab/traceable.rb,
lib/bitfab/http_client.rb,
lib/bitfab/span_context.rb

Defined Under Namespace

Modules: Replay, ReplayContext, Serialize, SpanContext, TraceState, Traceable Classes: Client, CurrentSpan, CurrentTrace, HttpClient, NoOpCurrentSpan, NoOpCurrentTrace

Constant Summary collapse

NO_OP_SPAN =
NoOpCurrentSpan.new.freeze
NO_OP_TRACE =
NoOpCurrentTrace.new.freeze
VERSION =
"0.10.4"
DEFAULT_SERVICE_URL =
"https://bitfab.ai"
REPLAY_CONTEXT_KEY =
:__bitfab_replay_context

Class Method Summary collapse

Class Method Details

._run_in_background(&block) ⇒ Object

Run a block in a background thread with tracking. Returns the thread for callers that need to join on it.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/bitfab/http_client.rb', line 148

def _run_in_background(&block)
  thread = Thread.new do
    block.call
  rescue => e
    begin
      warn "Bitfab: Failed to send request: #{e.message}"
    rescue
      # Never crash the host app
    end
  ensure
    @pending_threads_mutex.synchronize { @pending_threads.delete(Thread.current) }
  end

  @pending_threads_mutex.synchronize { @pending_threads << thread }
  thread
end

.clientObject

Returns the global client, raising if not configured.



62
63
64
# File 'lib/bitfab.rb', line 62

def client
  @client or raise "Bitfab not configured. Call Bitfab.configure(api_key: '...') first."
end

.configure(api_key:, service_url: nil, enabled: true) ⇒ Object

Configure the global Bitfab client.

Examples:

Bitfab.configure(api_key: ENV["BITFAB_API_KEY"])

Parameters:

  • api_key (String)

    API key for authentication

  • service_url (String, nil) (defaults to: nil)

    base URL (default: bitfab.ai)



57
58
59
# File 'lib/bitfab.rb', line 57

def configure(api_key:, service_url: nil, enabled: true)
  @client = Client.new(api_key:, service_url:, enabled:)
end

.current_spanCurrentSpan, NoOpCurrentSpan

Get a handle to the current active span.

Call this from inside a traced method to get a span handle that allows setting metadata at runtime.

Returns:



77
78
79
80
81
82
# File 'lib/bitfab.rb', line 77

def current_span
  entry = SpanContext.current
  return NO_OP_SPAN unless entry

  CurrentSpan.new(entry)
end

.current_traceCurrentTrace, NoOpCurrentTrace

Get a handle to the current active trace.

Call this from inside a traced method to get a trace handle that allows setting trace-level context at runtime.

Returns:



90
91
92
93
94
95
# File 'lib/bitfab.rb', line 90

def current_trace
  entry = SpanContext.current
  return NO_OP_TRACE unless entry

  CurrentTrace.new(entry[:trace_id])
end

.flush_traces(timeout: 30) ⇒ Object

Wait for all pending background threads to complete.



166
167
168
169
# File 'lib/bitfab/http_client.rb', line 166

def flush_traces(timeout: 30)
  threads = @pending_threads_mutex.synchronize { @pending_threads.dup }
  threads.each { |t| t.join(timeout) }
end

.reset!Object

Reset the global client (primarily for testing).



67
68
69
# File 'lib/bitfab.rb', line 67

def reset!
  @client = nil
end