Class: Datadog::ErrorTracking::Collector Private

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/error_tracking/collector.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

The Collector is in charge, for a SpanOperation of storing the span events created when an error is handled. Each SpanOperation has a Collector as soon as a span event is created and the Collector has the same life time as the SpanOp.

If an error is handled then rethrown, the SpanEvent corresponding to the error will be deleted. That is why we do not add directly the SpanEvent to the SpanOp.

API:

  • private

Constant Summary collapse

SPAN_EVENTS_LIMIT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

API:

  • private

100
LOCK =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

API:

  • private

Mutex.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCollector

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Collector.

API:

  • private



31
32
33
# File 'lib/datadog/error_tracking/collector.rb', line 31

def initialize
  @span_event_per_error = {}
end

Class Method Details

.after_stopObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Proc called when the span_operation :after_stop event is published

API:

  • private



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/datadog/error_tracking/collector.rb', line 19

def self.after_stop
  @after_stop ||= proc do |span_op, error|
    # if this proc is called, we are sure that span_op has a collector
    collector = span_op.get_collector_or_initialize
    # if an error exited the scope of the span, we delete the corresponding SpanEvent.
    collector.on_error(span_op, error) if error

    span_events = collector.span_events
    span_op.span_events.concat(span_events)
  end
end

Instance Method Details

#add_span_event(span_op, span_event, error) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/datadog/error_tracking/collector.rb', line 35

def add_span_event(span_op, span_event, error)
  # When this is the first time we add a span event for a span,
  # we suscribe to the :after_stop event
  if @span_event_per_error.empty?
    events = span_op.send(:events)
    events.after_stop.subscribe(&self.class.after_stop)

    # This tag is used by the Error Tracking product to report
    # the error in Error Tracking
    span_op.set_tag(Ext::SPAN_EVENTS_HAS_EXCEPTION, true)
  end
  # Set a limit to the number of span event we can store per SpanOp
  # If an error has been handled several times in the same span we can still
  # modify the event (even if the capacity is reached) in order to report
  # the information of the last rescue
  if @span_event_per_error.key?(error) || @span_event_per_error.length < SPAN_EVENTS_LIMIT
    @span_event_per_error[error] = span_event
  end
end

#on_error(span_op, error) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Up to ruby3.2, we are listening to :raise event. We need to ensure that an error exiting the scope of a span is not handled in a parent span. This function will propagate the span event to the parent span. If the error is not handled in the parent span, it will be deleted by design.

API:

  • private



59
60
61
# File 'lib/datadog/error_tracking/collector.rb', line 59

def on_error(_span_op, error)
  @span_event_per_error.delete(error)
end

#span_eventsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



82
83
84
# File 'lib/datadog/error_tracking/collector.rb', line 82

def span_events
  @span_event_per_error.values
end