Module: DeadBro::Collectors::Database
- Defined in:
- lib/dead_bro/collectors/database.rb
Overview
Database collector provides lightweight, best-effort information about the current ActiveRecord connection pool and a simple ping latency measurement.
Class Method Summary collapse
- .collect ⇒ Object
- .current_time ⇒ Object
- .db_enabled? ⇒ Boolean
- .elapsed_ms(started) ⇒ Object
- .ping_ms(base) ⇒ Object
- .pool_stats(pool) ⇒ Object
- .safe_connection_pool(base) ⇒ Object
- .safe_integer(value) ⇒ Object
Class Method Details
.collect ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/dead_bro/collectors/database.rb', line 12 def collect return {disabled: true} unless db_enabled? return {available: false} unless defined?(::ActiveRecord) base = ::ActiveRecord::Base return {available: false} unless base.respond_to?(:connection_pool) && base.connection_pool pool = safe_connection_pool(base) { available: true, pool: pool_stats(pool), ping_ms: ping_ms(base) } rescue => e { error_class: e.class.name, error_message: e..to_s[0, 500] } end |
.current_time ⇒ Object
107 108 109 110 111 |
# File 'lib/dead_bro/collectors/database.rb', line 107 def current_time Process.clock_gettime(Process::CLOCK_MONOTONIC) rescue Time.now.to_f end |
.db_enabled? ⇒ Boolean
33 34 35 36 37 38 |
# File 'lib/dead_bro/collectors/database.rb', line 33 def db_enabled? DeadBro.configuration.respond_to?(:enable_db_stats) && DeadBro.configuration.enable_db_stats rescue false end |
.elapsed_ms(started) ⇒ Object
113 114 115 116 117 |
# File 'lib/dead_bro/collectors/database.rb', line 113 def elapsed_ms(started) ((current_time - started) * 1000.0).round(2) rescue nil end |
.ping_ms(base) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/dead_bro/collectors/database.rb', line 89 def ping_ms(base) started = current_time base.connection_pool.with_connection do |conn| sql = case conn.adapter_name.to_s.downcase when /mysql/ "SELECT 1" when /sqlite/ "SELECT 1" else "SELECT 1" end conn.select_value(sql) end elapsed_ms(started) rescue nil end |
.pool_stats(pool) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/dead_bro/collectors/database.rb', line 54 def pool_stats(pool) return {} unless pool { size: begin safe_integer(pool.size) rescue nil end, connections: begin safe_integer(pool.connections.size) rescue nil end, busy: begin safe_integer(pool.respond_to?(:busy) ? pool.busy : nil) rescue nil end, dead: begin safe_integer(pool.respond_to?(:dead) ? pool.dead : nil) rescue nil end, num_waiting: begin safe_integer(pool.respond_to?(:num_waiting) ? pool.num_waiting : nil) rescue nil end, automatic_reconnect: pool.respond_to?(:automatic_reconnect) ? !!pool.automatic_reconnect : nil } rescue {} end |
.safe_connection_pool(base) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/dead_bro/collectors/database.rb', line 40 def safe_connection_pool(base) if base.respond_to?(:connection_pool) base.connection_pool elsif base.respond_to?(:connection_handler) begin base.connection_handler.retrieve_connection_pool(base) rescue nil end end rescue nil end |
.safe_integer(value) ⇒ Object
119 120 121 122 123 |
# File 'lib/dead_bro/collectors/database.rb', line 119 def safe_integer(value) Integer(value) rescue nil end |