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 15, 2023
1 parent 299e6e3 commit 0dae594
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type scraper struct {
ucals map[int32]*ucal.CPUUtilizationCalculator

// for mocking
getProcessCreateTime func(p processHandle, ctx context.Context) (int64, error)
getProcessCreateTime func(ctx context.Context, p processHandle) (int64, error)
getProcessHandles func(context.Context) (processHandles, error)

handleCountManager handlecount.Manager
Expand All @@ -61,7 +61,7 @@ func newProcessScraper(settings receiver.CreateSettings, cfg *Config) (*scraper,
scraper := &scraper{
settings: settings,
config: cfg,
getProcessCreateTime: processHandle.CreateTimeWithContext,
getProcessCreateTime: getProcessCreateTimeInternal,
getProcessHandles: getProcessHandlesInternal,
scrapeProcessDelay: cfg.ScrapeProcessDelay,
ucals: make(map[int32]*ucal.CPUUtilizationCalculator),
Expand Down Expand Up @@ -222,7 +222,7 @@ 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)
createTime, err := s.getProcessCreateTime(ctx, handle)
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ func getProcessCommand(ctx context.Context, proc processHandle) (*commandMetadat
return command, nil

}

func getProcessCreateTimeInternal(ctx context.Context, proc processHandle) (int64, error) {
return proc.CreateTimeWithContext(ctx)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"context"

"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/host"
"go.opentelemetry.io/collector/pdata/pcommon"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper/internal/metadata"
Expand Down Expand Up @@ -60,3 +61,18 @@ func getProcessCommand(ctx context.Context, proc processHandle) (*commandMetadat
command := &commandMetadata{command: cmd, commandLineSlice: cmdline}
return command, nil
}

func getProcessCreateTimeInternal(ctx context.Context, proc processHandle) (int64, error) {
vsystem, vrole, err := host.VirtualizationWithContext(ctx)
if err != nil {
return 0, err
}
if vsystem == "lxc" && vrole == "guest" {
bootTime, err := host.BootTimeWithContext(ctx)
if err != nil {
return 0, err
}
return int64(bootTime) * 1000, nil
}
return proc.CreateTimeWithContext(ctx)
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ func getProcessExecutable(context.Context, processHandle) (string, error) {
func getProcessCommand(context.Context, processHandle) (*commandMetadata, error) {
return nil, nil
}

func getProcessCreateTimeInternal(ctx context.Context, proc processHandle) (int64, error) {
return 0, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestScrape(t *testing.T) {
if test.mutateScraper != nil {
test.mutateScraper(scraper)
}
scraper.getProcessCreateTime = func(p processHandle, ctx context.Context) (int64, error) { return createTime, nil }
scraper.getProcessCreateTime = func(ctx context.Context, p processHandle) (int64, error) { return createTime, nil }
require.NoError(t, err, "Failed to create process scraper: %v", err)
err = scraper.start(context.Background(), componenttest.NewNopHost())
require.NoError(t, err, "Failed to initialize process scraper: %v", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ func getProcessCommand(ctx context.Context, proc processHandle) (*commandMetadat
command := &commandMetadata{command: cmd, commandLine: cmdline}
return command, nil
}

func getProcessCreateTimeInternal(ctx context.Context, proc processHandle) (int64, error) {
return proc.CreateTimeWithContext(ctx)
}

0 comments on commit 0dae594

Please sign in to comment.