Fix exclude-databases for collector package

The pg_database collector was not respecting the --exclude-databases flag and causing problems where databases were not accessible. This now respects the list of databases to exclude.

- Adjusts the Collector create func to take a config struct instead of a logger. This allows more changes like this in the future. I figured we would need to do this at some point but I wasn't sure if we could hold off.
- Split the database size collection to a separate query when database is not excluded.
- Comment some probe code that was not useful/accurate

Signed-off-by: Joe Adams <github@joeadams.io>
This commit is contained in:
Joe Adams
2022-10-03 22:30:07 -04:00
parent 2197e73643
commit 799f3e15b2
7 changed files with 97 additions and 39 deletions

View File

@@ -14,8 +14,10 @@
package main
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
@@ -101,13 +103,16 @@ func main() {
os.Exit(1)
}
excludedDatabases := strings.Split(*excludeDatabases, ",")
logger.Log("msg", "Excluded databases", "databases", fmt.Sprintf("%v", excludedDatabases))
opts := []ExporterOpt{
DisableDefaultMetrics(*disableDefaultMetrics),
DisableSettingsMetrics(*disableSettingsMetrics),
AutoDiscoverDatabases(*autoDiscoverDatabases),
WithUserQueriesPath(*queriesPath),
WithConstantLabels(*constantLabelsList),
ExcludeDatabases(*excludeDatabases),
ExcludeDatabases(excludedDatabases),
IncludeDatabases(*includeDatabases),
}
@@ -128,6 +133,7 @@ func main() {
pe, err := collector.NewPostgresCollector(
logger,
excludedDatabases,
dsn,
[]string{},
)
@@ -143,7 +149,7 @@ func main() {
w.Write(landingPage) // nolint: errcheck
})
http.HandleFunc("/probe", handleProbe(logger))
http.HandleFunc("/probe", handleProbe(logger, excludedDatabases))
srv := &http.Server{}
if err := web.ListenAndServe(srv, webConfig, logger); err != nil {

View File

@@ -484,9 +484,9 @@ func AutoDiscoverDatabases(b bool) ExporterOpt {
}
// ExcludeDatabases allows to filter out result from AutoDiscoverDatabases
func ExcludeDatabases(s string) ExporterOpt {
func ExcludeDatabases(s []string) ExporterOpt {
return func(e *Exporter) {
e.excludeDatabases = strings.Split(s, ",")
e.excludeDatabases = s
}
}

View File

@@ -16,7 +16,6 @@ package main
import (
"fmt"
"net/http"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
@@ -26,7 +25,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func handleProbe(logger log.Logger) http.HandlerFunc {
func handleProbe(logger log.Logger, excludeDatabases []string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
conf := c.GetConfig()
@@ -62,21 +61,21 @@ func handleProbe(logger log.Logger) http.HandlerFunc {
// TODO(@sysadmind): Timeout
probeSuccessGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_success",
Help: "Displays whether or not the probe was a success",
})
probeDurationGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_duration_seconds",
Help: "Returns how long the probe took to complete in seconds",
})
// probeSuccessGauge := prometheus.NewGauge(prometheus.GaugeOpts{
// Name: "probe_success",
// Help: "Displays whether or not the probe was a success",
// })
// probeDurationGauge := prometheus.NewGauge(prometheus.GaugeOpts{
// Name: "probe_duration_seconds",
// Help: "Returns how long the probe took to complete in seconds",
// })
tl := log.With(logger, "target", target)
start := time.Now()
// start := time.Now()
registry := prometheus.NewRegistry()
registry.MustRegister(probeSuccessGauge)
registry.MustRegister(probeDurationGauge)
// registry.MustRegister(probeSuccessGauge)
// registry.MustRegister(probeDurationGauge)
opts := []ExporterOpt{
DisableDefaultMetrics(*disableDefaultMetrics),
@@ -96,10 +95,10 @@ func handleProbe(logger log.Logger) http.HandlerFunc {
registry.MustRegister(exporter)
// Run the probe
pc, err := collector.NewProbeCollector(tl, registry, dsn)
pc, err := collector.NewProbeCollector(tl, excludeDatabases, registry, dsn)
if err != nil {
probeSuccessGauge.Set(0)
probeDurationGauge.Set(time.Since(start).Seconds())
// probeSuccessGauge.Set(0)
// probeDurationGauge.Set(time.Since(start).Seconds())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -115,10 +114,6 @@ func handleProbe(logger log.Logger) http.HandlerFunc {
registry.MustRegister(pc)
duration := time.Since(start).Seconds()
probeDurationGauge.Set(duration)
probeSuccessGauge.Set(1)
// TODO check success, etc
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)