Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Libbeat: Remove global loggers from libbeat/metric and libbeat/cloudid #18500

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions libbeat/cloudid/cloudid.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

// package cloudid contains functions for parsing the cloud.id and cloud.auth
// Package cloudid contains functions for parsing the cloud.id and cloud.auth
// settings and modifying the configuration to take them into account.
package cloudid

Expand Down Expand Up @@ -149,6 +149,7 @@ func (c *CloudID) decodeCloudAuth() error {
// settings.
func OverwriteSettings(cfg *common.Config) error {

logger := logp.NewLogger("cloudid")
cloudID, _ := cfg.String("cloud.id", -1)
cloudAuth, _ := cfg.String("cloud.auth", -1)

Expand All @@ -157,9 +158,9 @@ func OverwriteSettings(cfg *common.Config) error {
return nil
}

logp.Debug("cloudid", "cloud.id: %s, cloud.auth: %s", cloudID, cloudAuth)
logger.Debugf("cloud.id: %s, cloud.auth: %s", cloudID, cloudAuth)
if cloudID == "" {
return errors.New("cloud.auth specified but cloud.id is empty. Please specify both.")
return errors.New("cloud.auth specified but cloud.id is empty. Please specify both")
}

// cloudID overwrites
Expand All @@ -168,7 +169,7 @@ func OverwriteSettings(cfg *common.Config) error {
return errors.Errorf("Error decoding cloud.id: %v", err)
}

logp.Info("Setting Elasticsearch and Kibana URLs based on the cloud id: output.elasticsearch.hosts=%s and setup.kibana.host=%s", cid.esURL, cid.kibURL)
logger.Infof("Setting Elasticsearch and Kibana URLs based on the cloud id: output.elasticsearch.hosts=%s and setup.kibana.host=%s", cid.esURL, cid.kibURL)

esURLConfig, err := common.NewConfigFrom([]string{cid.ElasticsearchURL()})
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion libbeat/metric/system/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ func GetSwap() (*SwapStat, error) {
// this can provoke too big values for used swap.
// Workaround this by assuming that all swap is free in that case.
if swap.Free > swap.Total || swap.Used > swap.Total {
logp.Debug("memory",
logger := logp.NewLogger("memory")
logger.Debugf("memory",
fearful-symmetry marked this conversation as resolved.
Show resolved Hide resolved
"Unexpected values for swap memory - total: %v free: %v used: %v. Setting swap used to 0.",
swap.Total, swap.Free, swap.Used)
swap.Free = swap.Total
Expand Down
32 changes: 20 additions & 12 deletions libbeat/metric/system/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ type Stats struct {

procRegexps []match.Matcher // List of regular expressions used to whitelist processes.
envRegexps []match.Matcher // List of regular expressions used to whitelist env vars.

logger *logp.Logger
}

// Ticks of CPU for a process
Expand Down Expand Up @@ -217,22 +219,18 @@ func getProcEnv(pid int, out common.MapStr, filter func(v string) bool) error {
return nil
}

// GetProcMemPercentage returns process memory usage as a percent of total memory usage
func GetProcMemPercentage(proc *Process, totalPhyMem uint64) float64 {
// in unit tests, total_phymem is set to a value greater than zero
if totalPhyMem == 0 {
memStat, err := memory.Get()
if err != nil {
logp.Warn("Getting memory details: %v", err)
return 0
}
totalPhyMem = memStat.Mem.Total
return 0
fearful-symmetry marked this conversation as resolved.
Show resolved Hide resolved
}

perc := (float64(proc.Mem.Resident) / float64(totalPhyMem))

return common.Round(perc, 4)
}

// Pids returns a list of PIDs
func Pids() ([]int, error) {
pids := sigar.ProcList{}
err := pids.Get()
Expand Down Expand Up @@ -273,6 +271,15 @@ func GetOwnResourceUsageTimeInMillis() (int64, int64, error) {
}

func (procStats *Stats) getProcessEvent(process *Process) common.MapStr {

var totalPhyMem uint64
baseMem, err := memory.Get()
if err != nil {
procStats.logger.Warnf("Getting memory details: %v", err)
} else {
totalPhyMem = baseMem.Mem.Total
}

proc := common.MapStr{
"pid": process.Pid,
"ppid": process.Ppid,
Expand All @@ -284,7 +291,7 @@ func (procStats *Stats) getProcessEvent(process *Process) common.MapStr {
"size": process.Mem.Size,
"rss": common.MapStr{
"bytes": process.Mem.Resident,
"pct": GetProcMemPercentage(process, 0 /* read total mem usage */),
"pct": GetProcMemPercentage(process, totalPhyMem),
},
"share": process.Mem.Share,
},
Expand Down Expand Up @@ -380,6 +387,7 @@ func (procStats *Stats) matchProcess(name string) bool {
// Init initializes a Stats instance. It returns errors if the provided process regexes
// cannot be compiled.
func (procStats *Stats) Init() error {
procStats.logger = logp.NewLogger("Stats")
fearful-symmetry marked this conversation as resolved.
Show resolved Hide resolved
procStats.ProcsMap = make(ProcsMap)

if len(procStats.Procs) == 0 {
Expand Down Expand Up @@ -431,7 +439,7 @@ func (procStats *Stats) Get() ([]common.MapStr, error) {
procStats.ProcsMap = newProcs

processes = procStats.includeTopProcesses(processes)
logp.Debug("processes", "Filtered top processes down to %d processes", len(processes))
procStats.logger.Debugf("processes", "Filtered top processes down to %d processes", len(processes))

procs := make([]common.MapStr, 0, len(processes))
for _, process := range processes {
Expand Down Expand Up @@ -472,18 +480,18 @@ func (procStats *Stats) getSingleProcess(pid int, newProcs ProcsMap) *Process {

process, err := newProcess(pid, cmdline, env)
if err != nil {
logp.Debug("processes", "Skip process pid=%d: %v", pid, err)
procStats.logger.Debugf("processes", "Skip process pid=%d: %v", pid, err)
return nil
}

if !procStats.matchProcess(process.Name) {
logp.Debug("processes", "Process name does not matches the provided regex; pid=%d; name=%s: %v", pid, process.Name, err)
procStats.logger.Debugf("processes", "Process name does not matches the provided regex; pid=%d; name=%s: %v", pid, process.Name, err)
return nil
}

err = process.getDetails(procStats.isWhitelistedEnvVar)
if err != nil {
logp.Debug("processes", "Error getting details for process %s with pid=%d: %v", process.Name, process.Pid, err)
procStats.logger.Debugf("processes", "Error getting details for process %s with pid=%d: %v", process.Name, process.Pid, err)
return nil
}

Expand Down