Skip to content

Commit

Permalink
Cleanup exporter metrics
Browse files Browse the repository at this point in the history
* Remove scrape counter, covered by promhttp metrics.
* Combine up and error metrics to simplify use.
* Remove scrape duration counter, covered by `scrape_duration_seconds`.
* Use Const metric for `up` metric.

Signed-off-by: Ben Kochie <[email protected]>
  • Loading branch information
SuperQ committed Jul 6, 2020
1 parent 783865b commit 276db73
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 43 deletions.
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
50 changes: 10 additions & 40 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,29 @@ 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()
var up float64 = 1.0

e.mutex.RLock()
defer e.mutex.RUnlock()

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
3 changes: 0 additions & 3 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ type ColumnMapping struct {
type Exporter struct {
mutex sync.RWMutex

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

metricMap map[string]MetricMapNamespace

db *sql.DB
Expand Down

0 comments on commit 276db73

Please sign in to comment.