Class: NewRelic::PostgresPlugin::Agent
- Inherits:
-
NewRelic::Plugin::Agent::Base
- Object
- NewRelic::Plugin::Agent::Base
- NewRelic::PostgresPlugin::Agent
- Defined in:
- lib/newrelic_postgres_plugin/agent.rb
Instance Method Summary collapse
-
#connect ⇒ Object
Get a connection to postgres.
-
#initialize(*args) ⇒ Agent
constructor
A new instance of Agent.
-
#nine_two? ⇒ Boolean
Returns true if we’re talking to Postgres version >= 9.2.
-
#poll_cycle ⇒ Object
This is called on every polling cycle.
-
#port ⇒ Object
You do not have to specify the postgres port in the yaml if you don’t want to.
- #report_metrics ⇒ Object
-
#setup_metrics ⇒ Object
Required, but not used.
Constructor Details
#initialize(*args) ⇒ Agent
20 21 22 23 24 |
# File 'lib/newrelic_postgres_plugin/agent.rb', line 20 def initialize(*args) @previous_metrics = {} @previous_result_for_query ||= {} super end |
Instance Method Details
#connect ⇒ Object
Get a connection to postgres
42 43 44 |
# File 'lib/newrelic_postgres_plugin/agent.rb', line 42 def connect PG::Connection.new(:host => host, :port => port, :user => user, :password => password, :sslmode => sslmode, :dbname => dbname) end |
#nine_two? ⇒ Boolean
Returns true if we’re talking to Postgres version >= 9.2
49 50 51 |
# File 'lib/newrelic_postgres_plugin/agent.rb', line 49 def nine_two? @connection.server_version >= 90200 end |
#poll_cycle ⇒ Object
This is called on every polling cycle
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/newrelic_postgres_plugin/agent.rb', line 56 def poll_cycle @connection = self.connect report_metrics rescue => e $stderr.puts "#{e}: #{e.backtrace.join("\n ")}" ensure @connection.finish if @connection end |
#port ⇒ Object
You do not have to specify the postgres port in the yaml if you don’t want to.
35 36 37 |
# File 'lib/newrelic_postgres_plugin/agent.rb', line 35 def port @port || 5432 end |
#report_metrics ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/newrelic_postgres_plugin/agent.rb', line 67 def report_metrics @connection.exec(backend_query) do |result| report_metric "Backends/Active", 'connections', result[0]['backends_active'] report_metric "Backends/Idle", 'connections', result[0]['backends_idle'] end @connection.exec(database_query) do |result| result.each do |row| report_metric "Database/Backends", '', row['numbackends'].to_i report_derived_metric "Database/Transactions/Committed", 'transactions', row['xact_commit'].to_i report_derived_metric "Database/Transactions/Rolled Back", 'transactions', row['xact_rollback'].to_i report_derived_metric "Database/Rows/Selected", 'rows', row['tup_returned'].to_i + row['tup_fetched'].to_i report_derived_metric "Database/Rows/Inserted", 'rows', row['tup_inserted'].to_i report_derived_metric "Database/Rows/Updated", 'rows', row['tup_updated'].to_i report_derived_metric "Database/Rows/Deleted", 'rows', row['tup_deleted'].to_i end end @connection.exec(index_count_query) do |result| report_metric "Database/Indexes/Count", 'indexes', result[0]['indexes'].to_i end @connection.exec(index_size_query) do |result| report_metric "Database/Indexes/Size", 'bytes', result[0]['size'].to_i end @connection.exec(bgwriter_query) do |result| report_derived_metric "Background Writer/Checkpoints/Scheduled", 'checkpoints', result[0]['checkpoints_timed'].to_i report_derived_metric "Background Writer/Checkpoints/Requested", 'checkpoints', result[0]['checkpoints_requests'].to_i end report_metric "Alerts/Index Miss Ratio", '%', calculate_miss_ratio(%Q{SELECT SUM(idx_blks_hit) AS hits, SUM(idx_blks_read) AS reads FROM pg_statio_user_indexes}) report_metric "Alerts/Cache Miss Ratio", '%', calculate_miss_ratio(%Q{SELECT SUM(heap_blks_hit) AS hits, SUM(heap_blks_read) AS reads FROM pg_statio_user_tables}) # This is dependent on the pg_stat_statements being loaded, and assumes that pg_stat_statements.max has been set sufficiently high that most queries will be recorded. If your application typically generates more than 1000 distinct query plans per sampling interval, you're going to have a bad time. if extension_loaded? "pg_stat_statements" @connection.exec("SELECT SUM(calls) FROM pg_stat_statements") do |result| report_derived_metric "Database/Statements", '', result[0]["sum"].to_i end else puts "pg_stat_statements is not loaded; no Database/Statements metric will be reported." end end |
#setup_metrics ⇒ Object
Required, but not used
29 30 |
# File 'lib/newrelic_postgres_plugin/agent.rb', line 29 def setup_metrics end |