diff --git a/docs/loggers/logger_elasticsearch.md b/docs/loggers/logger_elasticsearch.md index fd33bed2..dce55201 100644 --- a/docs/loggers/logger_elasticsearch.md +++ b/docs/loggers/logger_elasticsearch.md @@ -33,6 +33,15 @@ Options: > Interval in seconds before to flush the buffer. > Set the maximum time interval before the buffer is flushed. If the bulk batches reach this interval before reaching the maximum size, they will be sent to Elasticsearch. +* `basic-auth-enable` (bool) + > Enable basic authentication (login+password) to ElasticSearch + +* `basic-auth-login` (string) + > The login username + +* `basic-auth-pwd` (string) + > The password + Defaults: ```yaml @@ -45,6 +54,9 @@ Defaults: flush-interval: 10 # in seconds compression: none bulk-channel-size: 10 + basic-auth-enable: false + basic-auth-login: "" + basic-auth-pwd: "" ``` > Could you explain the difference between `bulk-size` and `bulk-channel-size`? diff --git a/pkgconfig/loggers.go b/pkgconfig/loggers.go index 18495bbb..60a147be 100644 --- a/pkgconfig/loggers.go +++ b/pkgconfig/loggers.go @@ -231,6 +231,9 @@ type ConfigLoggers struct { BulkChannelSize int `yaml:"bulk-channel-size" default:"10"` FlushInterval int `yaml:"flush-interval" default:"10"` Compression string `yaml:"compression" default:"none"` + BasicAuthEnabled bool `yaml:"basic-auth-enable" default:"false"` + BasicAuthLogin string `yaml:"basic-auth-login" default:""` + BasicAuthPwd string `yaml:"basic-auth-pwd" default:""` } `yaml:"elasticsearch"` ScalyrClient struct { Enable bool `yaml:"enable" default:"false"` diff --git a/workers/elasticsearch.go b/workers/elasticsearch.go index 209c81bb..909d4bd6 100644 --- a/workers/elasticsearch.go +++ b/workers/elasticsearch.go @@ -199,7 +199,10 @@ func (w *ElasticSearchClient) sendBulk(bulk []byte) error { if err != nil { return err } - req.Header.Set("Content-Type", "application/json") + req.Header.Set("Content-Type", "application/x-ndjson") + if w.GetConfig().Loggers.ElasticSearchClient.BasicAuthEnabled { + req.SetBasicAuth(w.GetConfig().Loggers.ElasticSearchClient.BasicAuthLogin, w.GetConfig().Loggers.ElasticSearchClient.BasicAuthPwd) + } // Send the request using the HTTP client resp, err := w.httpClient.Do(req) @@ -238,8 +241,11 @@ func (w *ElasticSearchClient) sendCompressedBulk(bulk []byte) error { if err != nil { return err } - req.Header.Set("Content-Type", "application/json") + req.Header.Set("Content-Type", "application/x-ndjson") req.Header.Set("Content-Encoding", "gzip") // Set Content-Encoding header to gzip + if w.GetConfig().Loggers.ElasticSearchClient.BasicAuthEnabled { + req.SetBasicAuth(w.GetConfig().Loggers.ElasticSearchClient.BasicAuthLogin, w.GetConfig().Loggers.ElasticSearchClient.BasicAuthPwd) + } // Send the request using the HTTP client resp, err := w.httpClient.Do(req)