Commit a756bb0d authored by Jan Provaznik's avatar Jan Provaznik Committed by Bob Van Landuyt

Use background transaction for events

Email receiver is used only by sidekiq so it should use
BackgroundTransaction.
parent 2aecec13
...@@ -20,7 +20,7 @@ module Gitlab ...@@ -20,7 +20,7 @@ module Gitlab
raise UnknownIncomingEmail unless handler raise UnknownIncomingEmail unless handler
handler.execute.tap do handler.execute.tap do
Gitlab::Metrics.add_event(handler.metrics_event, handler.metrics_params) Gitlab::Metrics::BackgroundTransaction.current&.add_event(handler.metrics_event, handler.metrics_params)
end end
end end
......
...@@ -5,9 +5,13 @@ require 'spec_helper' ...@@ -5,9 +5,13 @@ require 'spec_helper'
RSpec.describe Gitlab::Email::Receiver do RSpec.describe Gitlab::Email::Receiver do
include_context :email_shared_context include_context :email_shared_context
shared_examples 'correctly finds the mail key' do shared_examples 'correctly finds the mail key and adds metric event' do
specify do let(:metric_transaction) { double('Gitlab::Metrics::WebTransaction') }
specify :aggregate_failures do
expect(Gitlab::Email::Handler).to receive(:for).with(an_instance_of(Mail::Message), 'gitlabhq/gitlabhq+auth_token').and_return(handler) expect(Gitlab::Email::Handler).to receive(:for).with(an_instance_of(Mail::Message), 'gitlabhq/gitlabhq+auth_token').and_return(handler)
expect(::Gitlab::Metrics::BackgroundTransaction).to receive(:current).and_return(metric_transaction)
expect(metric_transaction).to receive(:add_event).with(handler.metrics_event, handler.metrics_params)
receiver.execute receiver.execute
end end
...@@ -30,7 +34,7 @@ RSpec.describe Gitlab::Email::Receiver do ...@@ -30,7 +34,7 @@ RSpec.describe Gitlab::Email::Receiver do
context 'when in a Delivered-To header' do context 'when in a Delivered-To header' do
let(:email_raw) { fixture_file('emails/forwarded_new_issue.eml') } let(:email_raw) { fixture_file('emails/forwarded_new_issue.eml') }
it_behaves_like 'correctly finds the mail key' it_behaves_like 'correctly finds the mail key and adds metric event'
it 'parses the metadata' do it 'parses the metadata' do
expect(metadata[:delivered_to]). to eq(["incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com", "support@example.com"]) expect(metadata[:delivered_to]). to eq(["incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com", "support@example.com"])
...@@ -40,7 +44,7 @@ RSpec.describe Gitlab::Email::Receiver do ...@@ -40,7 +44,7 @@ RSpec.describe Gitlab::Email::Receiver do
context 'when in an Envelope-To header' do context 'when in an Envelope-To header' do
let(:email_raw) { fixture_file('emails/envelope_to_header.eml') } let(:email_raw) { fixture_file('emails/envelope_to_header.eml') }
it_behaves_like 'correctly finds the mail key' it_behaves_like 'correctly finds the mail key and adds metric event'
it 'parses the metadata' do it 'parses the metadata' do
expect(metadata[:envelope_to]). to eq(["incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com"]) expect(metadata[:envelope_to]). to eq(["incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com"])
...@@ -50,7 +54,7 @@ RSpec.describe Gitlab::Email::Receiver do ...@@ -50,7 +54,7 @@ RSpec.describe Gitlab::Email::Receiver do
context 'when in an X-Envelope-To header' do context 'when in an X-Envelope-To header' do
let(:email_raw) { fixture_file('emails/x_envelope_to_header.eml') } let(:email_raw) { fixture_file('emails/x_envelope_to_header.eml') }
it_behaves_like 'correctly finds the mail key' it_behaves_like 'correctly finds the mail key and adds metric event'
it 'parses the metadata' do it 'parses the metadata' do
expect(metadata[:x_envelope_to]). to eq(["incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com"]) expect(metadata[:x_envelope_to]). to eq(["incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com"])
...@@ -60,7 +64,7 @@ RSpec.describe Gitlab::Email::Receiver do ...@@ -60,7 +64,7 @@ RSpec.describe Gitlab::Email::Receiver do
context 'when enclosed with angle brackets in an Envelope-To header' do context 'when enclosed with angle brackets in an Envelope-To header' do
let(:email_raw) { fixture_file('emails/envelope_to_header_with_angle_brackets.eml') } let(:email_raw) { fixture_file('emails/envelope_to_header_with_angle_brackets.eml') }
it_behaves_like 'correctly finds the mail key' it_behaves_like 'correctly finds the mail key and adds metric event'
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