Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
71b97fb2
Commit
71b97fb2
authored
Dec 19, 2019
by
Kirstie Cook
Committed by
Rémy Coutable
Dec 19, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add range to sample prometheus data
Add sample prometheus data fixture
parent
9a6f5e58
Changes
9
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
369 additions
and
163 deletions
+369
-163
app/controllers/projects/environments/sample_metrics_controller.rb
...ollers/projects/environments/sample_metrics_controller.rb
+1
-1
app/services/metrics/sample_metrics_service.rb
app/services/metrics/sample_metrics_service.rb
+13
-3
changelogs/unreleased/generate-sample-metrics-intervals.yml
changelogs/unreleased/generate-sample-metrics-intervals.yml
+5
-0
doc/raketasks/generate_sample_prometheus_data.md
doc/raketasks/generate_sample_prometheus_data.md
+5
-5
lib/tasks/gitlab/generate_sample_prometheus_data.rake
lib/tasks/gitlab/generate_sample_prometheus_data.rake
+6
-1
spec/controllers/projects/environments/sample_metrics_controller_spec.rb
...s/projects/environments/sample_metrics_controller_spec.rb
+3
-1
spec/fixtures/gitlab/sample_metrics/sample_metric_query_result.yml
...ures/gitlab/sample_metrics/sample_metric_query_result.yml
+329
-148
spec/services/metrics/sample_metrics_service_spec.rb
spec/services/metrics/sample_metrics_service_spec.rb
+6
-3
spec/tasks/gitlab/generate_sample_prometheus_data_spec.rb
spec/tasks/gitlab/generate_sample_prometheus_data_spec.rb
+1
-1
No files found.
app/controllers/projects/environments/sample_metrics_controller.rb
View file @
71b97fb2
...
...
@@ -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
}
}
...
...
app/services/metrics/sample_metrics_service.rb
View file @
71b97fb2
...
...
@@ -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
changelogs/unreleased/generate-sample-metrics-intervals.yml
0 → 100644
View file @
71b97fb2
---
title
:
Generate Prometheus sample metrics over pre-set intervals
merge_request
:
22066
author
:
type
:
added
doc/raketasks/generate_sample_prometheus_data.md
View file @
71b97fb2
# 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.
...
...
lib/tasks/gitlab/generate_sample_prometheus_data.rake
View file @
71b97fb2
...
...
@@ -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
...
...
spec/controllers/projects/environments/sample_metrics_controller_spec.rb
View file @
71b97fb2
...
...
@@ -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
spec/fixtures/gitlab/sample_metrics/sample_metric_query_result.yml
View file @
71b97fb2
This diff is collapsed.
Click to expand it.
spec/services/metrics/sample_metrics_service_spec.rb
View file @
71b97fb2
...
...
@@ -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
...
...
spec/tasks/gitlab/generate_sample_prometheus_data_spec.rb
View file @
71b97fb2
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment