bitfab 0.76.0 → 0.76.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 944b6f0cbd7154e1273b43aba22b70e5cf7b55827f7e046fafdac7ee10d06e7c
4
- data.tar.gz: 94a69276be1db8d70a557e67bb0db363ca1dd46991020938537feeb83d5613ed
3
+ metadata.gz: f272aee3a4e633dffcc3b05633f62a5e2f51c629fd06cc39e1999eadd870dc5f
4
+ data.tar.gz: 96f0ca223998a9a8b26c13fc3cd161e2eee815e322190f19ffeabe6b8f1e930b
5
5
  SHA512:
6
- metadata.gz: 9d12ad3c98b90a58b424c63bb45d1dfb964860758ff47bec3292dbc39be7b9e25a285558f0a6b9b9b1cc3765eef8361ea5274043219cdcc18979a0c773c4522a
7
- data.tar.gz: b30aa0b927d59ae6387cd238da1ade630ca4191494cb676bb442d9495f94989b60c32a9408711ba978fd85d150a7e2a5b8b014549d01c12c32775070f79f85b1
6
+ metadata.gz: cfd82ea9b07e7395db282e94636470b7c530076298cd06bab9e2d6f8510fec42279084ce86c1aa39dd88e0c7abf04d9bf7b5b774659e92a40397f45c451b49fe
7
+ data.tar.gz: d550786d6876ec1a4d782f841b915f4a871e0cc517e75ebc92f4f5001f40dd84670375b367b1ad0818f073046b1a1be0c1c1414af203458549002c8973403338
data/lib/bitfab/client.rb CHANGED
@@ -601,6 +601,7 @@ module Bitfab
601
601
  error = nil
602
602
  span_contexts = nil
603
603
  span_prompt = nil
604
+ span_metadata = nil
604
605
  completed = false
605
606
  @http_client.trace_completion.start(trace_id, span_id)
606
607
  recording_override = managed_root ? {} : nil
@@ -638,7 +639,7 @@ module Bitfab
638
639
  end
639
640
 
640
641
  on_complete&.call(result: recorded_result, error: recorded_error, ended_at:,
641
- contexts: span_contexts, prompt: span_prompt)
642
+ contexts: span_contexts, prompt: span_prompt, metadata: span_metadata)
642
643
 
643
644
  span_data = span_data.merge("replay_reusable" => replay_reusable)
644
645
  output_json = finalize.nil? && !result.is_a?(Enumerator) && SelectiveReplay.fingerprint(final_result)
@@ -662,6 +663,7 @@ module Bitfab
662
663
  function_name: recording_override&.dig(:function_name) || function_name,
663
664
  contexts: span_contexts,
664
665
  prompt: span_prompt,
666
+ metadata: span_metadata,
665
667
  args:,
666
668
  kwargs:,
667
669
  result: recorded_result,
@@ -740,6 +742,7 @@ module Bitfab
740
742
  # Capture contexts before the span context is popped
741
743
  span_contexts = SpanContext.current&.dig(:contexts)
742
744
  span_prompt = SpanContext.current&.dig(:prompt)
745
+ span_metadata = SpanContext.current&.dig(:metadata)
743
746
  end
744
747
  end
745
748
  rescue => e
@@ -1049,7 +1052,7 @@ module Bitfab
1049
1052
 
1050
1053
  def send_span(trace_function_key:, trace_id:, span_id:, parent_span_id:,
1051
1054
  span_name:, span_type:, function_name:, contexts:, prompt:, args:, kwargs:, result:, error:,
1052
- started_at:, ended_at:, experiment_id: nil, input_source_span_id: nil, mocked: false,
1055
+ started_at:, ended_at:, metadata: nil, experiment_id: nil, input_source_span_id: nil, mocked: false,
1053
1056
  mock_target: nil, mock_source: nil, span_data: {}, instrumentation: "span", declared_node: false,
1054
1057
  nearest_captured_ancestor_span_id: nil)
1055
1058
  extra_span_data = span_data
@@ -1100,6 +1103,7 @@ module Bitfab
1100
1103
  end
1101
1104
  span_data["contexts"] = contexts if contexts&.any?
1102
1105
  span_data["prompt"] = prompt if prompt && !content_off
1106
+ span_data["metadata"] = metadata if metadata&.any?
1103
1107
 
1104
1108
  raw_span = {
1105
1109
  "id" => span_id,
@@ -55,6 +55,23 @@ module Bitfab
55
55
  rescue
56
56
  # Silently ignore - never crash the host app
57
57
  end
58
+
59
+ # Merge metadata into this span, with later values for a key winning. Use
60
+ # it to label what the span did, for example { "delivery" => "blocked" } on
61
+ # a message that was drafted but never sent. Values must be strings,
62
+ # numbers, or booleans; keys are 1 to 40 of A-Z a-z 0-9 _ . : -. Bitfab
63
+ # stores keys and values lowercased, returns them on spans.list, and
64
+ # narrows that list with its metadata filter. Sent even when content
65
+ # capture is off, like trace metadata.
66
+ #
67
+ # @param metadata [Hash] key-value pairs to merge into the span's metadata
68
+ def set_metadata(metadata)
69
+ return unless metadata.is_a?(Hash)
70
+
71
+ @span_state[:metadata] = (@span_state[:metadata] || {}).merge(metadata)
72
+ rescue
73
+ nil
74
+ end
58
75
  end
59
76
 
60
77
  # Handle to the current active trace, allowing trace-level context to be set.
data/lib/bitfab/spans.rb CHANGED
@@ -2,18 +2,45 @@
2
2
 
3
3
  module Bitfab
4
4
  class Spans
5
+ MAX_PAGE_SIZE = 200
6
+ MAX_PAGE_SIZE_WITH_FIELDS = 50
7
+ private_constant :MAX_PAGE_SIZE, :MAX_PAGE_SIZE_WITH_FIELDS
8
+
5
9
  def initialize(http_client)
6
10
  @http_client = http_client
7
11
  end
8
12
 
9
- def list(trace_id, span_type: nil, span_name: nil, fields: nil, cursor: nil, limit: nil)
13
+ # One page of a trace's spans in the order they ran. Each span carries its
14
+ # "metadata" Hash, keys and values lowercased, empty when none was set.
15
+ # metadata: keeps only spans whose metadata holds every given key with that
16
+ # value, matched exactly and case-insensitively, up to 10 keys. limit is 1
17
+ # to 200, or at most 50 when fields are requested; a larger value raises
18
+ # with the server's reason. Use list_all to read every span.
19
+ def list(trace_id, span_type: nil, span_name: nil, metadata: nil, fields: nil, cursor: nil, limit: nil)
10
20
  payload = {"traceId" => trace_id}
11
21
  payload["spanType"] = span_type unless span_type.nil?
12
22
  payload["spanName"] = span_name unless span_name.nil?
23
+ payload["metadata"] = metadata unless metadata.nil?
13
24
  payload["fields"] = fields unless fields.nil?
14
25
  payload["cursor"] = cursor unless cursor.nil?
15
26
  payload["limit"] = limit unless limit.nil?
16
27
  @http_client.request("/api/sdk/spans/list", payload)
17
28
  end
29
+
30
+ # Every span of one trace in the order they ran, fetched page by page until
31
+ # none are left. Takes the same filters as list. Pages hold 200 spans, or 50
32
+ # when fields are requested.
33
+ def list_all(trace_id, span_type: nil, span_name: nil, metadata: nil, fields: nil)
34
+ limit = (fields.nil? || fields.empty?) ? MAX_PAGE_SIZE : MAX_PAGE_SIZE_WITH_FIELDS
35
+ spans = []
36
+ cursor = nil
37
+ loop do
38
+ page = list(trace_id, span_type:, span_name:, metadata:, fields:, cursor:, limit:)
39
+ spans.concat(page["spans"] || [])
40
+ cursor = page["hasMore"] ? page["nextCursor"] : nil
41
+ break if cursor.nil?
42
+ end
43
+ spans
44
+ end
18
45
  end
19
46
  end
@@ -54,6 +54,7 @@ module Bitfab
54
54
  :declared_node,
55
55
  :contexts,
56
56
  :prompt,
57
+ :metadata,
57
58
  :jev_started_at,
58
59
  :jev_recorded_below
59
60
  )
@@ -152,7 +153,7 @@ module Bitfab
152
153
  span_id: @owner.current_parent_span_id, surface: :trace, &body)
153
154
  end
154
155
 
155
- def complete(result:, error:, ended_at:, contexts: nil, prompt: nil)
156
+ def complete(result:, error:, ended_at:, contexts: nil, prompt: nil, metadata: nil)
156
157
  return if @completed
157
158
 
158
159
  @completed = true
@@ -162,6 +163,7 @@ module Bitfab
162
163
  entry.error = error
163
164
  entry.contexts = contexts
164
165
  entry.prompt = prompt
166
+ entry.metadata = metadata
165
167
  session.emit_configured(entry, result:)
166
168
  end
167
169
  end
@@ -767,6 +769,7 @@ module Bitfab
767
769
  function_name: entry.name,
768
770
  contexts: entry.contexts,
769
771
  prompt: entry.prompt,
772
+ metadata: entry.metadata,
770
773
  args: entry.args,
771
774
  kwargs: entry.kwargs,
772
775
  result:,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bitfab
4
- VERSION = "0.76.0"
4
+ VERSION = "0.76.1"
5
5
  end
data/lib/bitfab.rb CHANGED
@@ -61,6 +61,9 @@ module Bitfab
61
61
  def set_prompt(_prompt)
62
62
  # No-op
63
63
  end
64
+
65
+ def set_metadata(_metadata)
66
+ end
64
67
  end
65
68
 
66
69
  # No-op trace handle returned when outside a span context.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitfab
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.76.0
4
+ version: 0.76.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harvest Team