Commit 71b97fb2 authored by Kirstie Cook's avatar Kirstie Cook Committed by Rémy Coutable

Add range to sample prometheus data

Add sample prometheus data fixture
parent 9a6f5e58
......@@ -2,7 +2,7 @@
class Projects::Environments::SampleMetricsController < Projects::ApplicationController
def query
result = Metrics::SampleMetricsService.new(params[:identifier]).query
result = Metrics::SampleMetricsService.new(params[:identifier], range_start: params[:start], range_end: params[:end]).query
if result
render json: { "status": "success", "data": { "resultType": "matrix", "result": result } }
......
......@@ -4,16 +4,17 @@ module Metrics
class SampleMetricsService
DIRECTORY = "sample_metrics"
attr_reader :identifier
attr_reader :identifier, :range_minutes
def initialize(identifier)
def initialize(identifier, range_start:, range_end:)
@identifier = identifier
@range_minutes = convert_range_minutes(range_start, range_end)
end
def query
return unless identifier && File.exist?(file_location)
YAML.load_file(File.expand_path(file_location, __dir__))
query_interval
end
private
......@@ -22,5 +23,14 @@ module Metrics
sanitized_string = identifier.gsub(/[^0-9A-Za-z_]/, '')
File.join(Rails.root, DIRECTORY, "#{sanitized_string}.yml")
end
def query_interval
result = YAML.load_file(File.expand_path(file_location, __dir__))
result[range_minutes]
end
def convert_range_minutes(range_start, range_end)
((range_end.to_time - range_start.to_time) / 1.minute).to_i
end
end
end
---
title: Generate Prometheus sample metrics over pre-set intervals
merge_request: 22066
author:
type: added
# Generate Sample Prometheus Data
This command will run Prometheus queries for each of the metrics of a specific environment
for a default time interval of 7 days ago to now. The results of each of query are stored
under a `sample_metrics` directory as a yaml file named by the metric's `identifier`.
When the environmental variable `USE_SAMPLE_METRICS` is set, the Prometheus API query is
re-routed to `Projects::Environments::SampleMetricsController` which loads the appropriate
data set if it is present within the `sample_metrics` directory.
for a series of time intervals: 30 minutes, 3 hours, 8 hours, 24 hours, 72 hours, and 7 days
to now. The results of each of query are stored under a `sample_metrics` directory as a yaml
file named by the metric's `identifier`. When the environmental variable `USE_SAMPLE_METRICS`
is set, the Prometheus API query is re-routed to `Projects::Environments::SampleMetricsController`
which loads the appropriate data set if it is present within the `sample_metrics` directory.
- This command requires an id from an Environment with an available Prometheus installation.
......
......@@ -8,12 +8,17 @@ namespace :gitlab do
sample_metrics_directory_name = Metrics::SampleMetricsService::DIRECTORY
FileUtils.mkdir_p(sample_metrics_directory_name)
sample_metrics_intervals = [30.minutes, 180.minutes, 8.hours, 24.hours, 72.hours, 7.days]
metrics.each do |metric|
query = metric.query % query_variables
result = environment.prometheus_adapter.prometheus_client.query_range(query, start: 7.days.ago)
next unless metric.identifier
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
File.write("#{sample_metrics_directory_name}/#{metric.identifier}.yml", result.to_yaml)
end
end
......
......@@ -58,7 +58,9 @@ describe Projects::Environments::SampleMetricsController do
id: environment.id.to_s,
namespace_id: project.namespace.full_path,
project_id: project.name,
identifier: 'sample_metric_query_result'
identifier: 'sample_metric_query_result',
start: '2019-12-02T23:31:45.000Z',
end: '2019-12-03T00:01:45.000Z'
}.merge(params)
end
end
---
30:
- metric: {}
values:
- - 1573560714.209
- '0.02361297607421875'
- - 1573560774.209
- '0.02361297607421875'
- - 1573560834.209
- '0.02362823486328125'
- - 1573560894.209
- '0.02361297607421875'
- - 1573560954.209
- '0.02385711669921875'
- - 1573561014.209
- '0.02361297607421875'
- - 1573561074.209
- '0.02361297607421875'
- - 1573561134.209
- '0.02362060546875'
- - 1573561194.209
- '0.02362060546875'
- - 1573561254.209
- '0.02362060546875'
- - 1573561314.209
- '0.02362060546875'
- - 1573561374.209
- '0.023624420166015625'
- - 1573561434.209
- '0.023651123046875'
- - 1573561494.209
- '0.02362060546875'
- - 1573561554.209
- '0.0236358642578125'
- - 1573561614.209
- '0.02362060546875'
- - 1573561674.209
- '0.02362060546875'
- - 1573561734.209
- '0.02362060546875'
- - 1573561794.209
- '0.02362060546875'
- - 1573561854.209
- '0.02362060546875'
- - 1573561914.209
- '0.023651123046875'
- - 1573561974.209
- '0.02362060546875'
- - 1573562034.209
- '0.02362060546875'
- - 1573562094.209
- '0.02362060546875'
- - 1573562154.209
- '0.02362060546875'
- - 1573562214.209
- '0.023624420166015625'
- - 1573562274.209
- '0.02362060546875'
- - 1573562334.209
- '0.023868560791015625'
- - 1573562394.209
- '0.02374267578125'
- - 1573562454.209
- '0.02362060546875'
- - 1573562514.209
- '0.02362060546875'
- - 1573562574.209
- '0.02362060546875'
- - 1573562634.209
- '0.02362060546875'
- - 1573562694.209
- '0.023639678955078125'
- - 1573562754.209
- '0.0236358642578125'
- - 1573562814.209
- '0.02362060546875'
- - 1573562874.209
- '0.0236358642578125'
- - 1573562934.209
- '0.023651123046875'
- - 1573562994.209
- '0.02362060546875'
- - 1573563054.209
- '0.023624420166015625'
- - 1573563114.209
- '0.02362060546875'
- - 1573563174.209
- '0.02362060546875'
- - 1573563234.209
- '0.02362060546875'
- - 1573563294.209
- '0.02362060546875'
- - 1573563354.209
- '0.02362060546875'
- - 1573563414.209
- '0.023651123046875'
- - 1573563474.209
- '0.023651123046875'
- - 1573563534.209
- '0.023651123046875'
- - 1573563594.209
- '0.023773193359375'
- - 1573563654.209
- '0.023681640625'
- - 1573563714.209
- '0.023895263671875'
- - 1573563774.209
- '0.023651123046875'
- - 1573563834.209
- '0.023651123046875'
- - 1573563894.209
- '0.023651123046875'
- - 1573563954.209
- '0.0236663818359375'
- - 1573564014.209
- '0.023651123046875'
- - 1573564074.209
- '0.023681640625'
- - 1573564134.209
- '0.0236663818359375'
- - 1573564194.209
- '0.0236663818359375'
- - 1573564254.209
- '0.023651123046875'
- - 1573564314.209
- '0.023651123046875'
- - 1573564374.209
- '0.023651123046875'
- - 1573564434.209
- '0.023773193359375'
- - 1573564494.209
- '0.023651123046875'
- - 1573564554.209
- '0.023681640625'
- - 1573564614.209
- '0.023773193359375'
- - 1573564674.209
- '0.023651123046875'
- - 1573564734.209
- '0.023651123046875'
- - 1573564794.209
- '0.023651123046875'
- - 1573564854.209
- '0.023651123046875'
- - 1573564914.209
- '0.023651123046875'
- - 1573564974.209
- '0.023651123046875'
- - 1573565034.209
- '0.023651123046875'
- - 1573565094.209
- '0.023895263671875'
\ No newline at end of file
- - 1576719533.248
- '0.0006172414678571515'
- - 1576719593.248
- '0.0006189408976190352'
- - 1576719653.248
- '0.0006182154988094691'
- - 1576719713.248
- '0.0006194998404763076'
- - 1576719773.248
- '0.0006194687678569856'
- - 1576719833.248
- '0.0006171203535713976'
- - 1576719893.248
- '0.0006244061773808577'
- - 1576719953.248
- '0.0006170288511561634'
- - 1576720013.248
- '0.0006243750281248557'
- - 1576720073.248
- '0.0006152456571427256'
- - 1576720133.248
- '0.0006215679095237733'
- - 1576720193.248
- '0.0006218523571429083'
- - 1576720253.248
- '0.0006200312440475792'
- - 1576720313.248
- '0.0006214166202382676'
- - 1576720373.248
- '0.0006152486976191084'
- - 1576720433.248
- '0.0006136406750000235'
- - 1576720493.248
- '0.0006135999154761997'
- - 1576720553.248
- '0.0006126559190475756'
- - 1576720613.248
- '0.0006153160392857769'
- - 1576720673.248
- '0.0006146447178572262'
- - 1576720733.248
- '0.0006146970476189988'
- - 1576720793.248
- '0.0006219259035715042'
- - 1576720853.248
- '0.0006111198750001481'
- - 1576720913.248
- '0.0006169941035715337'
- - 1576720973.248
- '0.0006102626761905379'
- - 1576721033.248
- '0.0006163839964285346'
180:
- metric: {}
values:
- - 1576719533.248
- '0.0006172414678571515'
- - 1576719593.248
- '0.0006189408976190352'
- - 1576719653.248
- '0.0006182154988094691'
- - 1576719713.248
- '0.0006194998404763076'
- - 1576719773.248
- '0.0006194687678569856'
- - 1576719833.248
- '0.0006171203535713976'
- - 1576719893.248
- '0.0006244061773808577'
- - 1576719953.248
- '0.0006170288511561634'
- - 1576720013.248
- '0.0006243750281248557'
- - 1576720073.248
- '0.0006152456571427256'
- - 1576720133.248
- '0.0006215679095237733'
- - 1576720193.248
- '0.0006218523571429083'
- - 1576720253.248
- '0.0006200312440475792'
- - 1576720313.248
- '0.0006214166202382676'
- - 1576720373.248
- '0.0006152486976191084'
- - 1576720433.248
- '0.0006136406750000235'
- - 1576720493.248
- '0.0006135999154761997'
- - 1576720553.248
- '0.0006126559190475756'
- - 1576720613.248
- '0.0006153160392857769'
- - 1576720673.248
- '0.0006146447178572262'
- - 1576720733.248
- '0.0006146970476189988'
- - 1576720793.248
- '0.0006219259035715042'
- - 1576720853.248
- '0.0006111198750001481'
- - 1576720913.248
- '0.0006169941035715337'
- - 1576720973.248
- '0.0006102626761905379'
- - 1576721033.248
- '0.0006163839964285346'
480:
- metric: {}
values:
- - 1576719533.248
- '0.0006172414678571515'
- - 1576719593.248
- '0.0006189408976190352'
- - 1576719653.248
- '0.0006182154988094691'
- - 1576719713.248
- '0.0006194998404763076'
- - 1576719773.248
- '0.0006194687678569856'
- - 1576719833.248
- '0.0006171203535713976'
- - 1576719893.248
- '0.0006244061773808577'
- - 1576719953.248
- '0.0006170288511561634'
- - 1576720013.248
- '0.0006243750281248557'
- - 1576720073.248
- '0.0006152456571427256'
- - 1576720133.248
- '0.0006215679095237733'
- - 1576720193.248
- '0.0006218523571429083'
- - 1576720253.248
- '0.0006200312440475792'
- - 1576720313.248
- '0.0006214166202382676'
- - 1576720373.248
- '0.0006152486976191084'
- - 1576720433.248
- '0.0006136406750000235'
- - 1576720493.248
- '0.0006135999154761997'
- - 1576720553.248
- '0.0006126559190475756'
- - 1576720613.248
- '0.0006153160392857769'
- - 1576720673.248
- '0.0006146447178572262'
- - 1576720733.248
- '0.0006146970476189988'
- - 1576720793.248
- '0.0006219259035715042'
- - 1576720853.248
- '0.0006111198750001481'
- - 1576720913.248
- '0.0006169941035715337'
- - 1576720973.248
- '0.0006102626761905379'
- - 1576721033.248
- '0.0006163839964285346'
1440:
- metric: {}
values:
- - 1576719533.248
- '0.0006172414678571515'
- - 1576719593.248
- '0.0006189408976190352'
- - 1576719653.248
- '0.0006182154988094691'
- - 1576719713.248
- '0.0006194998404763076'
- - 1576719773.248
- '0.0006194687678569856'
- - 1576719833.248
- '0.0006171203535713976'
- - 1576719893.248
- '0.0006244061773808577'
- - 1576719953.248
- '0.0006170288511561634'
- - 1576720013.248
- '0.0006243750281248557'
- - 1576720073.248
- '0.0006152456571427256'
- - 1576720133.248
- '0.0006215679095237733'
- - 1576720193.248
- '0.0006218523571429083'
- - 1576720253.248
- '0.0006200312440475792'
- - 1576720313.248
- '0.0006214166202382676'
- - 1576720373.248
- '0.0006152486976191084'
- - 1576720433.248
- '0.0006136406750000235'
- - 1576720493.248
- '0.0006135999154761997'
- - 1576720553.248
- '0.0006126559190475756'
- - 1576720613.248
- '0.0006153160392857769'
- - 1576720673.248
- '0.0006146447178572262'
- - 1576720733.248
- '0.0006146970476189988'
- - 1576720793.248
- '0.0006219259035715042'
- - 1576720853.248
- '0.0006111198750001481'
- - 1576720913.248
- '0.0006169941035715337'
- - 1576720973.248
- '0.0006102626761905379'
- - 1576721033.248
- '0.0006163839964285346'
4320:
- metric: {}
values:
- - 1576719533.248
- '0.0006172414678571515'
- - 1576719593.248
- '0.0006189408976190352'
- - 1576719653.248
- '0.0006182154988094691'
- - 1576719713.248
- '0.0006194998404763076'
- - 1576719773.248
- '0.0006194687678569856'
- - 1576719833.248
- '0.0006171203535713976'
- - 1576719893.248
- '0.0006244061773808577'
- - 1576719953.248
- '0.0006170288511561634'
- - 1576720013.248
- '0.0006243750281248557'
- - 1576720073.248
- '0.0006152456571427256'
- - 1576720133.248
- '0.0006215679095237733'
- - 1576720193.248
- '0.0006218523571429083'
- - 1576720253.248
- '0.0006200312440475792'
- - 1576720313.248
- '0.0006214166202382676'
- - 1576720373.248
- '0.0006152486976191084'
- - 1576720433.248
- '0.0006136406750000235'
- - 1576720493.248
- '0.0006135999154761997'
- - 1576720553.248
- '0.0006126559190475756'
- - 1576720613.248
- '0.0006153160392857769'
- - 1576720673.248
- '0.0006146447178572262'
- - 1576720733.248
- '0.0006146970476189988'
- - 1576720793.248
- '0.0006219259035715042'
- - 1576720853.248
- '0.0006111198750001481'
- - 1576720913.248
- '0.0006169941035715337'
- - 1576720973.248
- '0.0006102626761905379'
- - 1576721033.248
- '0.0006163839964285346'
10080:
- metric: {}
values:
- - 1576719533.248
- '0.0006172414678571515'
- - 1576719593.248
- '0.0006189408976190352'
- - 1576719653.248
- '0.0006182154988094691'
- - 1576719713.248
- '0.0006194998404763076'
- - 1576719773.248
- '0.0006194687678569856'
- - 1576719833.248
- '0.0006171203535713976'
- - 1576719893.248
- '0.0006244061773808577'
- - 1576719953.248
- '0.0006170288511561634'
- - 1576720013.248
- '0.0006243750281248557'
- - 1576720073.248
- '0.0006152456571427256'
- - 1576720133.248
- '0.0006215679095237733'
- - 1576720193.248
- '0.0006218523571429083'
- - 1576720253.248
- '0.0006200312440475792'
- - 1576720313.248
- '0.0006214166202382676'
- - 1576720373.248
- '0.0006152486976191084'
- - 1576720433.248
- '0.0006136406750000235'
- - 1576720493.248
- '0.0006135999154761997'
- - 1576720553.248
- '0.0006126559190475756'
- - 1576720613.248
- '0.0006153160392857769'
- - 1576720673.248
- '0.0006146447178572262'
- - 1576720733.248
- '0.0006146970476189988'
- - 1576720793.248
- '0.0006219259035715042'
- - 1576720853.248
- '0.0006111198750001481'
- - 1576720913.248
- '0.0006169941035715337'
- - 1576720973.248
- '0.0006102626761905379'
- - 1576721033.248
- '0.0006163839964285346'
\ No newline at end of file
......@@ -4,7 +4,10 @@ require 'spec_helper'
describe Metrics::SampleMetricsService do
describe 'query' do
subject { described_class.new(identifier).query }
let(:range_start) { '2019-12-02T23:31:45.000Z' }
let(:range_end) { '2019-12-03T00:01:45.000Z' }
subject { described_class.new(identifier, range_start: range_start, range_end: range_end).query }
context 'when the file is not found' do
let(:identifier) { nil }
......@@ -26,10 +29,10 @@ describe Metrics::SampleMetricsService do
FileUtils.rm(destination)
end
subject { described_class.new(identifier).query }
subject { described_class.new(identifier, range_start: range_start, range_end: range_end).query }
it 'loads data from the sample file correctly' do
expect(subject).to eq(YAML.load_file(source))
expect(subject).to eq(YAML.load_file(source)[30])
end
end
......
......@@ -17,7 +17,7 @@ describe 'gitlab:generate_sample_prometheus_data rake task' do
it 'creates the file correctly' do
Rake.application.rake_require 'tasks/gitlab/generate_sample_prometheus_data'
allow(Environment).to receive(:find).and_return(environment)
allow(environment).to receive_message_chain(:prometheus_adapter, :prometheus_client, :query_range) { sample_query_result }
allow(environment).to receive_message_chain(:prometheus_adapter, :prometheus_client, :query_range) { sample_query_result[30] }
run_rake_task('gitlab:generate_sample_prometheus_data', [environment.id])
expect(File.exist?(sample_query_file)).to be true
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment