Skip to content

Commit

Permalink
Enable separately-controlled logging in ES client (#2862)
Browse files Browse the repository at this point in the history
* Enable logging in ES client

Signed-off-by: albertteoh <[email protected]>

* Decouple es logger from main logger

Signed-off-by: albertteoh <[email protected]>

* Error on unrecognized level

Signed-off-by: albertteoh <[email protected]>
  • Loading branch information
albertteoh authored Mar 10, 2021
1 parent 98c0b0e commit 305ac76
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pkg/es/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"crypto/tls"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
Expand All @@ -32,6 +33,7 @@ import (
"github.com/olivere/elastic"
"github.com/uber/jaeger-lib/metrics"
"go.uber.org/zap"
"go.uber.org/zap/zapgrpc"

"github.com/jaegertracing/jaeger/pkg/config/tlscfg"
"github.com/jaegertracing/jaeger/pkg/es"
Expand Down Expand Up @@ -67,6 +69,7 @@ type Configuration struct {
CreateIndexTemplates bool `mapstructure:"create_mappings"`
UseILM bool `mapstructure:"use_ilm"`
Version uint `mapstructure:"version"`
LogLevel string `mapstructure:"log_level"`
}

// TagsAsFields holds configuration for tag schema.
Expand Down Expand Up @@ -102,6 +105,7 @@ type ClientBuilder interface {
GetVersion() uint
TagKeysAsFields() ([]string, error)
GetUseILM() bool
GetLogLevel() string
}

// NewClient creates a new ElasticSearch client
Expand Down Expand Up @@ -237,6 +241,9 @@ func (c *Configuration) ApplyDefaults(source *Configuration) {
if c.MaxDocCount == 0 {
c.MaxDocCount = source.MaxDocCount
}
if c.LogLevel == "" {
c.LogLevel = source.LogLevel
}
}

// GetNumShards returns number of shards from Configuration
Expand Down Expand Up @@ -300,6 +307,11 @@ func (c *Configuration) GetUseILM() bool {
return c.UseILM
}

// GetLogLevel returns the log-level the ES client should log at.
func (c *Configuration) GetLogLevel() string {
return c.LogLevel
}

// GetTokenFilePath returns file path containing the bearer token
func (c *Configuration) GetTokenFilePath() string {
return c.TokenFilePath
Expand Down Expand Up @@ -362,6 +374,12 @@ func (c *Configuration) getConfigOptions(logger *zap.Logger) ([]elastic.ClientOp
}
options = append(options, elastic.SetHttpClient(httpClient))
options = append(options, elastic.SetBasicAuth(c.Username, c.Password))

options, err := addLoggerOptions(options, c.LogLevel)
if err != nil {
return options, err
}

transport, err := GetHTTPRoundTripper(c, logger)
if err != nil {
return nil, err
Expand All @@ -370,6 +388,35 @@ func (c *Configuration) getConfigOptions(logger *zap.Logger) ([]elastic.ClientOp
return options, nil
}

func addLoggerOptions(options []elastic.ClientOptionFunc, logLevel string) ([]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.
prodConfig := zap.NewProductionConfig()
prodConfig.Level.SetLevel(zap.DebugLevel)

esLogger, err := prodConfig.Build()
if err != nil {
return options, err
}

// Elastic client requires a "Printf"-able logger.
l := zapgrpc.NewLogger(esLogger)
switch logLevel {
case "debug":
l = zapgrpc.NewLogger(esLogger, zapgrpc.WithDebug())
options = append(options, elastic.SetTraceLog(l))
case "info":
options = append(options, elastic.SetInfoLog(l))
case "error":
options = append(options, elastic.SetErrorLog(l))
default:
return options, fmt.Errorf("unrecognized log-level: \"%s\"", logLevel)
}
return options, nil
}

// GetHTTPRoundTripper returns configured http.RoundTripper
func GetHTTPRoundTripper(c *Configuration, logger *zap.Logger) (http.RoundTripper, error) {
if c.TLS.Enabled {
Expand Down
7 changes: 7 additions & 0 deletions plugin/storage/es/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const (
suffixEnabled = ".enabled"
suffixVersion = ".version"
suffixMaxDocCount = ".max-doc-count"
suffixLogLevel = ".log-level"
// default number of documents to return from a query (elasticsearch allowed limit)
// see search.max_buckets and index.max_result_window
defaultMaxDocCount = 10_000
Expand Down Expand Up @@ -102,6 +103,7 @@ func NewOptions(primaryNamespace string, otherNamespaces ...string) *Options {
Version: 0,
Servers: []string{defaultServerURL},
MaxDocCount: defaultMaxDocCount,
LogLevel: "error",
}
options := &Options{
Primary: namespaceConfig{
Expand Down Expand Up @@ -240,6 +242,10 @@ func addFlags(flagSet *flag.FlagSet, nsConfig *namespaceConfig) {
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]")

if nsConfig.namespace == archiveNamespace {
flagSet.Bool(
Expand Down Expand Up @@ -290,6 +296,7 @@ func initFromViper(cfg *namespaceConfig, v *viper.Viper) {
cfg.Enabled = v.GetBool(cfg.namespace + suffixEnabled)
cfg.CreateIndexTemplates = v.GetBool(cfg.namespace + suffixCreateIndexTemplate)
cfg.Version = uint(v.GetInt(cfg.namespace + suffixVersion))
cfg.LogLevel = v.GetString(cfg.namespace + suffixLogLevel)

cfg.MaxDocCount = v.GetInt(cfg.namespace + suffixMaxDocCount)
cfg.UseILM = v.GetBool(cfg.namespace + suffixUseILM)
Expand Down

0 comments on commit 305ac76

Please sign in to comment.