class Honeycomb::Span

Represents a Honeycomb span, which wraps a Honeycomb event and adds specific tracing functionality

Constants

INVALID_SPAN_ID

Attributes

builder[R]
children[R]
context[R]
event[R]
id[R]
parent[R]
parent_id[R]
presend_hook[R]
propagation_hook[R]
sample_hook[R]
trace[R]

Public Class Methods

new(trace:, builder:, context:, **options) click to toggle source
# File lib/honeycomb/span.rb, line 24
def initialize(trace:,
               builder:,
               context:,
               **options)
  @id = generate_span_id
  @context = context
  @context.current_span = self
  @builder = builder
  @event = builder.event
  @trace = trace
  @children = []
  @sent = false
  @started = clock_time
  parse_options(**options)
  parse_hooks(**options)
end

Public Instance Methods

create_child() click to toggle source
# File lib/honeycomb/span.rb, line 64
def create_child
  self.class.new(trace: trace,
                 builder: builder,
                 context: context,
                 parent: self,
                 parent_id: id,
                 sample_hook: sample_hook,
                 presend_hook: presend_hook,
                 propagation_hook: propagation_hook).tap do |c|
    children << c
  end
end
parse_hooks(sample_hook: nil, presend_hook: nil, propagation_hook: nil, **_options) click to toggle source
# File lib/honeycomb/span.rb, line 55
def parse_hooks(sample_hook: nil,
                presend_hook: nil,
                propagation_hook: nil,
                **_options)
  @presend_hook = presend_hook
  @sample_hook = sample_hook
  @propagation_hook = propagation_hook
end
parse_options(parent: nil, parent_id: nil, is_root: parent_id.nil?, _sample_hook: nil, _presend_hook: nil, **_options) click to toggle source
# File lib/honeycomb/span.rb, line 41
def parse_options(parent: nil,
                  parent_id: nil,
                  is_root: parent_id.nil?,
                  _sample_hook: nil,
                  _presend_hook: nil,
                  **_options)
  @parent = parent
  # parent_id should be removed in the next major version bump. It has been
  # replaced with passing the actual parent in. This is kept for backwards
  # compatability
  @parent_id = parent_id
  @is_root = is_root
end
send() click to toggle source
# File lib/honeycomb/span.rb, line 77
def send
  return if sent?

  send_internal
end
trace_headers(env) click to toggle source
# File lib/honeycomb/span.rb, line 83
def trace_headers(env)
  if propagation_hook
    propagation_hook.call(env, propagation_context)
  else
    {}
  end
end

Protected Instance Methods

remove_child(child) click to toggle source
# File lib/honeycomb/span.rb, line 100
def remove_child(child)
  children.delete child
end
send_by_parent() click to toggle source
# File lib/honeycomb/span.rb, line 93
def send_by_parent
  return if sent?

  add_field "meta.sent_by_parent", true
  send_internal
end

Private Instance Methods

add_additional_fields() click to toggle source
# File lib/honeycomb/span.rb, line 155
def add_additional_fields
  add_field "duration_ms", duration_ms
  add_field "trace.trace_id", trace.id
  add_field "trace.span_id", id
  add_field "meta.span_type", span_type
  parent_id && add_field("trace.parent_id", parent_id)
  add rollup_fields
  add trace.fields
  span_type == "root" && add(trace.rollup_fields)
end
clock_time() click to toggle source
# File lib/honeycomb/span.rb, line 176
def clock_time
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
duration_ms() click to toggle source
# File lib/honeycomb/span.rb, line 172
def duration_ms
  (clock_time - @started) * 1000
end
generate_span_id() click to toggle source
# File lib/honeycomb/span.rb, line 190
def generate_span_id
  loop do
    id = SecureRandom.hex(8)
    return id unless id == INVALID_SPAN_ID
  end
end
propagation_context() click to toggle source
# File lib/honeycomb/span.rb, line 118
def propagation_context
  Honeycomb::Propagation::Context.new(
    trace.id,
    id,
    trace.fields,
    builder.dataset,
  )
end
root?() click to toggle source
# File lib/honeycomb/span.rb, line 131
def root?
  @is_root
end
send_children() click to toggle source
# File lib/honeycomb/span.rb, line 166
def send_children
  children.each do |child|
    child.send_by_parent
  end
end
send_internal() click to toggle source
# File lib/honeycomb/span.rb, line 135
def send_internal
  add_additional_fields
  send_children
  sample = true
  if sample_hook.nil?
    sample = should_sample(event.sample_rate, trace.id)
  else
    sample, event.sample_rate = sample_hook.call(event.data)
  end

  if sample
    presend_hook && presend_hook.call(event.data)
    event.send_presampled
  end
  @sent = true
  context.span_sent(self)

  parent && parent.remove_child(self)
end
sent?() click to toggle source
# File lib/honeycomb/span.rb, line 127
def sent?
  @sent
end
span_type() click to toggle source
# File lib/honeycomb/span.rb, line 180
def span_type
  if root?
    parent_id.nil? ? "root" : "subroot"
  elsif children.empty?
    "leaf"
  else
    "mid"
  end
end