remote write and read: support header

This commit is contained in:
Ulric Qin 2022-06-10 17:37:33 +08:00
parent ae622e0c08
commit 1bcc5b77ec
5 changed files with 36 additions and 0 deletions

View File

@ -154,6 +154,8 @@ type ReaderOptions struct {
MaxConnsPerHost int
MaxIdleConns int
MaxIdleConnsPerHost int
Headers []string
}
type WriterOptions struct {
@ -171,6 +173,8 @@ type WriterOptions struct {
MaxConnsPerHost int
MaxIdleConns int
MaxIdleConnsPerHost int
Headers []string
}
type WriterGlobalOpt struct {

View File

@ -925,6 +925,16 @@ func (h *apiClientImpl) Do(ctx context.Context, req *http.Request) (*http.Respon
req.SetBasicAuth(Reader.Opts.BasicAuthUser, Reader.Opts.BasicAuthPass)
}
headerCount := len(Reader.Opts.Headers)
if headerCount > 0 && headerCount%2 == 0 {
for i := 0; i < len(Reader.Opts.Headers); i += 2 {
req.Header.Add(Reader.Opts.Headers[i], Reader.Opts.Headers[i+1])
if Reader.Opts.Headers[i] == "Host" {
req.Host = Reader.Opts.Headers[i+1]
}
}
}
resp, body, err := h.client.Do(ctx, req)
if err != nil {
return resp, body, nil, err

View File

@ -66,6 +66,16 @@ func (w WriterType) Post(req []byte, headers ...map[string]string) error {
httpReq.SetBasicAuth(w.Opts.BasicAuthUser, w.Opts.BasicAuthPass)
}
headerCount := len(w.Opts.Headers)
if headerCount > 0 && headerCount%2 == 0 {
for i := 0; i < len(w.Opts.Headers); i += 2 {
httpReq.Header.Add(w.Opts.Headers[i], w.Opts.Headers[i+1])
if w.Opts.Headers[i] == "Host" {
httpReq.Host = w.Opts.Headers[i+1]
}
}
}
resp, body, err := w.Client.Do(context.Background(), httpReq)
if err != nil {
logger.Warningf("push data with remote write request got error: %v, response body: %s", err, string(body))

View File

@ -105,6 +105,8 @@ type ClusterOptions struct {
BasicAuthUser string
BasicAuthPass string
Headers []string
Timeout int64
DialTimeout int64
KeepAlive int64

View File

@ -60,6 +60,16 @@ func prometheusProxy(c *gin.Context) {
if cluster.Opts.BasicAuthUser != "" {
req.SetBasicAuth(cluster.Opts.BasicAuthUser, cluster.Opts.BasicAuthPass)
}
headerCount := len(cluster.Opts.Headers)
if headerCount > 0 && headerCount%2 == 0 {
for i := 0; i < len(cluster.Opts.Headers); i += 2 {
req.Header.Add(cluster.Opts.Headers[i], cluster.Opts.Headers[i+1])
if cluster.Opts.Headers[i] == "Host" {
req.Host = cluster.Opts.Headers[i+1]
}
}
}
}
errFunc := func(w http.ResponseWriter, r *http.Request, err error) {