diff --git a/internal/storer/storer.go b/internal/storer/storer.go index 64f2ee851..8676715bb 100644 --- a/internal/storer/storer.go +++ b/internal/storer/storer.go @@ -36,7 +36,7 @@ type InMemoryStore struct { // Holder for any entry in the JSON storage. type jsonEntry struct { - //notice this is the timestamp of the creation of the jsonEntry, we do not keep track of the last-access timestamp. + // notice this is the timestamp of the creation of the jsonEntry, we do not keep track of the last-access timestamp. timestamp time.Time value interface{} } diff --git a/src/definition/fetch_test.go b/src/definition/fetch_test.go index f6eb67302..72fb10a09 100644 --- a/src/definition/fetch_test.go +++ b/src/definition/fetch_test.go @@ -111,8 +111,10 @@ func TestTransformBypassesError(t *testing.T) { assert.Nil(t, v) } -var dummyFilterError = fmt.Errorf("dummy filter error") -var dummyTransformError = fmt.Errorf("dummy transform error") +var ( + dummyFilterError = fmt.Errorf("dummy filter error") + dummyTransformError = fmt.Errorf("dummy transform error") +) func TestTransformAndFilter(t *testing.T) { //nolint: funlen type args struct { @@ -224,6 +226,7 @@ func TestTransformAndFilter(t *testing.T) { //nolint: funlen } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + t.Parallel() value, err := TransformAndFilter(tt.args.fetchFunc, tt.args.transformFunc, tt.args.filterFunc)(tt.args.groupLabel, tt.args.entityId, tt.args.raw) if len(tt.wantErr) > 0 { assert.EqualError(t, err, tt.wantErr, "wanted error %s, got %s", tt.wantErr, err.Error()) diff --git a/src/metric/definition.go b/src/metric/definition.go index c17384b8a..84cb74e76 100644 --- a/src/metric/definition.go +++ b/src/metric/definition.go @@ -3,9 +3,10 @@ package metric import ( "errors" "fmt" - log "github.com/sirupsen/logrus" "time" + log "github.com/sirupsen/logrus" + sdkMetric "github.com/newrelic/infra-integrations-sdk/data/metric" "github.com/newrelic/nri-kubernetes/v3/src/definition" @@ -17,7 +18,7 @@ import ( // Fetch Functions for computed metrics var ( workingSetBytes = definition.FromRaw("workingSetBytes") - cpuUsedCores = definition.TransformAndFilter(definition.FromRaw("usageNanoCores"), fromNano, filterCpuUsedCores) //nolint gochecknoglobals + cpuUsedCores = definition.TransformAndFilter(definition.FromRaw("usageNanoCores"), fromNano, filterCpuUsedCores) //nolint gochecknoglobals // needs significant refactoring cpuLimitCores = definition.Transform(definition.FromRaw("cpuLimitCores"), toCores) cpuRequestedCores = definition.Transform(definition.FromRaw("cpuRequestedCores"), toCores) processOpenFds = prometheus.FromValueWithOverriddenName("process_open_fds", "processOpenFds") @@ -1606,18 +1607,18 @@ func filterCpuUsedCores(fetchedValue definition.FetchedValue, groupLabel, entity // type assertion check val, ok := fetchedValue.(float64) if !ok { - return nil, fmt.Errorf("fetchedValue must be of type float64") + return nil, fmt.Errorf("fetchedValue must be of type float64") //nolint goerr113 } // fetch raw cpuLimitCores value group, ok := groups[groupLabel] if !ok { - return nil, fmt.Errorf("group %q not found", groupLabel) + return nil, fmt.Errorf("group %q not found", groupLabel) //nolint goerr113 } entity, ok := group[entityID] if !ok { - return nil, fmt.Errorf("entity %q not found", entityID) + return nil, fmt.Errorf("entity %q not found", entityID) //nolint goerr113 } value, ok := entity["cpuLimitCores"] @@ -1635,9 +1636,15 @@ func filterCpuUsedCores(fetchedValue definition.FetchedValue, groupLabel, entity return nil, err } + // check type assertion + cpuLimit, ok := cpuLimitCoresVal.(float64) + if !ok { + return nil, fmt.Errorf("cpuLimit must be of type float64") //nolint goerr113 + } + // check for impossibly high cpuUsedCoresVal - if val > cpuLimitCoresVal.(float64)*100 { - return nil, fmt.Errorf("impossibly high value %f received from kubelet for cpuUsedCoresVal", val) + if val > cpuLimit*100 { + return nil, fmt.Errorf("impossibly high value %f received from kubelet for cpuUsedCoresVal", val) //nolint goerr113 } // return valid raw value diff --git a/src/metric/definition_test.go b/src/metric/definition_test.go index c9d943b6c..9eff1b53e 100644 --- a/src/metric/definition_test.go +++ b/src/metric/definition_test.go @@ -134,7 +134,7 @@ func TestSubtract(t *testing.T) { } func TestUtilization(t *testing.T) { - var raw = definition.RawGroups{ + raw := definition.RawGroups{ "group1": { "entity1": { "dividend": uint64(10), @@ -167,11 +167,10 @@ func TestUtilization(t *testing.T) { assert.NotNil(t, value) assert.Equal(t, float64(50), value) } - } func TestUtilizationNotSupported(t *testing.T) { - var raw = definition.RawGroups{ + raw := definition.RawGroups{ "group1": { "entity1": { "dividend": definition.FetchedValues{}, @@ -378,6 +377,7 @@ func Test_filterCpuUsedCores(t *testing.T) { //nolint: funlen } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + t.Parallel() got, err := filterCpuUsedCores(tt.args.fetchedValue, tt.args.groupLabel, tt.args.entityID, tt.args.groups) if len(tt.wantErr) > 0 { assert.EqualErrorf(t, err, tt.wantErr, "expected %s, got %s", tt.wantErr, err.Error())