Sha256: ebf0b5c7110a87925910d80fdfb6632f03c1fe91ec117abb7bbde43056a38e50

Contents?: true

Size: 1.57 KB

Versions: 13

Compression:

Stored size: 1.57 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"])
        metric_key.merge!(m["custom_labels"]) if m["custom_labels"]

        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"] &&
         obj["metric_labels"]["pool_name"] == current["metric_labels"]["pool_name"]) ||
          (current["created_at"] + MAX_ACTIVERECORD_METRIC_AGE < now)
      end

      @active_record_metrics << obj
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
prometheus_exporter-2.0.8 lib/prometheus_exporter/server/active_record_collector.rb
prometheus_exporter-2.0.7 lib/prometheus_exporter/server/active_record_collector.rb
prometheus_exporter-2.0.6 lib/prometheus_exporter/server/active_record_collector.rb
prometheus_exporter-2.0.5 lib/prometheus_exporter/server/active_record_collector.rb
prometheus_exporter-2.0.4 lib/prometheus_exporter/server/active_record_collector.rb
prometheus_exporter-2.0.3 lib/prometheus_exporter/server/active_record_collector.rb
prometheus_exporter-2.0.2 lib/prometheus_exporter/server/active_record_collector.rb
prometheus_exporter-2.0.1 lib/prometheus_exporter/server/active_record_collector.rb
prometheus_exporter-2.0.0 lib/prometheus_exporter/server/active_record_collector.rb
prometheus_exporter-1.0.1 lib/prometheus_exporter/server/active_record_collector.rb
prometheus_exporter-1.0.0 lib/prometheus_exporter/server/active_record_collector.rb
prometheus_exporter-0.8.1 lib/prometheus_exporter/server/active_record_collector.rb
prometheus_exporter-0.8.0 lib/prometheus_exporter/server/active_record_collector.rb