@@ -16,86 +16,78 @@ package cpuscraper
1616
1717import (
1818 "context"
19+ "errors"
1920 "runtime"
2021 "testing"
2122
23+ "github.com/shirou/gopsutil/cpu"
2224 "github.com/stretchr/testify/assert"
2325 "github.com/stretchr/testify/require"
2426
2527 "go.opentelemetry.io/collector/consumer/pdata"
2628 "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
2729)
2830
29- type validationFn func (* testing.T , pdata.MetricSlice )
31+ func TestScrapeMetrics (t * testing.T ) {
32+ type testCase struct {
33+ name string
34+ timesFunc func (bool ) ([]cpu.TimesStat , error )
35+ expectedErr string
36+ }
3037
31- func TestScrapeMetrics_MinimalData (t * testing.T ) {
32- createScraperAndValidateScrapedMetrics (t , & Config {}, func (t * testing.T , metrics pdata.MetricSlice ) {
33- // expect 1 metric
34- assert .Equal (t , 1 , metrics .Len ())
38+ testCases := []testCase {
39+ {
40+ name : "Standard" ,
41+ },
42+ {
43+ name : "Error" ,
44+ timesFunc : func (bool ) ([]cpu.TimesStat , error ) { return nil , errors .New ("err1" ) },
45+ expectedErr : "err1" ,
46+ },
47+ }
3548
36- // for cpu seconds metric, expect a datapoint for each state label, including at least 4 standard states
37- cpuTimeMetric := metrics .At (0 )
38- internal .AssertDescriptorEqual (t , cpuTimeDescriptor , cpuTimeMetric .MetricDescriptor ())
39- assert .GreaterOrEqual (t , cpuTimeMetric .DoubleDataPoints ().Len (), 4 )
40- internal .AssertDoubleMetricLabelDoesNotExist (t , cpuTimeMetric , 0 , cpuLabelName )
41- internal .AssertDoubleMetricLabelHasValue (t , cpuTimeMetric , 0 , stateLabelName , userStateLabelValue )
42- internal .AssertDoubleMetricLabelHasValue (t , cpuTimeMetric , 1 , stateLabelName , systemStateLabelValue )
43- internal .AssertDoubleMetricLabelHasValue (t , cpuTimeMetric , 2 , stateLabelName , idleStateLabelValue )
44- internal .AssertDoubleMetricLabelHasValue (t , cpuTimeMetric , 3 , stateLabelName , interruptStateLabelValue )
45- })
46- }
49+ for _ , test := range testCases {
50+ t .Run (test .name , func (t * testing.T ) {
51+ scraper := newCPUScraper (context .Background (), & Config {})
52+ if test .timesFunc != nil {
53+ scraper .times = test .timesFunc
54+ }
4755
48- func TestScrapeMetrics_AllData (t * testing.T ) {
49- config := & Config {
50- ReportPerCPU : true ,
51- }
56+ err := scraper .Initialize (context .Background ())
57+ require .NoError (t , err , "Failed to initialize cpu scraper: %v" , err )
58+ defer func () { assert .NoError (t , scraper .Close (context .Background ())) }()
5259
53- createScraperAndValidateScrapedMetrics (t , config , func (t * testing.T , metrics pdata.MetricSlice ) {
54- // expect 1 metric
55- assert .Equal (t , 1 , metrics .Len ())
60+ metrics , err := scraper .ScrapeMetrics (context .Background ())
61+ if test .expectedErr != "" {
62+ assert .EqualError (t , err , test .expectedErr )
63+ return
64+ }
65+ require .NoError (t , err , "Failed to scrape metrics: %v" , err )
5666
57- // for cpu seconds metric, expect a datapoint for each state label & core combination with at least 4 standard states
58- cpuTimeMetric := metrics .At (0 )
59- internal .AssertDescriptorEqual (t , cpuTimeDescriptor , cpuTimeMetric .MetricDescriptor ())
60- assert .GreaterOrEqual (t , cpuTimeMetric .DoubleDataPoints ().Len (), runtime .NumCPU ()* 4 )
61- internal .AssertDoubleMetricLabelExists (t , cpuTimeMetric , 0 , cpuLabelName )
62- internal .AssertDoubleMetricLabelHasValue (t , cpuTimeMetric , 0 , stateLabelName , userStateLabelValue )
63- internal .AssertDoubleMetricLabelHasValue (t , cpuTimeMetric , 1 , stateLabelName , systemStateLabelValue )
64- internal .AssertDoubleMetricLabelHasValue (t , cpuTimeMetric , 2 , stateLabelName , idleStateLabelValue )
65- internal .AssertDoubleMetricLabelHasValue (t , cpuTimeMetric , 3 , stateLabelName , interruptStateLabelValue )
66- })
67- }
67+ assert .Equal (t , 1 , metrics .Len ())
6868
69- func TestScrapeMetrics_Linux (t * testing.T ) {
70- if runtime .GOOS != "linux" {
71- return
72- }
69+ assertCPUMetricValid (t , metrics .At (0 ), cpuTimeDescriptor )
7370
74- createScraperAndValidateScrapedMetrics (t , & Config {}, func (t * testing.T , metrics pdata.MetricSlice ) {
75- // for cpu seconds metric, expect a datapoint for all 8 state labels
76- cpuTimeMetric := metrics .At (0 )
77- internal .AssertDescriptorEqual (t , cpuTimeDescriptor , cpuTimeMetric .MetricDescriptor ())
78- assert .Equal (t , 8 , cpuTimeMetric .DoubleDataPoints ().Len ())
79- internal .AssertDoubleMetricLabelDoesNotExist (t , cpuTimeMetric , 0 , cpuLabelName )
80- internal .AssertDoubleMetricLabelHasValue (t , cpuTimeMetric , 0 , stateLabelName , userStateLabelValue )
81- internal .AssertDoubleMetricLabelHasValue (t , cpuTimeMetric , 1 , stateLabelName , systemStateLabelValue )
82- internal .AssertDoubleMetricLabelHasValue (t , cpuTimeMetric , 2 , stateLabelName , idleStateLabelValue )
83- internal .AssertDoubleMetricLabelHasValue (t , cpuTimeMetric , 3 , stateLabelName , interruptStateLabelValue )
84- internal .AssertDoubleMetricLabelHasValue (t , cpuTimeMetric , 4 , stateLabelName , niceStateLabelValue )
85- internal .AssertDoubleMetricLabelHasValue (t , cpuTimeMetric , 5 , stateLabelName , softIRQStateLabelValue )
86- internal .AssertDoubleMetricLabelHasValue (t , cpuTimeMetric , 6 , stateLabelName , stealStateLabelValue )
87- internal .AssertDoubleMetricLabelHasValue (t , cpuTimeMetric , 7 , stateLabelName , waitStateLabelValue )
88- })
71+ if runtime .GOOS == "linux" {
72+ assertCPUMetricHasLinuxSpecificStateLabels (t , metrics .At (0 ))
73+ }
74+ })
75+ }
8976}
9077
91- func createScraperAndValidateScrapedMetrics (t * testing.T , config * Config , assertFn validationFn ) {
92- scraper := newCPUScraper (context .Background (), config )
93- err := scraper .Initialize (context .Background ())
94- require .NoError (t , err , "Failed to initialize cpu scraper: %v" , err )
95- defer func () { assert .NoError (t , scraper .Close (context .Background ())) }()
96-
97- metrics , err := scraper .ScrapeMetrics (context .Background ())
98- require .NoError (t , err , "Failed to scrape metrics: %v" , err )
78+ func assertCPUMetricValid (t * testing.T , metric pdata.Metric , descriptor pdata.MetricDescriptor ) {
79+ internal .AssertDescriptorEqual (t , descriptor , metric .MetricDescriptor ())
80+ assert .GreaterOrEqual (t , metric .DoubleDataPoints ().Len (), 4 * runtime .NumCPU ())
81+ internal .AssertDoubleMetricLabelExists (t , metric , 0 , cpuLabelName )
82+ internal .AssertDoubleMetricLabelHasValue (t , metric , 0 , stateLabelName , userStateLabelValue )
83+ internal .AssertDoubleMetricLabelHasValue (t , metric , 1 , stateLabelName , systemStateLabelValue )
84+ internal .AssertDoubleMetricLabelHasValue (t , metric , 2 , stateLabelName , idleStateLabelValue )
85+ internal .AssertDoubleMetricLabelHasValue (t , metric , 3 , stateLabelName , interruptStateLabelValue )
86+ }
9987
100- assertFn (t , metrics )
88+ func assertCPUMetricHasLinuxSpecificStateLabels (t * testing.T , metric pdata.Metric ) {
89+ internal .AssertDoubleMetricLabelHasValue (t , metric , 4 , stateLabelName , niceStateLabelValue )
90+ internal .AssertDoubleMetricLabelHasValue (t , metric , 5 , stateLabelName , softIRQStateLabelValue )
91+ internal .AssertDoubleMetricLabelHasValue (t , metric , 6 , stateLabelName , stealStateLabelValue )
92+ internal .AssertDoubleMetricLabelHasValue (t , metric , 7 , stateLabelName , waitStateLabelValue )
10193}
0 commit comments