-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprobe.go
More file actions
82 lines (68 loc) · 2.22 KB
/
probe.go
File metadata and controls
82 lines (68 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"context"
"fmt"
"log/slog"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/webdevops/go-common/log/slogger"
"github.com/webdevops/fenecon-exporter/fenecon"
)
const (
DefaultTimeout = 30
)
func newFeneconProber(ctx context.Context, registry *prometheus.Registry, logger *slogger.Logger) *fenecon.FeneconProber {
sp := fenecon.New(ctx, registry, logger)
sp.SetUserAgent(UserAgent + gitTag)
sp.SetTimeout(Opts.Fenecon.Request.Timeout)
sp.SetParallelRequests(Opts.Fenecon.Request.Parallel)
sp.SetRetry(
Opts.Fenecon.Request.RetryCount,
Opts.Fenecon.Request.RetryWaitTime,
Opts.Fenecon.Request.RetryMaxWaitTime,
)
if len(Opts.Fenecon.Auth.Password) >= 1 {
sp.SetHttpAuth(Opts.Fenecon.Auth.Username, Opts.Fenecon.Auth.Password)
}
return sp
}
func probeFenecon(w http.ResponseWriter, r *http.Request) {
var (
err error
timeoutSeconds float64
target fenecon.FeneconProberTarget
)
// startTime := time.Now()
contextLogger := buildContextLoggerFromRequest(r)
registry := prometheus.NewRegistry()
// If a timeout is configured via the Prometheus header, add it to the request.
timeoutSeconds, err = getPrometheusTimeout(r, DefaultTimeout)
if err != nil {
contextLogger.Error("failed to parse prometheus timeout", slog.Any("error", err))
http.Error(w, fmt.Sprintf("failed to parse timeout from Prometheus header: %s", err), http.StatusBadRequest)
return
}
// param: target
if val, err := paramsGetRequired(r.URL.Query(), "target"); err == nil {
target.Target = val
} else {
contextLogger.Warn("failed to parse target", slog.Any("error", err))
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeoutSeconds*float64(time.Second)))
defer cancel()
r = r.WithContext(ctx)
prober := newFeneconProber(ctx, registry, contextLogger)
prober.Run(target)
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
func buildContextLoggerFromRequest(req *http.Request) *slogger.Logger {
return logger.With(
slog.String("method", req.Method),
slog.String("url", req.URL.String()),
)
}