提交 30a4bb66 编写于 作者: P Pawel Chojnacki

Fix sidekiq middleware tests

上级 043545de
module Gitlab module Gitlab
module Metrics module Metrics
class SidekiqTransaction class SidekiqTransaction < Transaction
def initialize(worker_class) def initialize(worker_class)
super()
@worker_class = worker_class @worker_class = worker_class
end end
protected protected
def labels def labels
{ controller: worker.class.name, action: 'perform' } { controller: @worker_class.name, action: 'perform' }
end end
end end
end end
......
module Gitlab module Gitlab
module Metrics module Metrics
# Class for storing metrics information of a single transaction. # Class for storing metrics information of a single transaction.
class BaseTransaction class Transaction
# base labels shared among all transactions # base labels shared among all transactions
BASE_LABELS = { controller: nil, action: nil }.freeze BASE_LABELS = { controller: nil, action: nil }.freeze
...@@ -131,8 +131,6 @@ module Gitlab ...@@ -131,8 +131,6 @@ module Gitlab
"#{labels[:controller]}##{labels[:action]}" if labels && !labels.empty? "#{labels[:controller]}##{labels[:action]}" if labels && !labels.empty?
end end
protected
def self.metric_transaction_duration_seconds def self.metric_transaction_duration_seconds
@metric_transaction_duration_seconds ||= Gitlab::Metrics.histogram( @metric_transaction_duration_seconds ||= Gitlab::Metrics.histogram(
:gitlab_transaction_duration_seconds, :gitlab_transaction_duration_seconds,
......
module Gitlab module Gitlab
module Metrics module Metrics
class WebTransaction < BaseTransaction class WebTransaction < Transaction
CONTROLLER_KEY = 'action_controller.instance'.freeze CONTROLLER_KEY = 'action_controller.instance'.freeze
ENDPOINT_KEY = 'api.endpoint'.freeze ENDPOINT_KEY = 'api.endpoint'.freeze
......
...@@ -6,7 +6,7 @@ describe Gitlab::Metrics::SidekiqMiddleware do ...@@ -6,7 +6,7 @@ describe Gitlab::Metrics::SidekiqMiddleware do
def run(worker, message) def run(worker, message)
expect(Gitlab::Metrics::Transaction).to receive(:new) expect(Gitlab::Metrics::Transaction).to receive(:new)
.with('TestWorker#perform') .with(worker.class)
.and_call_original .and_call_original
expect_any_instance_of(Gitlab::Metrics::Transaction).to receive(:set) expect_any_instance_of(Gitlab::Metrics::Transaction).to receive(:set)
...@@ -18,21 +18,22 @@ describe Gitlab::Metrics::SidekiqMiddleware do ...@@ -18,21 +18,22 @@ describe Gitlab::Metrics::SidekiqMiddleware do
end end
describe '#call' do describe '#call' do
it 'tracks the transaction' do let(:test_worker_class) { double(:class, name: 'TestWorker') }
worker = double(:worker, class: double(:class, name: 'TestWorker')) let(:worker) { double(:worker, class: test_worker_class) }
it 'reports correct action based on worker class' do
end
it 'tracks the transaction' do
run(worker, message) run(worker, message)
end end
it 'tracks the transaction (for messages without `enqueued_at`)' do it 'tracks the transaction (for messages without `enqueued_at`)' do
worker = double(:worker, class: double(:class, name: 'TestWorker'))
run(worker, {}) run(worker, {})
end end
it 'tracks any raised exceptions' do it 'tracks any raised exceptions' do
worker = double(:worker, class: double(:class, name: 'TestWorker'))
expect_any_instance_of(Gitlab::Metrics::Transaction) expect_any_instance_of(Gitlab::Metrics::Transaction)
.to receive(:run).and_raise(RuntimeError) .to receive(:run).and_raise(RuntimeError)
...@@ -45,18 +46,5 @@ describe Gitlab::Metrics::SidekiqMiddleware do ...@@ -45,18 +46,5 @@ describe Gitlab::Metrics::SidekiqMiddleware do
expect { middleware.call(worker, message, :test) } expect { middleware.call(worker, message, :test) }
.to raise_error(RuntimeError) .to raise_error(RuntimeError)
end end
it 'tags the metrics accordingly' do
tags = { one: 1, two: 2 }
worker = double(:worker, class: double(:class, name: 'TestWorker'))
allow(worker).to receive(:metrics_tags).and_return(tags)
tags.each do |tag, value|
expect_any_instance_of(Gitlab::Metrics::Transaction).to receive(:add_tag)
.with(tag, value)
end
run(worker, message)
end
end end
end end
...@@ -2,7 +2,7 @@ require 'spec_helper' ...@@ -2,7 +2,7 @@ require 'spec_helper'
describe Gitlab::Metrics::Subscribers::RailsCache do describe Gitlab::Metrics::Subscribers::RailsCache do
let(:env) { {} } let(:env) { {} }
let(:transaction) { Gitlab::Metrics::Transaction.new(env) } let(:transaction) { Gitlab::Metrics::WebTransaction.new(env) }
let(:subscriber) { described_class.new } let(:subscriber) { described_class.new }
let(:event) { double(:event, duration: 15.2) } let(:event) { double(:event, duration: 15.2) }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册