Class: PrometheusExporter::Instrumentation::ActiveRecord
- Inherits:
-
PeriodicStats
- Object
- PeriodicStats
- PrometheusExporter::Instrumentation::ActiveRecord
show all
- Defined in:
- lib/prometheus_exporter/instrumentation/active_record.rb
Constant Summary
collapse
- ALLOWED_CONFIG_LABELS =
%i[database username host port]
Class Method Summary
collapse
Instance Method Summary
collapse
started?, stop, worker_loop
Constructor Details
#initialize(metric_labels, config_labels) ⇒ ActiveRecord
Returns a new instance of ActiveRecord.
39
40
41
42
|
# File 'lib/prometheus_exporter/instrumentation/active_record.rb', line 39
def initialize(metric_labels, config_labels)
@metric_labels = metric_labels
@config_labels = config_labels
end
|
Class Method Details
.start(client: nil, frequency: 30, custom_labels: {}, config_labels: []) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/prometheus_exporter/instrumentation/active_record.rb', line 8
def self.start(client: nil, frequency: 30, custom_labels: {}, config_labels: [])
client ||= PrometheusExporter::Client.default
unless ::ActiveRecord::Base.connection_pool.respond_to?(:stat)
client.logger.error(
"ActiveRecord connection pool stats not supported in your rails version",
)
return
end
config_labels.map!(&:to_sym)
validate_config_labels(config_labels)
active_record_collector = new(custom_labels, config_labels)
worker_loop do
metrics = active_record_collector.collect
metrics.each { |metric| client.send_json metric }
end
super
end
|
.validate_config_labels(config_labels) ⇒ Object
32
33
34
35
36
37
|
# File 'lib/prometheus_exporter/instrumentation/active_record.rb', line 32
def self.validate_config_labels(config_labels)
return if config_labels.size == 0
if (config_labels - ALLOWED_CONFIG_LABELS).size > 0
raise "Invalid Config Labels, available options #{ALLOWED_CONFIG_LABELS}"
end
end
|
Instance Method Details
#collect ⇒ Object
44
45
46
47
48
|
# File 'lib/prometheus_exporter/instrumentation/active_record.rb', line 44
def collect
metrics = []
collect_active_record_pool_stats(metrics)
metrics
end
|
#collect_active_record_pool_stats(metrics) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/prometheus_exporter/instrumentation/active_record.rb', line 54
def collect_active_record_pool_stats(metrics)
ObjectSpace.each_object(::ActiveRecord::ConnectionAdapters::ConnectionPool) do |pool|
next if pool.connections.nil?
metric = {
pid: pid,
type: "active_record",
hostname: ::PrometheusExporter.hostname,
metric_labels: labels(pool),
}
metric.merge!(pool.stat)
metrics << metric
end
end
|
#pid ⇒ Object
50
51
52
|
# File 'lib/prometheus_exporter/instrumentation/active_record.rb', line 50
def pid
@pid = ::Process.pid
end
|