You've already forked postgres_exporter
mirror of
https://github.com/prometheus-community/postgres_exporter.git
synced 2025-08-09 15:42:47 +03:00
WIP: Add prelim multi-target support
- Remove multi server support from new collector package - Add http handler for multi-target support Signed-off-by: Joe Adams <github@joeadams.io>
This commit is contained in:
@@ -162,6 +162,12 @@ func getDataSources() ([]string, error) {
|
||||
uri = os.Getenv("DATA_SOURCE_URI")
|
||||
}
|
||||
|
||||
// No datasources found. This allows us to support the multi-target pattern
|
||||
// withouth an explicit datasource.
|
||||
if uri == "" {
|
||||
return []string{}, nil
|
||||
}
|
||||
|
||||
dsn = "postgresql://" + ui + "@" + uri
|
||||
|
||||
return []string{dsn}, nil
|
||||
|
@@ -85,16 +85,17 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
dsn, err := getDataSources()
|
||||
dsns, err := getDataSources()
|
||||
if err != nil {
|
||||
level.Error(logger).Log("msg", "Failed reading data sources", "err", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if len(dsn) == 0 {
|
||||
level.Error(logger).Log("msg", "Couldn't find environment variables describing the datasource to use")
|
||||
os.Exit(1)
|
||||
}
|
||||
// TODO(@sysadmind): Remove this with multi-target support
|
||||
// if len(dsn) == 0 {
|
||||
// level.Error(logger).Log("msg", "Couldn't find environment variables describing the datasource to use")
|
||||
// os.Exit(1)
|
||||
// }
|
||||
|
||||
opts := []ExporterOpt{
|
||||
DisableDefaultMetrics(*disableDefaultMetrics),
|
||||
@@ -106,7 +107,7 @@ func main() {
|
||||
IncludeDatabases(*includeDatabases),
|
||||
}
|
||||
|
||||
exporter := NewExporter(dsn, opts...)
|
||||
exporter := NewExporter(dsns, opts...)
|
||||
defer func() {
|
||||
exporter.servers.Close()
|
||||
}()
|
||||
@@ -115,6 +116,12 @@ func main() {
|
||||
|
||||
prometheus.MustRegister(exporter)
|
||||
|
||||
// TODO(@sysadmind): Remove this with multi-target support. We are removing multiple DSN support
|
||||
dsn := ""
|
||||
if len(dsns) > 0 {
|
||||
dsn = dsns[0]
|
||||
}
|
||||
|
||||
pe, err := collector.NewPostgresCollector(
|
||||
logger,
|
||||
dsn,
|
||||
@@ -122,9 +129,9 @@ func main() {
|
||||
)
|
||||
if err != nil {
|
||||
level.Error(logger).Log("msg", "Failed to create PostgresCollector", "err", err.Error())
|
||||
os.Exit(1)
|
||||
} else {
|
||||
prometheus.MustRegister(pe)
|
||||
}
|
||||
prometheus.MustRegister(pe)
|
||||
|
||||
http.Handle(*metricPath, promhttp.Handler())
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -132,6 +139,8 @@ func main() {
|
||||
w.Write(landingPage) // nolint: errcheck
|
||||
})
|
||||
|
||||
http.HandleFunc("/probe", handleProbe(logger))
|
||||
|
||||
level.Info(logger).Log("msg", "Listening on address", "address", *listenAddress)
|
||||
srv := &http.Server{Addr: *listenAddress}
|
||||
if err := web.ListenAndServe(srv, *webConfig, logger); err != nil {
|
||||
|
91
cmd/postgres_exporter/probe.go
Normal file
91
cmd/postgres_exporter/probe.go
Normal file
@@ -0,0 +1,91 @@
|
||||
// Copyright 2022 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/go-kit/log"
|
||||
"github.com/prometheus-community/postgres_exporter/collector"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
func handleProbe(logger log.Logger) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
params := r.URL.Query()
|
||||
target := params.Get("target")
|
||||
if target == "" {
|
||||
http.Error(w, "target is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: Timeout
|
||||
// TODO: Auth Module
|
||||
|
||||
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)
|
||||
_ = tl
|
||||
|
||||
start := time.Now()
|
||||
registry := prometheus.NewRegistry()
|
||||
registry.MustRegister(probeSuccessGauge)
|
||||
registry.MustRegister(probeDurationGauge)
|
||||
|
||||
// TODO(@sysadmind): this is a temp hack until we have a proper auth module
|
||||
target = "postgres://postgres:test@localhost:5432/circle_test?sslmode=disable"
|
||||
|
||||
// Run the probe
|
||||
pc, err := collector.NewProbeCollector(tl, registry, target)
|
||||
if err != nil {
|
||||
probeSuccessGauge.Set(0)
|
||||
probeDurationGauge.Set(time.Since(start).Seconds())
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
_ = ctx
|
||||
|
||||
// TODO: Which way should this be? Register or handle the collection manually?
|
||||
// Also, what about the context?
|
||||
|
||||
// Option 1: Register the collector
|
||||
registry.MustRegister(pc)
|
||||
|
||||
// Option 2: Handle the collection manually. This allows us to collect duration metrics.
|
||||
// The collectors themselves already support their own duration metrics.
|
||||
// err = pc.Update(ctx)
|
||||
// if err != nil {
|
||||
// probeSuccessGauge.Set(0)
|
||||
// } else {
|
||||
// probeSuccessGauge.Set(1)
|
||||
// }
|
||||
|
||||
duration := time.Since(start).Seconds()
|
||||
probeDurationGauge.Set(duration)
|
||||
|
||||
// TODO check success, etc
|
||||
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
|
||||
h.ServeHTTP(w, r)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user