|
| 1 | +// Copyright 2022 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | +"fmt" |
| 18 | +"net/http" |
| 19 | +"time" |
| 20 | + |
| 21 | +"github.com/go-kit/log" |
| 22 | +"github.com/go-kit/log/level" |
| 23 | +"github.com/prometheus-community/postgres_exporter/collector" |
| 24 | +"github.com/prometheus-community/postgres_exporter/config" |
| 25 | +"github.com/prometheus/client_golang/prometheus" |
| 26 | +"github.com/prometheus/client_golang/prometheus/promhttp" |
| 27 | +) |
| 28 | + |
| 29 | +func handleProbe(logger log.Logger) http.HandlerFunc { |
| 30 | +return func(w http.ResponseWriter, r *http.Request) { |
| 31 | +ctx := r.Context() |
| 32 | +conf := c.GetConfig() |
| 33 | +params := r.URL.Query() |
| 34 | +target := params.Get("target") |
| 35 | +if target == "" { |
| 36 | +http.Error(w, "target is required", http.StatusBadRequest) |
| 37 | +return |
| 38 | +} |
| 39 | +var authModule config.AuthModule |
| 40 | +authModuleName := params.Get("auth_module") |
| 41 | +if authModuleName == "" { |
| 42 | +level.Info(logger).Log("msg", "no auth_module specified, using default") |
| 43 | +} else { |
| 44 | +var ok bool |
| 45 | +authModule, ok = conf.AuthModules[authModuleName] |
| 46 | +if !ok { |
| 47 | +http.Error(w, fmt.Sprintf("auth_module %s not found", authModuleName), http.StatusBadRequest) |
| 48 | +return |
| 49 | +} |
| 50 | +if authModule.UserPass.Username == "" || authModule.UserPass.Password == "" { |
| 51 | +http.Error(w, fmt.Sprintf("auth_module %s has no username or password", authModuleName), http.StatusBadRequest) |
| 52 | +return |
| 53 | +} |
| 54 | +} |
| 55 | + |
| 56 | +dsn, err := authModule.ConfigureTarget(target) |
| 57 | +if err != nil { |
| 58 | +level.Error(logger).Log("msg", "failed to configure target", "err", err) |
| 59 | +http.Error(w, fmt.Sprintf("could not configure dsn for target: %v", err), http.StatusBadRequest) |
| 60 | +return |
| 61 | +} |
| 62 | + |
| 63 | +// TODO(@sysadmind): Timeout |
| 64 | + |
| 65 | +probeSuccessGauge := prometheus.NewGauge(prometheus.GaugeOpts{ |
| 66 | +Name: "probe_success", |
| 67 | +Help: "Displays whether or not the probe was a success", |
| 68 | +}) |
| 69 | +probeDurationGauge := prometheus.NewGauge(prometheus.GaugeOpts{ |
| 70 | +Name: "probe_duration_seconds", |
| 71 | +Help: "Returns how long the probe took to complete in seconds", |
| 72 | +}) |
| 73 | + |
| 74 | +tl := log.With(logger, "target", target) |
| 75 | + |
| 76 | +start := time.Now() |
| 77 | +registry := prometheus.NewRegistry() |
| 78 | +registry.MustRegister(probeSuccessGauge) |
| 79 | +registry.MustRegister(probeDurationGauge) |
| 80 | + |
| 81 | +// Run the probe |
| 82 | +pc, err := collector.NewProbeCollector(tl, registry, dsn) |
| 83 | +if err != nil { |
| 84 | +probeSuccessGauge.Set(0) |
| 85 | +probeDurationGauge.Set(time.Since(start).Seconds()) |
| 86 | +http.Error(w, err.Error(), http.StatusInternalServerError) |
| 87 | +return |
| 88 | +} |
| 89 | + |
| 90 | +// TODO(@sysadmind): Remove the registry.MustRegister() call below and instead handle the collection here. That will allow |
| 91 | +// for the passing of context, handling of timeouts, and more control over the collection. |
| 92 | +// The current NewProbeCollector() implementation relies on the MustNewConstMetric() call to create the metrics which is not |
| 93 | +// ideal to use without the registry.MustRegister() call. |
| 94 | +_ = ctx |
| 95 | + |
| 96 | +registry.MustRegister(pc) |
| 97 | + |
| 98 | +duration := time.Since(start).Seconds() |
| 99 | +probeDurationGauge.Set(duration) |
| 100 | + |
| 101 | +// TODO check success, etc |
| 102 | +h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{}) |
| 103 | +h.ServeHTTP(w, r) |
| 104 | +} |
| 105 | +} |
0 commit comments