Commit 0a62ff7c authored by Mikołaj Wawrzyniak's avatar Mikołaj Wawrzyniak

Merge branch 'ab/grafana-tag' into 'master'

Add a configurable Grafana tag for database reindexing annotations

See merge request gitlab-org/gitlab!50755
parents 41fb26cf 9584a714
......@@ -5,9 +5,10 @@ module Gitlab
module Reindexing
# This can be used to send annotations for reindexing to a Grafana API
class GrafanaNotifier
def initialize(api_key = ENV['GITLAB_GRAFANA_API_KEY'], api_url = ENV['GITLAB_GRAFANA_API_URL'])
def initialize(api_key = ENV['GITLAB_GRAFANA_API_KEY'], api_url = ENV['GITLAB_GRAFANA_API_URL'], additional_tag = ENV['GITLAB_REINDEXING_GRAFANA_TAG'] || Rails.env)
@api_key = api_key
@api_url = api_url
@additional_tag = additional_tag
end
def notify_start(action)
......@@ -37,7 +38,7 @@ module Gitlab
def base_payload(action)
{
time: (action.action_start.utc.to_f * 1000).to_i,
tags: ['reindex', action.index.tablename, action.index.name]
tags: ['reindex', @additional_tag, action.index.tablename, action.index.name].compact
}
end
......
......@@ -7,6 +7,7 @@ RSpec.describe Gitlab::Database::Reindexing::GrafanaNotifier do
let(:api_key) { "foo" }
let(:api_url) { "http://bar"}
let(:additional_tag) { "some-tag" }
let(:action) { create(:reindex_action) }
......@@ -73,32 +74,66 @@ RSpec.describe Gitlab::Database::Reindexing::GrafanaNotifier do
end
describe '#notify_start' do
subject { described_class.new(api_key, api_url).notify_start(action) }
let(:payload) do
{
time: (action.action_start.utc.to_f * 1000).to_i,
tags: ['reindex', action.index.tablename, action.index.name],
text: "Started reindexing of #{action.index.name} on #{action.index.tablename}"
}
context 'additional tag is nil' do
subject { described_class.new(api_key, api_url, nil).notify_start(action) }
let(:payload) do
{
time: (action.action_start.utc.to_f * 1000).to_i,
tags: ['reindex', action.index.tablename, action.index.name],
text: "Started reindexing of #{action.index.name} on #{action.index.tablename}"
}
end
it_behaves_like 'interacting with Grafana annotations API'
end
it_behaves_like 'interacting with Grafana annotations API'
context 'additional tag is not nil' do
subject { described_class.new(api_key, api_url, additional_tag).notify_start(action) }
let(:payload) do
{
time: (action.action_start.utc.to_f * 1000).to_i,
tags: ['reindex', additional_tag, action.index.tablename, action.index.name],
text: "Started reindexing of #{action.index.name} on #{action.index.tablename}"
}
end
it_behaves_like 'interacting with Grafana annotations API'
end
end
describe '#notify_end' do
subject { described_class.new(api_key, api_url).notify_end(action) }
let(:payload) do
{
time: (action.action_start.utc.to_f * 1000).to_i,
tags: ['reindex', action.index.tablename, action.index.name],
text: "Finished reindexing of #{action.index.name} on #{action.index.tablename} (#{action.state})",
timeEnd: (action.action_end.utc.to_f * 1000).to_i,
isRegion: true
}
context 'additional tag is nil' do
subject { described_class.new(api_key, api_url, nil).notify_end(action) }
let(:payload) do
{
time: (action.action_start.utc.to_f * 1000).to_i,
tags: ['reindex', action.index.tablename, action.index.name],
text: "Finished reindexing of #{action.index.name} on #{action.index.tablename} (#{action.state})",
timeEnd: (action.action_end.utc.to_f * 1000).to_i,
isRegion: true
}
end
it_behaves_like 'interacting with Grafana annotations API'
end
it_behaves_like 'interacting with Grafana annotations API'
context 'additional tag is not nil' do
subject { described_class.new(api_key, api_url, additional_tag).notify_end(action) }
let(:payload) do
{
time: (action.action_start.utc.to_f * 1000).to_i,
tags: ['reindex', additional_tag, action.index.tablename, action.index.name],
text: "Finished reindexing of #{action.index.name} on #{action.index.tablename} (#{action.state})",
timeEnd: (action.action_end.utc.to_f * 1000).to_i,
isRegion: true
}
end
it_behaves_like 'interacting with Grafana annotations API'
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