Class: PrometheusExporter::Server::ActiveRecordCollector

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

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initializeActiveRecordCollector

Returns a new instance of ActiveRecordCollector.



15
16
17
# File 'lib/prometheus_exporter/server/active_record_collector.rb', line 15

def initialize
  @active_record_metrics = []
end

Instance Method Details

#collect(obj) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/prometheus_exporter/server/active_record_collector.rb', line 44

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

#metricsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/prometheus_exporter/server/active_record_collector.rb', line 23

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

#typeObject



19
20
21
# File 'lib/prometheus_exporter/server/active_record_collector.rb', line 19

def type
  "active_record"
end