-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: add decimal precision to CPU and memory metrics in pod info #2221
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1598,24 +1598,24 @@ func (h *handler) getPodDetails(pod corev1.Pod) (PodDetails, error) { | |
memQuantity := container.Usage.Memory() | ||
details, ok := containerDetails[containerName] | ||
if !ok { | ||
details = ContainerDetails{Name: container.Name} // Initialize if not found | ||
details = ContainerDetails{Name: container.Name} | ||
} | ||
if cpuQuantity != nil { | ||
details.TotalCPU = strconv.FormatInt(cpuQuantity.MilliValue(), 10) + "m" | ||
details.TotalCPU = cpuToMillicores(cpuQuantity.String()) | ||
totalCPU.Add(*cpuQuantity) | ||
} | ||
if memQuantity != nil { | ||
details.TotalMemory = strconv.FormatInt(memQuantity.Value()/(1024*1024), 10) + "Mi" | ||
details.TotalMemory = fmt.Sprintf("%.2fMi", float64(memQuantity.Value())/(1024*1024)) | ||
totalMemory.Add(*memQuantity) | ||
} | ||
containerDetails[containerName] = details | ||
} | ||
if totalCPU != nil { | ||
podDetails.TotalCPU = strconv.FormatInt(totalCPU.MilliValue(), 10) + "m" | ||
podDetails.TotalCPU = cpuToMillicores(totalCPU.String()) | ||
} | ||
|
||
if totalMemory != nil { | ||
podDetails.TotalMemory = strconv.FormatInt(totalMemory.Value()/(1024*1024), 10) + "Mi" | ||
podDetails.TotalMemory = fmt.Sprintf("%.2fMi", float64(totalMemory.Value())/(1024*1024)) | ||
} | ||
} | ||
return podDetails, nil | ||
|
@@ -1684,3 +1684,24 @@ func (h *handler) getContainerStatus(state corev1.ContainerState) string { | |
return "Unknown" | ||
} | ||
} | ||
|
||
func cpuToMillicores(quantityStr string) string { | ||
var value float64 | ||
var format string | ||
|
||
// Parse the quantity string | ||
if _, err := fmt.Sscanf(quantityStr, "%f%s", &value, &format); err != nil { | ||
fmt.Println("Error parsing cpu quantity string:", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there's a need for error handling, return it and handle it in upper layer. |
||
return "0m" | ||
} | ||
|
||
// Adjust value based on the format suffix | ||
switch { | ||
case strings.HasSuffix(format, "n"): | ||
value /= 1e6 | ||
case !strings.HasSuffix(format, "m"): | ||
fmt.Println("Error parsing quantity string: invalid format") | ||
return "0m" | ||
} | ||
return fmt.Sprintf("%.2fm", value) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't take
Quantity
directly so that there's no need to parse?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quantity is like:
quantity::= signedNumber|suffix. To get the suffix and calculate accordingly we're parsing the string.
For eg: 2847159n this is the cpuQuantity,
cpuQuantity.MilliValue()
will give us 3, but we want 2.84m as our final result. For that we divide 2847159/1e6