lib/ddtrace/tracer.rb in ddtrace-0.37.0 vs lib/ddtrace/tracer.rb in ddtrace-0.38.0

- old
+ new

@@ -210,12 +210,12 @@ end 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.set_tags(@tags) unless @tags.empty? + span.set_tags(tags) 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? @@ -253,13 +253,15 @@ # 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. + # If not set, defaults to Tracer.call_context # * +tags+: extra tags which should be added to the span. def trace(name, options = {}) - options[:child_of] = call_context + options[:child_of] ||= call_context # call the finish only if a block is given; this ensures # that a call to tracer.trace() without a block, returns # a span that should be manually finished. if block_given? @@ -267,14 +269,19 @@ return_value = nil begin begin span = start_span(name, options) - # rubocop:disable Lint/UselessAssignment rescue StandardError => e - Datadog.logger.debug('Failed to start span: #{e}') + Datadog.logger.debug("Failed to start span: #{e}") ensure - return_value = yield(span) + # We should yield to the provided block when possible, as this + # block is application code that we don't want to hinder. We call: + # * `yield(span)` during normal execution. + # * `yield(nil)` if `start_span` fails with a runtime error. + # * We don't yield during a fatal error, as the application is likely trying to + # end its execution (either due to a system error or graceful shutdown). + return_value = yield(span) if span || e.is_a?(StandardError) end # rubocop:disable Lint/RescueException # Here we really want to catch *any* exception, not only StandardError, # as we really have no clue of what is in the block, # and it is user code which should be executed no matter what.