-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
options.go
462 lines (426 loc) · 20.2 KB
/
options.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2017 Uber Technologies, Inc.
// SPDX-License-Identifier: Apache-2.0
package es
import (
"flag"
"log"
"strings"
"time"
"github.com/spf13/viper"
"github.com/jaegertracing/jaeger/pkg/bearertoken"
"github.com/jaegertracing/jaeger/pkg/config/tlscfg"
"github.com/jaegertracing/jaeger/pkg/es/config"
)
const (
suffixUsername = ".username"
suffixPassword = ".password"
suffixSniffer = ".sniffer"
suffixSnifferTLSEnabled = ".sniffer-tls-enabled"
suffixTokenPath = ".token-file"
suffixPasswordPath = ".password-file"
suffixServerURLs = ".server-urls"
suffixRemoteReadClusters = ".remote-read-clusters"
suffixMaxSpanAge = ".max-span-age"
suffixAdaptiveSamplingLookback = ".adaptive-sampling.lookback"
suffixNumShards = ".num-shards"
suffixNumReplicas = ".num-replicas"
suffixPrioritySpanTemplate = ".prioirity-span-template"
suffixPriorityServiceTemplate = ".prioirity-service-template"
suffixPriorityDependenciesTemplate = ".prioirity-dependencies-template"
suffixBulkSize = ".bulk.size"
suffixBulkWorkers = ".bulk.workers"
suffixBulkActions = ".bulk.actions"
suffixBulkFlushInterval = ".bulk.flush-interval"
suffixTimeout = ".timeout"
suffixIndexPrefix = ".index-prefix"
suffixIndexDateSeparator = ".index-date-separator"
suffixIndexRolloverFrequencySpans = ".index-rollover-frequency-spans"
suffixIndexRolloverFrequencyServices = ".index-rollover-frequency-services"
suffixIndexRolloverFrequencySampling = ".index-rollover-frequency-adaptive-sampling"
suffixServiceCacheTTL = ".service-cache-ttl"
suffixTagsAsFields = ".tags-as-fields"
suffixTagsAsFieldsAll = suffixTagsAsFields + ".all"
suffixTagsAsFieldsInclude = suffixTagsAsFields + ".include"
suffixTagsFile = suffixTagsAsFields + ".config-file"
suffixTagDeDotChar = suffixTagsAsFields + ".dot-replacement"
suffixReadAlias = ".use-aliases"
suffixUseILM = ".use-ilm"
suffixCreateIndexTemplate = ".create-index-templates"
suffixEnabled = ".enabled"
suffixVersion = ".version"
suffixMaxDocCount = ".max-doc-count"
suffixLogLevel = ".log-level"
suffixSendGetBodyAs = ".send-get-body-as"
// default number of documents to return from a query (elasticsearch allowed limit)
// see search.max_buckets and index.max_result_window
defaultMaxDocCount = 10_000
defaultServerURL = "http://127.0.0.1:9200"
defaultRemoteReadClusters = ""
// default separator for Elasticsearch index date layout.
defaultIndexDateSeparator = "-"
defaultIndexRolloverFrequency = "day"
defaultSendGetBodyAs = ""
defaultIndexPrefix = ""
)
var defaultIndexOptions = config.IndexOptions{
DateLayout: initDateLayout(defaultIndexRolloverFrequency, defaultIndexDateSeparator),
RolloverFrequency: defaultIndexRolloverFrequency,
Shards: 5,
Replicas: 1,
Priority: 0,
}
// TODO this should be moved next to config.Configuration struct (maybe ./flags package)
// Options contains various type of Elasticsearch configs and provides the ability
// to bind them to command line flag and apply overlays, so that some configurations
// (e.g. archive) may be underspecified and infer the rest of its parameters from primary.
type Options struct {
Primary namespaceConfig `mapstructure:",squash"`
others map[string]*namespaceConfig
}
type namespaceConfig struct {
config.Configuration `mapstructure:",squash"`
namespace string
}
// NewOptions creates a new Options struct.
func NewOptions(primaryNamespace string, otherNamespaces ...string) *Options {
// TODO all default values should be defined via cobra flags
defaultConfig := DefaultConfig()
options := &Options{
Primary: namespaceConfig{
Configuration: defaultConfig,
namespace: primaryNamespace,
},
others: make(map[string]*namespaceConfig, len(otherNamespaces)),
}
// Other namespaces need to be explicitly enabled.
defaultConfig.Enabled = false
for _, namespace := range otherNamespaces {
options.others[namespace] = &namespaceConfig{
Configuration: defaultConfig,
namespace: namespace,
}
}
return options
}
func (cfg *namespaceConfig) getTLSFlagsConfig() tlscfg.ClientFlagsConfig {
return tlscfg.ClientFlagsConfig{
Prefix: cfg.namespace,
}
}
// AddFlags adds flags for Options
func (opt *Options) AddFlags(flagSet *flag.FlagSet) {
addFlags(flagSet, &opt.Primary)
for _, cfg := range opt.others {
addFlags(flagSet, cfg)
}
}
func addFlags(flagSet *flag.FlagSet, nsConfig *namespaceConfig) {
flagSet.String(
nsConfig.namespace+suffixUsername,
nsConfig.Authentication.BasicAuthentication.Username,
"The username required by Elasticsearch. The basic authentication also loads CA if it is specified.")
flagSet.String(
nsConfig.namespace+suffixPassword,
nsConfig.Authentication.BasicAuthentication.Password,
"The password required by Elasticsearch")
flagSet.String(
nsConfig.namespace+suffixTokenPath,
nsConfig.Authentication.BearerTokenAuthentication.FilePath,
"Path to a file containing bearer token. This flag also loads CA if it is specified.")
flagSet.String(
nsConfig.namespace+suffixPasswordPath,
nsConfig.Authentication.BasicAuthentication.PasswordFilePath,
"Path to a file containing password. This file is watched for changes.")
flagSet.Bool(
nsConfig.namespace+suffixSniffer,
nsConfig.Sniffing.Enabled,
"The sniffer config for Elasticsearch; client uses sniffing process to find all nodes automatically, disable if not required")
flagSet.String(
nsConfig.namespace+suffixServerURLs,
defaultServerURL,
"The comma-separated list of Elasticsearch servers, must be full url i.e. http://localhost:9200")
flagSet.String(
nsConfig.namespace+suffixRemoteReadClusters,
defaultRemoteReadClusters,
"Comma-separated list of Elasticsearch remote cluster names for cross-cluster querying."+
"See Elasticsearch remote clusters and cross-cluster query api.")
flagSet.Duration(
nsConfig.namespace+suffixTimeout,
nsConfig.QueryTimeout,
"Timeout used for queries. A Timeout of zero means no timeout")
flagSet.Int64(
nsConfig.namespace+suffixNumShards,
nsConfig.Indices.Spans.Shards,
"The number of shards per index in Elasticsearch")
flagSet.Duration(
nsConfig.namespace+suffixServiceCacheTTL,
nsConfig.ServiceCacheTTL,
"The TTL for the cache of known service names",
)
flagSet.Int64(
nsConfig.namespace+suffixNumReplicas,
nsConfig.Indices.Spans.Replicas,
"The number of replicas per index in Elasticsearch")
flagSet.Int64(
nsConfig.namespace+suffixPrioritySpanTemplate,
nsConfig.Indices.Spans.Priority,
"Priority of jaeger-span index template (ESv8 only)")
flagSet.Int64(
nsConfig.namespace+suffixPriorityServiceTemplate,
nsConfig.Indices.Services.Priority,
"Priority of jaeger-service index template (ESv8 only)")
flagSet.Int64(
nsConfig.namespace+suffixPriorityDependenciesTemplate,
nsConfig.Indices.Dependencies.Priority,
"Priority of jaeger-dependecies index template (ESv8 only)")
flagSet.Int(
nsConfig.namespace+suffixBulkSize,
nsConfig.BulkProcessing.MaxBytes,
"The number of bytes that the bulk requests can take up before the bulk processor decides to commit")
flagSet.Int(
nsConfig.namespace+suffixBulkWorkers,
nsConfig.BulkProcessing.Workers,
"The number of workers that are able to receive bulk requests and eventually commit them to Elasticsearch")
flagSet.Int(
nsConfig.namespace+suffixBulkActions,
nsConfig.BulkProcessing.MaxActions,
"The number of requests that can be enqueued before the bulk processor decides to commit")
flagSet.Duration(
nsConfig.namespace+suffixBulkFlushInterval,
nsConfig.BulkProcessing.FlushInterval,
"A time.Duration after which bulk requests are committed, regardless of other thresholds. Set to zero to disable. By default, this is disabled.")
flagSet.String(
nsConfig.namespace+suffixIndexPrefix,
string(nsConfig.Indices.IndexPrefix),
"Optional prefix of Jaeger indices. For example \"production\" creates \"production-jaeger-*\".")
flagSet.String(
nsConfig.namespace+suffixIndexDateSeparator,
defaultIndexDateSeparator,
"Optional date separator of Jaeger indices. For example \".\" creates \"jaeger-span-2020.11.20\".")
flagSet.String(
nsConfig.namespace+suffixIndexRolloverFrequencySpans,
nsConfig.Indices.Spans.RolloverFrequency,
"Rotates jaeger-span indices over the given period. For example \"day\" creates \"jaeger-span-yyyy-MM-dd\" every day after UTC 12AM. Valid options: [hour, day]. "+
"This does not delete old indices. For details on complete index management solutions supported by Jaeger, refer to: https://www.jaegertracing.io/docs/deployment/#elasticsearch-rollover")
flagSet.String(
nsConfig.namespace+suffixIndexRolloverFrequencyServices,
nsConfig.Indices.Services.RolloverFrequency,
"Rotates jaeger-service indices over the given period. For example \"day\" creates \"jaeger-service-yyyy-MM-dd\" every day after UTC 12AM. Valid options: [hour, day]. "+
"This does not delete old indices. For details on complete index management solutions supported by Jaeger, refer to: https://www.jaegertracing.io/docs/deployment/#elasticsearch-rollover")
flagSet.String(
nsConfig.namespace+suffixIndexRolloverFrequencySampling,
nsConfig.Indices.Sampling.RolloverFrequency,
"Rotates jaeger-sampling indices over the given period. For example \"day\" creates \"jaeger-sampling-yyyy-MM-dd\" every day after UTC 12AM. Valid options: [hour, day]. "+
"This does not delete old indices. For details on complete index management solutions supported by Jaeger, refer to: https://www.jaegertracing.io/docs/deployment/#elasticsearch-rollover")
flagSet.Bool(
nsConfig.namespace+suffixTagsAsFieldsAll,
nsConfig.Tags.AllAsFields,
"(experimental) Store all span and process tags as object fields. If true "+suffixTagsFile+" and "+suffixTagsAsFieldsInclude+" is ignored. Binary tags are always stored as nested objects.")
flagSet.String(
nsConfig.namespace+suffixTagsAsFieldsInclude,
nsConfig.Tags.Include,
"(experimental) Comma delimited list of tag keys which will be stored as object fields. Merged with the contents of "+suffixTagsFile)
flagSet.String(
nsConfig.namespace+suffixTagsFile,
nsConfig.Tags.File,
"(experimental) Optional path to a file containing tag keys which will be stored as object fields. Each key should be on a separate line. Merged with "+suffixTagsAsFieldsInclude)
flagSet.String(
nsConfig.namespace+suffixTagDeDotChar,
nsConfig.Tags.DotReplacement,
"(experimental) The character used to replace dots (\".\") in tag keys stored as object fields.")
flagSet.Bool(
nsConfig.namespace+suffixReadAlias,
nsConfig.UseReadWriteAliases,
"Use read and write aliases for indices. Use this option with Elasticsearch rollover "+
"API. It requires an external component to create aliases before startup and then performing its management. "+
"Note that es"+suffixMaxSpanAge+" will influence trace search window start times.")
flagSet.Bool(
nsConfig.namespace+suffixUseILM,
nsConfig.UseILM,
"(experimental) Option to enable ILM for jaeger span & service indices. Use this option with "+nsConfig.namespace+suffixReadAlias+". "+
"It requires an external component to create aliases before startup and then performing its management. "+
"ILM policy must be manually created in ES before startup. Supported only for elasticsearch version 7+.")
flagSet.Bool(
nsConfig.namespace+suffixCreateIndexTemplate,
nsConfig.CreateIndexTemplates,
"Create index templates at application startup. Set to false when templates are installed manually.")
flagSet.Uint(
nsConfig.namespace+suffixVersion,
0,
"The major Elasticsearch version. If not specified, the value will be auto-detected from Elasticsearch.")
flagSet.Bool(
nsConfig.namespace+suffixSnifferTLSEnabled,
nsConfig.Sniffing.UseHTTPS,
"Option to enable TLS when sniffing an Elasticsearch Cluster ; client uses sniffing process to find all nodes automatically, disabled by default")
flagSet.Int(
nsConfig.namespace+suffixMaxDocCount,
nsConfig.MaxDocCount,
"The maximum document count to return from an Elasticsearch query. This will also apply to aggregations.")
flagSet.String(
nsConfig.namespace+suffixLogLevel,
nsConfig.LogLevel,
"The Elasticsearch client log-level. Valid levels: [debug, info, error]")
flagSet.String(
nsConfig.namespace+suffixSendGetBodyAs,
nsConfig.SendGetBodyAs,
"HTTP verb for requests that contain a body [GET, POST].")
flagSet.Duration(
nsConfig.namespace+suffixAdaptiveSamplingLookback,
nsConfig.AdaptiveSamplingLookback,
"How far back to look for the latest adaptive sampling probabilities")
if nsConfig.namespace == archiveNamespace {
flagSet.Bool(
nsConfig.namespace+suffixEnabled,
nsConfig.Enabled,
"Enable extra storage")
} else {
// MaxSpanAge is only relevant when searching for unarchived traces.
// Archived traces are searched with no look-back limit.
flagSet.Duration(
nsConfig.namespace+suffixMaxSpanAge,
nsConfig.MaxSpanAge,
"The maximum lookback for spans in Elasticsearch")
}
nsConfig.getTLSFlagsConfig().AddFlags(flagSet)
}
// InitFromViper initializes Options with properties from viper
func (opt *Options) InitFromViper(v *viper.Viper) {
initFromViper(&opt.Primary, v)
for _, cfg := range opt.others {
initFromViper(cfg, v)
}
}
func initFromViper(cfg *namespaceConfig, v *viper.Viper) {
cfg.Authentication.BasicAuthentication.Username = v.GetString(cfg.namespace + suffixUsername)
cfg.Authentication.BasicAuthentication.Password = v.GetString(cfg.namespace + suffixPassword)
cfg.Authentication.BearerTokenAuthentication.FilePath = v.GetString(cfg.namespace + suffixTokenPath)
cfg.Authentication.BasicAuthentication.PasswordFilePath = v.GetString(cfg.namespace + suffixPasswordPath)
cfg.Sniffing.Enabled = v.GetBool(cfg.namespace + suffixSniffer)
cfg.Sniffing.UseHTTPS = v.GetBool(cfg.namespace + suffixSnifferTLSEnabled)
cfg.Servers = strings.Split(stripWhiteSpace(v.GetString(cfg.namespace+suffixServerURLs)), ",")
cfg.MaxSpanAge = v.GetDuration(cfg.namespace + suffixMaxSpanAge)
cfg.AdaptiveSamplingLookback = v.GetDuration(cfg.namespace + suffixAdaptiveSamplingLookback)
cfg.Indices.Spans.Shards = v.GetInt64(cfg.namespace + suffixNumShards)
cfg.Indices.Services.Shards = v.GetInt64(cfg.namespace + suffixNumShards)
cfg.Indices.Sampling.Shards = v.GetInt64(cfg.namespace + suffixNumShards)
cfg.Indices.Dependencies.Shards = v.GetInt64(cfg.namespace + suffixNumShards)
cfg.Indices.Spans.Replicas = v.GetInt64(cfg.namespace + suffixNumReplicas)
cfg.Indices.Services.Replicas = v.GetInt64(cfg.namespace + suffixNumReplicas)
cfg.Indices.Sampling.Replicas = v.GetInt64(cfg.namespace + suffixNumReplicas)
cfg.Indices.Dependencies.Replicas = v.GetInt64(cfg.namespace + suffixNumReplicas)
cfg.Indices.Spans.Priority = v.GetInt64(cfg.namespace + suffixPrioritySpanTemplate)
cfg.Indices.Services.Priority = v.GetInt64(cfg.namespace + suffixPriorityServiceTemplate)
// cfg.Indices.Sampling does not have a separate flag
cfg.Indices.Dependencies.Priority = v.GetInt64(cfg.namespace + suffixPriorityDependenciesTemplate)
cfg.BulkProcessing.MaxBytes = v.GetInt(cfg.namespace + suffixBulkSize)
cfg.BulkProcessing.Workers = v.GetInt(cfg.namespace + suffixBulkWorkers)
cfg.BulkProcessing.MaxActions = v.GetInt(cfg.namespace + suffixBulkActions)
cfg.BulkProcessing.FlushInterval = v.GetDuration(cfg.namespace + suffixBulkFlushInterval)
cfg.QueryTimeout = v.GetDuration(cfg.namespace + suffixTimeout)
cfg.ServiceCacheTTL = v.GetDuration(cfg.namespace + suffixServiceCacheTTL)
indexPrefix := v.GetString(cfg.namespace + suffixIndexPrefix)
cfg.Indices.IndexPrefix = config.IndexPrefix(indexPrefix)
cfg.Tags.AllAsFields = v.GetBool(cfg.namespace + suffixTagsAsFieldsAll)
cfg.Tags.Include = v.GetString(cfg.namespace + suffixTagsAsFieldsInclude)
cfg.Tags.File = v.GetString(cfg.namespace + suffixTagsFile)
cfg.Tags.DotReplacement = v.GetString(cfg.namespace + suffixTagDeDotChar)
cfg.UseReadWriteAliases = v.GetBool(cfg.namespace + suffixReadAlias)
cfg.Enabled = v.GetBool(cfg.namespace + suffixEnabled)
cfg.CreateIndexTemplates = v.GetBool(cfg.namespace + suffixCreateIndexTemplate)
cfg.Version = v.GetUint(cfg.namespace + suffixVersion)
cfg.LogLevel = v.GetString(cfg.namespace + suffixLogLevel)
cfg.SendGetBodyAs = v.GetString(cfg.namespace + suffixSendGetBodyAs)
cfg.MaxDocCount = v.GetInt(cfg.namespace + suffixMaxDocCount)
cfg.UseILM = v.GetBool(cfg.namespace + suffixUseILM)
// TODO: Need to figure out a better way for do this.
cfg.Authentication.BearerTokenAuthentication.AllowFromContext = v.GetBool(bearertoken.StoragePropagationKey)
remoteReadClusters := stripWhiteSpace(v.GetString(cfg.namespace + suffixRemoteReadClusters))
if len(remoteReadClusters) > 0 {
cfg.RemoteReadClusters = strings.Split(remoteReadClusters, ",")
}
cfg.Indices.Spans.RolloverFrequency = strings.ToLower(v.GetString(cfg.namespace + suffixIndexRolloverFrequencySpans))
cfg.Indices.Services.RolloverFrequency = strings.ToLower(v.GetString(cfg.namespace + suffixIndexRolloverFrequencyServices))
cfg.Indices.Sampling.RolloverFrequency = strings.ToLower(v.GetString(cfg.namespace + suffixIndexRolloverFrequencySampling))
separator := v.GetString(cfg.namespace + suffixIndexDateSeparator)
cfg.Indices.Spans.DateLayout = initDateLayout(cfg.Indices.Spans.RolloverFrequency, separator)
cfg.Indices.Services.DateLayout = initDateLayout(cfg.Indices.Services.RolloverFrequency, separator)
cfg.Indices.Sampling.DateLayout = initDateLayout(cfg.Indices.Sampling.RolloverFrequency, separator)
// Daily is recommended for dependencies calculation, and this index size is very small
cfg.Indices.Dependencies.DateLayout = initDateLayout(cfg.Indices.Dependencies.DateLayout, separator)
tlsconfig, err := cfg.getTLSFlagsConfig().InitFromViper(v)
if err != nil {
// TODO refactor to be able to return error
log.Fatal(err)
}
cfg.TLS = tlsconfig
}
// GetPrimary returns primary configuration.
func (opt *Options) GetPrimary() *config.Configuration {
return &opt.Primary.Configuration
}
// Get returns auxiliary named configuration.
func (opt *Options) Get(namespace string) *config.Configuration {
nsCfg, ok := opt.others[namespace]
if !ok {
nsCfg = &namespaceConfig{}
opt.others[namespace] = nsCfg
}
nsCfg.Configuration.ApplyDefaults(&opt.Primary.Configuration)
if len(nsCfg.Configuration.Servers) == 0 {
nsCfg.Servers = opt.Primary.Servers
}
return &nsCfg.Configuration
}
// stripWhiteSpace removes all whitespace characters from a string
func stripWhiteSpace(str string) string {
return strings.ReplaceAll(str, " ", "")
}
func initDateLayout(rolloverFreq, sep string) string {
// default to daily format
indexLayout := "2006" + sep + "01" + sep + "02"
if rolloverFreq == "hour" {
indexLayout = indexLayout + sep + "15"
}
return indexLayout
}
func DefaultConfig() config.Configuration {
return config.Configuration{
Authentication: config.Authentication{
BasicAuthentication: config.BasicAuthentication{
Username: "",
Password: "",
},
},
Sniffing: config.Sniffing{
Enabled: false,
},
MaxSpanAge: 72 * time.Hour,
AdaptiveSamplingLookback: 72 * time.Hour,
BulkProcessing: config.BulkProcessing{
MaxBytes: 5 * 1000 * 1000,
Workers: 1,
MaxActions: 1000,
FlushInterval: time.Millisecond * 200,
},
Tags: config.TagsAsFields{
DotReplacement: "@",
},
Enabled: true,
CreateIndexTemplates: true,
Version: 0,
UseReadWriteAliases: false,
UseILM: false,
Servers: []string{defaultServerURL},
RemoteReadClusters: []string{},
MaxDocCount: defaultMaxDocCount,
LogLevel: "error",
SendGetBodyAs: defaultSendGetBodyAs,
Indices: config.Indices{
Spans: defaultIndexOptions,
Services: defaultIndexOptions,
Dependencies: defaultIndexOptions,
Sampling: defaultIndexOptions,
},
}
}