Method: Datadog::Tracer#start_span

Defined in:
lib/ddtrace/tracer.rb

#start_span(name, options = {}) ⇒ Object

Return a span that will trace an operation called name. This method allows parenting passing child_of as an option. If it’s missing, the newly created span is a root span. Available options are:

  • service: the service name for this span

  • resource: the resource this span refers, or name if it’s missing

  • span_type: the type of the span (such as http, db and so on)

  • child_of: a Span or a Context instance representing the parent for this span.

  • start_time: when the span actually starts (defaults to now)

  • tags: extra tags which should be added to the span.



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/ddtrace/tracer.rb', line 195

def start_span(name, options = {})
  start_time = options.fetch(:start_time, Time.now.utc)
  tags = options.fetch(:tags, {})

  opts = options.select do |k, _v|
    # Filter options, we want no side effects with unexpected args.
    # Plus, this documents the code (Ruby 2 named args would be better but we're Ruby 1.9 compatible)
    [:service, :resource, :span_type].include?(k)
  end

  ctx, parent = guess_context_and_parent(options)
  opts[:context] = ctx unless ctx.nil?

  span = Span.new(self, name, opts)
  if parent.nil?
    # root span
    @sampler.sample(span)
    span.set_tag('system.pid', Process.pid)
  else
    # child span
    span.parent = parent # sets service, trace_id, parent_id, sampled
  end
  tags.each { |k, v| span.set_tag(k, v) } unless tags.empty?
  @tags.each { |k, v| span.set_tag(k, v) } unless @tags.empty?
  span.start_time = start_time

  # this could at some point be optional (start_active_span vs start_manual_span)
  ctx.add_span(span) unless ctx.nil?

  span
end