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

PMM-9973 Resolutions fix #2 #73

Merged
merged 5 commits into from
Sep 6, 2022
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
101 changes: 98 additions & 3 deletions cmd/postgres_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
package main

import (
"fmt"
"net/http"
"os"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus-community/postgres_exporter/collector"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
Expand Down Expand Up @@ -113,11 +115,12 @@ func main() {
exporter.servers.Close()
}()

prometheus.MustRegister(version.NewCollector(exporterName))
versionCollector := version.NewCollector(exporterName)
prometheus.MustRegister(versionCollector)

prometheus.MustRegister(exporter)

cleanup := initializePerconaExporters(dsn, opts)
cleanup, hr, mr, lr := initializePerconaExporters(dsn, opts)
defer cleanup()

pe, err := collector.NewPostgresCollector(
Expand All @@ -131,7 +134,22 @@ func main() {
}
prometheus.MustRegister(pe)

http.Handle(*metricPath, promhttp.Handler())
psCollector := collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})
goCollector := collectors.NewGoCollector()

promHandler := newHandler(map[string]prometheus.Collector{
"exporter": exporter,
"custom_query.hr": hr,
"custom_query.mr": mr,
"custom_query.lr": lr,
"standard.process": psCollector,
"standard.go": goCollector,
"version": versionCollector,
"postgres": pe,
})

http.Handle(*metricPath, promHandler)

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=UTF-8") // nolint: errcheck
w.Write(landingPage) // nolint: errcheck
Expand All @@ -152,3 +170,80 @@ func main() {
os.Exit(1)
}
}

// handler wraps an unfiltered http.Handler but uses a filtered handler,
// created on the fly, if filtering is requested. Create instances with
// newHandler. It used for collectors filtering.
type handler struct {
unfilteredHandler http.Handler
collectors map[string]prometheus.Collector
}

func newHandler(collectors map[string]prometheus.Collector) *handler {
h := &handler{collectors: collectors}

innerHandler, err := h.innerHandler()
if err != nil {
level.Error(logger).Log("msg", "Couldn't create metrics handler", "error", err)
os.Exit(1)
}

h.unfilteredHandler = innerHandler
return h
}

// ServeHTTP implements http.Handler.
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
filters := r.URL.Query()["collect[]"]
level.Debug(logger).Log("msg", "Collect query", "filters", filters)

if len(filters) == 0 {
// No filters, use the prepared unfiltered handler.
h.unfilteredHandler.ServeHTTP(w, r)
return
}

filteredHandler, err := h.innerHandler(filters...)
if err != nil {
level.Warn(logger).Log("msg", "Couldn't create filtered metrics handler", "error", err)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("Couldn't create filtered metrics handler: %s", err))) // nolint: errcheck
return
}

filteredHandler.ServeHTTP(w, r)
}

func (h *handler) innerHandler(filters ...string) (http.Handler, error) {
registry := prometheus.NewRegistry()

// register all collectors by default.
if len(filters) == 0 {
for name, c := range h.collectors {
if err := registry.Register(c); err != nil {
return nil, err
}
level.Debug(logger).Log("msg", "Collector was registered", "collector", name)
}
}

// register only filtered collectors.
for _, name := range filters {
if c, ok := h.collectors[name]; ok {
if err := registry.Register(c); err != nil {
return nil, err
}
level.Debug(logger).Log("msg", "Collector was registered", "collector", name)
}
}

handler := promhttp.HandlerFor(
registry,
promhttp.HandlerOpts{
//ErrorLog: log.NewNopLogger() .NewErrorLogger(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this.

ErrorHandling: promhttp.ContinueOnError,
},
)

return handler, nil
}
4 changes: 2 additions & 2 deletions cmd/postgres_exporter/percona_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var (
collectCustomQueryHrDirectory = kingpin.Flag("collect.custom_query.hr.directory", "Path to custom queries with high resolution directory.").Envar("PG_EXPORTER_EXTEND_QUERY_HR_PATH").String()
)

func initializePerconaExporters(dsn []string, opts []ExporterOpt) func() {
func initializePerconaExporters(dsn []string, opts []ExporterOpt) (func(), *Exporter, *Exporter, *Exporter) {
queriesPath := map[MetricResolution]string{
HR: *collectCustomQueryHrDirectory,
MR: *collectCustomQueryMrDirectory,
Expand Down Expand Up @@ -96,7 +96,7 @@ func initializePerconaExporters(dsn []string, opts []ExporterOpt) func() {
hrExporter.servers.Close()
mrExporter.servers.Close()
lrExporter.servers.Close()
}
}, hrExporter, mrExporter, lrExporter
}

func (e *Exporter) loadCustomQueries(res MetricResolution, version semver.Version, server *Server) {
Expand Down