Add last_scrape_connection_error metric.

This metric is only set to 0 after the database has been connected to and a
Ping() request via the pg driver has been successfully sent.

Closes #135.
This commit is contained in:
Will Rouesnel 2017-11-29 12:42:03 +11:00
parent 93f9646385
commit df3a71eb79
1 changed files with 13 additions and 1 deletions

View File

@ -661,7 +661,9 @@ func dbToString(t interface{}) (string, bool) {
type Exporter struct { type Exporter struct {
dsn string dsn string
userQueriesPath string userQueriesPath string
duration, error prometheus.Gauge duration prometheus.Gauge
error prometheus.Gauge
connectionError prometheus.Gauge
userQueriesError *prometheus.GaugeVec userQueriesError *prometheus.GaugeVec
totalScrapes prometheus.Counter totalScrapes prometheus.Counter
@ -703,6 +705,12 @@ func NewExporter(dsn string, userQueriesPath string) *Exporter {
Name: "last_scrape_error", Name: "last_scrape_error",
Help: "Whether the last scrape of metrics from PostgreSQL resulted in an error (1 for error, 0 for success).", Help: "Whether the last scrape of metrics from PostgreSQL resulted in an error (1 for error, 0 for success).",
}), }),
connectionError: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "last_scrape_connection_error",
Help: "Whether the last scrape of metrics from PostgreSQL was able to connect to the server (1 for error, 0 for success).",
}),
userQueriesError: prometheus.NewGaugeVec(prometheus.GaugeOpts{ userQueriesError: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace, Namespace: namespace,
Subsystem: exporter, Subsystem: exporter,
@ -955,12 +963,16 @@ func (e *Exporter) getDB(conn string) (*sql.DB, error) {
if e.dbConnection == nil { if e.dbConnection == nil {
d, err := sql.Open("postgres", conn) d, err := sql.Open("postgres", conn)
if err != nil { if err != nil {
e.connectionError.Set(1)
return nil, err return nil, err
} }
err = d.Ping() err = d.Ping()
if err != nil { if err != nil {
e.connectionError.Set(1)
return nil, err return nil, err
} }
e.connectionError.Set(0)
d.SetMaxOpenConns(1) d.SetMaxOpenConns(1)
d.SetMaxIdleConns(1) d.SetMaxIdleConns(1)
e.dbConnection = d e.dbConnection = d