lib/opentelemetry/sdk/trace/samplers/consistent_probability_tracestate.rb in opentelemetry-sdk-experimental-0.1.1 vs lib/opentelemetry/sdk/trace/samplers/consistent_probability_tracestate.rb in opentelemetry-sdk-experimental-0.2.0
- old
+ new
@@ -21,25 +21,34 @@
# sanitized_tracestate returns an OpenTelemetry Tracestate object with the
# tracestate sanitized according to the Context invariants defined in the
# tracestate probability sampling spec.
#
+ # If r is nil after the sanitization, it is generated from the trace_id.
+ #
+ # This method assumes the parent span context is valid.
+ #
+ # @param trace_id [OpenTelemetry::Trace::TraceId] the trace id
# @param span_context [OpenTelemetry::Trace::SpanContext] the parent span context
# @return [OpenTelemetry::Trace::Tracestate] the sanitized tracestate
- def sanitized_tracestate(span_context)
+ def sanitized_tracestate(trace_id, 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
+ elsif !r.nil?
return tracestate
end
+ if r.nil?
+ OpenTelemetry.logger.debug("ConsistentProbabilitySampler: potentially inconsistent trace detected - r: #{r.inspect}")
+ r = generate_r(trace_id)
+ end
update_tracestate(tracestate, p, r, rest)
end
end
# parse_ot_vendor_tag parses the 'ot' vendor tag of the tracestate.
@@ -83,21 +92,9 @@
tracestate.set_value('ot', "p:#{p};#{rest}")
elsif rest.nil?
tracestate.set_value('ot', "p:#{p};r:#{r}")
else
tracestate.set_value('ot', "p:#{p};r:#{r};#{rest}")
- end
- end
-
- def new_tracestate(p: nil, r: nil) # rubocop:disable Naming/UncommunicativeMethodParamName
- if p.nil? && r.nil?
- OpenTelemetry::Trace::Tracestate.DEFAULT
- elsif p.nil?
- OpenTelemetry::Trace::Tracestate.from_hash('ot' => "r:#{r}")
- elsif r.nil?
- OpenTelemetry::Trace::Tracestate.from_hash('ot' => "p:#{p}")
- else
- OpenTelemetry::Trace::Tracestate.from_hash('ot' => "p:#{p};r:#{r}")
end
end
def invariant(p, r, sampled) # rubocop:disable Naming/UncommunicativeMethodParamName
((p <= r) == sampled) || (sampled && (p == 63))