func getProperV2Paths()

in metric/system/cgroup/util.go [383:429]


func getProperV2Paths(rootfs resolve.Resolver, possibleV2Paths []string) string {
	if len(possibleV2Paths) > 1 {
		// try to sort out anything that looks like a docker fs
		filteredPaths := []string{}
		for _, path := range possibleV2Paths {
			if strings.Contains(path, "overlay2") {
				continue
			}
			filteredPaths = append(filteredPaths, path)
		}
		// if we have no correct paths, give up and use the last one
		// the "last one" ideom preserves behavior before we got more clever with looking for the V2 paths
		if len(filteredPaths) == 0 {
			usePath := possibleV2Paths[len(possibleV2Paths)-1]
			logp.L().Debugf("could not find correct cgroupv2 path, reverting to path that may produce errors: %s", usePath)
			return usePath
		}

		// if we're using an alternate hostfs, assume we want to monitor the host system, from inside a container
		// and use that path
		if rootfs.IsSet() {
			root := rootfs.ResolveHostFS("")
			hostFSPaths := []string{}
			for _, path := range filteredPaths {
				if strings.Contains(path, root) {
					hostFSPaths = append(hostFSPaths, path)
				}
			}
			// return the last path
			if len(hostFSPaths) > 0 {
				return hostFSPaths[len(hostFSPaths)-1]
			} else {
				usePath := filteredPaths[len(filteredPaths)-1]
				logp.L().Debugf("An alternate hostfs was specified, but could not find any cgroup mountpoints that contain a hostfs. Using: %s", usePath)
				return usePath
			}
		} else {
			// if no hosfs is set, just use the last element
			return filteredPaths[len(filteredPaths)-1]
		}

	} else if len(possibleV2Paths) == 1 {
		return possibleV2Paths[0]
	}

	return ""
}