summaryrefslogtreecommitdiff
path: root/app/services/error_tracking/collect_error_service.rb
blob: bc1f238d81ff701afab49fc06175dadb8e27e418 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true

module ErrorTracking
  class CollectErrorService < ::BaseService
    def execute
      # Error is a way to group events based on common data like name or cause
      # of exception. We need to keep a sane balance here between taking too little
      # and too much data into group logic.
      error = project.error_tracking_errors.report_error(
        name: exception['type'], # Example: ActionView::MissingTemplate
        description: exception['value'], # Example: Missing template posts/show in...
        actor: event['transaction'], # Example: PostsController#show
        platform: event['platform'], # Example: ruby
        timestamp: event['timestamp']
      )

      # The payload field contains all the data on error including stacktrace in jsonb.
      # Together with occured_at these are 2 main attributes that we need to save here.
      error.events.create!(
        environment: event['environment'],
        description: exception['type'],
        level: event['level'],
        occurred_at: event['timestamp'],
        payload: event
      )
    end

    private

    def event
      params[:event]
    end

    def exception
      event['exception']['values'].first
    end
  end
end