class Atatus::TraceContext::Tracestate

@api private

Attributes

entries[RW]

Public Class Methods

new(entries: {}, sample_rate: nil) click to toggle source
# File lib/atatus/trace_context/tracestate.rb, line 96
def initialize(entries: {}, sample_rate: nil)
  @entries = entries

  self.sample_rate = sample_rate if sample_rate
end
parse(header) click to toggle source
# File lib/atatus/trace_context/tracestate.rb, line 106
def self.parse(header)
  entries =
    split_by_nl_and_comma(header)
    .each_with_object({}) do |entry, hsh|
      k, v = entry.split('=')

      hsh[k] =
        case k
        when 'es' then EsEntry.new(v)
        else Entry.new(k, v)
        end
    end

  new(entries: entries)
end

Private Class Methods

split_by_nl_and_comma(str) click to toggle source
# File lib/atatus/trace_context/tracestate.rb, line 138
def split_by_nl_and_comma(str)
  # HTTP allows multiple headers with the same name, eg. multiple
  # Set-Cookie headers per response.
  # Rack handles this by joining the headers under the same key, separated
  # by newlines, see https://www.rubydoc.info/github/rack/rack/file/SPEC
  String(str).split("\n").map { |s| s.split(',') }.flatten
end

Public Instance Methods

to_header() click to toggle source
# File lib/atatus/trace_context/tracestate.rb, line 122
def to_header
  return "" unless entries.any?

  entries.values.map(&:to_s).join(',')
end

Private Instance Methods

es_entry() click to toggle source
# File lib/atatus/trace_context/tracestate.rb, line 130
def es_entry
  # lazy generate this so we only add it if necessary
  entries['es'] ||= EsEntry.new
end