prometheus plugin support read bearer token file

This commit is contained in:
Ulric Qin 2022-05-18 12:51:49 +08:00
parent 3e64da0f6f
commit e321f1fd53
2 changed files with 29 additions and 13 deletions

View File

@ -6,7 +6,10 @@
# "http://localhost:9104/metrics"
# ]
# bearer_token = ""
# bearer_token_string = ""
# e.g. /run/secrets/kubernetes.io/serviceaccount/token
# bearer_token_file = ""
# # basic auth
# username = ""

View File

@ -6,6 +6,8 @@ import (
"log"
"net/http"
"net/url"
"os"
"strings"
"sync"
"sync/atomic"
"time"
@ -26,7 +28,8 @@ type Instance struct {
URLs []string `toml:"urls"`
Labels map[string]string `toml:"labels"`
IntervalTimes int64 `toml:"interval_times"`
BearerToken string `toml:"bearer_token"`
BearerTokenString string `toml:"bearer_token_string"`
BearerTokeFile string `toml:"bearer_token_file"`
Username string `toml:"username"`
Password string `toml:"password"`
Timeout config.Duration `toml:"timeout"`
@ -216,8 +219,18 @@ func (ins *Instance) setHeaders(req *http.Request) {
req.SetBasicAuth(ins.Username, ins.Password)
}
if ins.BearerToken != "" {
req.Header.Set("Authorization", "Bearer "+ins.BearerToken)
if ins.BearerTokeFile != "" {
content, err := os.ReadFile(ins.BearerTokeFile)
if err != nil {
log.Println("E! failed to read bearer token file:", ins.BearerTokeFile, "error:", err)
return
}
ins.BearerTokenString = strings.TrimSpace(string(content))
}
if ins.BearerTokenString != "" {
req.Header.Set("Authorization", "Bearer "+ins.BearerTokenString)
}
req.Header.Set("Accept", acceptHeader)