Commit 1c439f93 authored by Quang-Minh Nguyen's avatar Quang-Minh Nguyen

Refactor subscriber observe method

parent e921514a
...@@ -54,7 +54,9 @@ module EE ...@@ -54,7 +54,9 @@ module EE
end end
def observe_db_role_duration(db_role, event) def observe_db_role_duration(db_role, event)
observe("gitlab_sql_#{db_role}_duration_seconds".to_sym, event) observe("gitlab_sql_#{db_role}_duration_seconds".to_sym, event) do
buckets ::Gitlab::Metrics::Subscribers::ActiveRecord::SQL_DURATION_BUCKET
end
duration = event.duration / 1000.0 duration = event.duration / 1000.0
duration_key = "db_#{db_role}_duration_s".to_sym duration_key = "db_#{db_role}_duration_s".to_sym
......
...@@ -11,14 +11,14 @@ module Gitlab ...@@ -11,14 +11,14 @@ module Gitlab
DB_COUNTERS = %i{db_count db_write_count db_cached_count}.freeze DB_COUNTERS = %i{db_count db_write_count db_cached_count}.freeze
SQL_COMMANDS_WITH_COMMENTS_REGEX = /\A(\/\*.*\*\/\s)?((?!(.*[^\w'"](DELETE|UPDATE|INSERT INTO)[^\w'"])))(WITH.*)?(SELECT)((?!(FOR UPDATE|FOR SHARE)).)*$/i.freeze SQL_COMMANDS_WITH_COMMENTS_REGEX = /\A(\/\*.*\*\/\s)?((?!(.*[^\w'"](DELETE|UPDATE|INSERT INTO)[^\w'"])))(WITH.*)?(SELECT)((?!(FOR UPDATE|FOR SHARE)).)*$/i.freeze
DURATION_BUCKET = [0.05, 0.1, 0.25].freeze SQL_DURATION_BUCKET = [0.05, 0.1, 0.25].freeze
TRANSACTION_DURATION_BUCKET = [0.1, 0.25, 1].freeze TRANSACTION_DURATION_BUCKET = [0.1, 0.25, 1].freeze
# This event is published from ActiveRecordBaseTransactionMetrics and # This event is published from ActiveRecordBaseTransactionMetrics and
# used to record a database transaction duration when calling # used to record a database transaction duration when calling
# ActiveRecord::Base.transaction {} block. # ActiveRecord::Base.transaction {} block.
def transaction(event) def transaction(event)
current_transaction&.observe(:gitlab_database_transaction_seconds, event.duration / 1000.0) do observe(:gitlab_database_transaction_seconds, event) do
buckets TRANSACTION_DURATION_BUCKET buckets TRANSACTION_DURATION_BUCKET
end end
end end
...@@ -36,7 +36,9 @@ module Gitlab ...@@ -36,7 +36,9 @@ module Gitlab
increment(:db_cached_count) if cached_query?(payload) increment(:db_cached_count) if cached_query?(payload)
increment(:db_write_count) unless select_sql_command?(payload) increment(:db_write_count) unless select_sql_command?(payload)
observe(:gitlab_sql_duration_seconds, event) observe(:gitlab_sql_duration_seconds, event) do
buckets SQL_DURATION_BUCKET
end
end end
def self.db_counter_payload def self.db_counter_payload
...@@ -69,10 +71,8 @@ module Gitlab ...@@ -69,10 +71,8 @@ module Gitlab
Gitlab::SafeRequestStore[counter] = Gitlab::SafeRequestStore[counter].to_i + 1 Gitlab::SafeRequestStore[counter] = Gitlab::SafeRequestStore[counter].to_i + 1
end end
def observe(histogram, event) def observe(histogram, event, &block)
current_transaction&.observe(histogram, event.duration / 1000.0) do current_transaction&.observe(histogram, event.duration / 1000.0, &block)
buckets DURATION_BUCKET
end
end end
def current_transaction def current_transaction
......
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