class Zipkin::Encoders::JsonEncoder

Constants

CONTENT_TYPE
OT_KIND_TO_ZIPKIN_KIND

Public Class Methods

new(local_endpoint) click to toggle source
# File lib/zipkin/encoders/json_encoder.rb, line 43
def initialize(local_endpoint)
  @adapter = defined?(Oj) ? OjAdapter : JsonAdapter
  @local_endpoint = serialize_endpoint(local_endpoint)
end

Public Instance Methods

content_type() click to toggle source
# File lib/zipkin/encoders/json_encoder.rb, line 48
def content_type
  CONTENT_TYPE
end
encode(spans) click to toggle source
# File lib/zipkin/encoders/json_encoder.rb, line 52
def encode(spans)
  @adapter.dump(spans.map(&method(:serialize)))
end

Private Instance Methods

serialize(span) click to toggle source
# File lib/zipkin/encoders/json_encoder.rb, line 58
def serialize(span)
  finish_ts = Timestamp.create(span.end_time)
  start_ts = Timestamp.create(span.start_time)
  duration = finish_ts - start_ts

  {
    Fields::TRACE_ID => span.context.trace_id,
    Fields::SPAN_ID => span.context.span_id,
    Fields::PARENT_ID => span.context.parent_id,
    Fields::OPERATION_NAME => span.operation_name,
    Fields::KIND => OT_KIND_TO_ZIPKIN_KIND[span.tags[:'span.kind'] || 'server'],
    Fields::TIMESTAMP => start_ts,
    Fields::DURATION => duration,
    Fields::DEBUG => false,
    Fields::SHARED => false,
    Fields::LOCAL_ENDPOINT => @local_endpoint,
    Fields::REMOTE_ENDPOINT => serialize_endpoint(Endpoint.remote_endpoint(span)),
    Fields::ANNOTATIONS => LogAnnotations.build(span),
    Fields::TAGS => span.tags
  }
end
serialize_endpoint(endpoint) click to toggle source
# File lib/zipkin/encoders/json_encoder.rb, line 80
def serialize_endpoint(endpoint)
  return nil unless endpoint

  {
    Fields::Endpoint::SERVICE_NAME => endpoint.service_name,
    Fields::Endpoint::IPV4 => endpoint.ipv4,
    Fields::Endpoint::IPV6 => endpoint.ipv6,
    Fields::Endpoint::PORT => endpoint.port
  }
end