func cpuinfoScanner()

in metric/cpu/metrics_procfs_common.go [62:113]


func cpuinfoScanner(scanner *bufio.Scanner) ([]CPUInfo, error) {
	cpuInfos := []CPUInfo{}
	current := CPUInfo{}
	// On my tests the order the cores appear on /proc/cpuinfo
	// is the same as on /proc/stats, this means it matches our
	// current 'system.core.id' metric. This information
	// is also the same as the 'processor' line on /proc/cpuinfo.
	coreID := 0
	for scanner.Scan() {
		line := scanner.Text()
		split := strings.Split(line, ":")
		if len(split) != 2 {
			// A blank line its a separation between CPUs
			// even the last CPU contains one blank line at the end
			cpuInfos = append(cpuInfos, current)
			current = CPUInfo{}
			coreID++

			continue
		}

		k, v := split[0], split[1]
		k = strings.TrimSpace(k)
		v = strings.TrimSpace(v)
		switch k {
		case "model":
			current.ModelNumber = v
		case "model name":
			current.ModelName = v
		case "physical id":
			id, err := strconv.Atoi(v)
			if err != nil {
				return []CPUInfo{}, fmt.Errorf("parsing physical ID: %w", err)
			}
			current.PhysicalID = id
		case "core id":
			id, err := strconv.Atoi(v)
			if err != nil {
				return []CPUInfo{}, fmt.Errorf("parsing core ID: %w", err)
			}
			current.CoreID = id
		case "cpu MHz":
			mhz, err := strconv.ParseFloat(v, 64)
			if err != nil {
				return []CPUInfo{}, fmt.Errorf("parsing CPU %d Mhz: %w", coreID, err)
			}
			current.Mhz = mhz
		}
	}

	return cpuInfos, nil
}