Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup exporter metrics #33

Merged
merged 1 commit into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## master / unreleased

* [CHANGE] Cleanup exporter metrics #33

## 0.3.0 / 2020-05-27

* [CHANGE] Switch logging to promlog #29
Expand Down
53 changes: 10 additions & 43 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ var (
"The pgbouncer version info",
[]string{"version"}, nil,
)
scrapeSuccessDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"),
"The pgbouncer scrape succeeded",
nil, nil,
)
)

func NewExporter(connectionString string, namespace string, logger log.Logger) *Exporter {
Expand All @@ -93,29 +98,6 @@ func NewExporter(connectionString string, namespace string, logger log.Logger) *
metricMap: makeDescMap(metricMaps, namespace, logger),
db: db,
logger: logger,
up: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "up",
Help: "Was the PgBouncer instance query successful?",
}),

duration: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "last_scrape_duration_seconds",
Help: "Duration of the last scrape of metrics from PgBouncer.",
}),

totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "scrapes_total",
Help: "Total number of times PgBouncer has been scraped for metrics.",
}),

error: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "last_scrape_error",
Help: "Whether the last scrape of metrics from PgBouncer resulted in an error (1 for error, 0 for success).",
}),
}
}

Expand Down Expand Up @@ -353,41 +335,26 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {

// Collect implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.scrape(ch)
ch <- e.duration
ch <- e.up
ch <- e.totalScrapes
ch <- e.error
}

func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
defer func(begun time.Time) {
e.duration.Set(time.Since(begun).Seconds())
}(time.Now())
level.Info(e.logger).Log("msg", "Starting scrape")

e.error.Set(0)
e.totalScrapes.Inc()

e.mutex.RLock()
defer e.mutex.RUnlock()
var up float64 = 1.0

err := queryVersion(ch, e.db)
if err != nil {
level.Error(e.logger).Log("msg", "error getting version", "err", err)
e.error.Set(1)
up = 0
}

errMap := queryNamespaceMappings(ch, e.db, e.metricMap, e.logger)
if len(errMap) > 0 {
level.Error(e.logger).Log("msg", "error querying namespace mappings", "err", errMap)
e.error.Set(1)
up = 0
}

e.up.Set(1)
if len(errMap) == len(e.metricMap) {
e.up.Set(0)
up = 0
}
ch <- prometheus.MustNewConstMetric(scrapeSuccessDesc, prometheus.GaugeValue, up)
}

// Turn the MetricMap column mapping into a prometheus descriptor mapping.
Expand Down
6 changes: 0 additions & 6 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package main
import (
"database/sql"
"fmt"
"sync"

"github.com/go-kit/kit/log"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -103,11 +102,6 @@ type ColumnMapping struct {
// Exporter collects PgBouncer stats from the given server and exports
// them using the prometheus metrics package.
type Exporter struct {
mutex sync.RWMutex

duration, up, error prometheus.Gauge
totalScrapes prometheus.Counter

metricMap map[string]MetricMapNamespace

db *sql.DB
Expand Down