bitfab. Ruby ≥ 3.1. Uses a global singleton client pattern (Bitfab.configure / Bitfab.client).
Framework Integrations
No framework-native adapters are shipped for Ruby yet. Instrument Ruby code manually viaBitfab::Traceable / Bitfab.span. See the Ruby SDK guide. Framework coverage status: Frameworks overview.
Module Layout
Module-Level API
Bitfab.configure(api_key:, service_url: nil, enabled: true)
Creates the global client. Subsequent calls replace it.
Empty/whitespace
api_key prints a warning and sets enabled = false.
Bitfab.client
Returns the configured Bitfab::Client. Raises RuntimeError("Bitfab not configured...") if configure has not been called.
Bitfab.reset!
Resets the global client to nil. Intended for tests.
Bitfab.current_span
Returns the active CurrentSpan, or Bitfab::NO_OP_SPAN outside a span context.
Bitfab.current_trace
Returns the active CurrentTrace, or Bitfab::NO_OP_TRACE outside a span context.
Bitfab.report_replay_progress(progress)
Ready-made on_progress-shaped callback. Pass it straight into replay as on_progress: Bitfab.method(:report_replay_progress). It writes the running totals to stderr (prefixed with Bitfab::BITFAB_PROGRESS_PREFIX), which the Bitfab plugin polls to report live progress while the replay runs in the background, while stdout remains available for direct-run ReplayResult JSON. It never raises.
class Bitfab::Client
SPAN_TYPES
attr_reader :api_key, :service_url, :enabled
get_trace_span
trace_id is the canonical Bitfab trace ID. Exactly one of the span’s Bitfab id or name is required. occurrence accepts "first", "last", or a zero-based integer and defaults to "last". Returns nil when no trace or span matches.
initialize(api_key:, service_url: nil, enabled: true)
Same semantics as Bitfab.configure.
replay(receiver, method_name, trace_function_key:, limit: nil, trace_ids: nil, name: nil, max_concurrency: 10, code_change_description: nil, code_change_files: nil, mock: "marked", experiment_group_id: nil, dataset_id: nil, grader_ids: nil, environment: nil, on_progress: nil)
Returns:
Hash with keys :items, :test_run_id, :test_run_url.
Each item has :input, :result, :original_output, :error, :duration_ms (Integer, nil), :tokens ({ input:, output:, cached:, total: } or nil), :model (String, nil), :trace_id (String, nil), :original_trace_id (String, nil), :original_span_id (String, nil), the deprecated aliases :source_trace_id and :source_span_id, and :db_snapshot_ref (Hash, nil, the source trace’s snapshot pin, if any). :duration_ms and :model come from the historical trace the item replays; :tokens is the replayed run’s token usage (compare it against the original trace’s recorded usage); :trace_id is the new replay trace’s server id, written in after the run completes (nil on older servers); :original_trace_id and :original_span_id identify the original trace and root span being replayed.
Mock strategies:
"marked": only child spans declared withmock_on_replay: truereturn historical output. Falls through to real execution when no historical entry matches. Default."none": every child span runs real code."all": every child span returns its historical output; real code is not executed.
trace_function_key are distinguished by call order (step:0, step:1, …). The per-key counter advances on every non-root child invocation (including unmarked ones), so a marked sibling following an unmarked sibling lines up with the right historical entry.
get_function(trace_function_key)
Returns a Bitfab::BitfabFunction bound to trace_function_key. Mirrors client.get_function in the Python SDK and client.getFunction in TypeScript.
execute_span(...) (internal)
Called by Traceable. Not intended for direct use.
class Bitfab::BitfabFunction
Fluent wrapper bound to a single trace_function_key, returned by Bitfab::Client#get_function.
attr_reader :trace_function_key
wrap(klass, method_name, name: nil, type: "custom", mock_on_replay: false)
Wraps method_name on klass with span tracing under the bound trace_function_key. Delegates to Bitfab::Traceable.wrap.
module Bitfab::Traceable
Mixin for declarative instance-method tracing.
include Bitfab::Traceable
Extends the including class with ClassMethods below.
Class Methods (after include)
bitfab_function(key)
Sets the default trace_function_key for all bitfab_span declarations in this class.
bitfab_span(method_name, trace_function_key: nil, name: nil, type: "custom", mock_on_replay: false)
Wraps method_name with span tracing.
Supports three call styles:
- Before
def: registered viamethod_addedhook, wrapped when method is defined - Inline:
bitfab_span def foo ... end, type: "function"(Ruby’sdefreturns:foo) - After
def: wraps immediately if the method already exists
RuntimeError if no trace_function_key is provided and no class-level bitfab_function was set.
Module-Level Wrapping
Bitfab::Traceable.wrap(klass, method_name, trace_function_key:, name: nil, type: "custom", mock_on_replay: false)
Wraps a method on a class you do not own. Uses Module#prepend.
class Bitfab::CurrentSpan
class Bitfab::CurrentTrace
drop flags the trace to be dropped: once flagged, spans that complete afterward are not uploaded at all, and the flag rides out on the completion payload, so at completion the server scrubs any payloads that already raced out (the trace, its external trace, and sibling spans), deletes the archived S3 objects, and marks it dropped instead of completed, keeping only a skeleton audit row. Calling it outside a span (on NO_OP_TRACE) is a no-op.
class Bitfab::NoOpCurrentSpan / NoOpCurrentTrace
Singleton instances exposed as Bitfab::NO_OP_SPAN and Bitfab::NO_OP_TRACE. All methods are no-ops. id and trace_id return "".
Thread & Concurrency Model
- Span stack is stored in
Thread.current[:__bitfab_span_stack](not propagated to child threads automatically) - Trace state is stored in a mutex-protected module-level hash (
Bitfab::TraceState) keyed bytrace_id - Span HTTP sends run in background threads; the client tracks pending threads per trace