{"record":{"id":"d190ae0a99b286a0","repo":"cilium/cilium","slug":"failed-to-parse-pod-metrics-json-w","errorCode":null,"errorMessage":"failed to parse pod metrics JSON: %w","messagePattern":"failed to parse pod metrics JSON: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cilium-cli/sysdump/sysdump.go","lineNumber":3789,"sourceCode":"\t\t\treturn \"<unknown>\"\n\t\t}\n\t\treturn strconv.FormatUint(*v, 10)\n\t}\n\n\tfs, imageFs := summary.Node.Fs, summary.Node.Runtime.ImageFs\n\treturn fmt.Sprintf(\"%s\\t%s\\t%s\\t%s\\t%s\\t%s\\t%s\\t%s\",\n\t\tnodeName,\n\t\tmib(fs.UsedBytes), mib(fs.CapacityBytes), pct(fs.UsedBytes, fs.CapacityBytes),\n\t\tmib(imageFs.UsedBytes), mib(imageFs.CapacityBytes),\n\t\tcount(fs.InodesFree), diskPressure)\n}\n\n// formatPodMetricsAsTable formats the raw pod metrics JSON into a table format like kubectl top pods\nfunc (c *Collector) formatPodMetricsAsTable(rawMetrics string) (string, error) {\n\t// Parse the metrics JSON\n\tvar podMetrics metricsapi.PodMetricsList\n\tif err := json.Unmarshal([]byte(rawMetrics), &podMetrics); err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to parse pod metrics JSON: %w\", err)\n\t}\n\n\tvar sb strings.Builder\n\ttw := tabwriter.NewWriter(&sb, 0, 0, 2, ' ', 0)\n\tfmt.Fprintln(tw, \"NAMESPACE\\tNAME\\tCPU(cores)\\tMEMORY(bytes)\")\n\n\tfor _, metric := range podMetrics.Items {\n\t\tnamespace := metric.Namespace\n\t\tname := metric.Name\n\n\t\t// Sum CPU and memory usage across all containers in the pod\n\t\tvar totalCPUMillis int64\n\t\tvar totalMemBytes int64\n\n\t\tfor _, container := range metric.Containers {\n\t\t\tif cpuUsage, exists := container.Usage[corev1.ResourceCPU]; exists {\n\t\t\t\ttotalCPUMillis += cpuUsage.MilliValue()\n\t\t\t}","sourceCodeStart":3771,"sourceCodeEnd":3807,"githubUrl":"https://github.com/cilium/cilium/blob/ac7b90affa4baf0642e6685319d56907b3a73a6d/cilium-cli/sysdump/sysdump.go#L3771-L3807","documentation":"`formatPodMetricsAsTable` unmarshals raw pod-metrics JSON (metrics.k8s.io PodMetricsList, as from `kubectl top pods`) into metricsapi.PodMetricsList. A malformed or unexpected payload yields this wrapped error instead of the pod metrics table.","triggerScenarios":"The raw pod metrics string collected for the sysdump is empty, truncated, non-JSON (HTML error page), or its schema does not match metricsapi.PodMetricsList (metrics API version drift).","commonSituations":"metrics-server unavailable or returning error bodies when the sysdump gathered pod metrics; an ingress/proxy returning a login/error page; partial capture of a large payload; older metrics API shapes.","solutions":["Inspect the raw pod metrics payload to see whether it is empty, HTML, or schema-mismatched JSON","Validate metrics-server works: `kubectl top pods -A` should return rows","Ensure the metrics-server version matches the cluster's metrics.k8s.io API version","Make collection fail fast so empty/garbage payloads never reach the formatter","Return an empty table instead of an error when there are no pod metrics"],"exampleFix":"// before\nif err := json.Unmarshal([]byte(rawMetrics), &podMetrics); err != nil {\n\treturn \"\", fmt.Errorf(\"failed to parse pod metrics JSON: %w\", err)\n}\n// after\nif strings.TrimSpace(rawMetrics) == \"\" {\n\treturn \"no pod metrics available\\n\", nil\n}\nif err := json.Unmarshal([]byte(rawMetrics), &podMetrics); err != nil {\n\treturn \"\", fmt.Errorf(\"failed to parse pod metrics JSON: %w (payload head: %.100s)\", err, rawMetrics)\n}","handlingStrategy":"validation","validationCode":"raw := strings.TrimSpace(rawMetrics)\nif raw == \"\" || raw[0] != '{' {\n\treturn errors.New(\"pod metrics payload is empty or not JSON\")\n}","typeGuard":"func validPodMetrics(s string) bool {\n\tvar m metricsapi.PodMetricsList\n\treturn json.Unmarshal([]byte(s), &m) == nil\n}","tryCatchPattern":"var podMetrics metricsapi.PodMetricsList\nif err := json.Unmarshal([]byte(rawMetrics), &podMetrics); err != nil {\n\tlog.Printf(\"pod metrics unparseable, skipping table: %v\", err)\n\treturn \"\", nil\n}","preventionTips":["Validate kubectl top pods works before sysdump","Check metrics-server health and version","Reject empty/HTML payloads at collection time","Log a payload preview inside parse errors for diagnosis"],"tags":["json","metrics-server","pods","sysdump","parsing"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"ac7b90affa4baf0642e6685319d56907b3a73a6d","analyzedAt":"2026-08-31T18:27:15.868Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}