Skip to content

Commit

Permalink
Fix more static analysis errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
sachin-shankar committed Jul 26, 2023
1 parent 619eebe commit 82eb1b1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion internal/storer/storer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
Expand Down
7 changes: 5 additions & 2 deletions src/definition/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Check warning on line 115 in src/definition/fetch_test.go

View workflow job for this annotation

GitHub Actions / Static analysis and linting

error-naming: error var dummyFilterError should have name of the form errFoo (revive)
dummyTransformError = fmt.Errorf("dummy transform error")

Check warning on line 116 in src/definition/fetch_test.go

View workflow job for this annotation

GitHub Actions / Static analysis and linting

error-naming: error var dummyTransformError should have name of the form errFoo (revive)
)

func TestTransformAndFilter(t *testing.T) { //nolint: funlen
type args struct {
Expand Down Expand Up @@ -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())
Expand Down
21 changes: 14 additions & 7 deletions src/metric/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
Expand Down Expand Up @@ -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"]
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/metric/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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{},
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 82eb1b1

Please sign in to comment.