Commit 41cc763b authored by Dmytro Zaporozhets's avatar Dmytro Zaporozhets

Merge branch '220144-substitute-variables-in-alerts' into 'master'

Use ProxyVariableSubstitutionService for variable substitution in alerts

See merge request gitlab-org/gitlab!33772
parents 65cc2d15 cb0c67f8
...@@ -132,19 +132,21 @@ module Clusters ...@@ -132,19 +132,21 @@ module Clusters
end end
def alerts(environment) def alerts(environment)
variables = Gitlab::Prometheus::QueryVariables.call(environment)
alerts = Projects::Prometheus::AlertsFinder alerts = Projects::Prometheus::AlertsFinder
.new(environment: environment) .new(environment: environment)
.execute .execute
alerts.map do |alert| alerts.map do |alert|
substitute_query_variables(alert.to_param, variables) hash = alert.to_param
hash['expr'] = substitute_query_variables(hash['expr'], environment)
hash
end end
end end
def substitute_query_variables(hash, variables) def substitute_query_variables(query, environment)
hash['expr'] %= variables result = ::Prometheus::ProxyVariableSubstitutionService.new(environment, query: query).execute
hash
result[:params][:query]
end end
def environments def environments
......
---
title: Fix bug with variable substitution in alerts
merge_request: 33772
author:
type: fixed
...@@ -90,23 +90,25 @@ describe Clusters::Applications::PrometheusConfigService do ...@@ -90,23 +90,25 @@ describe Clusters::Applications::PrometheusConfigService do
create(:prometheus_alert, create(:prometheus_alert,
project: project, project: project,
environment: production, environment: production,
prometheus_metric: metric) prometheus_metric: metric,
operator: PrometheusAlert.operators['gt'],
threshold: 0)
end end
let(:metric) do let(:metric) do
create(:prometheus_metric, query: query, project: project) create(:prometheus_metric, query: query, project: project)
end end
let(:query) { '%{ci_environment_slug}' } let(:query) { 'up{environment="{{ci_environment_slug}}"}' }
it 'substitutes query variables' do it 'substitutes query variables' do
expect(Gitlab::Prometheus::QueryVariables) expect(Gitlab::Prometheus::QueryVariables)
.to receive(:call) .to receive(:call)
.with(production) .with(production, start_time: nil, end_time: nil)
.and_call_original .and_call_original
expr = groups.dig(0, 'rules', 0, 'expr') expr = groups.dig(0, 'rules', 0, 'expr')
expect(expr).to include(production.name) expect(expr).to eq("up{environment=\"#{production.slug}\"} > 0.0")
end end
end end
...@@ -127,13 +129,15 @@ describe Clusters::Applications::PrometheusConfigService do ...@@ -127,13 +129,15 @@ describe Clusters::Applications::PrometheusConfigService do
end end
it 'substitutes query variables once per environment' do it 'substitutes query variables once per environment' do
allow(Gitlab::Prometheus::QueryVariables).to receive(:call).and_call_original
expect(Gitlab::Prometheus::QueryVariables) expect(Gitlab::Prometheus::QueryVariables)
.to receive(:call) .to receive(:call)
.with(production) .with(production, start_time: nil, end_time: nil)
expect(Gitlab::Prometheus::QueryVariables) expect(Gitlab::Prometheus::QueryVariables)
.to receive(:call) .to receive(:call)
.with(staging) .with(staging, start_time: nil, end_time: nil)
subject subject
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