Commit 39ac6acb authored by Pawel Chojnacki's avatar Pawel Chojnacki

Fix Active record and transaction specs

parent 38fbd25b
...@@ -101,7 +101,7 @@ module Gitlab ...@@ -101,7 +101,7 @@ module Gitlab
# tags - A set of tags to attach to the event. # tags - A set of tags to attach to the event.
def add_event(event_name, tags = {}) def add_event(event_name, tags = {})
self.class.metric_event_counter(event_name, tags).increment(tags.merge(labels)) self.class.metric_event_counter(event_name, tags).increment(tags.merge(labels))
@metrics << Metric.new(EVENT_SERIES, { count: 1 }, tags, :event) @metrics << Metric.new(EVENT_SERIES, { count: 1 }, tags.merge(event: event_name), :event)
end end
# Returns a MethodCall object for the given name. # Returns a MethodCall object for the given name.
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Metrics::Instrumentation do describe Gitlab::Metrics::Instrumentation do
let(:transaction) { Gitlab::Metrics::Transaction.new } let(:env) { {} }
let(:transaction) { Gitlab::Metrics::Transaction.new(env) }
before do before do
@dummy = Class.new do @dummy = Class.new do
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Metrics::Subscribers::ActiveRecord do describe Gitlab::Metrics::Subscribers::ActiveRecord do
let(:transaction) { Gitlab::Metrics::Transaction.new } let(:env) { {} }
let(:transaction) { Gitlab::Metrics::Transaction.new(env) }
let(:subscriber) { described_class.new } let(:subscriber) { described_class.new }
let(:event) do let(:event) do
double(:event, duration: 0.2, double(:event, duration: 2,
payload: { sql: 'SELECT * FROM users WHERE id = 10' }) payload: { sql: 'SELECT * FROM users WHERE id = 10' })
end end
...@@ -20,16 +21,24 @@ describe Gitlab::Metrics::Subscribers::ActiveRecord do ...@@ -20,16 +21,24 @@ describe Gitlab::Metrics::Subscribers::ActiveRecord do
end end
describe 'with a current transaction' do describe 'with a current transaction' do
it 'observes sql_duration metric' do
expect(subscriber).to receive(:current_transaction)
.at_least(:once)
.and_return(transaction)
expect(described_class.metric_sql_duration_seconds).to receive(:observe).with({}, 0.002)
subscriber.sql(event)
end
it 'increments the :sql_duration value' do it 'increments the :sql_duration value' do
expect(subscriber).to receive(:current_transaction) expect(subscriber).to receive(:current_transaction)
.at_least(:once) .at_least(:once)
.and_return(transaction) .and_return(transaction)
expect(transaction).to receive(:increment) expect(transaction).to receive(:increment)
.with(:sql_duration, 0.2) .with(:sql_duration, 2, false)
expect(transaction).to receive(:increment) expect(transaction).to receive(:increment)
.with(:sql_count, 1) .with(:sql_count, 1, false)
subscriber.sql(event) subscriber.sql(event)
end end
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Metrics::Transaction do describe Gitlab::Metrics::Transaction do
let(:transaction) { described_class.new } let(:env) { {} }
let(:transaction) { described_class.new(env) }
describe '#duration' do describe '#duration' do
it 'returns the duration of a transaction in seconds' do it 'returns the duration of a transaction in seconds' do
...@@ -48,7 +49,7 @@ describe Gitlab::Metrics::Transaction do ...@@ -48,7 +49,7 @@ describe Gitlab::Metrics::Transaction do
describe '#method_call_for' do describe '#method_call_for' do
it 'returns a MethodCall' do it 'returns a MethodCall' do
method = transaction.method_call_for('Foo#bar') method = transaction.method_call_for('Foo#bar', :Foo, '#bar')
expect(method).to be_an_instance_of(Gitlab::Metrics::MethodCall) expect(method).to be_an_instance_of(Gitlab::Metrics::MethodCall)
end end
...@@ -119,7 +120,7 @@ describe Gitlab::Metrics::Transaction do ...@@ -119,7 +120,7 @@ describe Gitlab::Metrics::Transaction do
end end
it 'adds the action as a tag for every metric' do it 'adds the action as a tag for every metric' do
transaction.action = 'Foo#bar' allow(transaction).to receive(:labels).and_return(controller: 'Foo', action: 'bar')
transaction.track_self transaction.track_self
hash = { hash = {
...@@ -136,7 +137,8 @@ describe Gitlab::Metrics::Transaction do ...@@ -136,7 +137,8 @@ describe Gitlab::Metrics::Transaction do
end end
it 'does not add an action tag for events' do it 'does not add an action tag for events' do
transaction.action = 'Foo#bar' allow(transaction).to receive(:labels).and_return(controller: 'Foo', action: 'bar')
transaction.add_event(:meow) transaction.add_event(:meow)
hash = { hash = {
......
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