Welcome to mirror list, hosted at ThFree Co, Russian Federation.

error_repository.rb « error_tracking « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fd2467add202b43498e413f72d63a0a227870d4e (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# frozen_string_literal: true

module Gitlab
  module ErrorTracking
    # Data access layer for errors and events related to Error Tracking feature.
    class ErrorRepository
      Pagination = Struct.new(:next, :prev)

      # Generic database error
      DatabaseError = Class.new(StandardError)
      # Record was invalid
      RecordInvalidError = Class.new(DatabaseError)

      # Builds an instance of error repository backed by a strategy.
      #
      # @return [self]
      def self.build(project)
        strategy =
          if Feature.enabled?(:use_click_house_database_for_error_tracking, project)
            OpenApiStrategy.new(project)
          else
            ActiveRecordStrategy.new(project)
          end

        new(strategy)
      end

      # @private
      def initialize(strategy)
        @strategy = strategy
      end

      # Stores an error and the related error event.
      #
      # @param name [String] name of the error
      # @param description [String] description of the error
      # @param actor [String] culprit (class/method/function) which triggered this error
      # @param platform [String] platform on which the error occurred
      # @param environment [String] environment on which the error occurred
      # @param level [String] severity of this error
      # @param occurred_at [Time] timestamp when the error occurred
      # @param payload [Hash] original error payload
      #
      # @return [void] nothing
      #
      # @raise [RecordInvalidError] if passed attributes were invalid to store an error or error event
      # @raise [DatabaseError] if generic error occurred
      def report_error(
        name:, description:, actor:, platform:,
        environment:, level:, occurred_at: Time.zone.now, payload: {}
      )
        strategy.report_error(
          name: name,
          description: description,
          actor: actor,
          platform: platform,
          environment: environment,
          level: level,
          occurred_at: occurred_at,
          payload: payload
        )

        nil
      end

      # Finds an error by +id+.
      #
      # @param id [Integer, String] unique error identifier
      #
      # @return [Gitlab::ErrorTracking::DetailedError] a detail error
      def find_error(id)
        strategy.find_error(id)
      end

      # Lists errors.
      #
      # @param sort [String] order list by 'first_seen', 'last_seen', or 'frequency'
      # @param filters [Hash<Symbol, String>] filter list by
      # @option filters [String] :status error status
      # @params query [String, nil] free text search
      # @param limit [Integer, String] limit result
      # @param cursor [Hash] pagination information
      #
      # @return [Array<Array<Gitlab::ErrorTracking::Error>, Pagination>]
      def list_errors(sort: 'last_seen', filters: {}, query: nil, limit: 20, cursor: {})
        limit = [limit.to_i, 100].min

        strategy.list_errors(filters: filters, query: query, sort: sort, limit: limit, cursor: cursor)
      end

      # Fetches last event for error +id+.
      #
      # @param id [Integer, String] unique error identifier
      #
      # @return [Gitlab::ErrorTracking::ErrorEvent]
      #
      # @raise [DatabaseError] if generic error occurred
      def last_event_for(id)
        strategy.last_event_for(id)
      end

      # Updates attributes of an error.
      #
      # @param id [Integer, String] unique error identifier
      # @param status [String] error status
      #
      # @return [true, false] if update was successful
      #
      # @raise [DatabaseError] if generic error occurred
      def update_error(id, status:)
        strategy.update_error(id, status: status)
      end

      def dsn_url(public_key)
        strategy.dsn_url(public_key)
      end

      private

      attr_reader :strategy
    end
  end
end