Skip to content

Commit

Permalink
add metric token
Browse files Browse the repository at this point in the history
  • Loading branch information
nkeonkeo committed Oct 6, 2024
1 parent 375b125 commit c1780ce
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
showVersion = kingpin.Flag("version", "Print version information").Default().Bool()
listenAddress = kingpin.Flag("web.listen-address", "Address on which to expose metrics and web interface").Default(":9427").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics").Default("/metrics").String()
metricsToken = kingpin.Flag("web.token", "Token (in http request headers of queries) which to expose metrics").Default("").String()
serverUseTLS = kingpin.Flag("web.tls.enabled", "Enable TLS for web server, default is false").Default().Bool()
serverTlsCertFile = kingpin.Flag("web.tls.cert-file", "The certificate file for the web server").Default("").String()
serverTlsKeyFile = kingpin.Flag("web.tls.key-file", "The key file for the web server").Default("").String()
Expand Down Expand Up @@ -293,7 +294,17 @@ func refreshDNS(tar *targets, monitor *mon.Monitor, cfg *config.Config) {
func startServer(cfg *config.Config, collector *pingCollector) {
var err error
log.Infof("Starting ping exporter (Version: %s)", version)
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if *metricsToken != "" {
token := r.URL.Query().Get("token")
if token == "" {
token = r.Header.Get("token")
}
if token != *metricsToken {
w.Write([]byte("wrong token"))

Check failure on line 304 in main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `w.Write` is not checked (errcheck)
return
}
}
fmt.Fprintf(w, indexHTML, *metricsPath)
})

Expand All @@ -307,7 +318,19 @@ func startServer(cfg *config.Config, collector *pingCollector) {
ErrorLog: l,
ErrorHandling: promhttp.ContinueOnError,
})
http.Handle(*metricsPath, h)
http.HandleFunc(*metricsPath, func(w http.ResponseWriter, r *http.Request) {
if *metricsToken != "" {
token := r.URL.Query().Get("token")
if token == "" {
token = r.Header.Get("token")
}
if token != *metricsToken {
w.Write([]byte("wrong token"))

Check failure on line 328 in main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `w.Write` is not checked (errcheck)
return
}
}
h.ServeHTTP(w, r)
})

server := http.Server{
Addr: *listenAddress,
Expand Down

0 comments on commit c1780ce

Please sign in to comment.