2021-02-05 07:09:31 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-12-16 16:08:00 -05:00
|
|
|
namespace :gitlab do
|
|
|
|
desc "GitLab | Generate Sample Prometheus Data"
|
|
|
|
task :generate_sample_prometheus_data, [:environment_id] => :gitlab_environment do |_, args|
|
|
|
|
environment = Environment.find(args[:environment_id])
|
|
|
|
metrics = PrometheusMetric.where(project_id: [environment.project.id, nil])
|
|
|
|
query_variables = Gitlab::Prometheus::QueryVariables.call(environment)
|
|
|
|
|
|
|
|
sample_metrics_directory_name = Metrics::SampleMetricsService::DIRECTORY
|
|
|
|
FileUtils.mkdir_p(sample_metrics_directory_name)
|
|
|
|
|
2019-12-19 13:07:48 -05:00
|
|
|
sample_metrics_intervals = [30.minutes, 180.minutes, 8.hours, 24.hours, 72.hours, 7.days]
|
|
|
|
|
2019-12-16 16:08:00 -05:00
|
|
|
metrics.each do |metric|
|
|
|
|
query = metric.query % query_variables
|
|
|
|
|
|
|
|
next unless metric.identifier
|
|
|
|
|
2019-12-19 13:07:48 -05:00
|
|
|
result = sample_metrics_intervals.each_with_object({}) do |interval, memo|
|
|
|
|
memo[interval.to_i / 60] = environment.prometheus_adapter.prometheus_client.query_range(query, start: interval.ago)
|
|
|
|
end
|
|
|
|
|
2019-12-16 16:08:00 -05:00
|
|
|
File.write("#{sample_metrics_directory_name}/#{metric.identifier}.yml", result.to_yaml)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|