Skip to content

Commit

Permalink
FEATURE: Add firing count to topic titles, and bump when count changes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaylorhq committed Feb 13, 2019
1 parent bdf25d8 commit 78b2c5d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
13 changes: 13 additions & 0 deletions app/jobs/concerns/alert_post_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ def prev_topic_link(topic_id)
"[Previous alert](#{Discourse.base_url}/t/#{topic_id}) #{local_date(created_at.to_s)}\n\n"
end

def generate_title(base_title, alert_history)
firing_count = alert_history&.count { |alert| is_firing?(alert["status"]) }
if firing_count > 0
I18n.t("prom_alert_receiver.topic_title.firing", base_title: base_title, count: firing_count)
else
I18n.t("prom_alert_receiver.topic_title.not_firing", base_title: base_title)
end
end

def first_post_body(receiver:,
topic_body: "",
alert_history:,
Expand Down Expand Up @@ -162,6 +171,10 @@ def revise_topic(topic:, title:, raw:, datacenters:, firing: nil, high_priority:
skip_validations: true,
validate_topic: true # This is a very weird API
)
if firing && title_changed
topic.update_column(:bumped_at, Time.now)
TopicTrackingState.publish_latest(topic)
end
end
end

Expand Down
8 changes: 6 additions & 2 deletions app/jobs/regular/process_alert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ def assigned_topic(receiver, params)
prev_topic_id: topic.custom_fields[::DiscoursePrometheusAlertReceiver::PREVIOUS_TOPIC_CUSTOM_FIELD]
)

title = params["commonAnnotations"]["topic_title"] ||
base_title = params["commonAnnotations"]["topic_title"] ||
"#{params["groupLabels"].to_hash.map { |k, v| "#{k}: #{v}" }.join(", ")}"

title = generate_title(base_title, alert_history)

revise_topic(
topic: topic,
title: title,
Expand Down Expand Up @@ -84,9 +86,11 @@ def assigned_topic(receiver, params)
end

def create_new_topic(receiver, params, alert_history)
topic_title = params["commonAnnotations"]["topic_title"] ||
base_title = params["commonAnnotations"]["topic_title"] ||
"#{params["groupLabels"].to_hash.map { |k, v| "#{k}: #{v}" }.join(", ")}"

topic_title = generate_title(base_title, alert_history)

datacenter = params["commonLabels"]["datacenter"]
topic_body = params["commonAnnotations"]["topic_body"]

Expand Down
2 changes: 1 addition & 1 deletion app/jobs/regular/process_grouped_alerts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def process_silenced_alerts(receiver, data)

revise_topic(
topic: topic,
title: annotations["topic_title"],
title: generate_title(annotations["topic_title"], stored_alerts),
raw: raw,
datacenters: datacenters(stored_alerts),
firing: stored_alerts.any? { |alert| is_firing?(alert["status"]) }
Expand Down
4 changes: 4 additions & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ en:
firing: "Firing Alerts"
history: "Alert History"
stale: "Stale Alerts"
topic_title:
not_firing: "%{base_title}"
firing: "%{base_title} (%{count} firing)"

Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@
)

expect(topic.title).to eq(
"Alert investigation required: AnAlert is on the loose"
"Alert investigation required: AnAlert is on the loose (1 firing)"
)

expect(receiver["topic_map"][alert_name]).to eq(topic.id)
Expand Down Expand Up @@ -510,7 +510,7 @@
post "/prometheus/receiver/#{token}", params: payload
end.to change { Topic.count }.by(1)

expect(topic.title).to eq("foo: bar, baz: wombat")
expect(topic.title).to eq("foo: bar, baz: wombat (1 firing)")

expect(topic.tags.pluck(:name)).to contain_exactly(
datacenter,
Expand Down Expand Up @@ -591,7 +591,7 @@
expect(messages.first.data[:firing_alerts_count]).to eq(1)

expect(topic.title).to eq(
"Alert investigation required: AnAlert is on the loose"
"Alert investigation required: AnAlert is on the loose (1 firing)"
)

expect(topic.tags.pluck(:name)).to contain_exactly(
Expand Down Expand Up @@ -692,7 +692,7 @@
topic.reload

expect(topic.title).to eq(
"Alert investigation required: AnAlert is on the loose"
"Alert investigation required: AnAlert is on the loose (2 firing)"
)

expect(topic.tags.pluck(:name)).to contain_exactly(
Expand Down Expand Up @@ -730,6 +730,40 @@
expect(raw).to match(/newalert.*date=2020-12-31 time=23:59:59/)
end

it "bumps the existing topic correctly" do
freeze_time(1.hour.from_now) do
expect do
post "/prometheus/receiver/#{token}", params: payload
end.to change { topic.reload.bumped_at }

expect(topic.reload.title).to eq(
"Alert investigation required: AnAlert is on the loose (2 firing)"
)
end

freeze_time(2.hours.from_now) do
payload["alerts"][0]["status"] = "stale"
expect do
post "/prometheus/receiver/#{token}", params: payload
end.to change { topic.reload.bumped_at }

expect(topic.reload.title).to eq(
"Alert investigation required: AnAlert is on the loose (1 firing)"
)
end

freeze_time(3.hours.from_now) do
payload["alerts"][1]["status"] = "stale"
expect do
post "/prometheus/receiver/#{token}", params: payload
end.to_not change { topic.reload.bumped_at }

expect(topic.reload.title).to eq(
"Alert investigation required: AnAlert is on the loose"
)
end
end

describe 'from another datacenter' do
let(:datacenter2) { "datacenter-2" }
let(:external_url2) { "supposed.be.a.url.2" }
Expand Down Expand Up @@ -761,7 +795,7 @@
topic.reload

expect(topic.title).to eq(
"Alert investigation required: AnAlert is on the loose"
"Alert investigation required: AnAlert is on the loose (2 firing)"
)

expect(topic.tags.pluck(:name)).to contain_exactly(
Expand Down Expand Up @@ -948,7 +982,7 @@
)

expect(keyed_topic.title).to eq(
"Alert investigation required: AnAlert is on the loose"
"Alert investigation required: AnAlert is on the loose (1 firing)"
)

expect(keyed_topic.tags.pluck(:name)).to contain_exactly(
Expand Down

0 comments on commit 78b2c5d

Please sign in to comment.