Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose elasticsearch client configs #39

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,28 @@ Check out on [go-dcp](https://github.com/Trendyol/go-dcp#configuration)
| `elasticsearch.maxIdleConnDuration` | time.Duration | no | 10s | Idle keep-alive connections are closed after this duration. |
| `elasticsearch.compressionEnabled` | boolean | no | false | Compression can be used if message size is large, CPU usage may be affected. |
| `elasticsearch.concurrentRequest` | int | no | 1 | Concurrent bulk request count |
| `elasticsearch.username` | string | no | "" | Elasticsearch username |
| `elasticsearch.password` | string | no | "" | Elasticsearch password |
| `elasticsearch.cloudId` | string | no | "" | Elasticsearch cloud id |
| `elasticsearch.apiKey` | string | no | "" | Elasticsearch API Key |
| `elasticsearch.serviceToken` | string | no | "" | Elasticsearch service token |
| `elasticsearch.certificateFingerprint` | string | no | "" | Elasticsearch certificate fingerprint |
| `elasticsearch.header` | map[string][]string | no | nil | Headers to set when client makes a request. |
| `elasticsearch.caCert` | []byte | no | nil | Elasticsearch PEM-encoded certificate authorities. |
| `elasticsearch.retryOnStatus` | []int | no | 502, 503, 504 | List of status codes for retry for the client |
| `elasticsearch.disableRetry` | bool | no | false | Disables retry on client. |
| `elasticsearch.enableRetryOnTimeout` | bool | no | false | Enables retry when timeout occurs. |
| `elasticsearch.discoverNodesOnStart` | bool | no | false | Enables discovery of nodes when initializing the client. |
| `elasticsearch.discoverNodesInterval` | time.Duration | no | 0 | Sets time interval to discover nodes periodically. |
| `elasticsearch.enableMetrics` | bool | no | false | Enables the metrics collection. |
| `elasticsearch.enableDebugLogger` | bool | no | false | Enables the debug logging. |
| `elasticsearch.enableCompatibilityMode`| bool | no | false | Enables sending compatibility header. |
| `elasticsearch.disableMetaHeader` | bool | no | false | Disables the additional "X-Elastic-Client-Meta" HTTP header when client performs a request. |
| `elasticsearch.useResponseCheckOnly` | bool | no | false | Disables checking if the cluster is a genuine Elasticsearch product before performing request.
| `elasticsearch.retryBackoff` | func(int) time.Duration | no | nil | Retry backoff duration. |
| `elasticsearch.logger` | estransport.Logger | no | nil | The logger object. |
| `elasticsearch.selector` | estransport.Selector | no | nil | The selector object. |
| `elasticsearch.connectionPoolFunc` | func([]*estransport.Connection, estransport.Selector) estransport.ConnectionPool | no | nil | Constructor function for a custom ConnectionPool. |

## Exposed metrics

Expand Down
43 changes: 33 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,42 @@ import (
"time"

"github.com/Trendyol/go-dcp/config"
"github.com/elastic/go-elasticsearch/v7/estransport"
)

type Elasticsearch struct {
CollectionIndexMapping map[string]string `yaml:"collectionIndexMapping"`
MaxConnsPerHost *int `yaml:"maxConnsPerHost"`
MaxIdleConnDuration *time.Duration `yaml:"maxIdleConnDuration"`
TypeName string `yaml:"typeName"`
Urls []string `yaml:"urls"`
BatchSizeLimit int `yaml:"batchSizeLimit"`
BatchByteSizeLimit int `yaml:"batchByteSizeLimit"`
BatchTickerDuration time.Duration `yaml:"batchTickerDuration"`
ConcurrentRequest int `yaml:"concurrentRequest"`
CompressionEnabled bool `yaml:"compressionEnabled"`
CollectionIndexMapping map[string]string `yaml:"collectionIndexMapping"`
MaxConnsPerHost *int `yaml:"maxConnsPerHost"`
MaxIdleConnDuration *time.Duration `yaml:"maxIdleConnDuration"`
TypeName string `yaml:"typeName"`
Urls []string `yaml:"urls"`
BatchSizeLimit int `yaml:"batchSizeLimit"`
BatchByteSizeLimit int `yaml:"batchByteSizeLimit"`
BatchTickerDuration time.Duration `yaml:"batchTickerDuration"`
ConcurrentRequest int `yaml:"concurrentRequest"`
CompressionEnabled bool `yaml:"compressionEnabled"`
Username string `yaml:"username"`
Password string `yaml:"password"`
CloudID string `yaml:"cloudId"`
APIKey string `yaml:"apiKey"`
ServiceToken string `yaml:"serviceToken"`
CertificateFingerprint string `yaml:"certificateFingerprint"`
Header map[string][]string `yaml:"header"`
CACert []byte `yaml:"caCert"`
RetryOnStatus []int `yaml:"retryOnStatus"`
DisableRetry bool `yaml:"disableRetry"`
EnableRetryOnTimeout bool `yaml:"enableRetryOnTimeout"`
DiscoverNodesOnStart bool `yaml:"discoverNodesOnStart"`
DiscoverNodesInterval time.Duration `yaml:"discoverNodesInterval"`
EnableMetrics bool `yaml:"enableMetrics"`
EnableDebugLogger bool `yaml:"enableDebugLogger"`
EnableCompatibilityMode bool `yaml:"enableCompatibilityMode"`
DisableMetaHeader bool `yaml:"disableMetaHeader"`
UseResponseCheckOnly bool `yaml:"useResponseCheckOnly"`
RetryBackoff func(int) time.Duration `yaml:"retryBackoff"`
Logger estransport.Logger `yaml:"logger"`
Selector estransport.Selector `yaml:"selector"`
ConnectionPoolFunc func([]*estransport.Connection, estransport.Selector) estransport.ConnectionPool `yaml:"connectionPoolFunc"`
}

type Config struct {
Expand Down
30 changes: 26 additions & 4 deletions elasticsearch/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,32 @@ import (

func NewElasticClient(config *config.Config) (*elasticsearch.Client, error) {
es, err := elasticsearch.NewClient(elasticsearch.Config{
MaxRetries: math.MaxInt,
Addresses: config.Elasticsearch.Urls,
Transport: newTransport(config.Elasticsearch),
CompressRequestBody: config.Elasticsearch.CompressionEnabled,
MaxRetries: math.MaxInt,
Addresses: config.Elasticsearch.Urls,
Transport: newTransport(config.Elasticsearch),
CompressRequestBody: config.Elasticsearch.CompressionEnabled,
Username: config.Elasticsearch.Username,
Password: config.Elasticsearch.Password,
CloudID: config.Elasticsearch.CloudID,
APIKey: config.Elasticsearch.APIKey,
ServiceToken: config.Elasticsearch.ServiceToken,
CertificateFingerprint: config.Elasticsearch.CertificateFingerprint,
Header: config.Elasticsearch.Header,
CACert: config.Elasticsearch.CACert,
RetryOnStatus: config.Elasticsearch.RetryOnStatus,
DisableRetry: config.Elasticsearch.DisableRetry,
EnableRetryOnTimeout: config.Elasticsearch.EnableRetryOnTimeout,
DiscoverNodesOnStart: config.Elasticsearch.DiscoverNodesOnStart,
DiscoverNodesInterval: config.Elasticsearch.DiscoverNodesInterval,
EnableMetrics: config.Elasticsearch.EnableMetrics,
EnableDebugLogger: config.Elasticsearch.EnableDebugLogger,
EnableCompatibilityMode: config.Elasticsearch.EnableCompatibilityMode,
DisableMetaHeader: config.Elasticsearch.DisableMetaHeader,
UseResponseCheckOnly: config.Elasticsearch.UseResponseCheckOnly,
RetryBackoff: config.Elasticsearch.RetryBackoff,
Logger: config.Elasticsearch.Logger,
Selector: config.Elasticsearch.Selector,
ConnectionPoolFunc: config.Elasticsearch.ConnectionPoolFunc,
})
if err != nil {
return nil, err
Expand Down