feat(inputs.elasticsearch): Add support for custom headers (#15544)

This commit is contained in:
Rob Sears 2024-06-24 15:31:29 -05:00 committed by GitHub
parent 78cbf533d2
commit 27eee98eca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 13 deletions

View File

@ -46,6 +46,9 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
## servers = ["http://user:pass@localhost:9200"] ## servers = ["http://user:pass@localhost:9200"]
servers = ["http://localhost:9200"] servers = ["http://localhost:9200"]
## HTTP headers to send with each request
# headers = { "X-Custom-Header" = "Custom" }
## Timeout for HTTP requests to the elastic search server(s) ## Timeout for HTTP requests to the elastic search server(s)
## deprecated in 1.29.0; use 'timeout' instead ## deprecated in 1.29.0; use 'timeout' instead
http_timeout = "5s" http_timeout = "5s"

View File

@ -96,19 +96,20 @@ type indexStat struct {
// Elasticsearch is a plugin to read stats from one or many Elasticsearch // Elasticsearch is a plugin to read stats from one or many Elasticsearch
// servers. // servers.
type Elasticsearch struct { type Elasticsearch struct {
Local bool `toml:"local"` Local bool `toml:"local"`
Servers []string `toml:"servers"` Servers []string `toml:"servers"`
HTTPTimeout config.Duration `toml:"http_timeout" deprecated:"1.29.0;1.35.0;use 'timeout' instead"` HTTPHeaders map[string]string `toml:"headers"`
ClusterHealth bool `toml:"cluster_health"` HTTPTimeout config.Duration `toml:"http_timeout" deprecated:"1.29.0;1.35.0;use 'timeout' instead"`
ClusterHealthLevel string `toml:"cluster_health_level"` ClusterHealth bool `toml:"cluster_health"`
ClusterStats bool `toml:"cluster_stats"` ClusterHealthLevel string `toml:"cluster_health_level"`
ClusterStatsOnlyFromMaster bool `toml:"cluster_stats_only_from_master"` ClusterStats bool `toml:"cluster_stats"`
IndicesInclude []string `toml:"indices_include"` ClusterStatsOnlyFromMaster bool `toml:"cluster_stats_only_from_master"`
IndicesLevel string `toml:"indices_level"` IndicesInclude []string `toml:"indices_include"`
NodeStats []string `toml:"node_stats"` IndicesLevel string `toml:"indices_level"`
Username string `toml:"username"` NodeStats []string `toml:"node_stats"`
Password string `toml:"password"` Username string `toml:"username"`
NumMostRecentIndices int `toml:"num_most_recent_indices"` Password string `toml:"password"`
NumMostRecentIndices int `toml:"num_most_recent_indices"`
Log telegraf.Logger `toml:"-"` Log telegraf.Logger `toml:"-"`
@ -641,6 +642,10 @@ func (e *Elasticsearch) getCatMaster(url string) (string, error) {
req.SetBasicAuth(e.Username, e.Password) req.SetBasicAuth(e.Username, e.Password)
} }
for key, value := range e.HTTPHeaders {
req.Header.Add(key, value)
}
r, err := e.client.Do(req) r, err := e.client.Do(req)
if err != nil { if err != nil {
return "", err return "", err
@ -677,6 +682,10 @@ func (e *Elasticsearch) gatherJSONData(url string, v interface{}) error {
req.SetBasicAuth(e.Username, e.Password) req.SetBasicAuth(e.Username, e.Password)
} }
for key, value := range e.HTTPHeaders {
req.Header.Add(key, value)
}
r, err := e.client.Do(req) r, err := e.client.Do(req)
if err != nil { if err != nil {
return err return err

View File

@ -5,6 +5,9 @@
## servers = ["http://user:pass@localhost:9200"] ## servers = ["http://user:pass@localhost:9200"]
servers = ["http://localhost:9200"] servers = ["http://localhost:9200"]
## HTTP headers to send with each request
# headers = { "X-Custom-Header" = "Custom" }
## Timeout for HTTP requests to the elastic search server(s) ## Timeout for HTTP requests to the elastic search server(s)
## deprecated in 1.29.0; use 'timeout' instead ## deprecated in 1.29.0; use 'timeout' instead
http_timeout = "5s" http_timeout = "5s"