Sha256: fb5892eaf2e417564a4e1baa2a1b3008e70af5499701d1ed2b937d68b70f0b88

Contents?: true

Size: 1.67 KB

Versions: 44

Compression:

Stored size: 1.67 KB

Contents

module GitLab
  module Exporter
    module Database
      # A helper class to collect tuple stats from the database
      #
      # It takes a connection string (e.g. "dbname=test port=5432")
      class TupleStatsCollector < Base
        COLUMNS = %w[relname seq_tup_read idx_tup_fetch n_tup_ins n_tup_upd n_tup_del n_tup_hot_upd n_dead_tup seq_scan]
                  .join(",")
        QUERY   = <<-SQL.freeze
          SELECT #{COLUMNS}
          FROM pg_stat_user_tables
          WHERE relname IN (SELECT tablename FROM pg_tables WHERE tableowner = 'gitlab')
          GROUP BY #{COLUMNS}
        SQL

        def run
          with_connection_pool do |conn|
            conn.exec(QUERY).each.with_object({}) do |row, stats|
              stats[row.delete("relname")] = row
            end
          end
        end
      end

      # Probes the DB specified by opts[:connection_string] for tuple stats, then converts them to metrics
      class TuplesProber
        def initialize(metrics: PrometheusMetrics.new, **opts)
          @metrics   = metrics
          @collector = TupleStatsCollector.new(**opts)
        end

        def probe_db
          result = @collector.run

          result.each do |table_name, tuple_stats|
            tuple_stats.each do |column_name, value|
              next if value.is_a?(Numeric)

              @metrics.add("gitlab_database_stat_table_#{column_name}",
                           value.to_f,
                           table_name: table_name)
            end
          end

          self
        rescue PG::ConnectionBad
          self
        end

        def write_to(target)
          target.write(@metrics.to_s)
        end
      end
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
gitlab-exporter-15.0.0 lib/gitlab_exporter/database/tuple_stats.rb
gitlab-exporter-14.5.0 lib/gitlab_exporter/database/tuple_stats.rb
gitlab-exporter-14.4.0 lib/gitlab_exporter/database/tuple_stats.rb
gitlab-exporter-14.3.0 lib/gitlab_exporter/database/tuple_stats.rb
gitlab-exporter-14.2.0 lib/gitlab_exporter/database/tuple_stats.rb
gitlab-exporter-14.1.0 lib/gitlab_exporter/database/tuple_stats.rb
gitlab-exporter-14.0.0 lib/gitlab_exporter/database/tuple_stats.rb
gitlab-exporter-13.5.0 lib/gitlab_exporter/database/tuple_stats.rb
gitlab-exporter-13.4.1 lib/gitlab_exporter/database/tuple_stats.rb
gitlab-exporter-13.4.0 lib/gitlab_exporter/database/tuple_stats.rb
gitlab-exporter-13.3.1 lib/gitlab_exporter/database/tuple_stats.rb
gitlab-exporter-13.3.0 lib/gitlab_exporter/database/tuple_stats.rb
gitlab-exporter-13.2.0 lib/gitlab_exporter/database/tuple_stats.rb
gitlab-exporter-13.1.0 lib/gitlab_exporter/database/tuple_stats.rb
gitlab-exporter-13.0.3 lib/gitlab_exporter/database/tuple_stats.rb
gitlab-exporter-13.0.0 lib/gitlab_exporter/database/tuple_stats.rb
gitlab-exporter-12.1.1 lib/gitlab_exporter/database/tuple_stats.rb
gitlab-exporter-12.1.0 lib/gitlab_exporter/database/tuple_stats.rb
gitlab-exporter-12.0.1 lib/gitlab_exporter/database/tuple_stats.rb
gitlab-exporter-12.0.0 lib/gitlab_exporter/database/tuple_stats.rb