Class: NewRelic::PostgresPlugin::Agent

Inherits:
NewRelic::Plugin::Agent::Base
  • Object
show all
Defined in:
lib/newrelic_postgres_plugin/agent.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Agent

Returns a new instance of Agent.



20
21
22
23
# File 'lib/newrelic_postgres_plugin/agent.rb', line 20

def initialize(*args)
  @previous_metrics = {}
  super
end

Instance Method Details

#connectObject

Get a connection to postgres



41
42
43
# File 'lib/newrelic_postgres_plugin/agent.rb', line 41

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

Returns:



48
49
50
# File 'lib/newrelic_postgres_plugin/agent.rb', line 48

def nine_two?
  @connection.server_version >= 90200
end

#poll_cycleObject

This is called on every polling cycle



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/newrelic_postgres_plugin/agent.rb', line 56

def poll_cycle
  @connection = self.connect

  report_backend_metrics
  report_bgwriter_metrics
  report_database_metrics
  report_index_metrics

rescue => e
  $stderr.puts "#{e}: #{e.backtrace.join("\n  ")}"
ensure
  @connection.finish if @connection
end

#portObject

You do not have to specify the postgres port in the yaml if you don’t want to.



34
35
36
# File 'lib/newrelic_postgres_plugin/agent.rb', line 34

def port
  @port || 5432
end

#report_backend_metricsObject



79
80
81
82
83
84
# File 'lib/newrelic_postgres_plugin/agent.rb', line 79

def report_backend_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
end

#report_bgwriter_metricsObject



108
109
110
111
112
113
# File 'lib/newrelic_postgres_plugin/agent.rb', line 108

def report_bgwriter_metrics
  @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
end

#report_cache_metricsObject



119
120
121
# File 'lib/newrelic_postgres_plugin/agent.rb', line 119

def report_cache_metrics
  report_metric "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})
end

#report_database_metricsObject



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 86

def report_database_metrics
  @connection.exec(database_query) do |result|
    result.each do |row|
      report_metric         "Database/Backends",                        '', row['numbackends'].to_i
      report_derived_metric "Database/Transactions/Committed",          '', row['xact_commit'].to_i
      report_derived_metric "Database/Transactions/Rolled Back",        '', row['xact_rollback'].to_i
      report_derived_metric "Database/Tuples/Returned/From Sequential", '', row['tup_returned'].to_i
      report_derived_metric "Database/Tuples/Returned/From Bitmap",     '', row['tup_fetched'].to_i
      report_derived_metric "Database/Tuples/Writes/Inserts",           '', row['tup_inserted'].to_i
      report_derived_metric "Database/Tuples/Writes/Updates",           '', row['tup_updated'].to_i
      report_derived_metric "Database/Tuples/Writes/Deletes",           '', row['tup_deleted'].to_i
      report_derived_metric "Database/Conflicts",                       '', row['conflicts'].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
end

#report_derived_metric(name, units, value) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/newrelic_postgres_plugin/agent.rb', line 70

def report_derived_metric(name, units, value)
  if previous_value = @previous_metrics[name]
    report_metric name, units, (value - previous_value)
  else
    report_metric name, units, 0
  end
  @previous_metrics[name] = value
end

#report_index_metricsObject



115
116
117
# File 'lib/newrelic_postgres_plugin/agent.rb', line 115

def report_index_metrics
  report_metric "Indexes/Miss Ratio", '%', calculate_miss_ratio(%Q{SELECT SUM(idx_blks_hit) AS hits, SUM(idx_blks_read) AS reads FROM pg_statio_user_indexes})
end

#setup_metricsObject

Required, but not used



28
29
# File 'lib/newrelic_postgres_plugin/agent.rb', line 28

def setup_metrics
end