2021-12-11 22:18:48 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Database
|
|
|
|
module QueryAnalyzers
|
|
|
|
# The purpose of this analyzer is to observe via prometheus metrics
|
|
|
|
# all unique schemas observed on a given connection
|
|
|
|
#
|
|
|
|
# This effectively allows to do sample 1% or 0.01% of queries hitting
|
|
|
|
# system and observe if on a given connection we observe queries that
|
|
|
|
# are misaligned (`ci_replica` sees queries doing accessing only `gitlab_main`)
|
|
|
|
#
|
|
|
|
class GitlabSchemasMetrics < Base
|
|
|
|
class << self
|
|
|
|
def enabled?
|
|
|
|
::Feature::FlipperFeature.table_exists? &&
|
2022-10-11 01:57:18 +05:30
|
|
|
Feature.enabled?(:query_analyzer_gitlab_schema_metrics, type: :ops)
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def analyze(parsed)
|
|
|
|
db_config_name = ::Gitlab::Database.db_config_name(parsed.connection)
|
|
|
|
return unless db_config_name
|
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
gitlab_schemas = ::Gitlab::Database::GitlabSchema.table_schemas!(parsed.pg.tables)
|
2021-12-11 22:18:48 +05:30
|
|
|
return if gitlab_schemas.empty?
|
|
|
|
|
|
|
|
# to reduce amount of labels sort schemas used
|
|
|
|
gitlab_schemas = gitlab_schemas.to_a.sort.join(",")
|
|
|
|
|
|
|
|
schemas_metrics.increment({
|
|
|
|
gitlab_schemas: gitlab_schemas,
|
2022-07-23 23:45:48 +05:30
|
|
|
db_config_name: db_config_name
|
2021-12-11 22:18:48 +05:30
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
def schemas_metrics
|
|
|
|
@schemas_metrics ||= ::Gitlab::Metrics.counter(
|
|
|
|
:gitlab_database_decomposition_gitlab_schemas_used,
|
|
|
|
'The number of observed schemas dependent on connection'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|