Sha256: e47241d166cc85410e3fa9d35e8e1b3e0ebba4ddb233c706aa5cd25cb33b92cd

Contents?: true

Size: 1.42 KB

Versions: 6

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module PrometheusExporter::Server
  class ActiveRecordCollector < TypeCollector
    MAX_ACTIVERECORD_METRIC_AGE = 60
    ACTIVE_RECORD_GAUGES = {
      connections: "Total connections in pool",
      busy: "Connections in use in pool",
      dead: "Dead connections in pool",
      idle: "Idle connections in pool",
      waiting: "Connection requests waiting",
      size: "Maximum allowed connection pool size"
    }

    def initialize
      @active_record_metrics = []
    end

    def type
      "active_record"
    end

    def metrics
      return [] if @active_record_metrics.length == 0

      metrics = {}

      @active_record_metrics.map do |m|
        metric_key = (m["metric_labels"] || {}).merge("pid" => m["pid"])

        ACTIVE_RECORD_GAUGES.map do |k, help|
          k = k.to_s
          if v = m[k]
            g = metrics[k] ||= PrometheusExporter::Metric::Gauge.new("active_record_connection_pool_#{k}", help)
            g.observe(v, metric_key)
          end
        end
      end

      metrics.values
    end

    def collect(obj)
      now = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)

      obj["created_at"] = now

      @active_record_metrics.delete_if do |current|
        (obj["pid"] == current["pid"] && obj["hostname"] == current["hostname"]) ||
          (current["created_at"] + MAX_ACTIVERECORD_METRIC_AGE < now)
      end

      @active_record_metrics << obj
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
prometheus_exporter-0.5.2 lib/prometheus_exporter/server/active_record_collector.rb
prometheus_exporter-0.5.1 lib/prometheus_exporter/server/active_record_collector.rb
prometheus_exporter-0.5.0 lib/prometheus_exporter/server/active_record_collector.rb
prometheus_exporter-0.4.17 lib/prometheus_exporter/server/active_record_collector.rb
prometheus_exporter-0.4.16 lib/prometheus_exporter/server/active_record_collector.rb
prometheus_exporter-0.4.15 lib/prometheus_exporter/server/active_record_collector.rb