From 56fd62485469e852689d4e1432bb5c82ea578e90 Mon Sep 17 00:00:00 2001 From: AndrewChubatiuk Date: Mon, 10 Jun 2024 15:34:24 +0300 Subject: [PATCH] feat(outputs.elasticsearch): Allow settings extra headers for elasticsearch output --- plugins/outputs/elasticsearch/README.md | 7 +++ .../outputs/elasticsearch/elasticsearch.go | 51 +++++++++++-------- plugins/outputs/elasticsearch/sample.conf | 5 ++ 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/plugins/outputs/elasticsearch/README.md b/plugins/outputs/elasticsearch/README.md index 2288de283f210..e0ecd2f5e3de7 100644 --- a/plugins/outputs/elasticsearch/README.md +++ b/plugins/outputs/elasticsearch/README.md @@ -323,6 +323,11 @@ to use them. ## no pipeline is used for the metric. # use_pipeline = "{{es_pipeline}}" # default_pipeline = "my_pipeline" + # + # Custom HTTP headers + # To pass custom HTTP headers please define it in a given below section + # [outputs.elasticsearch.headers] + # "X-Custom-Header" = "custom-value" ``` ### Permissions @@ -389,6 +394,8 @@ the `default_tag_value` will be used instead. instead. * `default_pipeline`: If dynamic pipeline names the tag does not exist in a particular metric, this value will be used instead. +* `headers`: Custom HTTP headers, which are passed to Elasticsearch header + before each request. ## Known issues diff --git a/plugins/outputs/elasticsearch/elasticsearch.go b/plugins/outputs/elasticsearch/elasticsearch.go index 1a6f530f4d7e5..ffed094aecf1f 100644 --- a/plugins/outputs/elasticsearch/elasticsearch.go +++ b/plugins/outputs/elasticsearch/elasticsearch.go @@ -28,26 +28,27 @@ import ( var sampleConfig string type Elasticsearch struct { - AuthBearerToken config.Secret `toml:"auth_bearer_token"` - DefaultPipeline string `toml:"default_pipeline"` - DefaultTagValue string `toml:"default_tag_value"` - EnableGzip bool `toml:"enable_gzip"` - EnableSniffer bool `toml:"enable_sniffer"` - FloatHandling string `toml:"float_handling"` - FloatReplacement float64 `toml:"float_replacement_value"` - ForceDocumentID bool `toml:"force_document_id"` - HealthCheckInterval config.Duration `toml:"health_check_interval"` - HealthCheckTimeout config.Duration `toml:"health_check_timeout"` - IndexName string `toml:"index_name"` - ManageTemplate bool `toml:"manage_template"` - OverwriteTemplate bool `toml:"overwrite_template"` - Username config.Secret `toml:"username"` - Password config.Secret `toml:"password"` - TemplateName string `toml:"template_name"` - Timeout config.Duration `toml:"timeout"` - URLs []string `toml:"urls"` - UsePipeline string `toml:"use_pipeline"` - Log telegraf.Logger `toml:"-"` + AuthBearerToken config.Secret `toml:"auth_bearer_token"` + DefaultPipeline string `toml:"default_pipeline"` + DefaultTagValue string `toml:"default_tag_value"` + EnableGzip bool `toml:"enable_gzip"` + EnableSniffer bool `toml:"enable_sniffer"` + FloatHandling string `toml:"float_handling"` + FloatReplacement float64 `toml:"float_replacement_value"` + ForceDocumentID bool `toml:"force_document_id"` + HealthCheckInterval config.Duration `toml:"health_check_interval"` + HealthCheckTimeout config.Duration `toml:"health_check_timeout"` + IndexName string `toml:"index_name"` + ManageTemplate bool `toml:"manage_template"` + OverwriteTemplate bool `toml:"overwrite_template"` + Username config.Secret `toml:"username"` + Password config.Secret `toml:"password"` + TemplateName string `toml:"template_name"` + Timeout config.Duration `toml:"timeout"` + URLs []string `toml:"urls"` + UsePipeline string `toml:"use_pipeline"` + Headers map[string]string `toml:"headers"` + Log telegraf.Logger `toml:"-"` majorReleaseNumber int pipelineName string pipelineTagKeys []string @@ -183,6 +184,16 @@ func (a *Elasticsearch) Connect() error { elastic.SetGzip(a.EnableGzip), ) + if len(a.Headers) > 0 { + headers := http.Header{} + for k, vals := range a.Headers { + for _, v := range strings.Split(vals, ",") { + headers.Add(k, v) + } + } + clientOptions = append(clientOptions, elastic.SetHeaders(headers)) + } + authOptions, err := a.getAuthOptions() if err != nil { return err diff --git a/plugins/outputs/elasticsearch/sample.conf b/plugins/outputs/elasticsearch/sample.conf index e9b37a50360a1..43fb03def3eee 100644 --- a/plugins/outputs/elasticsearch/sample.conf +++ b/plugins/outputs/elasticsearch/sample.conf @@ -77,3 +77,8 @@ ## no pipeline is used for the metric. # use_pipeline = "{{es_pipeline}}" # default_pipeline = "my_pipeline" + # + # Custom HTTP headers + # To pass custom HTTP headers please define it in a given below section + # [outputs.elasticsearch.headers] + # "X-Custom-Header" = "custom-value"