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

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/tracing/distributed/trace_state.rb,
lib/datadog/tracing/distributed/trace_state/ext.rb,
lib/datadog/tracing/distributed/trace_state/datadog.rb,
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.

Parsed vendor state from a W3C tracestate header.

Defined Under Namespace

Modules: Ext Classes: Datadog, OpenTelemetry

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unknown_vendors: nil, datadog: nil, open_telemetry: nil) ⇒ TraceState

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 TraceState.



113
114
115
116
117
# File 'lib/datadog/tracing/distributed/trace_state.rb', line 113

def initialize(unknown_vendors: nil, datadog: nil, open_telemetry: nil)
  @unknown_vendors = unknown_vendors
  @datadog = datadog || Datadog.new
  @open_telemetry = open_telemetry || OpenTelemetry.new
end

Instance Attribute Details

#datadog ⇒ 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.



111
112
113
# File 'lib/datadog/tracing/distributed/trace_state.rb', line 111

def datadog
  @datadog
end

#open_telemetry ⇒ 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.



111
112
113
# File 'lib/datadog/tracing/distributed/trace_state.rb', line 111

def open_telemetry
  @open_telemetry
end

#unknown_vendors ⇒ 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.



111
112
113
# File 'lib/datadog/tracing/distributed/trace_state.rb', line 111

def unknown_vendors
  @unknown_vendors
end

Class Method Details

.extract(tracestate) ⇒ 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 owned members while retaining other vendors unchanged.



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/datadog/tracing/distributed/trace_state.rb', line 32

def extract(tracestate)
  vendors = split(tracestate) || []
  datadog_index = vendors.index { |vendor| vendor.start_with?("dd=") }
  datadog_value = vendors.delete_at(datadog_index).delete_prefix("dd=") if datadog_index
  otel_index = vendors.index { |vendor| vendor.start_with?("ot=") }
  otel_value = vendors.delete_at(otel_index).delete_prefix("ot=") if otel_index

  new(
    unknown_vendors: vendors.empty? ? nil : vendors.join(","),
    datadog: Datadog.from_tracestate_member(datadog_value),
    open_telemetry: OpenTelemetry.from_tracestate_member(otel_value),
  )
end

.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.



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

def from_digest(digest, propagate_sampling: true)
  new(
    unknown_vendors: digest.trace_state,
    datadog: Datadog.from_digest(digest),
    open_telemetry: OpenTelemetry.from_digest(digest, propagate_sampling: propagate_sampling),
  )
end

.serialize_digest(digest) ⇒ 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.



23
24
25
26
27
28
29
# File 'lib/datadog/tracing/distributed/trace_state.rb', line 23

def serialize_digest(digest)
  serialize(
    unknown_vendors: digest.trace_state,
    datadog: Datadog.from_digest(digest),
    open_telemetry: OpenTelemetry.from_digest(digest),
  )
end

.split(tracestate) ⇒ 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.

Partial members must not be propagated, but complete members within the limits are retained.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/datadog/tracing/distributed/trace_state.rb', line 47

def split(tracestate)
  return if tracestate.nil? || tracestate.empty?

  remove_last_vendor = false
  if tracestate.bytesize > Ext::TRACESTATE_MAX_SIZE_LIMIT
    remove_last_vendor = tracestate.byteslice(Ext::TRACESTATE_MAX_SIZE_LIMIT, 1) != ","
    tracestate = tracestate.byteslice(0, Ext::TRACESTATE_MAX_SIZE_LIMIT + 1) #: String
    tracestate = tracestate.chop
  end

  tracestate = ::Datadog::Core::Utils.utf8_encode(tracestate, placeholder: nil)
  return unless tracestate

  vendors = tracestate.split(",", Ext::TRACESTATE_MAX_LIST_VENDORS + 1)
  vendors.pop if vendors.length > Ext::TRACESTATE_MAX_LIST_VENDORS || remove_last_vendor
  vendors.each(&:strip!)
  vendors.pop while vendors.last == ""
  vendors
end