Skip to content

Commit

Permalink
remove global logp calls
Browse files Browse the repository at this point in the history
  • Loading branch information
kvch committed May 6, 2020
1 parent fed44d5 commit d89f54f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
7 changes: 5 additions & 2 deletions libbeat/kibana/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Connection struct {

type Client struct {
Connection
log *logp.Logger
}

func addToURL(_url, _path string, params url.Values) string {
Expand Down Expand Up @@ -115,7 +116,8 @@ func NewClientWithConfig(config *ClientConfig) (*Client, error) {
kibanaURL = u.String()
}

logp.Info("Kibana url: %s", kibanaURL)
log := logp.NewLogger("kibana")
log.Info("Kibana url: %s", kibanaURL)

var dialer, tlsDialer transport.Dialer

Expand Down Expand Up @@ -144,6 +146,7 @@ func NewClientWithConfig(config *ClientConfig) (*Client, error) {
Timeout: config.Timeout,
},
},
log: log,
}

if !config.IgnoreVersion {
Expand Down Expand Up @@ -271,7 +274,7 @@ func (client *Client) ImportJSON(url string, params url.Values, jsonBody map[str

body, err := json.Marshal(jsonBody)
if err != nil {
logp.Err("Failed to json encode body (%v): %#v", err, jsonBody)
client.log.Errorf("Failed to json encode body (%v): %#v", err, jsonBody)
return fmt.Errorf("fail to marshal the json content: %v", err)
}

Expand Down
66 changes: 39 additions & 27 deletions libbeat/template/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ type Loader interface {

// ESLoader implements Loader interface for loading templates to Elasticsearch.
type ESLoader struct {
client ESClient
client ESClient
builder *templateBuilder
log *logp.Logger
}

// ESClient is a subset of the Elasticsearch client API capable of
Expand All @@ -50,7 +52,9 @@ type ESClient interface {

// FileLoader implements Loader interface for loading templates to a File.
type FileLoader struct {
client FileClient
client FileClient
builder *templateBuilder
log *logp.Logger
}

// FileClient defines the minimal interface required for the FileLoader
Expand All @@ -59,22 +63,30 @@ type FileClient interface {
Write(component string, name string, body string) error
}

type templateBuilder struct {
log *logp.Logger
}

// NewESLoader creates a new template loader for ES
func NewESLoader(client ESClient) *ESLoader {
return &ESLoader{client: client}
return &ESLoader{client: client, builder: newTemplateBuilder(), log: logp.NewLogger("template_loader")}
}

// NewFileLoader creates a new template loader for the given file.
func NewFileLoader(c FileClient) *FileLoader {
return &FileLoader{client: c}
return &FileLoader{client: c, builder: newTemplateBuilder(), log: logp.NewLogger("file_template_loader")}
}

func newTemplateBuilder() *templateBuilder {
return &templateBuilder{log: logp.NewLogger("template")}
}

// Load checks if the index mapping template should be loaded
// In case the template is not already loaded or overwriting is enabled, the
// template is built and written to index
func (l *ESLoader) Load(config TemplateConfig, info beat.Info, fields []byte, migration bool) error {
//build template from config
tmpl, err := template(config, info, l.client.GetVersion(), migration)
tmpl, err := l.builder.template(config, info, l.client.GetVersion(), migration)
if err != nil || tmpl == nil {
return err
}
Expand All @@ -86,27 +98,27 @@ func (l *ESLoader) Load(config TemplateConfig, info beat.Info, fields []byte, mi
}

if l.templateExists(templateName) && !config.Overwrite {
logp.Info("Template %s already exists and will not be overwritten.", templateName)
l.log.Infof("Template %s already exists and will not be overwritten.", templateName)
return nil
}

//loading template to ES
body, err := buildBody(tmpl, config, fields)
body, err := l.builder.buildBody(tmpl, config, fields)
if err != nil {
return err
}
if err := l.loadTemplate(templateName, body); err != nil {
return fmt.Errorf("could not load template. Elasticsearch returned: %v. Template is: %s", err, body.StringToPrint())
}
logp.Info("template with name '%s' loaded.", templateName)
l.log.Infof("template with name '%s' loaded.", templateName)
return nil
}

// loadTemplate loads a template into Elasticsearch overwriting the existing
// template if it exists. If you wish to not overwrite an existing template
// then use CheckTemplate prior to calling this method.
func (l *ESLoader) loadTemplate(templateName string, template map[string]interface{}) error {
logp.Info("Try loading template %s to Elasticsearch", templateName)
l.log.Infof("Try loading template %s to Elasticsearch", templateName)
path := "/_template/" + templateName
params := esVersionParams(l.client.GetVersion())
status, body, err := l.client.Request("PUT", path, "", params, template)
Expand Down Expand Up @@ -134,13 +146,13 @@ func (l *ESLoader) templateExists(templateName string) bool {
// Load reads the template from the config, creates the template body and prints it to the configured file.
func (l *FileLoader) Load(config TemplateConfig, info beat.Info, fields []byte, migration bool) error {
//build template from config
tmpl, err := template(config, info, l.client.GetVersion(), migration)
tmpl, err := l.builder.template(config, info, l.client.GetVersion(), migration)
if err != nil || tmpl == nil {
return err
}

//create body to print
body, err := buildBody(tmpl, config, fields)
body, err := l.builder.buildBody(tmpl, config, fields)
if err != nil {
return err
}
Expand All @@ -152,9 +164,9 @@ func (l *FileLoader) Load(config TemplateConfig, info beat.Info, fields []byte,
return nil
}

func template(config TemplateConfig, info beat.Info, esVersion common.Version, migration bool) (*Template, error) {
func (b *templateBuilder) template(config TemplateConfig, info beat.Info, esVersion common.Version, migration bool) (*Template, error) {
if !config.Enabled {
logp.Info("template config not enabled")
b.log.Info("template config not enabled")
return nil, nil
}
tmpl, err := New(info.Version, info.IndexPrefix, esVersion, config, migration)
Expand All @@ -164,29 +176,29 @@ func template(config TemplateConfig, info beat.Info, esVersion common.Version, m
return tmpl, nil
}

func buildBody(tmpl *Template, config TemplateConfig, fields []byte) (common.MapStr, error) {
func (b *templateBuilder) buildBody(tmpl *Template, config TemplateConfig, fields []byte) (common.MapStr, error) {
if config.Overwrite {
logp.Info("Existing template will be overwritten, as overwrite is enabled.")
b.log.Info("Existing template will be overwritten, as overwrite is enabled.")
}

if config.JSON.Enabled {
return buildBodyFromJSON(config)
return b.buildBodyFromJSON(config)
}
if config.Fields != "" {
return buildBodyFromFile(tmpl, config)
return b.buildBodyFromFile(tmpl, config)
}
if fields == nil {
return buildMinimalTemplate(tmpl)
return b.buildMinimalTemplate(tmpl)
}
return buildBodyFromFields(tmpl, fields)
return b.buildBodyFromFields(tmpl, fields)
}

func buildBodyFromJSON(config TemplateConfig) (common.MapStr, error) {
func (b *templateBuilder) buildBodyFromJSON(config TemplateConfig) (common.MapStr, error) {
jsonPath := paths.Resolve(paths.Config, config.JSON.Path)
if _, err := os.Stat(jsonPath); err != nil {
return nil, fmt.Errorf("error checking json file %s for template: %v", jsonPath, err)
}
logp.Debug("template", "Loading json template from file %s", jsonPath)
b.log.Debugf("Loading json template from file %s", jsonPath)
content, err := ioutil.ReadFile(jsonPath)
if err != nil {
return nil, fmt.Errorf("error reading file %s for template: %v", jsonPath, err)
Expand All @@ -200,8 +212,8 @@ func buildBodyFromJSON(config TemplateConfig) (common.MapStr, error) {
return body, nil
}

func buildBodyFromFile(tmpl *Template, config TemplateConfig) (common.MapStr, error) {
logp.Debug("template", "Load fields.yml from file: %s", config.Fields)
func (b *templateBuilder) buildBodyFromFile(tmpl *Template, config TemplateConfig) (common.MapStr, error) {
b.log.Debugf("Load fields.yml from file: %s", config.Fields)
fieldsPath := paths.Resolve(paths.Config, config.Fields)
body, err := tmpl.LoadFile(fieldsPath)
if err != nil {
Expand All @@ -210,17 +222,17 @@ func buildBodyFromFile(tmpl *Template, config TemplateConfig) (common.MapStr, er
return body, nil
}

func buildBodyFromFields(tmpl *Template, fields []byte) (common.MapStr, error) {
logp.Debug("template", "Load default fields")
func (b *templateBuilder) buildBodyFromFields(tmpl *Template, fields []byte) (common.MapStr, error) {
b.log.Debug("Load default fields")
body, err := tmpl.LoadBytes(fields)
if err != nil {
return nil, fmt.Errorf("error creating template: %v", err)
}
return body, nil
}

func buildMinimalTemplate(tmpl *Template) (common.MapStr, error) {
logp.Debug("template", "Load minimal template")
func (b *templateBuilder) buildMinimalTemplate(tmpl *Template) (common.MapStr, error) {
b.log.Debug("Load minimal template")
body, err := tmpl.LoadMinimal()
if err != nil {
return nil, fmt.Errorf("error creating mimimal template: %v", err)
Expand Down

0 comments on commit d89f54f

Please sign in to comment.