class Jaeger::Encoders::ThriftEncoder

Public Class Methods

new(service_name:, tags: {}) click to toggle source
# File lib/jaeger/encoders/thrift_encoder.rb, line 6
def initialize(service_name:, tags: {})
  @service_name = service_name
  @tags = prepare_tags(tags)
  @process = Jaeger::Thrift::Process.new('serviceName' => @service_name, 'tags' => @tags)
end

Public Instance Methods

encode(spans) click to toggle source
# File lib/jaeger/encoders/thrift_encoder.rb, line 12
def encode(spans)
  encode_batch(spans.map(&method(:encode_span)))
end
encode_limited_size(spans, protocol_class, max_message_length) click to toggle source
# File lib/jaeger/encoders/thrift_encoder.rb, line 16
def encode_limited_size(spans, protocol_class, max_message_length)
  batches = []
  current_batch = []
  transport.flush
  spans.each do |span|
    encoded_span = encode_span(span)
    if aggregated_span_size(encoded_span, protocol_class) > max_message_length && !current_batch.empty?
      batches << encode_batch(current_batch)
      current_batch = []
      transport.flush
    end
    current_batch << encoded_span
  end
  batches << encode_batch(current_batch) unless current_batch.empty?
  batches
end

Private Instance Methods

aggregated_span_size(span, protocol_class) click to toggle source
# File lib/jaeger/encoders/thrift_encoder.rb, line 131
def aggregated_span_size(span, protocol_class)
  @protocol ||= protocol_class.new(transport)
  span.write(@protocol)
  transport.size
end
build_references(references) click to toggle source
# File lib/jaeger/encoders/thrift_encoder.rb, line 58
def build_references(references)
  references.map do |ref|
    Jaeger::Thrift::SpanRef.new(
      'refType' => span_ref_type(ref.type),
      'traceIdLow' => TraceId.uint64_id_to_int64(ref.context.trace_id),
      'traceIdHigh' => 0,
      'spanId' => TraceId.uint64_id_to_int64(ref.context.span_id)
    )
  end
end
build_timestamps(span) click to toggle source
# File lib/jaeger/encoders/thrift_encoder.rb, line 69
def build_timestamps(span)
  start_ts = (span.start_time.to_f * 1_000_000).to_i
  end_ts = (span.end_time.to_f * 1_000_000).to_i
  duration = end_ts - start_ts
  [start_ts, duration]
end
encode_batch(encoded_spans) click to toggle source
# File lib/jaeger/encoders/thrift_encoder.rb, line 35
def encode_batch(encoded_spans)
  Jaeger::Thrift::Batch.new('process' => @process, 'spans' => encoded_spans)
end
encode_span(span) click to toggle source
# File lib/jaeger/encoders/thrift_encoder.rb, line 39
def encode_span(span)
  context = span.context
  start_ts, duration = build_timestamps(span)

  Jaeger::Thrift::Span.new(
    'traceIdLow' => TraceId.uint64_id_to_int64(trace_id_to_low(context.trace_id)),
    'traceIdHigh' => TraceId.uint64_id_to_int64(trace_id_to_high(context.trace_id)),
    'spanId' => TraceId.uint64_id_to_int64(context.span_id),
    'parentSpanId' => TraceId.uint64_id_to_int64(context.parent_id),
    'operationName' => span.operation_name,
    'references' => build_references(span.references || []),
    'flags' => context.flags,
    'startTime' => start_ts,
    'duration' => duration,
    'tags' => span.tags,
    'logs' => span.logs
  )
end
prepare_tags(tags) click to toggle source
# File lib/jaeger/encoders/thrift_encoder.rb, line 88
def prepare_tags(tags)
  with_default_tags = tags.dup
  with_default_tags['jaeger.version'] = 'Ruby-' + Jaeger::Client::VERSION
  with_default_tags['hostname'] ||= Socket.gethostname

  unless with_default_tags['ip']
    ipv4 = Socket.ip_address_list.find { |ai| ai.ipv4? && !ai.ipv4_loopback? }
    with_default_tags['ip'] = ipv4.ip_address unless ipv4.nil?
  end

  with_default_tags.map do |key, value|
    ThriftTagBuilder.build(key, value)
  end
end
span_ref_type(type) click to toggle source
# File lib/jaeger/encoders/thrift_encoder.rb, line 76
def span_ref_type(type)
  case type
  when OpenTracing::Reference::CHILD_OF
    Jaeger::Thrift::SpanRefType::CHILD_OF
  when OpenTracing::Reference::FOLLOWS_FROM
    Jaeger::Thrift::SpanRefType::FOLLOWS_FROM
  else
    warn "Jaeger::Client with format #{type} is not supported yet"
    nil
  end
end
trace_id_to_high(trace_id) click to toggle source

Returns the left most 64 bits of trace id

# File lib/jaeger/encoders/thrift_encoder.rb, line 109
def trace_id_to_high(trace_id)
  (trace_id >> 64) & TraceId::MAX_64BIT_UNSIGNED_INT
end
trace_id_to_low(trace_id) click to toggle source

Returns the right most 64 bits of trace id

# File lib/jaeger/encoders/thrift_encoder.rb, line 104
def trace_id_to_low(trace_id)
  trace_id & TraceId::MAX_64BIT_UNSIGNED_INT
end
transport() click to toggle source
# File lib/jaeger/encoders/thrift_encoder.rb, line 137
def transport
  @transport ||= DummyTransport.new
end