Skip to content

Conversation

@osullivandonal
Copy link
Contributor

@osullivandonal osullivandonal commented Jan 5, 2026

Description

Improve unit-test code coverage for the cpuscraper in receiver/hostmetricsreceiver.

Previously the results were:

  • 61.5%
make test-with-cover | grep cpuscraper
✓  internal/scraper/cpuscraper (1.058s) (coverage: 61.5% of statements)
✓  internal/scraper/cpuscraper/ucal (1.067s) (coverage: 100.0% of statements)
✓  internal/scraper/cpuscraper/internal/metadata (1.08s) (coverage: 88.2% of statements)

Results now are:

  • 92.3%
make test-with-cover | grep cpuscraper
✓  internal/scraper/cpuscraper (1.058s) (coverage: 92.3% of statements)
✓  internal/scraper/cpuscraper/ucal (1.071s) (coverage: 100.0% of statements)
✓  internal/scraper/cpuscraper/internal/metadata (1.072s) (coverage: 88.2% of statements)

Link to tracking issue

Issue -> #44936

This PR shouldn't close the issue, another PR is need to finish the documentation.

Testing

Tests added include:

  • TestScrape_CpuCount in receiver/hostmetricsreciever/internal/scraper/cpuscraper/cpu_scraper_test.go

    • This adds tests for:
      • Physical CPU
      • Virtual CPU
  • TestScrape_CpuFrequency in receiver/hostmetricsreceiver/internal/scraper/cpuscraper/cpu_scraper_linux_test.go

    • This adds tests for:
      • Frequency, this is only available on Linux.


assert.Equal(t, test.expectedMetricCount, md.MetricCount())

if md.MetricCount() > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test will pass if this is 0, this should change to a require check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have refactored this test and removed this line, now we use require.

md, err := scraper.scrape(t.Context())
require.NoError(t, err, "Failed to scrape metrics: %v", err)

assert.Equal(t, test.expectedMetricCount, md.MetricCount())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not mix assert and require, especially in the same test. We tend to default to require throughout the receiver so let's continue that trend here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot, I have removed all instances of assert and replaced them with require.

pcommon.NewValueStr(metadata.AttributeStateWait.String()))
}

func assertCPUBothPhysicalAndLogicalCountMetricsValid(t *testing.T, metrics pmetric.MetricSlice) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would pass if you gave it any 2 metrics, as it doesn't check that both system.cpu.physical.count and system.cpu.logical.count are present and none others.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this function as we now check in the test function for present metrics and that no others are present.

When running in CI we cannot get the frequency value on Linux due to the
CPU being virtual
We now use only require instead of assert, refactored test function to
make it more clear
Copy link
Contributor

@braydonk braydonk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed comments from last review all look good, one small comment on the new diff.

}

metrics := md.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics()
reportedMetrics := make(map[string]int)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section below would make more sense if we didn't already check above that there's only one metric. This is doing a lot of extra ceremony for something that would have already failed the test on line 60. I think this can be simplified with that in mind. I understand it's meant to match the other test in the multiplatform test file but it'd probably be more valuable for this test to be simpler than to look the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I have removed the extra validation, I think its simpler now!

@ChrsMark ChrsMark merged commit 7466d88 into open-telemetry:main Jan 7, 2026
189 checks passed
@otelbot
Copy link
Contributor

otelbot bot commented Jan 7, 2026

Thank you for your contribution @osullivandonal! 🎉 We would like to hear from you about your experience contributing to OpenTelemetry by taking a few minutes to fill out this survey. If you are getting started contributing, you can also join the CNCF Slack channel #opentelemetry-new-contributors to ask for guidance and get help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants