Module: Bitfab::Replay
- Defined in:
- lib/bitfab/replay.rb
Overview
Replay historical traces through a traced method and create a test run.
Class Method Summary collapse
-
.execute_item(item, receiver, method_name, test_run_id, input_source_span_id = nil, metrics = {}, input_source_trace_id: nil) ⇒ Object
Execute a single replay item: deserialize inputs, call method with replay context.
-
.extract_server_item_metrics(server_item) ⇒ Object
Pull durationMs / tokens / model from the start-replay server item.
-
.extract_span_data(span) ⇒ Object
Extract input/output data from an external span’s rawData.
-
.process_items(http_client, server_items, receiver, method_name, test_run_id, max_concurrency) ⇒ Object
Process all replay items, optionally in parallel using threads.
-
.process_single_item(http_client, server_item, receiver, method_name, test_run_id) ⇒ Object
Fetch span data and execute a single replay item.
-
.run(client, receiver, method_name, trace_function_key:, limit: 5, trace_ids: nil, max_concurrency: 10, code_change_description: nil, code_change_files: nil) ⇒ Hash
Replay historical traces through a method and create a test run.
Class Method Details
.execute_item(item, receiver, method_name, test_run_id, input_source_span_id = nil, metrics = {}, input_source_trace_id: nil) ⇒ Object
Execute a single replay item: deserialize inputs, call method with replay context.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/bitfab/replay.rb', line 167 def execute_item(item, receiver, method_name, test_run_id, input_source_span_id = nil, metrics = {}, input_source_trace_id: nil) args, kwargs = Serialize.deserialize_inputs(item) fn_result = nil fn_error = nil ReplayContext.with_context(test_run_id:, input_source_span_id:, input_source_trace_id:) do fn_result = if kwargs.empty? receiver.send(method_name, *args) else receiver.send(method_name, *args, **kwargs) end rescue => e fn_error = e. end { input: args, result: fn_result, original_output: item["output"], error: fn_error, duration_ms: metrics[:duration_ms], tokens: metrics[:tokens], model: metrics[:model] } end |
.extract_server_item_metrics(server_item) ⇒ Object
Pull durationMs / tokens / model from the start-replay server item. Normalizes to symbol-keyed tokens hash and nil-safe defaults so older servers without these fields still produce a consistent shape.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/bitfab/replay.rb', line 148 def extract_server_item_metrics(server_item) raw_tokens = server_item["tokens"] tokens = if raw_tokens.is_a?(Hash) { input: raw_tokens["input"], output: raw_tokens["output"], cached: raw_tokens["cached"], total: raw_tokens["total"] } end { duration_ms: server_item["durationMs"], tokens:, model: server_item["model"] } end |
.extract_span_data(span) ⇒ Object
Extract input/output data from an external span’s rawData.
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/bitfab/replay.rb', line 133 def extract_span_data(span) raw_data = span["rawData"] || {} span_data = raw_data["span_data"] || {} { "input" => span_data["input"], "output" => span_data["output"], "inputSerialized" => span_data["input_serialized"], "outputSerialized" => span_data["output_serialized"] } end |
.process_items(http_client, server_items, receiver, method_name, test_run_id, max_concurrency) ⇒ Object
Process all replay items, optionally in parallel using threads.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/bitfab/replay.rb', line 88 def process_items(http_client, server_items, receiver, method_name, test_run_id, max_concurrency) concurrency = max_concurrency || server_items.length if concurrency <= 1 server_items.map { |item| process_single_item(http_client, item, receiver, method_name, test_run_id) } else results_mutex = Mutex.new results = [] work_queue = server_items.each_with_index.to_a work_mutex = Mutex.new workers = [concurrency, server_items.length].min.times.map do Thread.new do loop do item, idx = work_mutex.synchronize { work_queue.shift } break unless item result = process_single_item(http_client, item, receiver, method_name, test_run_id) results_mutex.synchronize { results[idx] = result } end end end workers.each(&:join) results.compact end end |
.process_single_item(http_client, server_item, receiver, method_name, test_run_id) ⇒ Object
Fetch span data and execute a single replay item.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/bitfab/replay.rb', line 117 def process_single_item(http_client, server_item, receiver, method_name, test_run_id) span = http_client.get_external_span(server_item["externalSpanId"]) item_data = extract_span_data(span) metrics = extract_server_item_metrics(server_item) execute_item( item_data, receiver, method_name, test_run_id, span["id"], metrics, input_source_trace_id: span["externalTraceId"] ) end |
.run(client, receiver, method_name, trace_function_key:, limit: 5, trace_ids: nil, max_concurrency: 10, code_change_description: nil, code_change_files: nil) ⇒ Hash
Replay historical traces through a method and create a test run.
Fetches the last N traces for the given trace function key, re-runs each through the provided receiver and method, and returns comparison data.
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 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/bitfab/replay.rb', line 51 def run(client, receiver, method_name, trace_function_key:, limit: 5, trace_ids: nil, max_concurrency: 10, code_change_description: nil, code_change_files: nil) http_client = client.instance_variable_get(:@http_client) replay_data = http_client.start_replay( trace_function_key, limit, trace_ids:, code_change_description:, code_change_files: ) test_run_id = replay_data["testRunId"] test_run_url = replay_data["testRunUrl"] server_items = replay_data["items"] || [] result_items = if server_items.any? process_items(http_client, server_items, receiver, method_name, test_run_id, max_concurrency) else [] end Bitfab.flush_traces begin http_client.complete_replay(test_run_id) rescue => e warn "Bitfab: Failed to complete replay: #{e.}" end { items: result_items, test_run_id:, test_run_url: "#{client.service_url}#{test_run_url}" } end |