{"record":{"id":"7d35a6434f7bde63","repo":"cloudflare/cloudflared","slug":"parsing-memory-information-w-key-s","errorCode":null,"errorMessage":"parsing memory information: %w, key=%s","messagePattern":"parsing memory information: %w, key=(.+?)","errorType":"exception","errorClass":"ErrKeyNotFound","httpStatus":null,"severity":"error","filePath":"diagnostic/system_collector_utils.go","lineNumber":296,"sourceCode":"\treturn &FileDescriptorInformation{fileDescriptorMaximum, fileDescriptorCurrent}, nil\n}\n\ntype MemoryInformation struct {\n\tMemoryMaximum uint64 // size in KB\n\tMemoryCurrent uint64 // size in KB\n}\n\nfunc ParseMemoryInformationFromKV(\n\toutput string,\n\tmemoryMaximumKey string,\n\tmemoryAvailableKey string,\n\tmapper func(field string) (uint64, error),\n) (*MemoryInformation, error) {\n\tpairs := findColonSeparatedPairs(output, []string{memoryMaximumKey, memoryAvailableKey}, mapper)\n\n\tmemoryMaximum, exists := pairs[memoryMaximumKey]\n\tif !exists {\n\t\treturn nil, fmt.Errorf(\"parsing memory information: %w, key=%s\", ErrKeyNotFound, memoryMaximumKey)\n\t}\n\n\tmemoryAvailable, exists := pairs[memoryAvailableKey]\n\tif !exists {\n\t\treturn nil, fmt.Errorf(\"parsing memory information: %w, key=%s\", ErrKeyNotFound, memoryAvailableKey)\n\t}\n\n\tmemoryCurrent := memoryMaximum - memoryAvailable\n\n\treturn &MemoryInformation{memoryMaximum, memoryCurrent}, nil\n}\n\nfunc RawSystemInformation(osInfoRaw string, memoryInfoRaw string, fdInfoRaw string, disksRaw string) string {\n\tvar builder strings.Builder\n\n\tformatInfo := func(info string, builder *strings.Builder) {\n\t\tif info == \"\" {\n\t\t\tbuilder.WriteString(\"No information\\n\")","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/cloudflare/cloudflared/blob/2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f/diagnostic/system_collector_utils.go#L278-L314","documentation":"ParseMemoryInformationFromKV parses colon-separated key/value output to find memory maximum and available values using the provided mapper. This error is returned when the memoryMaximumKey is not present in the parsed pairs, wrapped with ErrKeyNotFound, so MemoryInformation cannot be computed.","triggerScenarios":"Calling ParseMemoryInformationFromKV with output lacking the maximum-memory key: wrong key string (e.g. \"MemTotal\" vs \"total memory\"), units in values that the mapper rejects (dropping the pair), empty output, or OS output format changes.","commonSituations":"cgroup v1 vs v2 memory limit files with different labels; free(1)/sysctl output wording differences across platforms; values like \"16 GB\" or \"unlimited\" failing a numeric mapper; reading the wrong memory file (e.g. memory.max absent, meaning no limit).","solutions":["Compare the passed memoryMaximumKey against the literal key text in the raw output","Provide a mapper that strips units and handles special values (e.g. treat \"max\"/\"unlimited\" as a sentinel) before ParseUint","Ensure the source file/command actually contains a memory-max key (cgroups may omit memory.max when unlimited)","Check the upstream command output is non-empty and successful before parsing"],"exampleFix":"// before\nmapper := func(f string) (uint64, error) { return strconv.ParseUint(f, 10, 64) }\ninfo, err := ParseMemoryInformationFromKV(out, \"MemMax\", \"MemAvailable\", mapper)\n// after\ninfo, err := ParseMemoryInformationFromKV(out, \"MemTotal\", \"MemAvailable\", mapper) // key as emitted by source","handlingStrategy":"try-catch","validationCode":"if !strings.Contains(out, memoryMaximumKey+\":\") {\n    return fmt.Errorf(\"output %q missing key %q\", out, memoryMaximumKey)\n}","typeGuard":null,"tryCatchPattern":"mem, err := ParseMemoryInformationFromKV(out, maxKey, availKey, mapper)\nif err != nil {\n    if errors.Is(err, ErrKeyNotFound) {\n        // degrade: report memory info as unavailable\n        return nil, nil\n    }\n    return nil, err\n}","preventionTips":["Match key names against the literal source (procfs/cgroup/sysctl) before parsing","Supply a mapper that handles unit suffixes and sentinel values like \"max\"","Confirm the memory-max key exists in the environment (cgroup v2 may omit it when unlimited)","Guard against empty output and command failures upstream of the parser"],"tags":["go","parsing","memory","key-not-found","kv-parsing"],"backgroundTag":"missing-config-key","analyzedSha":"2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f","analyzedAt":"2026-09-06T04:14:33.757Z","contentChangedAt":"2026-09-06T04:14:33.757Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}