Skip to content

Commit

Permalink
NSOF-8274 receiver/hostmetricsreceiver:processscraper: Workaround pro…
Browse files Browse the repository at this point in the history
…cess createTime bogus value

Process createTime collected using gopsutils/process module and uses as
startTime of metric sequence.
When running inside lxc, function that calculates create time mixes host
and guest stats.
See shirou/gopsutil#1562 for more details
This leads to two issues:
1. Calculated createTime has future value eventually causing scrapper to
skip process collection.
2. dp series startTime is set to future in time value, the consequences
   are unknown.
In order to overcome this issue, we will set createTime to system boot time.
In case of lxc, container boot time
  • Loading branch information
drubinMeta committed Dec 13, 2023
1 parent 1e22295 commit 4bcd312
Showing 1 changed file with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/shirou/gopsutil/v3/common"
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/process"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
Expand Down Expand Up @@ -54,6 +55,7 @@ type scraper struct {
getProcessHandles func(context.Context) (processHandles, error)

handleCountManager handlecount.Manager
bootTime int64
}

// newProcessScraper creates a Process Scraper
Expand Down Expand Up @@ -87,8 +89,15 @@ func newProcessScraper(settings receiver.CreateSettings, cfg *Config) (*scraper,
return scraper, nil
}

func (s *scraper) start(context.Context, component.Host) error {
func (s *scraper) start(ctx context.Context, chost component.Host) error {
s.mb = metadata.NewMetricsBuilder(s.config.MetricsBuilderConfig, s.settings)
ctx = context.WithValue(ctx, common.EnvKey, s.config.EnvMap)
bt, err := host.BootTimeWithContext(ctx)
if err == nil {
s.bootTime = int64(bt) * 1000
} else {
s.bootTime = time.Now().UnixMilli()
}
return nil
}

Expand Down Expand Up @@ -222,12 +231,10 @@ func (s *scraper) getProcessMetadata() ([]*processMetadata, error) {
errs.AddPartial(0, fmt.Errorf("error reading username for process %q (pid %v): %w", executable.name, pid, err))
}

createTime, err := s.getProcessCreateTime(handle, ctx)
if err != nil {
errs.AddPartial(0, fmt.Errorf("error reading create time for process %q (pid %v): %w", executable.name, pid, err))
// set the start time to now to avoid including this when a scrape_process_delay is set
createTime = time.Now().UnixMilli()
}
// getProcessCreateTime is bogus for lxc env
// see https://github.com/shirou/gopsutil/issues/1562
// use container boot time as createTime => resource startTime
createTime := s.bootTime
if s.scrapeProcessDelay.Milliseconds() > (time.Now().UnixMilli() - createTime) {
continue
}
Expand Down

0 comments on commit 4bcd312

Please sign in to comment.