Sha256: 826ef7d3f8218cf147138f675868fcd02fb30ff3156f46b72c39e7f695198892

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module PrometheusExporter::Server
  class ActiveRecordCollector < TypeCollector
    MAX_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 = MetricsContainer.new(ttl: MAX_METRIC_AGE)
      @active_record_metrics.filter = -> (new_metric, old_metric) do
        new_metric["pid"] == old_metric["pid"] &&
        new_metric["hostname"] == old_metric["hostname"] &&
        new_metric["metric_labels"]["pool_name"] == old_metric["metric_labels"]["pool_name"]
      end
    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"], "hostname" => m["hostname"])
        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)
      @active_record_metrics << obj
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
prometheus_exporter-2.1.1 lib/prometheus_exporter/server/active_record_collector.rb
prometheus_exporter-2.1.0 lib/prometheus_exporter/server/active_record_collector.rb