bitfab 0.78.7 → 0.78.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +117 -120
- data/lib/bitfab/client.rb +17 -5
- data/lib/bitfab/subtree.rb +1 -1
- data/lib/bitfab/traceable.rb +36 -9
- data/lib/bitfab/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8f8c68d73bbd3106dda8a73869bf47be8352aaea7173b7d76db8263244972995
|
|
4
|
+
data.tar.gz: f798f2990f652c06cc2f1ce56c4d07dadfbe48be90fc6803bd59e9e7e723dd4d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: afba98886a92a4933c6d677c78d83f7757d4f685f8e246d7e7417ccb6501d0e685da8b8eac024ccadbda1e776cf0cb2260d0ec1bb0c29c88a065c4fae8973875
|
|
7
|
+
data.tar.gz: d1f8feb7aaaa0380436b1210da0a91589c7702b56be757a086bdf09dd7cc27c87dd098426aa7f32dec3d3a484dda911b95f74cb37a92a7efa784d26eab08156d
|
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Bitfab Ruby SDK
|
|
2
2
|
|
|
3
|
-
Ruby client library for [Bitfab](https://bitfab.ai) - trace and monitor your Ruby application's function execution
|
|
3
|
+
Ruby client library for [Bitfab](https://bitfab.ai) - trace and monitor your Ruby application's function execution. Declare `bitfab_trace` on the root of a workflow and every first-party Ruby method it calls is recorded beneath it.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -31,19 +31,23 @@ Bitfab.configure(
|
|
|
31
31
|
api_key: ENV.fetch('BITFAB_API_KEY')
|
|
32
32
|
)
|
|
33
33
|
|
|
34
|
-
#
|
|
34
|
+
# Trace the root of a workflow; every first-party method it calls is recorded beneath it
|
|
35
35
|
class OrderService
|
|
36
36
|
include Bitfab::Traceable
|
|
37
37
|
bitfab_function "order-processing"
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
bitfab_trace :process_order, type: "agent"
|
|
40
40
|
def process_order(order_id)
|
|
41
|
-
|
|
41
|
+
validate(order_id)
|
|
42
42
|
{ status: "completed" }
|
|
43
43
|
end
|
|
44
|
+
|
|
45
|
+
def validate(order_id)
|
|
46
|
+
{ order_id:, valid: true }
|
|
47
|
+
end
|
|
44
48
|
end
|
|
45
49
|
|
|
46
|
-
# Use your code normally
|
|
50
|
+
# Use your code normally: spans are sent automatically
|
|
47
51
|
service = OrderService.new
|
|
48
52
|
service.process_order("order-123")
|
|
49
53
|
|
|
@@ -113,47 +117,51 @@ When `enabled: false`:
|
|
|
113
117
|
|
|
114
118
|
## Usage
|
|
115
119
|
|
|
116
|
-
###
|
|
120
|
+
### Tracing a Workflow
|
|
117
121
|
|
|
118
|
-
|
|
122
|
+
`bitfab_trace` is the recommended way to instrument. Declare it on the root method of a workflow and it records the root and every first-party Ruby method the root calls, without declaring each descendant. `bitfab_function` sets the trace function key for the class:
|
|
119
123
|
|
|
120
124
|
```ruby
|
|
121
|
-
class
|
|
125
|
+
class OrderPipeline
|
|
122
126
|
include Bitfab::Traceable
|
|
123
|
-
bitfab_function "
|
|
127
|
+
bitfab_function "order-pipeline"
|
|
124
128
|
|
|
125
|
-
|
|
126
|
-
def
|
|
127
|
-
|
|
129
|
+
bitfab_trace :process, type: "agent"
|
|
130
|
+
def process(order_id)
|
|
131
|
+
validate(order_id)
|
|
128
132
|
end
|
|
129
133
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
# Also uses "payment-processing" key
|
|
134
|
+
def validate(order_id)
|
|
135
|
+
{ order_id:, valid: true }
|
|
133
136
|
end
|
|
134
137
|
end
|
|
135
138
|
```
|
|
136
139
|
|
|
137
|
-
|
|
140
|
+
Capture uses `TracePoint` only during the root call. It skips Ruby/runtime code, dependency gems, Bitfab SDK internals, blocks, lambdas, and forwarding wrappers. When the root belongs to an installed application gem, that gem is the first-party boundary; neighboring dependency gems remain excluded. It follows recursion, modules, inheritance, `define_method`, and same-thread child Fibers; aliases are recorded and excluded by the name that was invoked. Child Fibers created during capture inherit node policies, trace identity, parenting, and depth limits while retaining separate call stacks. Pre-existing Fibers, `Fiber.new(storage: nil)`, and child threads do not inherit subtree context. When an inner capture ends, child Fibers retain any still-active outer capture; inherited context expires once all enclosing captures end. While a root remains active, hitting a span limit stops new capture but allows already-open spans in every Fiber to finish. `max_depth:` (default `30`), `max_captured_subtree_spans:` (default `500`), `exclude:`, and `include_wrappers:` control the subtree. There is no cap on the total number of spans one trace root sends. `max_spans:` was removed, and passing it raises `ArgumentError`. `max_captured_subtree_spans:` caps discovered calls sent with their inputs and outputs. It does not count `bitfab_node` calls, nested trace roots, or spans the loaded sim plan turned capture off for, so those keep recording past it. Spans sent without inputs and outputs only because the sim plan could not be read still count toward it. Past it, further discovered calls with capture on record nothing. Once the plan has loaded, the SDK does not send a span whose capture it turned off, and that span's children move up to the nearest visible parent. The trace's root span, a framework integration span, a span that recorded an error, and a span that recorded LLM usage (token counts or a cost in its contexts) are still sent, and the SDK never builds their inputs or outputs. A `bitfab_node` call and a nested trace root ignore the plan and always keep their inputs and outputs. The client starts reading the sim plan when it is built. A `bitfab_trace` root that starts while that first read is still running waits for it, for at most five seconds. Once the first read has finished, whether it succeeded or failed, no later root waits. Nested roots and the calls beneath a root never wait, and nothing waits when the plan is disabled. If that first read fails, spans other than each trace's root span, `bitfab_node` calls, and nested trace roots are sent without inputs and outputs until a read succeeds, and they still count toward `max_captured_subtree_spans:`. Automatically captured calls are not replay mock targets. Declare a call with `bitfab_node` when replay must substitute it. The configured node records once in each enclosing trace. Mixing explicit spans with subtree tracing raises `Bitfab::MixedTracingError`.
|
|
138
141
|
|
|
139
|
-
|
|
142
|
+
When capture ends, already-open automatic calls in suspended Fibers are emitted once with a nil output and `Subtree capture ended before the call returned` as the error, unless a real error was already observed. Their inputs and parenting are retained. The SDK does not resume Fibers, and later resumption cannot add spans to the ended capture. If hot reload moves the root to an unresolved source, the last valid first-party boundary and its exclusions remain in use.
|
|
140
143
|
|
|
141
|
-
|
|
142
|
-
class NotificationService
|
|
143
|
-
include Bitfab::Traceable
|
|
144
|
+
Returning an `Enumerator` keeps capture pending until iteration finishes or raises, including calls already paused in child Fibers. Capture is inactive between the initial return and consumption. Automatic spans record lazy return values as placeholders without iterating them; the traced root records the yielded values.
|
|
144
145
|
|
|
145
|
-
|
|
146
|
-
def send_email(to, subject)
|
|
147
|
-
# Uses "email-notifications" key
|
|
148
|
-
end
|
|
146
|
+
Each nested `bitfab_trace` starts an independent trace during ordinary capture, including recursion and calls using the same key. Every enclosing trace retains its own recording of the shared subtree. The enclosing span and nested root carry reciprocal trace and span links. Each trace applies its own limits and exclusions. During replay and seed execution, nested annotations remain in the managed trace.
|
|
149
147
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
148
|
+
Configure a discovered method with `bitfab_node`. A node never creates a span or trace by itself; the active `bitfab_trace` owns its key, limits, parent, and lifecycle:
|
|
149
|
+
|
|
150
|
+
```ruby
|
|
151
|
+
bitfab_node :summarize,
|
|
152
|
+
name: "Model call",
|
|
153
|
+
type: "llm",
|
|
154
|
+
mock_on_replay: true,
|
|
155
|
+
finalize: ->(response) { {text: response.text} }
|
|
156
|
+
def summarize(order)
|
|
157
|
+
model.generate(order)
|
|
154
158
|
end
|
|
155
159
|
```
|
|
156
160
|
|
|
161
|
+
Nodes support `name:`, `type:`, `capture:`, `experiment_id:`, `mock_on_replay:`, and `finalize:`. `capture: false` omits the method while attaching captured descendants to its nearest visible parent. `finalize:` changes only the recorded output, never the return value. A finalizer failure is recorded on the node without crashing the caller. The before-`def`, inline, and after-`def` forms all preserve method visibility. For external code, use `Bitfab.client.node(MyClass, :method)`.
|
|
162
|
+
|
|
163
|
+
`bitfab_trace` prepares its project boundary and exclusions when the method is declared, refreshes the boundary if the root definition moves during hot reload, caches canonical source paths, and shares one `TracePoint` dispatcher across active roots on the calling thread. After a span limit is reached, it stops reading inputs and outputs immediately, then stops processing events for that session once its captured calls finish. Each active root still captures and serializes its own copy of every overlapping call. Use `exclude:` to leave a hot helper out of the subtree. Run `mise exec -- ruby -Ilib benchmark/subtree.rb` from this package to measure the no-network caller path on your Ruby build.
|
|
164
|
+
|
|
157
165
|
### Span Types
|
|
158
166
|
|
|
159
167
|
Bitfab supports the following span types:
|
|
@@ -166,23 +174,68 @@ Bitfab supports the following span types:
|
|
|
166
174
|
- `"custom"` - Custom span types (default)
|
|
167
175
|
|
|
168
176
|
```ruby
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
177
|
+
bitfab_trace :agent_loop, type: "agent"
|
|
178
|
+
bitfab_node :validate_input, type: "guardrail"
|
|
179
|
+
bitfab_node :call_openai, type: "llm"
|
|
172
180
|
```
|
|
173
181
|
|
|
174
182
|
### Custom Span Names
|
|
175
183
|
|
|
176
|
-
By default,
|
|
184
|
+
By default, a span is named after its class and method (`OrderService.process_order`). Override it on the root or on a node:
|
|
177
185
|
|
|
178
186
|
```ruby
|
|
179
|
-
|
|
187
|
+
bitfab_trace :process_order, name: "ProcessOrderV2", type: "agent"
|
|
180
188
|
def process_order(order_id)
|
|
181
189
|
# Span will be named "ProcessOrderV2"
|
|
182
190
|
end
|
|
183
191
|
```
|
|
184
192
|
|
|
185
|
-
###
|
|
193
|
+
### Opt-in Spans (Deprecated)
|
|
194
|
+
|
|
195
|
+
`bitfab_span`, `Bitfab::Traceable.wrap`, and `BitfabFunction#wrap` record only the methods you declare, one span each. They are deprecated in favor of `bitfab_trace`, with `bitfab_node` for a call that needs its own name, type, or replay policy. They keep working and nothing warns at runtime, but they cannot be mixed with `bitfab_trace` in one call stack: doing so raises `Bitfab::MixedTracingError`. `bitfab_trace` records only calls made on the thread its root runs on, so a workflow whose calls run on threads it starts is recorded with spans and `trace_across_threads: true` on the client.
|
|
196
|
+
|
|
197
|
+
#### Class-Level Trace Function Key
|
|
198
|
+
|
|
199
|
+
All spans in the class share the same trace function key:
|
|
200
|
+
|
|
201
|
+
```ruby
|
|
202
|
+
class PaymentService
|
|
203
|
+
include Bitfab::Traceable
|
|
204
|
+
bitfab_function "payment-processing"
|
|
205
|
+
|
|
206
|
+
bitfab_span :charge_card, type: "function"
|
|
207
|
+
def charge_card(amount)
|
|
208
|
+
# Traced automatically
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
bitfab_span :refund, type: "function"
|
|
212
|
+
def refund(transaction_id)
|
|
213
|
+
# Also uses "payment-processing" key
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
#### Per-Span Trace Function Key
|
|
219
|
+
|
|
220
|
+
Each span can declare its own key:
|
|
221
|
+
|
|
222
|
+
```ruby
|
|
223
|
+
class NotificationService
|
|
224
|
+
include Bitfab::Traceable
|
|
225
|
+
|
|
226
|
+
bitfab_span :send_email, trace_function_key: "email-notifications", type: "function"
|
|
227
|
+
def send_email(to, subject)
|
|
228
|
+
# Uses "email-notifications" key
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
bitfab_span :send_sms, trace_function_key: "sms-notifications", type: "function"
|
|
232
|
+
def send_sms(to, message)
|
|
233
|
+
# Uses "sms-notifications" key
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
#### Nested Spans
|
|
186
239
|
|
|
187
240
|
Spans automatically track parent-child relationships:
|
|
188
241
|
|
|
@@ -215,57 +268,12 @@ end
|
|
|
215
268
|
# └─ check_fraud (grandchild)
|
|
216
269
|
```
|
|
217
270
|
|
|
218
|
-
### Experimental Subtree Tracing
|
|
219
|
-
|
|
220
|
-
Use `bitfab_trace` to record a root method and every first-party Ruby method it calls without wrapping each descendant:
|
|
221
|
-
|
|
222
|
-
```ruby
|
|
223
|
-
class OrderPipeline
|
|
224
|
-
include Bitfab::Traceable
|
|
225
|
-
bitfab_function "order-pipeline"
|
|
226
|
-
|
|
227
|
-
bitfab_trace :process, type: "agent"
|
|
228
|
-
def process(order_id)
|
|
229
|
-
validate(order_id)
|
|
230
|
-
end
|
|
231
|
-
|
|
232
|
-
def validate(order_id)
|
|
233
|
-
{ order_id:, valid: true }
|
|
234
|
-
end
|
|
235
|
-
end
|
|
236
|
-
```
|
|
237
|
-
|
|
238
|
-
Capture uses `TracePoint` only during the root call. It skips Ruby/runtime code, dependency gems, Bitfab SDK internals, blocks, lambdas, and forwarding wrappers. When the root belongs to an installed application gem, that gem is the first-party boundary; neighboring dependency gems remain excluded. It follows recursion, modules, inheritance, `define_method`, and same-thread child Fibers; aliases are recorded and excluded by the name that was invoked. Child Fibers created during capture inherit node policies, trace identity, parenting, and depth limits while retaining separate call stacks. Pre-existing Fibers, `Fiber.new(storage: nil)`, and child threads do not inherit subtree context. When an inner capture ends, child Fibers retain any still-active outer capture; inherited context expires once all enclosing captures end. While a root remains active, hitting a span limit stops new capture but allows already-open spans in every Fiber to finish. `max_depth:` (default `30`), `max_captured_subtree_spans:` (default `500`), `exclude:`, and `include_wrappers:` control the subtree. There is no cap on the total number of spans one trace root sends. `max_spans:` was removed, and passing it raises `ArgumentError`. `max_captured_subtree_spans:` caps discovered calls sent with their inputs and outputs. It does not count `bitfab_node` calls, nested trace roots, or spans the loaded sim plan turned capture off for, so those keep recording past it. Spans sent without inputs and outputs only because the sim plan could not be read still count toward it. Past it, further discovered calls with capture on record nothing. Once the plan has loaded, the SDK does not send a span whose capture it turned off, and that span's children move up to the nearest visible parent. The trace's root span, a framework integration span, a span that recorded an error, and a span that recorded LLM usage (token counts or a cost in its contexts) are still sent, and the SDK never builds their inputs or outputs. A `bitfab_node` call and a nested trace root ignore the plan and always keep their inputs and outputs. The client starts reading the sim plan when it is built. A `bitfab_trace` root that starts while that first read is still running waits for it, for at most five seconds. Once the first read has finished, whether it succeeded or failed, no later root waits. Nested roots and the calls beneath a root never wait, and nothing waits when the plan is disabled. If that first read fails, spans other than each trace's root span, `bitfab_node` calls, and nested trace roots are sent without inputs and outputs until a read succeeds, and they still count toward `max_captured_subtree_spans:`. Automatically captured calls are not replay mock targets. Declare a call with `bitfab_node` when replay must substitute it. The configured node records once in each enclosing trace. Mixing explicit spans with subtree tracing raises `Bitfab::MixedTracingError`.
|
|
239
|
-
|
|
240
|
-
When capture ends, already-open automatic calls in suspended Fibers are emitted once with a nil output and `Subtree capture ended before the call returned` as the error, unless a real error was already observed. Their inputs and parenting are retained. The SDK does not resume Fibers, and later resumption cannot add spans to the ended capture. If hot reload moves the root to an unresolved source, the last valid first-party boundary and its exclusions remain in use.
|
|
241
|
-
|
|
242
|
-
Returning an `Enumerator` keeps capture pending until iteration finishes or raises, including calls already paused in child Fibers. Capture is inactive between the initial return and consumption. Automatic spans record lazy return values as placeholders without iterating them; the traced root records the yielded values.
|
|
243
|
-
|
|
244
|
-
Each nested `bitfab_trace` starts an independent trace during ordinary capture, including recursion and calls using the same key. Every enclosing trace retains its own recording of the shared subtree. The enclosing span and nested root carry reciprocal trace and span links. Each trace applies its own limits and exclusions. During replay and seed execution, nested annotations remain in the managed trace.
|
|
245
|
-
|
|
246
|
-
Configure a discovered method with `bitfab_node`. A node never creates a span or trace by itself; the active `bitfab_trace` owns its key, limits, parent, and lifecycle:
|
|
247
|
-
|
|
248
|
-
```ruby
|
|
249
|
-
bitfab_node :summarize,
|
|
250
|
-
name: "Model call",
|
|
251
|
-
type: "llm",
|
|
252
|
-
mock_on_replay: true,
|
|
253
|
-
finalize: ->(response) { {text: response.text} }
|
|
254
|
-
def summarize(order)
|
|
255
|
-
model.generate(order)
|
|
256
|
-
end
|
|
257
|
-
```
|
|
258
|
-
|
|
259
|
-
Nodes support `name:`, `type:`, `capture:`, `experiment_id:`, `mock_on_replay:`, and `finalize:`. `capture: false` omits the method while attaching captured descendants to its nearest visible parent. `finalize:` changes only the recorded output, never the return value. A finalizer failure is recorded on the node without crashing the caller. The before-`def`, inline, and after-`def` forms all preserve method visibility. For external code, use `Bitfab.client.node(MyClass, :method)`.
|
|
260
|
-
|
|
261
|
-
`bitfab_trace` prepares its project boundary and exclusions when the method is declared, refreshes the boundary if the root definition moves during hot reload, caches canonical source paths, and shares one `TracePoint` dispatcher across active roots on the calling thread. After a span limit is reached, it stops reading inputs and outputs immediately, then stops processing events for that session once its captured calls finish. Each active root still captures and serializes its own copy of every overlapping call, so this remains a discovery tool rather than the production default. Prefer explicit `bitfab_span` declarations on hot paths. Run `mise exec -- ruby -Ilib benchmark/subtree.rb` from this package to measure the no-network caller path on your Ruby build.
|
|
262
|
-
|
|
263
271
|
### Input/Output Capture
|
|
264
272
|
|
|
265
273
|
Positional and keyword arguments are automatically captured:
|
|
266
274
|
|
|
267
275
|
```ruby
|
|
268
|
-
|
|
276
|
+
bitfab_trace :process_order, type: "agent"
|
|
269
277
|
def process_order(order_id, priority: :normal)
|
|
270
278
|
# Input captured: { "order_id" => "123", "priority" => "normal" }
|
|
271
279
|
{ status: "completed" }
|
|
@@ -297,46 +305,35 @@ def process_order(order_id)
|
|
|
297
305
|
end
|
|
298
306
|
```
|
|
299
307
|
|
|
300
|
-
###
|
|
308
|
+
### Tracing Classes You Do Not Own
|
|
301
309
|
|
|
302
|
-
|
|
310
|
+
Bind a `trace_function_key` once with `client.get_function` and trace a method on a class you do not want to edit. Mirrors `client.get_function` in the Python SDK and `client.getFunction` in TypeScript. `client.node` configures one discovered method of such a class, like `bitfab_node`:
|
|
303
311
|
|
|
304
312
|
```ruby
|
|
305
|
-
|
|
306
|
-
def get(url)
|
|
307
|
-
# Third-party code
|
|
308
|
-
end
|
|
309
|
-
end
|
|
310
|
-
|
|
311
|
-
# Add tracing via wrap
|
|
312
|
-
Bitfab::Traceable.wrap(
|
|
313
|
-
ExternalHttpClient,
|
|
314
|
-
:get,
|
|
315
|
-
trace_function_key: "http-client",
|
|
316
|
-
type: "function"
|
|
317
|
-
)
|
|
313
|
+
fn = Bitfab.client.get_function("order-processing")
|
|
318
314
|
|
|
319
|
-
|
|
320
|
-
client
|
|
321
|
-
client.get("https://api.example.com")
|
|
315
|
+
fn.trace(OrderPipeline, :process, type: "agent")
|
|
316
|
+
Bitfab.client.node(OrderPipeline, :summarize, name: "Model call", type: "llm")
|
|
322
317
|
```
|
|
323
318
|
|
|
324
|
-
|
|
319
|
+
`#trace` accepts the same options as `bitfab_trace`, but the `trace_function_key` is fixed to the one bound on the `BitfabFunction`. `Bitfab::Traceable.trace` is the same call with `trace_function_key:` passed directly.
|
|
325
320
|
|
|
326
|
-
|
|
321
|
+
#### Wrapping External Code (Deprecated)
|
|
322
|
+
|
|
323
|
+
`Bitfab::Traceable.wrap` and `BitfabFunction#wrap` wrap one method in an opt-in span. They are deprecated in favor of the trace forms above, with the same meaning as [Opt-in Spans (Deprecated)](#opt-in-spans-deprecated):
|
|
327
324
|
|
|
328
325
|
```ruby
|
|
329
|
-
|
|
326
|
+
Bitfab::Traceable.wrap(ExternalHttpClient, :get, trace_function_key: "http-client", type: "function")
|
|
330
327
|
|
|
328
|
+
fn = Bitfab.client.get_function("openai")
|
|
331
329
|
fn.wrap(OpenAI::Client, :chat, name: "Chat", type: "llm")
|
|
332
|
-
fn.wrap(OpenAI::Client, :embeddings, name: "Embed", type: "llm")
|
|
333
330
|
```
|
|
334
331
|
|
|
335
332
|
`#wrap` accepts the same options as `Bitfab::Traceable.wrap` (`name`, `type`, `mock_on_replay`), but the `trace_function_key` is fixed to the one bound on the `BitfabFunction`.
|
|
336
333
|
|
|
337
334
|
### Replay with Mock Strategies
|
|
338
335
|
|
|
339
|
-
Replay reruns historical traces through your code so you can compare outputs after an iteration. By default,
|
|
336
|
+
Replay reruns historical traces through your code so you can compare outputs after an iteration. By default, nodes tagged with `mock_on_replay: true` return their historical output and other calls run real code. Three strategies control whether child spans return their historical output instead of executing.
|
|
340
337
|
|
|
341
338
|
Every replay that is not a dry run needs an experiment group: one series of experiments testing one thing, named after what is being tested. Pass the id of an existing group when the run tests the same thing as that group, or a new UUID with `experiment_group_name:` to start one.
|
|
342
339
|
|
|
@@ -354,28 +351,27 @@ client.replay(pipeline, :process, trace_function_key: "my-fn", mock: "none", **g
|
|
|
354
351
|
client.replay(pipeline, :process, trace_function_key: "my-fn", mock: "all", **group)
|
|
355
352
|
```
|
|
356
353
|
|
|
357
|
-
Tag the
|
|
354
|
+
Tag the calls you want mocked at definition time with `bitfab_node`. Calls discovered beneath the root without a node are not replay mock targets, so they run real code:
|
|
358
355
|
|
|
359
356
|
```ruby
|
|
360
357
|
class Pipeline
|
|
361
358
|
include Bitfab::Traceable
|
|
362
359
|
bitfab_function "my-fn"
|
|
363
360
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
361
|
+
bitfab_trace :process, type: "agent"
|
|
362
|
+
def process(text)
|
|
363
|
+
call_llm(text)
|
|
364
|
+
transform(text)
|
|
368
365
|
end
|
|
369
366
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
367
|
+
# mock_on_replay: true returns historical output under mock: "marked"
|
|
368
|
+
bitfab_node :call_llm, type: "llm", mock_on_replay: true
|
|
369
|
+
def call_llm(prompt)
|
|
370
|
+
# paid OpenAI call, skipped during replay
|
|
373
371
|
end
|
|
374
372
|
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
call_llm(text)
|
|
378
|
-
transform(text)
|
|
373
|
+
def transform(text)
|
|
374
|
+
# cheap and deterministic, keeps running real
|
|
379
375
|
end
|
|
380
376
|
end
|
|
381
377
|
```
|
|
@@ -387,7 +383,7 @@ Use the default `mock: "marked"` behavior when you want to iterate on `process`'
|
|
|
387
383
|
Errors are automatically captured and re-raised:
|
|
388
384
|
|
|
389
385
|
```ruby
|
|
390
|
-
|
|
386
|
+
bitfab_trace :risky_operation, type: "agent"
|
|
391
387
|
def risky_operation
|
|
392
388
|
raise StandardError, "Something went wrong"
|
|
393
389
|
end
|
|
@@ -433,7 +429,7 @@ A shared client needs no explicit close; `at_exit` shuts remaining transports do
|
|
|
433
429
|
|
|
434
430
|
## OpenTelemetry Transport
|
|
435
431
|
|
|
436
|
-
Bitfab keeps its public API (`Bitfab::Traceable`, `
|
|
432
|
+
Bitfab keeps its public API (`Bitfab::Traceable`, `bitfab_trace`, `bitfab_node`, `bitfab_span`), while one private
|
|
437
433
|
OpenTelemetry `TracerProvider` and `BatchSpanProcessor` per client manage the bounded queue, batch
|
|
438
434
|
worker, export scheduling, flush, and shutdown lifecycle. Pipelines are created lazily on the first
|
|
439
435
|
trace send and are never installed globally, so an unused or disabled client starts no OTel worker
|
|
@@ -467,6 +463,7 @@ Bitfab.reset!
|
|
|
467
463
|
## Thread Safety
|
|
468
464
|
|
|
469
465
|
- Each thread has its own span context stack (using `Thread.current`)
|
|
466
|
+
- `bitfab_trace` records only calls made on the thread its root runs on; calls inside a thread started beneath the root are not recorded
|
|
470
467
|
- Set `trace_across_threads: true` to propagate explicit spans and replay mocks to submitted threads and optional `concurrent-ruby` thread-pool jobs
|
|
471
468
|
- Background span sending is thread-safe
|
|
472
469
|
|
|
@@ -474,13 +471,13 @@ Bitfab.reset!
|
|
|
474
471
|
|
|
475
472
|
See [bitfab-ruby-example/](../bitfab-ruby-example/) for complete working examples:
|
|
476
473
|
|
|
477
|
-
- [test_span.rb](../bitfab-ruby-example/scripts/test_span.rb) - Basic span creation
|
|
478
|
-
- [test_nested_spans.rb](../bitfab-ruby-example/scripts/test_nested_spans.rb) - Nested span hierarchies
|
|
479
474
|
- [test_subtree.rb](../bitfab-ruby-example/scripts/test_subtree.rb) - Whole-subtree capture with independent overlapping roots
|
|
480
475
|
- [test_subtree_comprehensive.rb](../bitfab-ruby-example/scripts/test_subtree_comprehensive.rb) - Self-checking subtree and node behavior examples, including child-Fiber context and installed application gems
|
|
481
476
|
- [test_subtree_replay.rb](../bitfab-ruby-example/scripts/test_subtree_replay.rb) - Live subtree record and replay
|
|
482
477
|
- [subtree coverage map](../bitfab-ruby-example/subtree_examples/README.md) - Every subtree spec behavior mapped to its runnable example
|
|
483
|
-
- [
|
|
478
|
+
- [test_span.rb](../bitfab-ruby-example/scripts/test_span.rb) - Basic opt-in span creation (deprecated)
|
|
479
|
+
- [test_nested_spans.rb](../bitfab-ruby-example/scripts/test_nested_spans.rb) - Nested opt-in span hierarchies (deprecated)
|
|
480
|
+
- [test_wrap.rb](../bitfab-ruby-example/scripts/test_wrap.rb) - Wrapping external code with opt-in spans (deprecated)
|
|
484
481
|
- [test_enabled.rb](../bitfab-ruby-example/scripts/test_enabled.rb) - Using the enabled flag
|
|
485
482
|
- [test_metadata.rb](../bitfab-ruby-example/scripts/test_metadata.rb) - Custom metadata
|
|
486
483
|
|
data/lib/bitfab/client.rb
CHANGED
|
@@ -402,14 +402,15 @@ module Bitfab
|
|
|
402
402
|
# Get a function wrapper bound to a specific trace function key.
|
|
403
403
|
#
|
|
404
404
|
# This provides a fluent API for binding a trace_function_key once and
|
|
405
|
-
# then
|
|
405
|
+
# then tracing methods on classes you do not own with that key. Mirrors
|
|
406
406
|
# `client.get_function(key)` in the Python SDK and `client.getFunction(key)`
|
|
407
|
-
# in the TypeScript SDK.
|
|
407
|
+
# in the TypeScript SDK. Its trace method is the recommended way to
|
|
408
|
+
# instrument a workflow from outside the class.
|
|
408
409
|
#
|
|
409
410
|
# @example
|
|
410
411
|
# fn = Bitfab.client.get_function("order-processing")
|
|
411
|
-
# fn.
|
|
412
|
-
#
|
|
412
|
+
# fn.trace(OrderService, :process_order, type: "agent")
|
|
413
|
+
# Bitfab.client.node(OrderService, :validate_order, type: "guardrail")
|
|
413
414
|
#
|
|
414
415
|
# @param trace_function_key [String]
|
|
415
416
|
# @return [BitfabFunction]
|
|
@@ -1369,6 +1370,15 @@ module Bitfab
|
|
|
1369
1370
|
# so non-global `Bitfab::Client` instances don't silently fall back to
|
|
1370
1371
|
# `Bitfab.client`.
|
|
1371
1372
|
#
|
|
1373
|
+
# @deprecated Use #trace instead, with Bitfab::Client#node for a call that
|
|
1374
|
+
# needs its own name, type, or replay policy.
|
|
1375
|
+
# It keeps working and nothing warns at runtime, but it cannot be mixed
|
|
1376
|
+
# with bitfab_trace in one call stack: doing so raises
|
|
1377
|
+
# Bitfab::MixedTracingError.
|
|
1378
|
+
# bitfab_trace records only calls made on the thread its root runs on, so a
|
|
1379
|
+
# workflow whose calls run on threads it starts is recorded with spans and
|
|
1380
|
+
# trace_across_threads: true on the client.
|
|
1381
|
+
#
|
|
1372
1382
|
# @example
|
|
1373
1383
|
# fn = Bitfab.client.get_function("openai")
|
|
1374
1384
|
# fn.wrap(OpenAI::Client, :chat, name: "Chat", type: "llm")
|
|
@@ -1399,7 +1409,9 @@ module Bitfab
|
|
|
1399
1409
|
)
|
|
1400
1410
|
end
|
|
1401
1411
|
|
|
1402
|
-
#
|
|
1412
|
+
# Trace an existing method and every first-party Ruby method it calls, under
|
|
1413
|
+
# this function's trace_function_key. This is the external-class form of
|
|
1414
|
+
# bitfab_trace, the recommended way to instrument a workflow.
|
|
1403
1415
|
# @param max_payload_bytes_per_function [Integer] maximum serialized bytes of
|
|
1404
1416
|
# inputs and outputs that one automatically captured function may record
|
|
1405
1417
|
# within a single call of the root. Defaults to 250,000,000. Once a function
|
data/lib/bitfab/subtree.rb
CHANGED
|
@@ -74,7 +74,7 @@ module Bitfab
|
|
|
74
74
|
|
|
75
75
|
raise MixedTracingError,
|
|
76
76
|
"opt-in and opt-out tracing cannot be mixed: #{surface} entered inside #{enclosing}. " \
|
|
77
|
-
"Use bitfab_node inside bitfab_trace,
|
|
77
|
+
"Use bitfab_node inside bitfab_trace, and make the caller a bitfab_trace root too; bitfab_span is deprecated."
|
|
78
78
|
rescue MixedTracingError
|
|
79
79
|
raise
|
|
80
80
|
rescue
|
data/lib/bitfab/traceable.rb
CHANGED
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
require_relative "subtree"
|
|
4
4
|
|
|
5
5
|
module Bitfab
|
|
6
|
-
# Mixin for declarative
|
|
6
|
+
# Mixin for declarative tracing on instance methods. bitfab_trace is the
|
|
7
|
+
# recommended way to instrument a workflow: declare it on the root method and
|
|
8
|
+
# every first-party Ruby method the root calls is recorded beneath it. Use
|
|
9
|
+
# bitfab_node to give one of those calls its own name, type, or replay policy.
|
|
7
10
|
#
|
|
8
11
|
# @example
|
|
9
12
|
# Bitfab.configure(api_key: "...")
|
|
@@ -12,12 +15,13 @@ module Bitfab
|
|
|
12
15
|
# include Bitfab::Traceable
|
|
13
16
|
# bitfab_function "order-processing"
|
|
14
17
|
#
|
|
15
|
-
#
|
|
18
|
+
# bitfab_trace :process_order, type: "agent"
|
|
16
19
|
# def process_order(order_id)
|
|
20
|
+
# validate_order(order_id)
|
|
17
21
|
# { total: 100 }
|
|
18
22
|
# end
|
|
19
23
|
#
|
|
20
|
-
#
|
|
24
|
+
# bitfab_node :validate_order, name: "Validate", type: "guardrail"
|
|
21
25
|
# def validate_order(order_id)
|
|
22
26
|
# { valid: true }
|
|
23
27
|
# end
|
|
@@ -31,6 +35,16 @@ module Bitfab
|
|
|
31
35
|
# Wrap an existing method on an external class with span tracing.
|
|
32
36
|
# Use this to trace third-party library calls without modifying their source.
|
|
33
37
|
#
|
|
38
|
+
# @deprecated Use Bitfab::Traceable.trace (the external-class form of
|
|
39
|
+
# bitfab_trace) instead, with Bitfab::Client#node for a call that needs its
|
|
40
|
+
# own name, type, or replay policy.
|
|
41
|
+
# It keeps working and nothing warns at runtime, but it cannot be mixed
|
|
42
|
+
# with bitfab_trace in one call stack: doing so raises
|
|
43
|
+
# Bitfab::MixedTracingError.
|
|
44
|
+
# bitfab_trace records only calls made on the thread its root runs on, so a
|
|
45
|
+
# workflow whose calls run on threads it starts is recorded with spans and
|
|
46
|
+
# trace_across_threads: true on the client.
|
|
47
|
+
#
|
|
34
48
|
# @example
|
|
35
49
|
# Bitfab::Traceable.wrap(OpenAI::Client, :chat,
|
|
36
50
|
# trace_function_key: "openai", name: "Chat", type: "llm")
|
|
@@ -79,9 +93,11 @@ module Bitfab
|
|
|
79
93
|
klass.prepend(wrapper)
|
|
80
94
|
end
|
|
81
95
|
|
|
82
|
-
#
|
|
83
|
-
#
|
|
84
|
-
#
|
|
96
|
+
# Trace an existing method on an external class and every first-party Ruby
|
|
97
|
+
# method it calls. This is the external-class form of bitfab_trace, the
|
|
98
|
+
# recommended way to instrument a workflow. The root records with the
|
|
99
|
+
# requested type; first-party Ruby methods called beneath it record
|
|
100
|
+
# automatically as function spans.
|
|
85
101
|
# @param max_payload_bytes_per_function [Integer] maximum serialized bytes of
|
|
86
102
|
# inputs and outputs that one automatically captured function may record
|
|
87
103
|
# within a single call of the root. Defaults to 250,000,000. Once a function
|
|
@@ -310,6 +326,15 @@ module Bitfab
|
|
|
310
326
|
|
|
311
327
|
# Declare that a method should be wrapped with span tracing.
|
|
312
328
|
#
|
|
329
|
+
# @deprecated Use bitfab_trace instead, with bitfab_node for a call that
|
|
330
|
+
# needs its own name, type, or replay policy.
|
|
331
|
+
# It keeps working and nothing warns at runtime, but it cannot be mixed
|
|
332
|
+
# with bitfab_trace in one call stack: doing so raises
|
|
333
|
+
# Bitfab::MixedTracingError.
|
|
334
|
+
# bitfab_trace records only calls made on the thread its root runs on, so a
|
|
335
|
+
# workflow whose calls run on threads it starts is recorded with spans and
|
|
336
|
+
# trace_across_threads: true on the client.
|
|
337
|
+
#
|
|
313
338
|
# Supports three styles:
|
|
314
339
|
# bitfab_span :foo, type: "function" # before def foo (uses method_added hook)
|
|
315
340
|
# def foo; end
|
|
@@ -361,8 +386,10 @@ module Bitfab
|
|
|
361
386
|
end
|
|
362
387
|
end
|
|
363
388
|
|
|
364
|
-
#
|
|
365
|
-
#
|
|
389
|
+
# Trace a method and every first-party Ruby method it calls. This is the
|
|
390
|
+
# recommended way to instrument a workflow: descendants require no Bitfab
|
|
391
|
+
# declarations and record as function spans. Use bitfab_node to give one of
|
|
392
|
+
# them its own name, type, or replay policy.
|
|
366
393
|
# @param max_payload_bytes_per_function [Integer] maximum serialized bytes of
|
|
367
394
|
# inputs and outputs that one automatically captured function may record
|
|
368
395
|
# within a single call of the root. Defaults to 250,000,000. Once a function
|
|
@@ -406,7 +433,7 @@ module Bitfab
|
|
|
406
433
|
end
|
|
407
434
|
end
|
|
408
435
|
|
|
409
|
-
#
|
|
436
|
+
# Configure one method discovered beneath bitfab_trace.
|
|
410
437
|
# The node inherits the active trace key and never traces by itself.
|
|
411
438
|
def bitfab_node(method_name, name: nil, type: "custom", capture: true,
|
|
412
439
|
experiment_id: nil, mock_on_replay: nil, replay_reusable: false, finalize: nil, test_run_id: nil)
|
data/lib/bitfab/version.rb
CHANGED