Commit 5f445d90 authored by Peter Leitzen's avatar Peter Leitzen

Parametrize alerting rules with variables

parent 5c741fc8
...@@ -132,7 +132,7 @@ module Clusters ...@@ -132,7 +132,7 @@ module Clusters
@environments_with_alerts ||= @environments_with_alerts ||=
environments.each_with_object({}) do |environment, hsh| environments.each_with_object({}) do |environment, hsh|
name = rule_name(environment) name = rule_name(environment)
hsh[name] = environment.prometheus_alerts.map(&:to_param) hsh[name] = alerts(environment)
end end
end end
...@@ -140,6 +140,20 @@ module Clusters ...@@ -140,6 +140,20 @@ module Clusters
"#{environment.name}.rules" "#{environment.name}.rules"
end end
def alerts(environment)
ci_environment_slug = environment.slug
kube_namespace = environment.deployment_platform&.actual_namespace || ''
environment.prometheus_alerts.map do |alert|
alert.to_param.tap do |hash|
hash['expr'] %= {
ci_environment_slug: ci_environment_slug,
kube_namespace: kube_namespace
}
end
end
end
def environments def environments
project.environments_for_scope(cluster.environment_scope) project.environments_for_scope(cluster.environment_scope)
end end
......
---
title: Parameterize alerting rules with variables
merge_request: 8481
author:
type: fixed
...@@ -4,16 +4,20 @@ describe Clusters::Applications::PrometheusUpdateService do ...@@ -4,16 +4,20 @@ describe Clusters::Applications::PrometheusUpdateService do
describe '#execute' do describe '#execute' do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:environment) { create(:environment, project: project) } let(:environment) { create(:environment, project: project) }
let(:cluster) { create(:cluster, :with_installed_helm, projects: [project]) } let(:cluster) { create(:cluster, :provided_by_user, :with_installed_helm, projects: [project]) }
let(:application) { create(:clusters_applications_prometheus, :installed, cluster: cluster) } let(:application) { create(:clusters_applications_prometheus, :installed, cluster: cluster) }
let!(:get_command_values) { OpenStruct.new(data: OpenStruct.new('values.yaml': application.values)) } let!(:get_command_values) { OpenStruct.new(data: OpenStruct.new('values.yaml': application.values)) }
let!(:upgrade_command) { application.upgrade_command("") } let!(:upgrade_command) { application.upgrade_command("") }
let(:upgrade_values_yaml) { StringIO.new }
let(:upgrade_values) { YAML.safe_load(upgrade_values_yaml.string) }
let(:helm_client) { instance_double(::Gitlab::Kubernetes::Helm::Api) } let(:helm_client) { instance_double(::Gitlab::Kubernetes::Helm::Api) }
subject(:service) { described_class.new(application, project) } subject(:service) { described_class.new(application, project) }
before do before do
allow(service).to receive(:upgrade_command).and_return(upgrade_command) allow(service)
.to receive(:upgrade_command) { |values| upgrade_values_yaml.write(values) }
.and_return(upgrade_command)
allow(service).to receive(:helm_api).and_return(helm_client) allow(service).to receive(:helm_api).and_return(helm_client)
end end
...@@ -25,20 +29,45 @@ describe Clusters::Applications::PrometheusUpdateService do ...@@ -25,20 +29,45 @@ describe Clusters::Applications::PrometheusUpdateService do
end end
context 'when prometheus alerts exist' do context 'when prometheus alerts exist' do
it 'generates the alert manager values' do let(:metric) do
create(:prometheus_alert, project: project, environment: environment) create(:prometheus_metric,
project: project,
query: '{pod_name=~"^%{ci_environment_slug}",namespace="%{kube_namespace}"}')
end
expect(service).to receive(:generate_alert_manager).once let!(:alert) do
create(:prometheus_alert,
project: project,
environment: environment,
prometheus_metric: metric)
end
it 'generates the alert manager values' do
service.execute service.execute
expect(upgrade_values.dig('alertmanager', 'enabled')).to eq(true)
alertmanager = upgrade_values.dig('alertmanagerFiles', 'alertmanager.yml')
expect(alertmanager).not_to be_nil
expect(alertmanager.dig('receivers', 0, 'name')).to eq('gitlab')
expect(alertmanager.dig('route', 'receiver')).to eq('gitlab')
alerts = upgrade_values.dig('serverFiles', 'alerts', 'groups')
expect(alerts).not_to be_nil
expect(alerts.size).to eq(1)
expect(alerts.dig(0, 'name')).to eq("#{environment.name}.rules")
expect(alerts.dig(0, 'rules', 0, 'expr')).to include(
environment.slug, environment.deployment_platform.actual_namespace)
end end
end end
context 'when prometheus alerts do not exist' do context 'when prometheus alerts do not exist' do
it 'resets the alert manager values' do it 'resets the alert manager values' do
expect(service).to receive(:reset_alert_manager).once
service.execute service.execute
expect(upgrade_values.dig('alertmanager', 'enabled')).to eq(false)
expect(upgrade_values).not_to include('alertmanagerFiles')
expect(upgrade_values.dig('serverFiles', 'alerts')).to eq({})
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