summaryrefslogtreecommitdiff
path: root/lib/gitlab/import/metrics.rb
blob: 7a0cf1682a6b40f6e9f8b23afc32e88de7bd6e5b (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
# frozen_string_literal: true

module Gitlab
  module Import
    class Metrics
      include Gitlab::Utils::UsageData

      IMPORT_DURATION_BUCKETS = [0.5, 1, 3, 5, 10, 60, 120, 240, 360, 720, 1440].freeze

      attr_reader :importer, :duration

      def initialize(importer, project)
        @importer = importer
        @project = project
      end

      def track_start_import
        return unless project.github_import?

        track_usage_event(:github_import_project_start, project.id)
      end

      def track_finished_import
        @duration = Time.zone.now - project.created_at

        observe_histogram
        projects_counter.increment
        track_finish_metric
      end

      def track_failed_import
        return unless project.github_import?

        track_usage_event(:github_import_project_failure, project.id)
      end

      def issues_counter
        @issues_counter ||= Gitlab::Metrics.counter(
          :"#{importer}_imported_issues_total",
          'The number of imported issues'
        )
      end

      def merge_requests_counter
        @merge_requests_counter ||= Gitlab::Metrics.counter(
          :"#{importer}_imported_merge_requests_total",
          'The number of imported merge (pull) requests'
        )
      end

      private

      attr_reader :project

      def duration_histogram
        @duration_histogram ||= Gitlab::Metrics.histogram(
          :"#{importer}_total_duration_seconds",
          'Total time spent importing projects, in seconds',
          {},
          IMPORT_DURATION_BUCKETS
        )
      end

      def projects_counter
        @projects_counter ||= Gitlab::Metrics.counter(
          :"#{importer}_imported_projects_total",
          'The number of imported projects'
        )
      end

      def observe_histogram
        duration_histogram.observe({ importer: importer }, duration)
      end

      def track_finish_metric
        return unless project.github_import?

        track_usage_event(:github_import_project_success, project.id)
      end
    end
  end
end