The config module supports adding configuration to the exporter via a config file. This supports adding authentication details in a config file so that /probe requests can specify authentication for endpoints Signed-off-by: Joe Adams <github@joeadams.io>
114 lines
3.5 KiB
Go
114 lines
3.5 KiB
Go
// 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 (
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/go-kit/log"
|
|
"github.com/go-kit/log/level"
|
|
"github.com/prometheus-community/postgres_exporter/collector"
|
|
"github.com/prometheus-community/postgres_exporter/config"
|
|
"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()
|
|
conf := c.GetConfig()
|
|
params := r.URL.Query()
|
|
target := params.Get("target")
|
|
if target == "" {
|
|
http.Error(w, "target is required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
var authModule config.AuthModule
|
|
authModuleName := params.Get("auth_module")
|
|
if authModuleName == "" {
|
|
level.Info(logger).Log("msg", "no auth_module specified, using default")
|
|
} else {
|
|
var ok bool
|
|
authModule, ok = conf.AuthModules[authModuleName]
|
|
if !ok {
|
|
http.Error(w, fmt.Sprintf("auth_module %s not found", authModuleName), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if authModule.UserPass.Username == "" || authModule.UserPass.Password == "" {
|
|
http.Error(w, fmt.Sprintf("auth_module %s has no username or password", authModuleName), http.StatusBadRequest)
|
|
return
|
|
}
|
|
}
|
|
|
|
dsn, err := authModule.ConfigureTarget(target)
|
|
if err != nil {
|
|
level.Error(logger).Log("msg", "failed to configure target", "err", err)
|
|
http.Error(w, fmt.Sprintf("could not configure dsn for target: %v", err), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// TODO: 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",
|
|
})
|
|
|
|
tl := log.With(logger, "target", target)
|
|
|
|
start := time.Now()
|
|
registry := prometheus.NewRegistry()
|
|
registry.MustRegister(probeSuccessGauge)
|
|
registry.MustRegister(probeDurationGauge)
|
|
|
|
// Run the probe
|
|
pc, err := collector.NewProbeCollector(tl, registry, dsn)
|
|
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)
|
|
}
|
|
}
|