Skip to content

Commit

Permalink
fix: Refactor consistent prob sampler for reuse (#1427)
Browse files Browse the repository at this point in the history
* fix: Refactor consistent prob sampler for reuse

* chore: trigger ci

---------

Co-authored-by: Robert Laurin <robert.laurin@shopify.com>
Co-authored-by: Robert <robertlaurin@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 6, 2023
1 parent d1050a0 commit 52a9c38
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,6 @@ def should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:

private

def generate_r(trace_id)
x = trace_id[8, 8].unpack1('Q>') | 0x3
clz = 64 - x.bit_length
clz
end

def probabilistic_p
if Random.rand < @p_ceil_probability
@p_ceil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,31 @@ module ConsistentProbabilityTraceState
MAX_LIST_LENGTH = 256 # Defined by https://www.w3.org/TR/trace-context/
private_constant(:DECIMAL, :MAX_LIST_LENGTH)

private

# sanitized_tracestate returns an OpenTelemetry Tracestate object with the
# tracestate sanitized according to the Context invariants defined in the
# tracestate probability sampling spec.
#
# @param span_context [OpenTelemetry::Trace::SpanContext] the parent span context
# @return [OpenTelemetry::Trace::Tracestate] the sanitized tracestate
def sanitized_tracestate(span_context)
sampled = span_context.trace_flags.sampled?
tracestate = span_context.tracestate
parse_ot_vendor_tag(tracestate) do |p, r, rest|
if !r.nil? && r > 62
p = r = nil
elsif !p.nil? && p > 63
p = nil
elsif !p.nil? && !r.nil? && !invariant(p, r, sampled)
p = nil
else
return tracestate
end
update_tracestate(tracestate, p, r, rest)
end
end

# parse_ot_vendor_tag parses the 'ot' vendor tag of the tracestate.
# It yields the parsed probability fields and the remaining tracestate.
# It returns the result of the block.
Expand Down Expand Up @@ -82,6 +107,12 @@ def invariant(p, r, sampled) # rubocop:disable Naming/UncommunicativeMethodParam
def decimal(str)
str.to_i if !str.nil? && DECIMAL.match?(str)
end

def generate_r(trace_id)
x = trace_id[8, 8].unpack1('Q>') | 0x3
clz = 64 - x.bit_length
clz
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,6 @@ def should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:
protected

attr_reader :root

private

# sanitized_tracestate returns an OpenTelemetry Tracestate object with the
# tracestate sanitized according to the Context invariants defined in the
# tracestate probability sampling spec.
#
# @param span_context [OpenTelemetry::Trace::SpanContext] the parent span context
# @return [OpenTelemetry::Trace::Tracestate] the sanitized tracestate
def sanitized_tracestate(span_context)
sampled = span_context.trace_flags.sampled?
tracestate = span_context.tracestate
parse_ot_vendor_tag(tracestate) do |p, r, rest|
if !r.nil? && r > 62
p = r = nil
elsif !p.nil? && p > 63
p = nil
elsif !p.nil? && !r.nil? && !invariant(p, r, sampled)
p = nil
else
return tracestate
end
update_tracestate(tracestate, p, r, rest)
end
end
end
end
end
Expand Down

0 comments on commit 52a9c38

Please sign in to comment.