summaryrefslogtreecommitdiff
path: root/lib/gitlab/error_tracking/processor/grpc_error_processor.rb
blob: 045a18f4110481ab08e72b5a2d2c2592954453b5 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# frozen_string_literal: true

module Gitlab
  module ErrorTracking
    module Processor
      module GrpcErrorProcessor
        DEBUG_ERROR_STRING_REGEX = RE2('(.*) debug_error_string:(.*)')

        class << self
          def call(event)
            process_first_exception_value(event)
            process_custom_fingerprint(event)

            event
          end

          # Sentry can report multiple exceptions in an event. Sanitize
          # only the first one since that's what is used for grouping.
          def process_first_exception_value(event)
            # Better in new version, will be event.exception.values
            exceptions = extract_exceptions_from(event)

            return unless exceptions.is_a?(Array)

            exception = exceptions.first

            return unless valid_exception?(exception)

            raw_message = exception.value

            return unless exception.type&.start_with?('GRPC::')
            return unless raw_message.present?

            message, debug_str = split_debug_error_string(raw_message)

            # Worse in new version, no setter! Have to poke at the
            # instance variable
            if message.present?
              exceptions.each do |exception|
                next unless valid_exception?(exception)

                if exception.respond_to?(:value=)
                  exception.value = message
                else
                  exception.instance_variable_set(:@value, message)
                end
              end
            end

            event.extra[:grpc_debug_error_string] = debug_str if debug_str
          end

          def process_custom_fingerprint(event)
            fingerprint = event.fingerprint

            return event unless custom_grpc_fingerprint?(fingerprint)

            message, _ = split_debug_error_string(fingerprint[1])
            fingerprint[1] = message if message
          end

          private

          def extract_exceptions_from(event)
            if event.is_a?(Raven::Event)
              event.instance_variable_get(:@interfaces)[:exception]&.values
            else
              event.exception&.instance_variable_get(:@values)
            end
          end

          def custom_grpc_fingerprint?(fingerprint)
            fingerprint.is_a?(Array) && fingerprint.length == 2 && fingerprint[0].start_with?('GRPC::')
          end

          def split_debug_error_string(message)
            return unless message

            match = DEBUG_ERROR_STRING_REGEX.match(message)

            return unless match

            [match[1], match[2]]
          end

          def valid_exception?(exception)
            case exception
            when Raven::SingleExceptionInterface, Sentry::SingleExceptionInterface
              exception&.value
            else
              false
            end
          end
        end
      end
    end
  end
end