-
Notifications
You must be signed in to change notification settings - Fork 3
/
bmc_exporter.go
173 lines (155 loc) · 5.13 KB
/
bmc_exporter.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"context"
"fmt"
"log"
"net"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"runtime"
"sync"
"syscall"
"time"
"github.com/gebn/bmc_exporter/bmc/collector"
"github.com/gebn/bmc_exporter/bmc/target"
"github.com/gebn/bmc_exporter/handler/bmc"
"github.com/gebn/bmc_exporter/handler/root"
"github.com/gebn/bmc_exporter/session/file"
"github.com/alecthomas/kingpin"
"github.com/gebn/go-stamp/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/automaxprocs/maxprocs"
)
var (
namespace = "bmc"
subsystem = "exporter"
buildInfo = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "build_info",
Help: "The version and commit of the running exporter. Constant 1.",
},
// the runtime version is already exposed by the default Go collector
[]string{"version", "commit"},
)
buildTime = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "build_time_seconds",
Help: "When the running exporter was built, as seconds since the Unix Epoch.",
})
requestDuration = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "request_duration_seconds",
Help: "The time taken to execute the handlers of web server " +
"endpoints.",
},
[]string{"path"},
)
help = "An IPMI v2.0 Prometheus exporter."
listenAddr = kingpin.Flag("web.listen-address", "Address on which to "+
"expose metrics.").
Default(":9622").
String()
scrapeTimeout = kingpin.Flag("scrape.timeout", "Maximum time allowed for "+
"each request to /bmc, including time spent queuing. The aim is to "+
"return what we have rather than Prometheus give up and throw "+
"everything away, so this value should be slightly shorter than the "+
"scrape_timeout.").
Default("9s"). // network RTT
Duration()
collectTimeout = kingpin.Flag("collect.timeout", "Maximum time allowed "+
"for a single scrape to query the BMC once it has reached the front "+
"of the queue. After this, the exporter will return what is has. "+
"This parameter is most useful to ensure fairness when the exporter "+
"is being scraped by multiple Prometheis.").
Default("9s"). // network RTT
Duration()
secretsStatic = kingpin.Flag("secrets.static", "Credentials file used by "+
"the static session provider.").
Default("secrets.yml").
String() // we don't use ExistingFile() due to kingpin issue #261
)
func init() {
maxprocs.Set() // use the library this way to avoid logging when CPU quota is undefined
for _, path := range []string{"/", "/bmc", "/metrics"} {
requestDuration.WithLabelValues(path)
}
buildInfo.WithLabelValues(stamp.Version, stamp.Commit).Set(1)
buildTime.Set(float64(stamp.Time().UnixNano()) / float64(time.Second))
runtime.SetMutexProfileFraction(5)
}
func main() {
kingpin.CommandLine.Help = help
kingpin.Version(stamp.Summary())
kingpin.Parse()
provider, err := file.New(*secretsStatic)
if err != nil {
log.Fatal(err)
}
mapper := target.NewMapper(target.ProviderFunc(func(addr string) *target.Target {
return target.New(&collector.Collector{
Target: addr,
Provider: provider,
Timeout: *collectTimeout,
})
}))
defer mapper.Close()
registerHandler("/", root.Handler())
registerHandler("/bmc", bmc.Handler(mapper, *scrapeTimeout))
registerHandler("/metrics", promhttp.Handler())
listener, err := net.Listen("tcp", *listenAddr)
if err != nil {
log.Printf("failed to start web server: %v", err)
return
}
defer listener.Close()
srv := &http.Server{
// solves us waiting indefinitely before we get to a handler; handlers
// are capable of timing out themselves. This isn't intended to ensure
// we have time to do something useful with the request - it is only to
// avoid a possible goroutine leak (#39).
ReadHeaderTimeout: *scrapeTimeout,
// this is above the max recommended scrape interval
// (https://stackoverflow.com/a/40233721)
IdleTimeout: time.Minute * 3,
}
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
if err := srv.Serve(listener); err != http.ErrServerClosed {
// without the wait group, this line may not be printed in case of
// failure
log.Printf("server did not close cleanly: %v", err)
}
}()
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
<-quit
fmt.Println() // avoids "^C" being printed on the same line as the log date
log.Println("waiting for in-progress requests to finish...")
if err := srv.Shutdown(context.Background()); err != nil {
// either a context or listener error, and it cannot be the former as
// we're using the background ctx
log.Printf("failed to close listener: %v", err)
}
wg.Wait()
}
// registerHandler adds an instrumented version of the provided handler to the
// default mux at the indicated path.
func registerHandler(path string, handler http.Handler) {
http.Handle(path, promhttp.InstrumentHandlerDuration(
requestDuration.MustCurryWith(prometheus.Labels{
"path": path,
}),
handler,
))
}