lib/ddtrace/tracer.rb in ddtrace-0.13.2 vs lib/ddtrace/tracer.rb in ddtrace-0.14.0.beta1

- old
+ new

@@ -22,10 +22,11 @@ attr_reader :sampler, :services, :tags, :provider attr_accessor :enabled, :writer attr_writer :default_service ALLOWED_SPAN_OPTIONS = [:service, :resource, :span_type].freeze + DEFAULT_ON_ERROR = proc { |span, error| span.set_error(error) unless span.nil? } # Global, memoized, lazy initialized instance of a logger that is used within the the Datadog # namespace. This logger outputs to +STDOUT+ by default, and is considered thread-safe. def self.log unless defined? @logger @@ -275,31 +276,42 @@ # * +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) # * +tags+: extra tags which should be added to the span. def trace(name, options = {}) options[:child_of] = call_context - span = start_span(name, options) # 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? + span = nil + return_value = nil + begin - yield(span) + begin + span = start_span(name, options) + # rubocop:disable Lint/UselessAssignment + rescue StandardError => e + Datadog::Tracer.log.debug('Failed to start span: #{e}') + ensure + return_value = yield(span) + 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. # It's not a problem since we re-raise it afterwards so for example a # SignalException::Interrupt would still bubble up. rescue Exception => e - span.set_error(e) + (options[:on_error] || DEFAULT_ON_ERROR).call(span, e) raise e ensure - span.finish() + span.finish unless span.nil? end + + return_value else - span + start_span(name, options) end end # Record the given +context+. For compatibility with previous versions, # +context+ can also be a span. It is similar to the +child_of+ argument,