Skip to content

Commit 52a9c38

Browse files
fix: Refactor consistent prob sampler for reuse (#1427)
* fix: Refactor consistent prob sampler for reuse * chore: trigger ci --------- Co-authored-by: Robert Laurin <[email protected]> Co-authored-by: Robert <[email protected]>
1 parent d1050a0 commit 52a9c38

File tree

3 files changed

+31
-31
lines changed

3 files changed

+31
-31
lines changed

sdk_experimental/lib/opentelemetry/sdk/trace/samplers/consistent_probability_based.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,6 @@ def should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:
6969

7070
private
7171

72-
def generate_r(trace_id)
73-
x = trace_id[8, 8].unpack1('Q>') | 0x3
74-
clz = 64 - x.bit_length
75-
clz
76-
end
77-
7872
def probabilistic_p
7973
if Random.rand < @p_ceil_probability
8074
@p_ceil

sdk_experimental/lib/opentelemetry/sdk/trace/samplers/consistent_probability_tracestate.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,31 @@ module ConsistentProbabilityTraceState
1717
MAX_LIST_LENGTH = 256 # Defined by https://www.w3.org/TR/trace-context/
1818
private_constant(:DECIMAL, :MAX_LIST_LENGTH)
1919

20+
private
21+
22+
# sanitized_tracestate returns an OpenTelemetry Tracestate object with the
23+
# tracestate sanitized according to the Context invariants defined in the
24+
# tracestate probability sampling spec.
25+
#
26+
# @param span_context [OpenTelemetry::Trace::SpanContext] the parent span context
27+
# @return [OpenTelemetry::Trace::Tracestate] the sanitized tracestate
28+
def sanitized_tracestate(span_context)
29+
sampled = span_context.trace_flags.sampled?
30+
tracestate = span_context.tracestate
31+
parse_ot_vendor_tag(tracestate) do |p, r, rest|
32+
if !r.nil? && r > 62
33+
p = r = nil
34+
elsif !p.nil? && p > 63
35+
p = nil
36+
elsif !p.nil? && !r.nil? && !invariant(p, r, sampled)
37+
p = nil
38+
else
39+
return tracestate
40+
end
41+
update_tracestate(tracestate, p, r, rest)
42+
end
43+
end
44+
2045
# parse_ot_vendor_tag parses the 'ot' vendor tag of the tracestate.
2146
# It yields the parsed probability fields and the remaining tracestate.
2247
# It returns the result of the block.
@@ -82,6 +107,12 @@ def invariant(p, r, sampled) # rubocop:disable Naming/UncommunicativeMethodParam
82107
def decimal(str)
83108
str.to_i if !str.nil? && DECIMAL.match?(str)
84109
end
110+
111+
def generate_r(trace_id)
112+
x = trace_id[8, 8].unpack1('Q>') | 0x3
113+
clz = 64 - x.bit_length
114+
clz
115+
end
85116
end
86117
end
87118
end

sdk_experimental/lib/opentelemetry/sdk/trace/samplers/parent_consistent_probability_based.rb

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,6 @@ def should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:
5555
protected
5656

5757
attr_reader :root
58-
59-
private
60-
61-
# sanitized_tracestate returns an OpenTelemetry Tracestate object with the
62-
# tracestate sanitized according to the Context invariants defined in the
63-
# tracestate probability sampling spec.
64-
#
65-
# @param span_context [OpenTelemetry::Trace::SpanContext] the parent span context
66-
# @return [OpenTelemetry::Trace::Tracestate] the sanitized tracestate
67-
def sanitized_tracestate(span_context)
68-
sampled = span_context.trace_flags.sampled?
69-
tracestate = span_context.tracestate
70-
parse_ot_vendor_tag(tracestate) do |p, r, rest|
71-
if !r.nil? && r > 62
72-
p = r = nil
73-
elsif !p.nil? && p > 63
74-
p = nil
75-
elsif !p.nil? && !r.nil? && !invariant(p, r, sampled)
76-
p = nil
77-
else
78-
return tracestate
79-
end
80-
update_tracestate(tracestate, p, r, rest)
81-
end
82-
end
8358
end
8459
end
8560
end

0 commit comments

Comments
 (0)