class Trace::Span

A span may contain many annotations

Constants

STATUS_ERROR_REGEXP
UNKNOWN_DURATION

Attributes

annotations[RW]
debug[RW]
kind[RW]
local_endpoint[RW]
name[RW]
remote_endpoint[RW]
tags[RW]

Public Class Methods

new(name, span_id, timestamp = Time.now) click to toggle source
# File lib/zipkin-tracer/trace.rb, line 200
def initialize(name, span_id, timestamp = Time.now)
  @name = name
  @span_id = span_id
  @kind = nil
  @local_endpoint = nil
  @remote_endpoint = nil
  @annotations = []
  @tags = {}
  @debug = span_id.debug?
  @timestamp = to_microseconds(timestamp)
  @duration = UNKNOWN_DURATION
end

Public Instance Methods

close(timestamp = Time.now) click to toggle source
# File lib/zipkin-tracer/trace.rb, line 213
def close(timestamp = Time.now)
  @duration = to_microseconds(timestamp) - @timestamp
end
has_parent_span?() click to toggle source
# File lib/zipkin-tracer/trace.rb, line 249
def has_parent_span?
  !@span_id.parent_id.nil?
end
record(value) click to toggle source

We record information into spans, then we send these spans to zipkin

# File lib/zipkin-tracer/trace.rb, line 237
def record(value)
  annotations << Trace::Annotation.new(value.to_s)
end
record_local_component(value) click to toggle source
# File lib/zipkin-tracer/trace.rb, line 245
def record_local_component(value)
  record_tag(Tag::LOCAL_COMPONENT, value)
end
record_status(status) click to toggle source
# File lib/zipkin-tracer/trace.rb, line 255
def record_status(status)
  return if status.nil?
  status = status.to_s
  record_tag(Tag::STATUS, status)
  record_tag(Tag::ERROR, status) if STATUS_ERROR_REGEXP.match(status)
end
record_tag(key, value) click to toggle source
# File lib/zipkin-tracer/trace.rb, line 241
def record_tag(key, value)
  @tags[key] = value.to_s
end
to_h() click to toggle source
# File lib/zipkin-tracer/trace.rb, line 217
def to_h
  h = {
    name: @name,
    traceId: @span_id.trace_id.to_s,
    id: @span_id.span_id.to_s,
    localEndpoint: @local_endpoint.to_h,
    timestamp: @timestamp,
    duration: @duration,
    debug: @debug
  }
  h[:parentId] = @span_id.parent_id.to_s unless @span_id.parent_id.nil?
  h[:kind] = @kind unless @kind.nil?
  h[:remoteEndpoint] = @remote_endpoint.to_h unless @remote_endpoint.nil?
  h[:annotations] = @annotations.map(&:to_h) unless @annotations.empty?
  h[:tags] = @tags unless @tags.empty?
  h[:shared] = true if @span_id.shared && @kind == Kind::SERVER
  h
end

Private Instance Methods

to_microseconds(time) click to toggle source
# File lib/zipkin-tracer/trace.rb, line 266
def to_microseconds(time)
  (time.to_f * 1_000_000).to_i
end