Merge branch 'skip-irrelevant-sql-commands-in-metrics' into 'master'

Ignore irrelevant sql commands in metrics

Closes #51005

See merge request gitlab-org/gitlab-ce!21498
This commit is contained in:
Grzegorz Bizon 2018-09-05 13:02:37 +00:00
commit cff47b2045
3 changed files with 70 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
title: Ignore irrelevant sql commands in metrics
merge_request: 21498
author:
type: other

View file

@ -6,9 +6,15 @@ module Gitlab
include Gitlab::Metrics::Methods
attach_to :active_record
IGNORABLE_SQL = %w{BEGIN COMMIT}.freeze
def sql(event)
return unless current_transaction
payload = event.payload
return if payload[:name] == 'SCHEMA' || IGNORABLE_SQL.include?(payload[:sql])
self.class.gitlab_sql_duration_seconds.observe(current_transaction.labels, event.duration / 1000.0)
current_transaction.increment(:sql_duration, event.duration, false)

View file

@ -42,6 +42,65 @@ describe Gitlab::Metrics::Subscribers::ActiveRecord do
subscriber.sql(event)
end
context 'events are internal to Rails or irrelevant' do
let(:schema_event) do
double(
:event,
name: 'sql.active_record',
payload: {
sql: "SELECT attr.attname FROM pg_attribute attr INNER JOIN pg_constraint cons ON attr.attrelid = cons.conrelid AND attr.attnum = any(cons.conkey) WHERE cons.contype = 'p' AND cons.conrelid = '\"projects\"'::regclass",
name: 'SCHEMA',
connection_id: 135,
statement_name: nil,
binds: []
},
duration: 0.7
)
end
let(:begin_event) do
double(
:event,
name: 'sql.active_record',
payload: {
sql: "BEGIN",
name: nil,
connection_id: 231,
statement_name: nil,
binds: []
},
duration: 1.1
)
end
let(:commit_event) do
double(
:event,
name: 'sql.active_record',
payload: {
sql: "COMMIT",
name: nil,
connection_id: 212,
statement_name: nil,
binds: []
},
duration: 1.6
)
end
it 'skips schema/begin/commit sql commands' do
expect(subscriber).to receive(:current_transaction)
.at_least(:once)
.and_return(transaction)
expect(transaction).not_to receive(:increment)
subscriber.sql(schema_event)
subscriber.sql(begin_event)
subscriber.sql(commit_event)
end
end
end
end
end