Commit 460e72c2 authored by rpereira2's avatar rpereira2

Add built in variable called __range

- This variable can be used in a Prometheus query, when you want a value
equalling the range of the dashboard/chart.

- If the range of the dashboard is 8 hours, '28800s' will be
substituted in place of __range.
parent 1b3d3066
......@@ -58,7 +58,7 @@ module Prometheus
def substitute_variables(result)
return success(result) unless query(result)
result[:params][:query] = gsub(query(result), full_context)
result[:params][:query] = gsub(query(result), full_context(result))
success(result)
end
......@@ -75,12 +75,16 @@ module Prometheus
end
end
def predefined_context
Gitlab::Prometheus::QueryVariables.call(@environment).stringify_keys
def predefined_context(result)
Gitlab::Prometheus::QueryVariables.call(
@environment,
start_time: start_timestamp(result),
end_time: end_timestamp(result)
).stringify_keys
end
def full_context
@full_context ||= predefined_context.reverse_merge(variables_hash)
def full_context(result)
@full_context ||= predefined_context(result).reverse_merge(variables_hash)
end
def variables
......@@ -91,6 +95,16 @@ module Prometheus
variables.to_h
end
def start_timestamp(result)
Time.rfc3339(result[:params][:start])
rescue ArgumentError
end
def end_timestamp(result)
Time.rfc3339(result[:params][:end])
rescue ArgumentError
end
def query(result)
result[:params][:query]
end
......
---
title: Provide `__range` variable for Prometheus queries
merge_request: 33521
author:
type: added
......@@ -3,8 +3,10 @@
module Gitlab
module Prometheus
module QueryVariables
def self.call(environment)
# start_time and end_time should be Time objects.
def self.call(environment, start_time: nil, end_time: nil)
{
__range: range(start_time, end_time),
ci_environment_slug: environment.slug,
kube_namespace: environment.deployment_namespace || '',
environment_filter: %{container_name!="POD",environment="#{environment.slug}"},
......@@ -14,6 +16,16 @@ module Gitlab
ci_environment_name: environment.name
}
end
private
def self.range(start_time, end_time)
if start_time && end_time
range_seconds = (end_time - start_time).to_i
"#{range_seconds}s"
end
end
private_class_method :range
end
end
end
......@@ -7,8 +7,9 @@ describe Gitlab::Prometheus::QueryVariables do
let(:project) { environment.project }
let(:environment) { create(:environment) }
let(:slug) { environment.slug }
let(:params) { {} }
subject { described_class.call(environment) }
subject { described_class.call(environment, params) }
it { is_expected.to include(ci_environment_slug: slug) }
it { is_expected.to include(ci_project_name: project.name) }
......@@ -53,5 +54,42 @@ describe Gitlab::Prometheus::QueryVariables do
it { is_expected.to include(kube_namespace: kube_namespace) }
end
end
context '__range' do
context 'when start_time and end_time are present' do
let(:params) do
{
start_time: Time.rfc3339('2020-05-29T07:23:05.008Z'),
end_time: Time.rfc3339('2020-05-29T15:23:05.008Z')
}
end
it { is_expected.to include(__range: "#{8.hours.to_i}s") }
end
context 'when start_time and end_time are not present' do
it { is_expected.to include(__range: nil) }
end
context 'when end_time is not present' do
let(:params) do
{
start_time: Time.rfc3339('2020-05-29T07:23:05.008Z')
}
end
it { is_expected.to include(__range: nil) }
end
context 'when start_time is not present' do
let(:params) do
{
end_time: Time.rfc3339('2020-05-29T07:23:05.008Z')
}
end
it { is_expected.to include(__range: nil) }
end
end
end
end
......@@ -186,5 +186,19 @@ describe Prometheus::ProxyVariableSubstitutionService do
end
end
end
context '__range' do
let(:params_keys) do
{
query: 'topk(5, sum by (method) (rate(rest_client_requests_total[{{__range}}])))',
start_time: '2020-05-29T08:19:07.142Z',
end_time: '2020-05-29T16:19:07.142Z'
}
end
it_behaves_like 'success' do
let(:expected_query) { "topk(5, sum by (method) (rate(rest_client_requests_total[#{8.hours.to_i}s])))" }
end
end
end
end
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