From cb2728a97506ae8aaa1b36aef27a994f81eb2039 Mon Sep 17 00:00:00 2001 From: Hardik Choksi Date: Tue, 25 Jun 2024 15:45:07 +0530 Subject: [PATCH] fixed host metrics receiver 1. Changed Memory and CPU Percentage Metric Methods, 2. Added DiskSpeedCalculator field in scraper --- .../scraper/diskscraper/disk_scraper_others.go | 2 +- .../scal/disk_speed_calculator_test.go | 17 ++++++++++------- .../bcal/network_bandwidth_calculator_test.go | 16 ++++++++++------ .../internal/scraper/processscraper/process.go | 3 +-- .../scraper/processscraper/process_scraper.go | 8 ++++---- .../processscraper/process_scraper_test.go | 7 +------ 6 files changed, 27 insertions(+), 26 deletions(-) diff --git a/receiver/hostmetricsreceiver/internal/scraper/diskscraper/disk_scraper_others.go b/receiver/hostmetricsreceiver/internal/scraper/diskscraper/disk_scraper_others.go index cdeca8ce69cb..d924e193835d 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/diskscraper/disk_scraper_others.go +++ b/receiver/hostmetricsreceiver/internal/scraper/diskscraper/disk_scraper_others.go @@ -46,7 +46,7 @@ type scraper struct { // newDiskScraper creates a Disk Scraper func newDiskScraper(_ context.Context, settings receiver.Settings, cfg *Config) (*scraper, error) { - scraper := &scraper{settings: settings, config: cfg, bootTime: host.BootTimeWithContext, ioCounters: disk.IOCountersWithContext} + scraper := &scraper{settings: settings, config: cfg, bootTime: host.BootTimeWithContext, ioCounters: disk.IOCountersWithContext, scal: &scal.DiskSpeedCalculator{}} var err error diff --git a/receiver/hostmetricsreceiver/internal/scraper/diskscraper/scal/disk_speed_calculator_test.go b/receiver/hostmetricsreceiver/internal/scraper/diskscraper/scal/disk_speed_calculator_test.go index a6ab7b717410..d47e9f288ee0 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/diskscraper/scal/disk_speed_calculator_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/diskscraper/scal/disk_speed_calculator_test.go @@ -26,8 +26,10 @@ type inMemoryRecorder struct { diskSpeeds []DiskSpeed } -func (r *inMemoryRecorder) record(_ pcommon.Timestamp, bandwidth DiskSpeed) { - r.diskSpeeds = append(r.diskSpeeds, bandwidth) +func (r *inMemoryRecorder) record(t pcommon.Timestamp, m map[string]DiskSpeed) { + for device, speed := range m { + r.diskSpeeds = append(r.diskSpeeds, DiskSpeed{Name: device, ReadSpeed: speed.ReadSpeed, WriteSpeed: speed.WriteSpeed}) + } } func TestDiskSpeedCalculator_Calculate(t *testing.T) { @@ -202,11 +204,12 @@ func Test_DiskSpeed(t *testing.T) { return 2 } - actualUtilization := diskSpeed(1, timeStart, timeEnd) - assert.Equal(t, expectedUtilization.Name, actualUtilization.Name, 0.00001) - assert.InDelta(t, expectedUtilization.ReadSpeed, actualUtilization.ReadSpeed, 0.00001) - assert.InDelta(t, expectedUtilization.WriteSpeed, actualUtilization.WriteSpeed, 0.00001) - + actualUtilization := diskSpeed(1, timeStart, timeEnd, "device0") + if actualUtilizationObj, ok := actualUtilization["device0"]; ok { + assert.Equal(t, expectedUtilization.Name, actualUtilizationObj.Name, 0.00001) + assert.InDelta(t, expectedUtilization.ReadSpeed, actualUtilizationObj.ReadSpeed, 0.00001) + assert.InDelta(t, expectedUtilization.WriteSpeed, actualUtilizationObj.WriteSpeed, 0.00001) + } } func Test_diskCounterForDeviceName(t *testing.T) { diff --git a/receiver/hostmetricsreceiver/internal/scraper/networkscraper/bcal/network_bandwidth_calculator_test.go b/receiver/hostmetricsreceiver/internal/scraper/networkscraper/bcal/network_bandwidth_calculator_test.go index 38911fd740f3..13d5b8c8c321 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/networkscraper/bcal/network_bandwidth_calculator_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/networkscraper/bcal/network_bandwidth_calculator_test.go @@ -26,8 +26,10 @@ type inMemoryRecorder struct { networkBandwidths []NetworkBandwidth } -func (r *inMemoryRecorder) record(_ pcommon.Timestamp, bandwidth NetworkBandwidth) { - r.networkBandwidths = append(r.networkBandwidths, bandwidth) +func (r *inMemoryRecorder) record(t pcommon.Timestamp, m map[string]NetworkBandwidth) { + for device, speed := range m { + r.networkBandwidths = append(r.networkBandwidths, NetworkBandwidth{Name: device, InboundRate: speed.InboundRate, OutboundRate: speed.OutboundRate}) + } } func TestNetworkBandwidthCalculator_Calculate(t *testing.T) { @@ -202,10 +204,12 @@ func Test_NetworkBandwidth(t *testing.T) { return 2 } - actualUtilization := networkBandwidth(1, timeStart, timeEnd) - assert.Equal(t, expectedUtilization.Name, actualUtilization.Name, 0.00001) - assert.InDelta(t, expectedUtilization.InboundRate, actualUtilization.InboundRate, 0.00001) - assert.InDelta(t, expectedUtilization.OutboundRate, actualUtilization.OutboundRate, 0.00001) + actualUtilization := networkBandwidth(1, timeStart, timeEnd, "interface0") + if actualUtilizationObj, ok := actualUtilization["interface0"]; ok { + assert.Equal(t, expectedUtilization.Name, actualUtilizationObj.Name, 0.00001) + assert.InDelta(t, expectedUtilization.InboundRate, actualUtilizationObj.InboundRate, 0.00001) + assert.InDelta(t, expectedUtilization.OutboundRate, actualUtilizationObj.OutboundRate, 0.00001) + } } diff --git a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process.go b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process.go index 3323c21e31e6..f7b1354acfa7 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process.go @@ -80,8 +80,7 @@ type processHandles interface { } type processHandle interface { - CPUPercentWithContext() (float64, error) - MemoryPercentWithContext() (float32, error) + CPUPercentWithContext(context.Context) (float64, error) NameWithContext(context.Context) (string, error) ExeWithContext(context.Context) (string, error) UsernameWithContext(context.Context) (string, error) diff --git a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper.go b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper.go index 002f14c28aad..471d897a63e2 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper.go @@ -322,8 +322,8 @@ func (s *scraper) scrapeAndAppendCPUTimeMetric(ctx context.Context, now pcommon. return err } -func (s *scraper) scrapeAndAppendMemoryPercentMetric(now pcommon.Timestamp, handle processHandle) error { - percent, err := handle.MemoryPercent() +func (s *scraper) scrapeAndAppendMemoryPercentMetric(ctx context.Context, now pcommon.Timestamp, handle processHandle) error { + percent, err := handle.MemoryPercentWithContext(ctx) if err != nil { return err } @@ -332,11 +332,11 @@ func (s *scraper) scrapeAndAppendMemoryPercentMetric(now pcommon.Timestamp, hand } func (s *scraper) scrapeAndAppendCPUPercentMetric(ctx context.Context, now pcommon.Timestamp, handle processHandle) error { - percent, err := handle.CPUPercent() + percent, err := handle.CPUPercentWithContext(ctx) if err != nil { return err } - s.recordCPUPercentMetricWithContext(ctx, now, percent) + s.recordCPUPercentMetric(now, percent) return nil } diff --git a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go index c1ab3637bc46..c9354a94f4c2 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go @@ -420,16 +420,11 @@ func (p *processHandleMock) TimesWithContext(ctx context.Context) (*cpu.TimesSta return args.Get(0).(*cpu.TimesStat), args.Error(1) } -func (p *processHandleMock) CPUPercentWithContext() (float64, error) { +func (p *processHandleMock) CPUPercentWithContext(ctx context.Context) (float64, error) { args := p.MethodCalled("CPUPercentWithContext") return args.Get(0).(float64), args.Error(1) } -func (p *processHandleMock) MemoryPercentWithContext() (float32, error) { - args := p.MethodCalled("MemoryPercentWithContext") - return args.Get(0).(float32), args.Error(1) -} - func (p *processHandleMock) PercentWithContext(ctx context.Context, d time.Duration) (float64, error) { args := p.MethodCalled("PercentWithContext", ctx, d) return args.Get(0).(float64), args.Error(1)