From b246ba281f352d7e863834acd62b9d7553b4d650 Mon Sep 17 00:00:00 2001 From: Nico Stewart Date: Tue, 23 Apr 2024 11:28:47 -0400 Subject: [PATCH] fix: don't collect system.cpu.utilization per-cpu --- .../internal/scraper/cpuscraper/cpu_scraper.go | 3 ++- .../internal/scraper/cpuscraper/cpu_scraper_test.go | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/cpu_scraper.go b/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/cpu_scraper.go index dbd1238c717a..deef833e658d 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/cpu_scraper.go +++ b/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/cpu_scraper.go @@ -60,7 +60,8 @@ func (s *scraper) start(ctx context.Context, _ component.Host) error { func (s *scraper) scrape(ctx context.Context) (pmetric.Metrics, error) { ctx = context.WithValue(ctx, common.EnvKey, s.config.EnvMap) now := pcommon.NewTimestampFromTime(s.now()) - cpuTimes, err := s.times(ctx, true /*percpu=*/) + // set percpu=false to support Windows 7 + cpuTimes, err := s.times(ctx, false /*percpu=*/) if err != nil { return pmetric.NewMetrics(), scrapererror.NewPartialScrapeError(err, metricsLen) } diff --git a/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/cpu_scraper_test.go b/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/cpu_scraper_test.go index 6938f578143b..d4a006609555 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/cpu_scraper_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/cpu_scraper_test.go @@ -342,7 +342,10 @@ func assertCPUMetricValid(t *testing.T, metric pmetric.Metric, startTime pcommon if startTime != 0 { internal.AssertSumMetricStartTimeEquals(t, metric, startTime) } - assert.GreaterOrEqual(t, metric.Sum().DataPoints().Len(), 4*runtime.NumCPU()) + cpuUtilizationStates := []string{"user", "system", "idle", "interrupt"} + + // CPU time is not per-CPU, just assert we collected at least one metric for each state + assert.GreaterOrEqual(t, metric.Sum().DataPoints().Len(), len(cpuUtilizationStates)) internal.AssertSumMetricHasAttribute(t, metric, 0, "cpu") internal.AssertSumMetricHasAttributeValue(t, metric, 0, "state", pcommon.NewValueStr(metadata.AttributeStateUser.String()))