debian-mirror-gitlab/lib/gitlab/import/metrics.rb

87 lines
2.1 KiB
Ruby
Raw Normal View History

2020-04-22 19:07:51 +05:30
# frozen_string_literal: true
module Gitlab
module Import
2020-07-28 23:09:34 +05:30
class Metrics
2021-11-18 22:05:49 +05:30
include Gitlab::Utils::UsageData
2020-07-28 23:09:34 +05:30
IMPORT_DURATION_BUCKETS = [0.5, 1, 3, 5, 10, 60, 120, 240, 360, 720, 1440].freeze
2020-04-22 19:07:51 +05:30
2021-11-18 22:05:49 +05:30
attr_reader :importer, :duration
2020-04-22 19:07:51 +05:30
2020-07-28 23:09:34 +05:30
def initialize(importer, project)
@importer = importer
@project = project
end
2021-11-18 22:05:49 +05:30
def track_start_import
return unless project.github_import?
track_usage_event(:github_import_project_start, project.id)
end
2020-07-28 23:09:34 +05:30
def track_finished_import
2021-11-18 22:05:49 +05:30
@duration = Time.zone.now - project.created_at
2020-07-28 23:09:34 +05:30
2021-11-18 22:05:49 +05:30
observe_histogram
2020-07-28 23:09:34 +05:30
projects_counter.increment
2021-11-18 22:05:49 +05:30
track_finish_metric
2020-07-28 23:09:34 +05:30
end
2020-04-22 19:07:51 +05:30
2021-11-18 22:05:49 +05:30
def track_failed_import
return unless project.github_import?
track_usage_event(:github_import_project_failure, project.id)
2020-07-28 23:09:34 +05:30
end
def issues_counter
@issues_counter ||= Gitlab::Metrics.counter(
:"#{importer}_imported_issues_total",
'The number of imported issues'
)
end
2020-04-22 19:07:51 +05:30
2020-07-28 23:09:34 +05:30
def merge_requests_counter
@merge_requests_counter ||= Gitlab::Metrics.counter(
:"#{importer}_imported_merge_requests_total",
'The number of imported merge (pull) requests'
)
2020-04-22 19:07:51 +05:30
end
2020-07-28 23:09:34 +05:30
private
2021-11-18 22:05:49 +05:30
attr_reader :project
2020-07-28 23:09:34 +05:30
def duration_histogram
@duration_histogram ||= Gitlab::Metrics.histogram(
:"#{importer}_total_duration_seconds",
'Total time spent importing projects, in seconds',
{},
IMPORT_DURATION_BUCKETS
)
2020-04-22 19:07:51 +05:30
end
2021-11-18 22:05:49 +05:30
def projects_counter
@projects_counter ||= Gitlab::Metrics.counter(
:"#{importer}_imported_projects_total",
'The number of imported projects'
)
end
def observe_histogram
if project.github_import?
duration_histogram.observe({ project: project.full_path }, duration)
else
duration_histogram.observe({ importer: importer }, duration)
end
end
def track_finish_metric
return unless project.github_import?
track_usage_event(:github_import_project_success, project.id)
end
2020-04-22 19:07:51 +05:30
end
end
end