Module: Bitfab::ReplayContext

Defined in:
lib/bitfab/replay.rb

Overview

Thread-local replay context management.

Class Method Summary collapse

Class Method Details

.currentObject



27
28
29
# File 'lib/bitfab/replay.rb', line 27

def current
  Thread.current[REPLAY_CONTEXT_KEY]
end

.with_context(test_run_id:, input_source_span_id: nil, input_source_trace_id: nil, trace_id: nil, mock_tree: nil, mock_strategy: nil, mock_overrides: nil, fetch_span_output: nil, db_branch_lease: nil, source_bitfab_trace_id: nil) ⇒ Object

Execute a block with replay context set on the current thread. The context is automatically cleared when the block completes.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bitfab/replay.rb', line 33

def with_context(test_run_id:, input_source_span_id: nil, input_source_trace_id: nil, trace_id: nil,
  mock_tree: nil, mock_strategy: nil, mock_overrides: nil, fetch_span_output: nil,
  db_branch_lease: nil, source_bitfab_trace_id: nil)
  previous = Thread.current[REPLAY_CONTEXT_KEY]
  ctx = {
    test_run_id:,
    input_source_span_id:,
    input_source_trace_id:,
    trace_id:
  }
  if mock_tree
    ctx[:mock_tree] = mock_tree
    ctx[:mock_strategy] = mock_strategy || "marked"
    ctx[:call_counters] = {}
    # Selective mock overrides ride alongside the tree: they gate on the
    # same non-root call-counter machinery, so they only fire when a tree
    # is present. process_single_item fetches the tree whenever overrides
    # exist, even under mock: "none".
    ctx[:mock_overrides] = mock_overrides if mock_overrides && !mock_overrides.empty?
    # Lazy per-span output fetcher for the payload-free ("marked"/override)
    # path. Absent on the eager "all" path, whose outputs are inline.
    ctx[:fetch_span_output] = fetch_span_output if fetch_span_output
  end
  # The per-trace DB branch (resolved server-side) and the Bitfab trace ID
  # it belongs to ride on the context so ReplayBranch can read them inside
  # the replayed method.
  #
  # ReplayBranch also sets ctx[:db_snapshot_accessed] = true the first
  # time customer code actually obtains the branch URL for this item
  # (via +database_url+). Reported on the trace
  # completion inside the +db_snapshot_usage+ record (its +accessed+
  # field) so the server can distinguish "branch was provisioned and
  # exposed" from "branch URL was actually consumed". Any future
  # consumption path that hands the URL to customer code by other means
  # (e.g. a process-isolated runner writing an env overlay) must also
  # set this.
  ctx[:db_branch_lease] = db_branch_lease if db_branch_lease
  ctx[:source_bitfab_trace_id] = source_bitfab_trace_id if source_bitfab_trace_id
  Thread.current[REPLAY_CONTEXT_KEY] = ctx
  yield
ensure
  Thread.current[REPLAY_CONTEXT_KEY] = previous
end