summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzegorz@gitlab.com>2018-09-05 13:02:37 +0000
committerGrzegorz Bizon <grzegorz@gitlab.com>2018-09-05 13:02:37 +0000
commitcff47b2045999ba5ab1af7cf8c0aa45b2c52fb36 (patch)
treefcd7771ea9ca3097b5d9c853afc483902c4c0b70
parentc6d315b4bb06be644c1c8ca47268bed71c679dd6 (diff)
parentbec7f0609744025050d7f093f106bfd190813d56 (diff)
downloadgitlab-ce-cff47b2045999ba5ab1af7cf8c0aa45b2c52fb36.tar.gz
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
-rw-r--r--changelogs/unreleased/skip-irrelevant-sql-commands-in-metrics.yml5
-rw-r--r--lib/gitlab/metrics/subscribers/active_record.rb6
-rw-r--r--spec/lib/gitlab/metrics/subscribers/active_record_spec.rb59
3 files changed, 70 insertions, 0 deletions
diff --git a/changelogs/unreleased/skip-irrelevant-sql-commands-in-metrics.yml b/changelogs/unreleased/skip-irrelevant-sql-commands-in-metrics.yml
new file mode 100644
index 00000000000..56d236d0029
--- /dev/null
+++ b/changelogs/unreleased/skip-irrelevant-sql-commands-in-metrics.yml
@@ -0,0 +1,5 @@
+---
+title: Ignore irrelevant sql commands in metrics
+merge_request: 21498
+author:
+type: other
diff --git a/lib/gitlab/metrics/subscribers/active_record.rb b/lib/gitlab/metrics/subscribers/active_record.rb
index c205f348023..04107296ae3 100644
--- a/lib/gitlab/metrics/subscribers/active_record.rb
+++ b/lib/gitlab/metrics/subscribers/active_record.rb
@@ -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)
diff --git a/spec/lib/gitlab/metrics/subscribers/active_record_spec.rb b/spec/lib/gitlab/metrics/subscribers/active_record_spec.rb
index 4e7bd433a9c..ee6d6fc961f 100644
--- a/spec/lib/gitlab/metrics/subscribers/active_record_spec.rb
+++ b/spec/lib/gitlab/metrics/subscribers/active_record_spec.rb
@@ -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