-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathclient.go
165 lines (137 loc) · 3.84 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package es
import (
"context"
"errors"
"fmt"
"net/http"
"runtime"
"syscall"
"time"
"go.elastic.co/apm/module/apmelasticsearch/v2"
"github.com/elastic/fleet-server/v7/internal/pkg/build"
"github.com/elastic/fleet-server/v7/internal/pkg/config"
"github.com/rs/zerolog"
backoff "github.com/cenkalti/backoff/v4"
"github.com/elastic/go-elasticsearch/v8"
)
const (
initialRetryBackoff = 500 * time.Millisecond
maxRetryBackoff = 10 * time.Second
randomizationFactor = 0.5
defaultMaxRetries = 5
)
type ConfigOption func(config *elasticsearch.Config)
func applyDefaultOptions(escfg *elasticsearch.Config) {
exp := backoff.NewExponentialBackOff()
exp.InitialInterval = initialRetryBackoff
exp.RandomizationFactor = randomizationFactor
exp.MaxInterval = maxRetryBackoff
opts := []ConfigOption{
WithRetryOnErrs(syscall.ECONNREFUSED, syscall.ECONNRESET), // server may be restarting
WithRetryOnStatus(http.StatusTooManyRequests),
WithRetryOnStatus(http.StatusRequestTimeout),
WithRetryOnStatus(http.StatusTooEarly),
WithRetryOnStatus(http.StatusBadGateway),
WithRetryOnStatus(http.StatusServiceUnavailable),
WithRetryOnStatus(http.StatusGatewayTimeout),
WithBackoff(exp),
WithMaxRetries(defaultMaxRetries),
}
for _, opt := range opts {
opt(escfg)
}
}
func NewClient(ctx context.Context, cfg *config.Config, longPoll bool, opts ...ConfigOption) (*elasticsearch.Client, error) {
escfg, err := cfg.Output.Elasticsearch.ToESConfig(longPoll)
if err != nil {
return nil, err
}
addr := cfg.Output.Elasticsearch.Hosts
mcph := cfg.Output.Elasticsearch.MaxConnPerHost
// apply default config
applyDefaultOptions(&escfg)
// Apply configuration options
for _, opt := range opts {
opt(&escfg)
}
zlog := zerolog.Ctx(ctx).With().
Strs("cluster.addr", addr).
Int("cluster.maxConnsPersHost", mcph).
Logger()
zlog.Debug().Msg("init es")
es, err := elasticsearch.NewClient(escfg)
if err != nil {
zlog.Error().Err(err).Msg("fail elasticsearch init")
return nil, err
}
return es, nil
}
func WithUserAgent(name string, bi build.Info) ConfigOption {
return func(config *elasticsearch.Config) {
ua := userAgent(name, bi)
// Set User-Agent header
if config.Header == nil {
config.Header = http.Header{}
}
config.Header.Set("User-Agent", ua)
}
}
func InstrumentRoundTripper() ConfigOption {
return func(config *elasticsearch.Config) {
config.Transport = apmelasticsearch.WrapRoundTripper(
config.Transport,
)
}
}
func WithRetryOnErrs(errs ...error) ConfigOption {
return func(config *elasticsearch.Config) {
config.RetryOnError = func(_ *http.Request, err error) bool {
for _, e := range errs {
if errors.Is(err, e) {
return true
}
}
return false
}
}
}
func WithMaxRetries(retries int) ConfigOption {
return func(config *elasticsearch.Config) {
config.MaxRetries = retries
}
}
func WithRetryOnStatus(status int) ConfigOption {
return func(config *elasticsearch.Config) {
for _, s := range config.RetryOnStatus {
// check for duplicities
if s == status {
return
}
}
config.RetryOnStatus = append(config.RetryOnStatus, status)
}
}
func WithBackoff(exp *backoff.ExponentialBackOff) ConfigOption {
return func(config *elasticsearch.Config) {
if exp == nil {
// no retry backoff
config.RetryBackoff = nil
return
}
config.RetryBackoff = func(attempt int) time.Duration {
if attempt == 1 {
exp.Reset()
}
return exp.NextBackOff()
}
}
}
func userAgent(name string, bi build.Info) string {
return fmt.Sprintf("Elastic-%s/%s (%s; %s; %s; %s)",
name,
bi.Version, runtime.GOOS, runtime.GOARCH,
bi.Commit, bi.BuildTime)
}