class ElasticAPM::Metadata::SystemInfo::ContainerInfo

def read_from_cgroup!

rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize
rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
def read_from_cgroup!
  return unless File.exist?(cgroup_path)
  IO.readlines(cgroup_path).each do |line|
    parts = line.strip.split(':')
    next if parts.length != 3
    cgroup_path = parts[2]
    # Depending on the filesystem driver used for cgroup
    # management, the paths in /proc/pid/cgroup will have
    # one of the following formats in a Docker container:
    #
    #   systemd: /system.slice/docker-<container-ID>.scope
    #   cgroupfs: /docker/<container-ID>
    #
    # In a Kubernetes pod, the cgroup path will look like:
    #
    #   systemd:
    #      /kubepods.slice/kubepods-<QoS-class>.slice/kubepods-\
    #        <QoS-class>-pod<pod-UID>.slice/<container-iD>.scope
    #   cgroupfs:
    #      /kubepods/<QoS-class>/pod<pod-UID>/<container-iD>
    directory, container_id = File.split(cgroup_path)
    if container_id.end_with?(SYSTEMD_SCOPE_SUFFIX)
      container_id = container_id[0...-SYSTEMD_SCOPE_SUFFIX.length]
      if container_id.include?('-')
        container_id = container_id.split('-', 2)[1]
      end
    end
    if (kubepods_match = KUBEPODS_REGEX.match(directory))
      pod_id = kubepods_match[1] || kubepods_match[2]
      self.container_id = container_id
      self.kubernetes_pod_uid = pod_id
    elsif CONTAINER_ID_REGEX.match(container_id)
      self.container_id = container_id
    end
  end
end