Skip to content

Commit

Permalink
Remove redundant logging in Metricbeat (elastic#3048)
Browse files Browse the repository at this point in the history
The individual MetricSets don’t need to log the host information on initialization because the Metricbeat core does this when it initializes the MetricSets.
  • Loading branch information
andrewkroh authored and ruflin committed Nov 22, 2016
1 parent 4e1d630 commit 4c2389f
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 22 deletions.
3 changes: 2 additions & 1 deletion metricbeat/mb/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ func initMetricSets(r *Register, modules []Module) (map[Module][]MetricSet, erro
if hostParser != nil {
bms.hostData, err = hostParser(bms.Module(), bms.host)
if err != nil {
errs = append(errs, err)
errs = append(errs, errors.Wrapf(err, "host parsing failed for %v-%v",
bms.Module().Name(), bms.Name()))
continue
}
bms.host = bms.hostData.Host
Expand Down
57 changes: 46 additions & 11 deletions metricbeat/mb/mb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ to implement Modules and their associated MetricSets.
package mb

import (
"fmt"
"time"

"github.com/elastic/beats/libbeat/common"
Expand Down Expand Up @@ -39,6 +40,12 @@ type BaseModule struct {
rawConfig *common.Config
}

func (m *BaseModule) String() string {
return fmt.Sprintf(`{name:"%v", config:%v}`, m.name, m.config.String())
}

func (m *BaseModule) GoString() string { return m.String() }

// Name returns the name of the Module.
func (m *BaseModule) Name() string { return m.name }

Expand Down Expand Up @@ -77,6 +84,26 @@ type EventsFetcher interface {
Fetch() ([]common.MapStr, error)
}

// HostData contains values parsed from the 'host' configuration. Other
// configuration data like protocols, usernames, and passwords may also be
// used to construct this HostData data.
type HostData struct {
URI string // The full URI that should be used in connections.
SanitizedURI string // A sanitized version of the URI without credentials.

// Parts of the URI.

Host string // The host and possibly port.
User string // Username
Password string // Password
}

func (h HostData) String() string {
return fmt.Sprintf(`{SanitizedURI:"%v", Host:"%v"}`, h.SanitizedURI, h.Host)
}

func (h HostData) GoString() string { return h.String() }

// BaseMetricSet implements the MetricSet interface.
//
// The BaseMetricSet type can be embedded into another struct to satisfy the
Expand All @@ -89,19 +116,17 @@ type BaseMetricSet struct {
hostData HostData
}

// HostData contains values parsed from the 'host' configuration. Other
// configuration data like protocols, usernames, and passwords may also be
// used to construct this HostData data.
type HostData struct {
URI string // The full URI that should be used in connections.
SanitizedURI string // A sanitized version of the URI without credentials.

// Parts of the URI.
Host string // The host and possibly port.
User string // Username
Password string // Password
func (b *BaseMetricSet) String() string {
moduleName := "nil"
if b.module != nil {
moduleName = b.module.Name()
}
return fmt.Sprintf(`{name:"%v", module:"%v", hostData:%v}`,
b.name, moduleName, b.hostData.String())
}

func (b *BaseMetricSet) GoString() string { return b.String() }

// Name returns the name of the MetricSet. It should not include the name of
// the module.
func (b *BaseMetricSet) Name() string {
Expand Down Expand Up @@ -144,6 +169,16 @@ type ModuleConfig struct {
common.EventMetadata `config:",inline"` // Fields and tags to add to events.
}

func (c ModuleConfig) String() string {
return fmt.Sprintf(`{Module:"%v", MetricSets:%v, Enabled:%v, `+
`Hosts:[%v hosts], Period:"%v", Timeout:"%v", Raw:%v, Fields:%v, `+
`FieldsUnderRoot:%v, Tags:%v}`,
c.Module, c.MetricSets, c.Enabled, len(c.Hosts), c.Period, c.Timeout,
c.Raw, c.Fields, c.FieldsUnderRoot, c.Tags)
}

func (c ModuleConfig) GoString() string { return c.String() }

// defaultModuleConfig contains the default values for ModuleConfig instances.
var defaultModuleConfig = ModuleConfig{
Enabled: true,
Expand Down
6 changes: 4 additions & 2 deletions metricbeat/module/apache/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ type MetricSet struct {

// New creates new instance of MetricSet.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
debugf("apache-status URL=%s", base.HostData().SanitizedURI)
return &MetricSet{
BaseMetricSet: base,
client: &http.Client{Timeout: base.Module().Config().Timeout},
Expand All @@ -60,7 +59,10 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
// Fetch makes an HTTP request to fetch status metrics from the mod_status
// endpoint.
func (m *MetricSet) Fetch() (common.MapStr, error) {
req, err := http.NewRequest("GET", m.HostData().URI, nil)
req, err := http.NewRequest("GET", m.HostData().SanitizedURI, nil)
if m.HostData().User != "" || m.HostData().Password != "" {
req.SetBasicAuth(m.HostData().User, m.HostData().Password)
}
resp, err := m.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error making http request: %v", err)
Expand Down
4 changes: 3 additions & 1 deletion metricbeat/module/haproxy/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {

// Fetch fetches info stats from the haproxy service.
func (m *MetricSet) Fetch() (common.MapStr, error) {
hapc, err := haproxy.NewHaproxyClient(m.HostData().URI)
// haproxy doesn't accept a username or password so ignore them if they
// are in the URL.
hapc, err := haproxy.NewHaproxyClient(m.HostData().SanitizedURI)
if err != nil {
return nil, errors.Wrap(err, "failed creating haproxy client")
}
Expand Down
5 changes: 3 additions & 2 deletions metricbeat/module/haproxy/stat/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {

// Fetch methods returns a list of stats metrics.
func (m *MetricSet) Fetch() ([]common.MapStr, error) {
hapc, err := haproxy.NewHaproxyClient(m.HostData().URI)
// haproxy doesn't accept a username or password so ignore them if they
// are in the URL.
hapc, err := haproxy.NewHaproxyClient(m.HostData().SanitizedURI)
if err != nil {
return nil, errors.Wrap(err, "failed creating haproxy client")
}
Expand All @@ -49,5 +51,4 @@ func (m *MetricSet) Fetch() ([]common.MapStr, error) {
}

return eventMapping(res), nil

}
2 changes: 1 addition & 1 deletion metricbeat/module/mongodb/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func init() {
// Register the ModuleFactory function for the "mysql" module.
// Register the ModuleFactory function for the "mongodb" module.
if err := mb.Registry.AddModule("mongodb", NewModule); err != nil {
panic(err)
}
Expand Down
1 change: 0 additions & 1 deletion metricbeat/module/mongodb/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
}
dialInfo.Timeout = base.Module().Config().Timeout

debugf("mongodb-status url=%v", base.HostData().SanitizedURI)
return &MetricSet{
BaseMetricSet: base,
dialInfo: dialInfo,
Expand Down
1 change: 0 additions & 1 deletion metricbeat/module/mysql/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ type MetricSet struct {

// New creates and returns a new MetricSet instance.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
debugf("mysql-status dsn=%v", base.HostData().SanitizedURI)
return &MetricSet{BaseMetricSet: base}, nil
}

Expand Down
6 changes: 4 additions & 2 deletions metricbeat/module/nginx/stubstatus/stubstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ type MetricSet struct {

// New creates new instance of MetricSet
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
debugf("nginx-stubstatus URL=%s", base.HostData().SanitizedURI)
return &MetricSet{
BaseMetricSet: base,
client: &http.Client{Timeout: base.Module().Config().Timeout},
Expand All @@ -54,7 +53,10 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {

// Fetch makes an HTTP request to fetch status metrics from the stubstatus endpoint.
func (m *MetricSet) Fetch() (common.MapStr, error) {
req, err := http.NewRequest("GET", m.HostData().URI, nil)
req, err := http.NewRequest("GET", m.HostData().SanitizedURI, nil)
if m.HostData().User != "" || m.HostData().Password != "" {
req.SetBasicAuth(m.HostData().User, m.HostData().Password)
}
resp, err := m.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error making http request: %v", err)
Expand Down

0 comments on commit 4c2389f

Please sign in to comment.