add custom headers in writer (#144) (#165)

* add nginx plugin

* update nginx.toml for nginx plugin

* Update nginx.go

* add custom headers in writer

* Update writer.go

Co-authored-by: ulricqin <ulricqin@qq.com>
This commit is contained in:
lesteryou 2022-08-23 19:12:05 +08:00 committed by GitHub
parent 01c9403fed
commit 909d5e2e7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 3 deletions

View File

@ -38,6 +38,9 @@ basic_auth_user = ""
# Basic auth password
basic_auth_pass = ""
## Optional headers
# headers = ["X-From", "categraf", "X-Xyz", "abc"]
# timeout settings, unit: ms
timeout = 5000
dial_timeout = 2500

View File

@ -35,9 +35,10 @@ type WriterOpt struct {
}
type WriterOption struct {
Url string `toml:"url"`
BasicAuthUser string `toml:"basic_auth_user"`
BasicAuthPass string `toml:"basic_auth_pass"`
Url string `toml:"url"`
BasicAuthUser string `toml:"basic_auth_user"`
BasicAuthPass string `toml:"basic_auth_pass"`
Headers []string `toml:"headers"`
Timeout int64 `toml:"timeout"`
DialTimeout int64 `toml:"dial_timeout"`

View File

@ -52,6 +52,13 @@ func (w WriterType) Post(req []byte) error {
httpReq.Header.Set("User-Agent", "categraf")
httpReq.Header.Set("X-Prometheus-Remote-Write-Version", "0.1.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]
}
}
if w.Opts.BasicAuthUser != "" {
httpReq.SetBasicAuth(w.Opts.BasicAuthUser, w.Opts.BasicAuthPass)
}