-
Notifications
You must be signed in to change notification settings - Fork 0
/
probe.go
111 lines (94 loc) · 2.77 KB
/
probe.go
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"context"
"fmt"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
"github.com/webdevops/fenecon-exporter/fenecon"
)
const (
DefaultTimeout = 30
)
func newFeneconProber(ctx context.Context, registry *prometheus.Registry, logger *zap.SugaredLogger) *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(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.Warnln(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// param: meters
if val, err := paramsGetInt(r.URL.Query(), "meter"); err == nil {
if val != nil && *val >= 0 {
target.Meter = *val
}
} else {
contextLogger.Warnln(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// param: chargers
if val, err := paramsGetInt(r.URL.Query(), "charger"); err == nil {
if val != nil && *val >= 0 {
target.Charger = *val
}
} else {
contextLogger.Warnln(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// param: ess
if val, err := paramsGetInt(r.URL.Query(), "ess"); err == nil {
if val != nil && *val >= 0 {
target.Ess = *val
}
} else {
contextLogger.Warnln(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(r *http.Request) *zap.SugaredLogger {
return logger.With(zap.String("requestPath", r.URL.Path))
}