require 'raven/event'
require 'raven/interfaces/stack_trace'


module Cardinal
  class Event < Raven::Event
    def stack_trace_interface
      :cardinal_stacktrace
    end

    def parse_backtrace_line(line, frame)
      frame = super
      frame.context_type = if Rails.backtrace_cleaner.clean([line]).empty?
                             'framework'
                           else
                             'application'
                           end
      return frame
    end
  end

  class StacktraceInterface < Raven::StacktraceInterface
    name 'sentry_cardinal.interfaces.Stacktrace'

    def frame(attributes=nil, &block)
      Frame.new(attributes, &block)
    end

    class Frame < Raven::StacktraceInterface::Frame
      property :context_type, :default => 'framework'
    end
  end

  Raven.register_interface :cardinal_stacktrace => StacktraceInterface
end

require 'raven/event'
require 'raven/interfaces/stack_trace'


module Cardinal
  class Event < Raven::Event
    def stack_trace_interface
      :cardinal_stacktrace
    end

    def parse_backtrace_line(line, frame)
      frame = super
      frame.context_type = if Rails.backtrace_cleaner.clean([line]).empty?
                             'framework'
                           else
                             'application'
                           end
      frame.cleaned_function = frame.function.gsub(/\d+/, 'N')
      return frame
    end
  end

  class StacktraceInterface < Raven::StacktraceInterface
    name 'sentry_cardinal.interfaces.Stacktrace'

    def frame(attributes=nil, &block)
      Frame.new(attributes, &block)
    end

    class Frame < Raven::StacktraceInterface::Frame
      property :context_type, :default => 'framework'
      property :cleaned_function
    end
  end

  Raven.register_interface :cardinal_stacktrace => StacktraceInterface
end

Send `cleaned_function` attributes for backtrace frames
Send `cleaned_function` attributes for backtrace frames

Rails frequently includes frames with gross dynamic function names. One
of the most frequent offenders are callbacks, which take the form:

    _run__4437211773795971890__process_action__1264216456344873512__callbacks

This is breaking sentry's exception grouping. Let's send a
'cleaned_function' attribute in which we replace all integer substrings
with the constant 'N'. This may be problematic for applications in which
there are functions whose names differ only by an integer; that seems
like a rare enough edge case to ignore for now. If it becomes
problematic later we can write a more clever replacement algorithm.
