class ElasticAPM::Metadata::SystemInfo::ContainerInfo

def read_from_cgroup!

rubocop:disable Metrics/CyclomaticComplexity
rubocop:disable 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 = match_kubepods(directory))
      unless (pod_id = kubepods_match[1])
        pod_id = kubepods_match[2]
        pod_id&.tr!('_', '-')
      end
      self.container_id = container_id
      self.kubernetes_pod_uid = pod_id
    elsif match_container(container_id)
      self.container_id = container_id
    end
  end
end