-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathconfig.go
More file actions
705 lines (632 loc) · 24.4 KB
/
config.go
File metadata and controls
705 lines (632 loc) · 24.4 KB
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2017 Uber Technologies, Inc.
// SPDX-License-Identifier: Apache-2.0
package config
import (
"bufio"
"context"
"crypto/tls"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/asaskevich/govalidator"
esv8 "github.com/elastic/go-elasticsearch/v9"
"github.com/olivere/elastic/v7"
"go.opentelemetry.io/collector/config/configoptional"
"go.opentelemetry.io/collector/config/configtls"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zapgrpc"
"github.com/jaegertracing/jaeger/internal/auth"
"github.com/jaegertracing/jaeger/internal/metrics"
es "github.com/jaegertracing/jaeger/internal/storage/elasticsearch"
eswrapper "github.com/jaegertracing/jaeger/internal/storage/elasticsearch/wrapper"
"github.com/jaegertracing/jaeger/internal/storage/v1/api/spanstore/spanstoremetrics"
)
const (
IndexPrefixSeparator = "-"
)
// IndexOptions describes the index format and rollover frequency
type IndexOptions struct {
// Priority contains the priority of index template (ESv8 only).
Priority int64 `mapstructure:"priority"`
// DateLayout contains the format string used to format current time to part of the index name.
// For example, "2006-01-02" layout will result in "jaeger-spans-yyyy-mm-dd".
// If not specified, the default value is "2006-01-02".
// See https://pkg.go.dev/time#Layout for more details on the syntax.
DateLayout string `mapstructure:"date_layout"`
// Shards is the number of shards per index in Elasticsearch.
Shards int64 `mapstructure:"shards"`
// Replicas is the number of replicas per index in Elasticsearch.
Replicas *int64 `mapstructure:"replicas"`
// RolloverFrequency contains the rollover frequency setting used to fetch
// indices from elasticsearch.
// Valid configuration options are: [hour, day].
// This setting does not affect the index rotation and is simply used for
// fetching indices.
RolloverFrequency string `mapstructure:"rollover_frequency"`
}
// Indices describes different configuration options for each index type
type Indices struct {
// IndexPrefix is an optional prefix to prepend to Jaeger indices.
// For example, setting this field to "production" creates "production-jaeger-*".
IndexPrefix IndexPrefix `mapstructure:"index_prefix"`
Spans IndexOptions `mapstructure:"spans"`
Services IndexOptions `mapstructure:"services"`
Dependencies IndexOptions `mapstructure:"dependencies"`
Sampling IndexOptions `mapstructure:"sampling"`
}
type bulkCallback struct {
startTimes sync.Map
sm *spanstoremetrics.WriteMetrics
logger *zap.Logger
}
type IndexPrefix string
func (p IndexPrefix) Apply(indexName string) string {
ps := string(p)
if ps == "" {
return indexName
}
if strings.HasSuffix(ps, IndexPrefixSeparator) {
return ps + indexName
}
return ps + IndexPrefixSeparator + indexName
}
// Configuration describes the configuration properties needed to connect to an ElasticSearch cluster
type Configuration struct {
// ---- connection related configs ----
// Servers is a list of Elasticsearch servers. The strings must must contain full URLs
// (i.e. http://localhost:9200).
Servers []string `mapstructure:"server_urls" valid:"required,url"`
// RemoteReadClusters is a list of Elasticsearch remote cluster names for cross-cluster
// querying.
RemoteReadClusters []string `mapstructure:"remote_read_clusters"`
Authentication Authentication `mapstructure:"auth"`
// TLS contains the TLS configuration for the connection to the ElasticSearch clusters.
TLS configtls.ClientConfig `mapstructure:"tls"`
Sniffing Sniffing `mapstructure:"sniffing"`
// Disable the Elasticsearch health check
DisableHealthCheck bool `mapstructure:"disable_health_check"`
// SendGetBodyAs is the HTTP verb to use for requests that contain a body.
SendGetBodyAs string `mapstructure:"send_get_body_as"`
// QueryTimeout contains the timeout used for queries. A timeout of zero means no timeout.
QueryTimeout time.Duration `mapstructure:"query_timeout"`
// HTTPCompression can be set to false to disable gzip compression for requests to ElasticSearch
HTTPCompression bool `mapstructure:"http_compression"`
// ---- elasticsearch client related configs ----
BulkProcessing BulkProcessing `mapstructure:"bulk_processing"`
// Version contains the major Elasticsearch version. If this field is not specified,
// the value will be auto-detected from Elasticsearch.
Version uint `mapstructure:"version"`
// LogLevel contains the Elasticsearch client log-level. Valid values for this field
// are: [debug, info, error]
LogLevel string `mapstructure:"log_level"`
// ---- index related configs ----
Indices Indices `mapstructure:"indices"`
// UseReadWriteAliases, if set to true, will 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.
UseReadWriteAliases bool `mapstructure:"use_aliases"`
// ReadAliasSuffix is the suffix to append to the index name used for reading.
// This configuration only exists to provide backwards compatibility for jaeger-v1
// which is why it is not exposed as a configuration option for jaeger-v2
ReadAliasSuffix string `mapstructure:"-"`
// WriteAliasSuffix is the suffix to append to the write index name.
// This configuration only exists to provide backwards compatibility for jaeger-v1
// which is why it is not exposed as a configuration option for jaeger-v2
WriteAliasSuffix string `mapstructure:"-"`
// CreateIndexTemplates, if set to true, creates index templates at application startup.
// This configuration should be set to false when templates are installed manually.
CreateIndexTemplates bool `mapstructure:"create_mappings"`
// Option to enable Index Lifecycle Management (ILM) for Jaeger span and service indices.
// Read more about ILM at
// https://www.jaegertracing.io/docs/deployment/#enabling-ilm-support
UseILM bool `mapstructure:"use_ilm"`
// ---- jaeger-specific configs ----
// MaxDocCount Defines maximum number of results to fetch from storage per query.
MaxDocCount int `mapstructure:"max_doc_count"`
// MaxSpanAge configures the maximum lookback on span reads.
MaxSpanAge time.Duration `mapstructure:"max_span_age"`
// ServiceCacheTTL contains the TTL for the cache of known service names.
ServiceCacheTTL time.Duration `mapstructure:"service_cache_ttl"`
// AdaptiveSamplingLookback contains the duration to look back for the
// latest adaptive sampling probabilities.
AdaptiveSamplingLookback time.Duration `mapstructure:"adaptive_sampling_lookback"`
Tags TagsAsFields `mapstructure:"tags_as_fields"`
// Enabled, if set to true, enables the namespace for storage pointed to by this configuration.
Enabled bool `mapstructure:"-"`
}
// TagsAsFields holds configuration for tag schema.
// By default Jaeger stores tags in an array of nested objects.
// This configurations allows to store tags as object fields for better Kibana support.
type TagsAsFields struct {
// Store all tags as object fields, instead nested objects
AllAsFields bool `mapstructure:"all"`
// Dot replacement for tag keys when stored as object fields
DotReplacement string `mapstructure:"dot_replacement"`
// File path to tag keys which should be stored as object fields
File string `mapstructure:"config_file"`
// Comma delimited list of tags to store as object fields
Include string `mapstructure:"include"`
}
// Sniffing sets the sniffing configuration for the ElasticSearch client, which is the process
// of finding all the nodes of your cluster. Read more about sniffing at
// https://github.com/olivere/elastic/wiki/Sniffing.
type Sniffing struct {
// Enabled, if set to true, enables sniffing for the ElasticSearch client.
Enabled bool `mapstructure:"enabled"`
// UseHTTPS, if set to true, sets the HTTP scheme to HTTPS when performing sniffing.
// For ESV8, the scheme is set to HTTPS by default, so this configuration is ignored.
UseHTTPS bool `mapstructure:"use_https"`
}
type BulkProcessing struct {
// MaxBytes, contains the number of bytes which specifies when to flush.
MaxBytes int `mapstructure:"max_bytes"`
// MaxActions contain the number of added actions which specifies when to flush.
MaxActions int `mapstructure:"max_actions"`
// FlushInterval is the interval at the end of which a flush occurs.
FlushInterval time.Duration `mapstructure:"flush_interval"`
// Workers contains the number of concurrent workers allowed to be executed.
Workers int `mapstructure:"workers"`
}
// TokenAuthentication contains the common fields shared by all token-based authentication methods
type TokenAuthentication struct {
// FilePath contains the path to a file containing the token.
FilePath string `mapstructure:"file_path"`
// AllowFromContext, if set to true, allows the token to be retrieved from the context.
AllowFromContext bool `mapstructure:"from_context"`
// ReloadInterval contains the interval at which the token file is reloaded.
// If set to 0 then the file is only loaded once on startup.
ReloadInterval time.Duration `mapstructure:"reload_interval"`
}
type Authentication struct {
BasicAuthentication configoptional.Optional[BasicAuthentication] `mapstructure:"basic"`
BearerTokenAuth configoptional.Optional[TokenAuthentication] `mapstructure:"bearer_token"`
APIKeyAuth configoptional.Optional[TokenAuthentication] `mapstructure:"api_key"`
}
type BasicAuthentication struct {
// Username contains the username required to connect to Elasticsearch.
Username string `mapstructure:"username"`
// Password contains The password required by Elasticsearch
Password string `mapstructure:"password" json:"-"`
// PasswordFilePath contains the path to a file containing password.
// This file is watched for changes.
PasswordFilePath string `mapstructure:"password_file"`
// ReloadInterval contains the interval at which the password file is reloaded.
// If set to 0 then the file is only loaded once on startup.
ReloadInterval time.Duration `mapstructure:"reload_interval"`
}
// BearerTokenAuthentication contains the configuration for attaching bearer tokens
// when making HTTP requests. Note that TokenFilePath and AllowTokenFromContext
// should not both be enabled. If both TokenFilePath and AllowTokenFromContext are set,
// the TokenFilePath will be ignored.
// For more information about token-based authentication in elasticsearch, check out
// https://www.elastic.co/guide/en/elasticsearch/reference/current/token-authentication-services.html.
// NewClient creates a new ElasticSearch client
func NewClient(ctx context.Context, c *Configuration, logger *zap.Logger, metricsFactory metrics.Factory) (es.Client, error) {
if len(c.Servers) < 1 {
return nil, errors.New("no servers specified")
}
options, err := c.getConfigOptions(ctx, logger)
if err != nil {
return nil, err
}
rawClient, err := elastic.NewClient(options...)
if err != nil {
return nil, err
}
bcb := bulkCallback{
sm: spanstoremetrics.NewWriter(metricsFactory, "bulk_index"),
logger: logger,
}
bulkProc, err := rawClient.BulkProcessor().
Before(func(id int64, _ /* requests */ []elastic.BulkableRequest) {
bcb.startTimes.Store(id, time.Now())
}).
After(bcb.invoke).
BulkSize(c.BulkProcessing.MaxBytes).
Workers(c.BulkProcessing.Workers).
BulkActions(c.BulkProcessing.MaxActions).
FlushInterval(c.BulkProcessing.FlushInterval).
Do(ctx)
if err != nil {
return nil, err
}
if c.Version == 0 {
// Determine ElasticSearch Version
pingResult, _, err := rawClient.Ping(c.Servers[0]).Do(ctx)
if err != nil {
return nil, err
}
esVersion, err := strconv.Atoi(string(pingResult.Version.Number[0]))
if err != nil {
return nil, err
}
// OpenSearch is based on ES 7.x
if strings.Contains(pingResult.TagLine, "OpenSearch") {
if pingResult.Version.Number[0] == '1' {
logger.Info("OpenSearch 1.x detected, using ES 7.x index mappings")
esVersion = 7
}
if pingResult.Version.Number[0] == '2' {
logger.Info("OpenSearch 2.x detected, using ES 7.x index mappings")
esVersion = 7
}
if pingResult.Version.Number[0] == '3' {
logger.Info("OpenSearch 3.x detected, using ES 7.x index mappings")
esVersion = 7
}
}
logger.Info("Elasticsearch detected", zap.Int("version", esVersion))
//nolint:gosec // G115
c.Version = uint(esVersion)
}
var rawClientV8 *esv8.Client
if c.Version >= 8 {
rawClientV8, err = newElasticsearchV8(ctx, c, logger)
if err != nil {
return nil, fmt.Errorf("error creating v8 client: %w", err)
}
}
return eswrapper.WrapESClient(rawClient, bulkProc, c.Version, rawClientV8), nil
}
func (bcb *bulkCallback) invoke(id int64, requests []elastic.BulkableRequest, response *elastic.BulkResponse, err error) {
start, ok := bcb.startTimes.Load(id)
if ok {
bcb.startTimes.Delete(id)
} else {
start = time.Now()
}
// Log individual errors
if response != nil && response.Errors {
for _, it := range response.Items {
for key, val := range it {
if val.Error != nil {
bcb.logger.Error("Elasticsearch part of bulk request failed",
zap.String("map-key", key), zap.Reflect("response", val))
}
}
}
}
latency := time.Since(start.(time.Time))
if err != nil {
bcb.sm.LatencyErr.Record(latency)
} else {
bcb.sm.LatencyOk.Record(latency)
}
var failed int
if response != nil {
failed = len(response.Failed())
}
total := len(requests)
bcb.sm.Attempts.Inc(int64(total))
bcb.sm.Inserts.Inc(int64(total - failed))
bcb.sm.Errors.Inc(int64(failed))
if err != nil {
bcb.logger.Error("Elasticsearch could not process bulk request",
zap.Int("request_count", total),
zap.Int("failed_count", failed),
zap.Error(err),
zap.Any("response", response))
}
}
func newElasticsearchV8(ctx context.Context, c *Configuration, logger *zap.Logger) (*esv8.Client, error) {
var options esv8.Config
options.Addresses = c.Servers
if c.Authentication.BasicAuthentication.HasValue() {
basicAuth := c.Authentication.BasicAuthentication.Get()
options.Username = basicAuth.Username
options.Password = basicAuth.Password
}
options.DiscoverNodesOnStart = c.Sniffing.Enabled
options.CompressRequestBody = c.HTTPCompression
transport, err := GetHTTPRoundTripper(ctx, c, logger)
if err != nil {
return nil, err
}
options.Transport = transport
return esv8.NewClient(options)
}
func setDefaultIndexOptions(target, source *IndexOptions) {
if target.Shards == 0 {
target.Shards = source.Shards
}
if target.Replicas == nil {
target.Replicas = source.Replicas
}
if target.Priority == 0 {
target.Priority = source.Priority
}
if target.DateLayout == "" {
target.DateLayout = source.DateLayout
}
if target.RolloverFrequency == "" {
target.RolloverFrequency = source.RolloverFrequency
}
}
// ApplyDefaults copies settings from source unless its own value is non-zero.
func (c *Configuration) ApplyDefaults(source *Configuration) {
if len(c.RemoteReadClusters) == 0 {
c.RemoteReadClusters = source.RemoteReadClusters
}
// Handle BasicAuthentication defaults
sourceHasBasicAuth := source.Authentication.BasicAuthentication.HasValue()
targetHasBasicAuth := c.Authentication.BasicAuthentication.HasValue()
if sourceHasBasicAuth {
// If target doesn't have BasicAuth, copy it from source
if !targetHasBasicAuth {
c.Authentication.BasicAuthentication = source.Authentication.BasicAuthentication
} else {
// Target has BasicAuth, apply field-level defaults
sourceBasicAuth := source.Authentication.BasicAuthentication.Get()
// Make a copy of target BasicAuth
basicAuth := *c.Authentication.BasicAuthentication.Get()
// Apply defaults for username if not set
if basicAuth.Username == "" && sourceBasicAuth.Username != "" {
basicAuth.Username = sourceBasicAuth.Username
}
// Apply defaults for password if not set
if basicAuth.Password == "" && sourceBasicAuth.Password != "" {
basicAuth.Password = sourceBasicAuth.Password
}
// Only update BasicAuthentication if we have values to set
if basicAuth.Username != "" || basicAuth.Password != "" {
c.Authentication.BasicAuthentication = configoptional.Some(basicAuth)
}
}
}
if !c.Sniffing.Enabled {
c.Sniffing.Enabled = source.Sniffing.Enabled
}
if c.MaxSpanAge == 0 {
c.MaxSpanAge = source.MaxSpanAge
}
if c.AdaptiveSamplingLookback == 0 {
c.AdaptiveSamplingLookback = source.AdaptiveSamplingLookback
}
if c.Indices.IndexPrefix == "" {
c.Indices.IndexPrefix = source.Indices.IndexPrefix
}
setDefaultIndexOptions(&c.Indices.Spans, &source.Indices.Spans)
setDefaultIndexOptions(&c.Indices.Services, &source.Indices.Services)
setDefaultIndexOptions(&c.Indices.Dependencies, &source.Indices.Dependencies)
if c.BulkProcessing.MaxBytes == 0 {
c.BulkProcessing.MaxBytes = source.BulkProcessing.MaxBytes
}
if c.BulkProcessing.Workers == 0 {
c.BulkProcessing.Workers = source.BulkProcessing.Workers
}
if c.BulkProcessing.MaxActions == 0 {
c.BulkProcessing.MaxActions = source.BulkProcessing.MaxActions
}
if c.BulkProcessing.FlushInterval == 0 {
c.BulkProcessing.FlushInterval = source.BulkProcessing.FlushInterval
}
if !c.Sniffing.UseHTTPS {
c.Sniffing.UseHTTPS = source.Sniffing.UseHTTPS
}
if !c.Tags.AllAsFields {
c.Tags.AllAsFields = source.Tags.AllAsFields
}
if c.Tags.DotReplacement == "" {
c.Tags.DotReplacement = source.Tags.DotReplacement
}
if c.Tags.Include == "" {
c.Tags.Include = source.Tags.Include
}
if c.Tags.File == "" {
c.Tags.File = source.Tags.File
}
if c.MaxDocCount == 0 {
c.MaxDocCount = source.MaxDocCount
}
if c.LogLevel == "" {
c.LogLevel = source.LogLevel
}
if c.SendGetBodyAs == "" {
c.SendGetBodyAs = source.SendGetBodyAs
}
if !c.HTTPCompression {
c.HTTPCompression = source.HTTPCompression
}
}
// RolloverFrequencyAsNegativeDuration returns the index rollover frequency duration for the given frequency string
func RolloverFrequencyAsNegativeDuration(frequency string) time.Duration {
if frequency == "hour" {
return -1 * time.Hour
}
return -24 * time.Hour
}
// TagKeysAsFields returns tags from the file and command line merged
func (c *Configuration) TagKeysAsFields() ([]string, error) {
var tags []string
// from file
if c.Tags.File != "" {
file, err := os.Open(filepath.Clean(c.Tags.File))
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if tag := strings.TrimSpace(line); tag != "" {
tags = append(tags, tag)
}
}
if err := file.Close(); err != nil {
return nil, err
}
}
// from params
if c.Tags.Include != "" {
tags = append(tags, strings.Split(c.Tags.Include, ",")...)
}
return tags, nil
}
func (c *Configuration) getESOptions(disableHealthCheck bool) []elastic.ClientOptionFunc {
// Get base Elasticsearch options
options := []elastic.ClientOptionFunc{
elastic.SetURL(c.Servers...), elastic.SetSniff(c.Sniffing.Enabled), elastic.SetHealthcheck(!disableHealthCheck),
}
if c.Sniffing.UseHTTPS {
options = append(options, elastic.SetScheme("https"))
}
if c.SendGetBodyAs != "" {
options = append(options, elastic.SetSendGetBodyAs(c.SendGetBodyAs))
}
options = append(options, elastic.SetGzip(c.HTTPCompression))
return options
}
// getConfigOptions wraps the configs to feed to the ElasticSearch client init
func (c *Configuration) getConfigOptions(ctx context.Context, logger *zap.Logger) ([]elastic.ClientOptionFunc, error) {
// (has problems on AWS OpenSearch) see https://github.com/jaegertracing/jaeger/pull/7212
// Disable health check only in the following cases:
// 1. When health check is explicitly disabled
// 2. When tokens are EXCLUSIVELY available from context (not from file)
// because at startup we don't have a valid token to do the health check
disableHealthCheck := c.DisableHealthCheck
// Check if we have bearer token or API key authentication that only allows from context
if c.Authentication.BearerTokenAuth.HasValue() || c.Authentication.APIKeyAuth.HasValue() {
bearerAuth := c.Authentication.BearerTokenAuth.Get()
apiKeyAuth := c.Authentication.APIKeyAuth.Get()
disableHealthCheck = disableHealthCheck ||
(bearerAuth != nil && bearerAuth.AllowFromContext && bearerAuth.FilePath == "") ||
(apiKeyAuth != nil && apiKeyAuth.AllowFromContext && apiKeyAuth.FilePath == "")
}
// Get base Elasticsearch options using the helper function
options := c.getESOptions(disableHealthCheck)
// Configure HTTP transport with TLS and authentication
transport, err := GetHTTPRoundTripper(ctx, c, logger)
if err != nil {
return nil, err
}
// HTTP client setup with timeout and transport
httpClient := &http.Client{
Timeout: c.QueryTimeout,
Transport: transport,
}
options = append(options, elastic.SetHttpClient(httpClient))
// Add logging configuration
options, err = addLoggerOptions(options, c.LogLevel, logger)
if err != nil {
return options, err
}
return options, nil
}
func addLoggerOptions(options []elastic.ClientOptionFunc, logLevel string, logger *zap.Logger) ([]elastic.ClientOptionFunc, error) {
// Decouple ES logger from the log-level assigned to the parent application's log-level; otherwise, the least
// permissive log-level will dominate.
// e.g. --log-level=info and --es.log-level=debug would mute ES's debug logging and would require --log-level=debug
// to show ES debug logs.
var lvl zapcore.Level
var setLogger func(logger elastic.Logger) elastic.ClientOptionFunc
switch logLevel {
case "debug":
lvl = zap.DebugLevel
setLogger = elastic.SetTraceLog
case "info":
lvl = zap.InfoLevel
setLogger = elastic.SetInfoLog
case "error":
lvl = zap.ErrorLevel
setLogger = elastic.SetErrorLog
default:
return options, fmt.Errorf("unrecognized log-level: \"%s\"", logLevel)
}
esLogger := logger.WithOptions(
zap.IncreaseLevel(lvl),
zap.AddCallerSkip(2), // to ensure the right caller:lineno are logged
)
// Elastic client requires a "Printf"-able logger.
l := zapgrpc.NewLogger(esLogger)
options = append(options, setLogger(l))
return options, nil
}
// GetHTTPRoundTripper returns configured http.RoundTripper.
func GetHTTPRoundTripper(ctx context.Context, c *Configuration, logger *zap.Logger) (http.RoundTripper, error) {
// Configure base transport.
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
}
// Configure TLS.
if c.TLS.Insecure {
// #nosec G402
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
} else {
tlsConfig, err := c.TLS.LoadTLSConfig(ctx)
if err != nil {
return nil, err
}
transport.TLSClientConfig = tlsConfig
}
// Initialize authentication methods.
var authMethods []auth.Method
// API Key Authentication
if c.Authentication.APIKeyAuth.HasValue() {
apiKeyAuth := c.Authentication.APIKeyAuth.Get()
ak, err := initAPIKeyAuth(apiKeyAuth, logger)
if err != nil {
return nil, fmt.Errorf("failed to initialize API key authentication: %w", err)
}
if ak != nil {
authMethods = append(authMethods, *ak)
}
}
// Bearer Token Authentication
if c.Authentication.BearerTokenAuth.HasValue() {
bearerAuth := c.Authentication.BearerTokenAuth.Get()
ba, err := initBearerAuth(bearerAuth, logger)
if err != nil {
return nil, fmt.Errorf("failed to initialize bearer authentication: %w", err)
}
if ba != nil {
authMethods = append(authMethods, *ba)
}
}
// Basic Authentication
if c.Authentication.BasicAuthentication.HasValue() {
basicAuth := c.Authentication.BasicAuthentication.Get()
ba, err := initBasicAuth(basicAuth, logger)
if err != nil {
return nil, fmt.Errorf("failed to initialize basic authentication: %w", err)
}
if ba != nil {
authMethods = append(authMethods, *ba)
}
}
// Wrap with authentication layer.
var roundTripper http.RoundTripper = transport
if len(authMethods) > 0 {
roundTripper = &auth.RoundTripper{
Transport: transport,
Auths: authMethods,
}
}
return roundTripper, nil
}
func loadTokenFromFile(path string) (string, error) {
b, err := os.ReadFile(filepath.Clean(path))
if err != nil {
return "", err
}
return strings.TrimRight(string(b), "\r\n"), nil
}
func (c *Configuration) Validate() error {
_, err := govalidator.ValidateStruct(c)
if err != nil {
return err
}
if c.UseILM && !c.UseReadWriteAliases {
return errors.New("UseILM must always be used in conjunction with UseReadWriteAliases to ensure ES writers and readers refer to the single index mapping")
}
if c.CreateIndexTemplates && c.UseILM {
return errors.New("when UseILM is set true, CreateIndexTemplates must be set to false and index templates must be created by init process of es-rollover app")
}
return nil
}