Skip to content

Commit

Permalink
Added ElasticSearch API basic authentication
Browse files Browse the repository at this point in the history
Update content-type to x-ndjson
  • Loading branch information
dmachard committed Nov 9, 2024
1 parent 201da23 commit 65b6c0c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
12 changes: 12 additions & 0 deletions docs/loggers/logger_elasticsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`?
Expand Down
3 changes: 3 additions & 0 deletions pkgconfig/loggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
10 changes: 8 additions & 2 deletions workers/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 65b6c0c

Please sign in to comment.