Class: PrometheusExporter::Server::ActiveRecordCollector

Inherits:
TypeCollector
  • Object
show all
Defined in:
lib/prometheus_exporter/server/active_record_collector.rb

Constant Summary collapse

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"
}

Instance Method Summary collapse

Constructor Details

#initializeActiveRecordCollector

Returns a new instance of ActiveRecordCollector.



16
17
18
19
20
21
22
23
# File 'lib/prometheus_exporter/server/active_record_collector.rb', line 16

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

Instance Method Details

#collect(obj) ⇒ Object



50
51
52
# File 'lib/prometheus_exporter/server/active_record_collector.rb', line 50

def collect(obj)
  @active_record_metrics << obj
end

#metricsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/prometheus_exporter/server/active_record_collector.rb', line 29

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

#typeObject



25
26
27
# File 'lib/prometheus_exporter/server/active_record_collector.rb', line 25

def type
  "active_record"
end