bitfab 0.78.6 → 0.78.7
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/lib/bitfab/client.rb +1 -0
- data/lib/bitfab/http_client.rb +27 -4
- data/lib/bitfab/span_tree.rb +227 -0
- data/lib/bitfab/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4b353684b58fb3c9e8f31789c3cdb77242941e7aad1146fdda406f498d58dae1
|
|
4
|
+
data.tar.gz: 0fffad5d8910f4688e1ecd0632cf63302a4f3dab1c635c812da8ce8f1ce97037
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 76c24c957fe8412ce6aa2697e2af932a8ef9b8059ddf116db65f631e53011f1f10175ca76509cd647f994edee63d48c248e45c7b8966228157ee28dffa49fa93
|
|
7
|
+
data.tar.gz: d2a79f324d5292430464570d692e225e6fb9ed936dccd48ffad637656e3d580d09d08d4b6dce1fb6dbdfe6b494d6e436314eafcbb1d7ebc52a00d823c3ccafcb
|
data/lib/bitfab/client.rb
CHANGED
|
@@ -1162,6 +1162,7 @@ module Bitfab
|
|
|
1162
1162
|
"sourceTraceId" => trace_id,
|
|
1163
1163
|
"traceFunctionKey" => trace_function_key,
|
|
1164
1164
|
"rootTraceFunctionKey" => TraceState.get(trace_id)&.dig(:trace_function_key) || trace_function_key,
|
|
1165
|
+
SpanTree::SPAN_SHAPE_FIELD => SpanTree.span_shape_of(args, kwargs, result),
|
|
1165
1166
|
"rawSpan" => raw_span
|
|
1166
1167
|
}
|
|
1167
1168
|
if experiment_id
|
data/lib/bitfab/http_client.rb
CHANGED
|
@@ -17,6 +17,7 @@ require_relative "transient_retry"
|
|
|
17
17
|
require_relative "version"
|
|
18
18
|
require_relative "warn_once"
|
|
19
19
|
require_relative "simulation_plan"
|
|
20
|
+
require_relative "span_tree"
|
|
20
21
|
require_relative "span_origin"
|
|
21
22
|
require_relative "thread_propagation"
|
|
22
23
|
|
|
@@ -38,7 +39,7 @@ module Bitfab
|
|
|
38
39
|
REPLAY_DB_BRANCH_REQUEST_TIMEOUT_SECONDS = 300
|
|
39
40
|
private_constant :REPLAY_DB_BRANCH_REQUEST_TIMEOUT_SECONDS
|
|
40
41
|
|
|
41
|
-
attr_reader :service_url, :trace_completion
|
|
42
|
+
attr_reader :service_url, :trace_completion, :span_tree
|
|
42
43
|
|
|
43
44
|
# Retry-After as milliseconds. The header is either a delay in seconds or an
|
|
44
45
|
# HTTP date; both forms appear in the wild, so both are read. Anything else,
|
|
@@ -89,6 +90,7 @@ module Bitfab
|
|
|
89
90
|
@delivery_mutex = Mutex.new
|
|
90
91
|
@trace_deliveries = {}
|
|
91
92
|
@trace_completion = TraceCompletion.new
|
|
93
|
+
@span_tree = SpanTree::Recorder.new
|
|
92
94
|
@delivery_pid = Process.pid
|
|
93
95
|
@carrier_seq_mutex = Mutex.new
|
|
94
96
|
@carrier_seq = 0
|
|
@@ -239,6 +241,8 @@ module Bitfab
|
|
|
239
241
|
|
|
240
242
|
# Queue a span payload on this client's trace transport (fire-and-forget).
|
|
241
243
|
def send_span_payload(payload, declared_node = false, snapshot = nil)
|
|
244
|
+
record_span_tree(payload)
|
|
245
|
+
payload = payload.except(SpanTree::SPAN_SHAPE_FIELD) if payload.key?(SpanTree::SPAN_SHAPE_FIELD)
|
|
242
246
|
if (data = payload.dig("rawSpan", "span_data")).is_a?(Hash) && !data.key?("runtime")
|
|
243
247
|
payload = payload.merge("rawSpan" => payload["rawSpan"].merge("span_data" => data.merge("runtime" => ThreadPropagation.runtime)))
|
|
244
248
|
end
|
|
@@ -261,7 +265,10 @@ module Bitfab
|
|
|
261
265
|
}
|
|
262
266
|
discard = ->(record) {
|
|
263
267
|
ref = carrier_ref(record)
|
|
264
|
-
|
|
268
|
+
if ref&.span_id
|
|
269
|
+
@span_tree.drop(ref.trace_id, ref.span_id)
|
|
270
|
+
@trace_completion.discard(ref.trace_id, ref.span_id)
|
|
271
|
+
end
|
|
265
272
|
}
|
|
266
273
|
@simulation_plan ? @simulation_plan.send_span(payload, declared_node:, discard:, &submit) : submit.call(payload)
|
|
267
274
|
end
|
|
@@ -474,8 +481,14 @@ module Bitfab
|
|
|
474
481
|
submit = ->(record) {
|
|
475
482
|
ref = carrier_ref(record)
|
|
476
483
|
if record["completed"] == true && ref
|
|
477
|
-
|
|
478
|
-
|
|
484
|
+
dropped = record["dropped"] == true
|
|
485
|
+
@trace_completion.close(ref.trace_id, dropped:) do |count|
|
|
486
|
+
trace_function_key = record["traceFunctionKey"].is_a?(String) ? record["traceFunctionKey"] : ""
|
|
487
|
+
span_tree = dropped ? nil : @span_tree.take_to_send(ref.trace_id, trace_function_key)
|
|
488
|
+
@span_tree.forget(ref.trace_id)
|
|
489
|
+
completion = record.merge("expectedSpanCount" => count)
|
|
490
|
+
completion["spanTree"] = span_tree if span_tree
|
|
491
|
+
submit_trace_payload(completion)
|
|
479
492
|
end
|
|
480
493
|
else
|
|
481
494
|
@trace_completion.open(ref.trace_id) if ref
|
|
@@ -510,6 +523,16 @@ module Bitfab
|
|
|
510
523
|
|
|
511
524
|
private
|
|
512
525
|
|
|
526
|
+
def record_span_tree(payload)
|
|
527
|
+
raw_span = payload["rawSpan"]
|
|
528
|
+
span_id = raw_span["id"] if raw_span.is_a?(Hash)
|
|
529
|
+
return unless span_id.is_a?(String)
|
|
530
|
+
|
|
531
|
+
ref = carrier_ref(payload)
|
|
532
|
+
recorded = ref && SpanTree.recorded_span_of(payload, ref.trace_id, span_id)
|
|
533
|
+
@span_tree.record(recorded) if recorded
|
|
534
|
+
end
|
|
535
|
+
|
|
513
536
|
# The delivery identity of a carrier, read from the payload here because
|
|
514
537
|
# this is where the payload shape is owned. The transport is handed the
|
|
515
538
|
# result and never looks inside a payload itself.
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "set" # standard:disable Lint/RedundantRequireStatement -- Required on Ruby 3.4.
|
|
5
|
+
|
|
6
|
+
module Bitfab
|
|
7
|
+
# Summarizes every span a trace recorded, without inputs or outputs, so the
|
|
8
|
+
# trace completion can carry the call tree even for spans that were not sent.
|
|
9
|
+
module SpanTree
|
|
10
|
+
SPAN_SHAPE_FIELD = "spanShape"
|
|
11
|
+
MAX_NAMES = 500
|
|
12
|
+
MAX_NAME_LENGTH = 200
|
|
13
|
+
MAX_PARENT_NAMES = 5
|
|
14
|
+
MAX_PARAMETER_TYPES = 32
|
|
15
|
+
MAX_TYPE_LENGTH = 100
|
|
16
|
+
MAX_OPEN_TRACES = 10_000
|
|
17
|
+
# The most bytes the tree's nodes may take as compact UTF-8 JSON.
|
|
18
|
+
MAX_BYTES = 128 * 1024
|
|
19
|
+
# How long an unchanged tree waits before it is sent again for the same function.
|
|
20
|
+
RESEND_SECONDS = 10 * 60
|
|
21
|
+
# The most trace functions whose last sent tree is remembered at once.
|
|
22
|
+
MAX_FUNCTIONS = 1_000
|
|
23
|
+
|
|
24
|
+
RecordedSpan = Struct.new(:trace_id, :span_id, :name, :parent_id, :error, :shape)
|
|
25
|
+
|
|
26
|
+
NameStats = Struct.new(:calls, :dropped, :error_calls, :parent_ids, :has_root_call, :shape)
|
|
27
|
+
private_constant :NameStats
|
|
28
|
+
|
|
29
|
+
# The type name of a value as the span tree reports it.
|
|
30
|
+
def self.value_type_name(value)
|
|
31
|
+
case value
|
|
32
|
+
when nil then "null"
|
|
33
|
+
when true, false then "boolean"
|
|
34
|
+
when String, Symbol then "string"
|
|
35
|
+
when Integer, Float then "number"
|
|
36
|
+
when Array then "array"
|
|
37
|
+
when Hash then "object"
|
|
38
|
+
else
|
|
39
|
+
name = value.class.name
|
|
40
|
+
(name.is_a?(String) && !name.empty?) ? name[0, MAX_TYPE_LENGTH] : "object"
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# The parameter and output types of one call: positional arguments first,
|
|
45
|
+
# then keyword argument values. A nil result has no output type.
|
|
46
|
+
def self.span_shape_of(args, kwargs, result)
|
|
47
|
+
inputs = Array(args) + (kwargs.is_a?(Hash) ? kwargs.values : [])
|
|
48
|
+
{
|
|
49
|
+
"parameterTypes" => inputs.first(MAX_PARAMETER_TYPES).map { |value| value_type_name(value) },
|
|
50
|
+
"outputType" => result.nil? ? nil : value_type_name(result)
|
|
51
|
+
}
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# The span a payload describes, or nil when it has no name.
|
|
55
|
+
def self.recorded_span_of(payload, trace_id, span_id)
|
|
56
|
+
raw_span = payload["rawSpan"].is_a?(Hash) ? payload["rawSpan"] : nil
|
|
57
|
+
span_data = raw_span&.dig("span_data")
|
|
58
|
+
span_data = nil unless span_data.is_a?(Hash)
|
|
59
|
+
name = span_data&.dig("name")
|
|
60
|
+
return nil unless name.is_a?(String) && !name.empty?
|
|
61
|
+
|
|
62
|
+
parent_id = raw_span["parent_id"]
|
|
63
|
+
shape = payload[SPAN_SHAPE_FIELD]
|
|
64
|
+
RecordedSpan.new(
|
|
65
|
+
trace_id:,
|
|
66
|
+
span_id:,
|
|
67
|
+
name:,
|
|
68
|
+
parent_id: parent_id.is_a?(String) ? parent_id : nil,
|
|
69
|
+
error: !span_data["error"].nil?,
|
|
70
|
+
shape: span_shape?(shape) ? shape : nil
|
|
71
|
+
)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def self.span_shape?(value)
|
|
75
|
+
value.is_a?(Hash) && value["parameterTypes"].is_a?(Array) &&
|
|
76
|
+
(value["outputType"].nil? || value["outputType"].is_a?(String))
|
|
77
|
+
end
|
|
78
|
+
private_class_method :span_shape?
|
|
79
|
+
|
|
80
|
+
# Collects the spans of each open trace until its completion takes them.
|
|
81
|
+
class Recorder
|
|
82
|
+
def initialize
|
|
83
|
+
@pid = Process.pid
|
|
84
|
+
@mutex = Mutex.new
|
|
85
|
+
@traces = {}
|
|
86
|
+
@last_sent_by_function = {}
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Count one span toward its trace. A span recorded twice counts once.
|
|
90
|
+
def record(span)
|
|
91
|
+
synchronize do
|
|
92
|
+
spans = spans_of(span.trace_id)
|
|
93
|
+
next if spans[:name_by_id].key?(span.span_id)
|
|
94
|
+
|
|
95
|
+
spans[:name_by_id][span.span_id] = span.name
|
|
96
|
+
stats = (spans[:stats_by_name][span.name] ||= NameStats.new(0, 0, 0, Set.new, false, nil))
|
|
97
|
+
stats.calls += 1
|
|
98
|
+
stats.error_calls += 1 if span.error
|
|
99
|
+
if span.parent_id.nil?
|
|
100
|
+
stats.has_root_call = true
|
|
101
|
+
else
|
|
102
|
+
stats.parent_ids.add(span.parent_id)
|
|
103
|
+
end
|
|
104
|
+
stats.shape ||= span.shape
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Mark a recorded span as not sent.
|
|
109
|
+
def drop(trace_id, span_id)
|
|
110
|
+
synchronize do
|
|
111
|
+
spans = @traces[trace_id]
|
|
112
|
+
name = spans&.dig(:name_by_id, span_id)
|
|
113
|
+
next if name.nil?
|
|
114
|
+
|
|
115
|
+
stats = spans[:stats_by_name][name]
|
|
116
|
+
stats.dropped += 1 if stats && stats.dropped < stats.calls
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Stop tracking a trace without reporting it.
|
|
121
|
+
def forget(trace_id)
|
|
122
|
+
synchronize { @traces.delete(trace_id) }
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# The span tree of a trace when it says something new for its function: a
|
|
126
|
+
# span was not sent, and the shape changed or the last send is old enough.
|
|
127
|
+
# Otherwise nil. Stops tracking the trace either way.
|
|
128
|
+
def take_to_send(trace_id, trace_function_key, now = Process.clock_gettime(Process::CLOCK_MONOTONIC))
|
|
129
|
+
tree = take(trace_id)
|
|
130
|
+
return nil if tree.nil? || tree["nodes"].none? { |node| node["sentCalls"] < node["calls"] }
|
|
131
|
+
|
|
132
|
+
signature = shape_signature(tree)
|
|
133
|
+
synchronize do
|
|
134
|
+
last = @last_sent_by_function[trace_function_key]
|
|
135
|
+
next nil if last && last[:signature] == signature && now - last[:sent_at] < RESEND_SECONDS
|
|
136
|
+
|
|
137
|
+
@last_sent_by_function.delete(trace_function_key)
|
|
138
|
+
@last_sent_by_function[trace_function_key] = {sent_at: now, signature:}
|
|
139
|
+
@last_sent_by_function.shift if @last_sent_by_function.size > MAX_FUNCTIONS
|
|
140
|
+
tree
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# The span tree of a trace, or nil when none was recorded. Stops tracking it.
|
|
145
|
+
def take(trace_id)
|
|
146
|
+
spans = synchronize { @traces.delete(trace_id) }
|
|
147
|
+
return nil if spans.nil?
|
|
148
|
+
|
|
149
|
+
nodes = []
|
|
150
|
+
omitted_name_count = 0
|
|
151
|
+
spans[:stats_by_name].each do |name, stats|
|
|
152
|
+
if name.length > MAX_NAME_LENGTH
|
|
153
|
+
omitted_name_count += 1
|
|
154
|
+
next
|
|
155
|
+
end
|
|
156
|
+
nodes << {
|
|
157
|
+
"name" => name,
|
|
158
|
+
"parentNames" => parent_names_of(stats, spans[:name_by_id]),
|
|
159
|
+
"calls" => stats.calls,
|
|
160
|
+
"sentCalls" => stats.calls - stats.dropped,
|
|
161
|
+
"errorCalls" => stats.error_calls,
|
|
162
|
+
"parameterTypes" => stats.shape ? stats.shape["parameterTypes"] : [],
|
|
163
|
+
"outputType" => stats.shape&.dig("outputType")
|
|
164
|
+
}
|
|
165
|
+
end
|
|
166
|
+
ranked = nodes.each_with_index
|
|
167
|
+
.sort_by { |node, index| [-node["calls"], index] }
|
|
168
|
+
.first(MAX_NAMES)
|
|
169
|
+
kept = fitting(ranked).sort_by { |_, index| index }.map(&:first)
|
|
170
|
+
{
|
|
171
|
+
"spanCount" => spans[:name_by_id].size,
|
|
172
|
+
"omittedNameCount" => omitted_name_count + nodes.size - kept.size,
|
|
173
|
+
"nodes" => kept
|
|
174
|
+
}
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
private
|
|
178
|
+
|
|
179
|
+
def fitting(ranked)
|
|
180
|
+
bytes = 2
|
|
181
|
+
kept = []
|
|
182
|
+
ranked.each do |entry|
|
|
183
|
+
node_bytes = JSON.generate(entry.first).bytesize + (kept.empty? ? 0 : 1)
|
|
184
|
+
next if bytes + node_bytes > MAX_BYTES
|
|
185
|
+
|
|
186
|
+
bytes += node_bytes
|
|
187
|
+
kept << entry
|
|
188
|
+
end
|
|
189
|
+
kept
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def shape_signature(tree)
|
|
193
|
+
JSON.generate(tree["nodes"].map { |node| node.values_at("name", "parentNames", "parameterTypes", "outputType") })
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def synchronize(&block)
|
|
197
|
+
if @pid != Process.pid
|
|
198
|
+
@pid = Process.pid
|
|
199
|
+
@mutex = Mutex.new
|
|
200
|
+
@traces = {}
|
|
201
|
+
@last_sent_by_function = {}
|
|
202
|
+
end
|
|
203
|
+
@mutex.synchronize(&block)
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def spans_of(trace_id)
|
|
207
|
+
spans = @traces[trace_id]
|
|
208
|
+
return spans if spans
|
|
209
|
+
|
|
210
|
+
spans = {name_by_id: {}, stats_by_name: {}}
|
|
211
|
+
@traces[trace_id] = spans
|
|
212
|
+
@traces.shift if @traces.size > MAX_OPEN_TRACES
|
|
213
|
+
spans
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def parent_names_of(stats, name_by_id)
|
|
217
|
+
names = Set.new
|
|
218
|
+
stats.parent_ids.each do |parent_id|
|
|
219
|
+
name = name_by_id[parent_id]
|
|
220
|
+
names.add(name) if name && name.length <= MAX_NAME_LENGTH
|
|
221
|
+
break if names.size >= MAX_PARENT_NAMES
|
|
222
|
+
end
|
|
223
|
+
names.to_a
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
end
|
data/lib/bitfab/version.rb
CHANGED
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.78.
|
|
4
|
+
version: 0.78.7
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Harvest Team
|
|
@@ -217,6 +217,7 @@ files:
|
|
|
217
217
|
- lib/bitfab/span_context.rb
|
|
218
218
|
- lib/bitfab/span_meta.rb
|
|
219
219
|
- lib/bitfab/span_origin.rb
|
|
220
|
+
- lib/bitfab/span_tree.rb
|
|
220
221
|
- lib/bitfab/spans.rb
|
|
221
222
|
- lib/bitfab/subtree.rb
|
|
222
223
|
- lib/bitfab/thread_propagation.rb
|