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

fixed host metrics receiver #78

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading