Class: Datadog::Tracing::Distributed::TraceState::OpenTelemetry Private

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/tracing/distributed/trace_state/open_telemetry.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Encodes consistent probability sampling in the OpenTelemetry ot= tracestate member.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(random_value: nil, threshold: nil, unknown_fields: nil) ⇒ OpenTelemetry

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of OpenTelemetry.



57
58
59
60
61
# File 'lib/datadog/tracing/distributed/trace_state/open_telemetry.rb', line 57

def initialize(random_value: nil, threshold: nil, unknown_fields: nil)
  @random_value = random_value
  @threshold = threshold
  @unknown_fields = unknown_fields
end

Instance Attribute Details

#random_value ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



55
56
57
# File 'lib/datadog/tracing/distributed/trace_state/open_telemetry.rb', line 55

def random_value
  @random_value
end

#threshold ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



55
56
57
# File 'lib/datadog/tracing/distributed/trace_state/open_telemetry.rb', line 55

def threshold
  @threshold
end

#unknown_fields ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



55
56
57
# File 'lib/datadog/tracing/distributed/trace_state/open_telemetry.rb', line 55

def unknown_fields
  @unknown_fields
end

Class Method Details

.from_digest(digest, propagate_sampling: true) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



14
15
16
17
18
19
20
# File 'lib/datadog/tracing/distributed/trace_state/open_telemetry.rb', line 14

def from_digest(digest, propagate_sampling: true)
  new(
    random_value: (digest.trace_otel_random_value if propagate_sampling),
    threshold: (digest.trace_otel_threshold if propagate_sampling),
    unknown_fields: digest.trace_otel_unknown_fields,
  )
end

.from_tracestate_member(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parses known fields and retains unknown fields for propagation.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/datadog/tracing/distributed/trace_state/open_telemetry.rb', line 23

def from_tracestate_member(value)
  return new unless value

  # @type var random_value: ::String?
  # @type var threshold: ::String?
  random_value = nil
  threshold = nil
  unknown = []

  value.split(";").each do |pair|
    key, field = pair.split(":", 2)
    case key
    when "rv"
      random_value = field
    when "th"
      threshold = field
    else
      unknown << pair
    end
  end

  threshold = nil if threshold && !Ext::OpenTelemetry::VALID_THRESHOLD.match?(threshold)
  random_value = nil if random_value && !Ext::OpenTelemetry::VALID_RANDOM_VALUE.match?(random_value)

  new(
    random_value: random_value,
    threshold: threshold,
    unknown_fields: unknown.empty? ? nil : "#{unknown.join(";")};",
  )
end

Instance Method Details

#outbound(trace_id:, sampling_priority:, decision_maker:, applied_rate:, rate_limiter_rate:, distributed_sampling_priority:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolves fields that represent the trace's current sampling decision.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/datadog/tracing/distributed/trace_state/open_telemetry.rb', line 64

def outbound(
  trace_id:,
  sampling_priority:,
  decision_maker:,
  applied_rate:,
  rate_limiter_rate:,
  distributed_sampling_priority:
)
  if Ext::OpenTelemetry::NON_PROBABILITY_DECISIONS.include?(decision_maker)
    return self.class.new(random_value: random_value, unknown_fields: unknown_fields)
  end

  trace_kept = sampling_priority && sampling_priority >= Sampling::Ext::Priority::AUTO_KEEP
  if rate_limiter_rate && !trace_kept
    return self.class.new(random_value: random_value, unknown_fields: unknown_fields)
  end

  return self if random_value || threshold
  return self.class.new(unknown_fields: unknown_fields) if distributed_sampling_priority || !applied_rate

  outbound_threshold = threshold_for(applied_rate)
  outbound_random_value = reconcile_random_value(
    random_value_for(trace_id),
    outbound_threshold,
    !!trace_kept
  )
  self.class.new(
    random_value: format_random_value(outbound_random_value),
    threshold: format_threshold(outbound_threshold),
    unknown_fields: unknown_fields,
  )
end

#to_s ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/datadog/tracing/distributed/trace_state/open_telemetry.rb', line 97

def to_s
  fields = []
  fields << "rv:#{random_value}" if random_value
  fields << "th:#{threshold}" if threshold
  fields.concat(unknown_fields.chomp(";").split(";")) if unknown_fields

  value = +""
  fields.each do |field|
    separator = value.empty? ? "" : ";"
    break if value.bytesize + separator.bytesize + field.bytesize > Ext::TRACESTATE_VALUE_SIZE_LIMIT

    value << separator << field
  end
  value.empty? ? "" : "ot=#{value}"
end