Skip to content

Commit

Permalink
fix: stops round up CPU allocatable and capacity metrics (#412)
Browse files Browse the repository at this point in the history
- CPU allocatable and capacity metrics are not rounded up any more.
  • Loading branch information
gsanchezgavier authored May 5, 2022
1 parent f09ffb8 commit 825d7b3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/kubelet/metric/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ func oneAttributePerResource(rawResources definition.FetchedValue, r resourceTyp
modified := make(definition.FetchedValues, len(resources))
for resourceName, resourceQuantity := range resources {
n := camelcase.Camelcase(string(r) + strings.Title(addResourceUnit(resourceName)))
modified[n] = resourceQuantity.Value()

switch resourceName {
case v1.ResourceCPU:
// AsApproximateFloat64 is used to avoid round up CPU cores metrics which are reported in cores to New Relic.
modified[n] = resourceQuantity.AsApproximateFloat64()
default:
modified[n] = resourceQuantity.Value()
}
}

return modified, nil
Expand Down
6 changes: 3 additions & 3 deletions src/kubelet/metric/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ func TestOneAttributePerResource(t *testing.T) {
}

rawResources := v1.ResourceList{
v1.ResourceCPU: *resource.NewQuantity(2, resource.DecimalSI),
v1.ResourceCPU: resource.MustParse("1985m"),
v1.ResourcePods: *resource.NewQuantity(110, resource.DecimalSI),
v1.ResourceEphemeralStorage: *resource.NewQuantity(18211580000, resource.BinarySI),
v1.ResourceStorage: *resource.NewQuantity(18211580000, resource.BinarySI),
v1.ResourceMemory: *resource.NewQuantity(2033280000, resource.BinarySI),
v1.ResourceReplicationControllers: *resource.NewQuantity(1, resource.DecimalSI),
resourceTestBeta: *resource.NewQuantity(11, resource.DecimalSI),
resourceTestBeta: resource.MustParse("10985m"),
resourceTestAlpha: *resource.NewQuantity(111, resource.BinarySI),
}

for _, testCase := range testCases {
t.Run(string(testCase.resourceType), func(t *testing.T) {
expected := definition.FetchedValues{
fmt.Sprintf("%sCpuCores", testCase.resourceType): int64(2),
fmt.Sprintf("%sCpuCores", testCase.resourceType): float64(1.985),
fmt.Sprintf("%sPods", testCase.resourceType): int64(110),
fmt.Sprintf("%sEphemeralStorageBytes", testCase.resourceType): int64(18211580000),
fmt.Sprintf("%sStorageBytes", testCase.resourceType): int64(18211580000),
Expand Down

0 comments on commit 825d7b3

Please sign in to comment.