We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Expand k8s pod ID discovery regex
Implementing elastic/apm#344

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `elasticapm/utils/cgroup.py`
Content:
```
1 #  BSD 3-Clause License
2 #
3 #  Copyright (c) 2019, Elasticsearch BV
4 #  All rights reserved.
5 #
6 #  Redistribution and use in source and binary forms, with or without
7 #  modification, are permitted provided that the following conditions are met:
8 #
9 #  * Redistributions of source code must retain the above copyright notice, this
10 #    list of conditions and the following disclaimer.
11 #
12 #  * Redistributions in binary form must reproduce the above copyright notice,
13 #    this list of conditions and the following disclaimer in the documentation
14 #    and/or other materials provided with the distribution.
15 #
16 #  * Neither the name of the copyright holder nor the names of its
17 #    contributors may be used to endorse or promote products derived from
18 #    this software without specific prior written permission.
19 #
20 #  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 #  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 #  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 #  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 #  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 #  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 #  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 #  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 #  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 #  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 import os
32 import re
33 
34 CGROUP_PATH = "/proc/self/cgroup"
35 
36 SYSTEMD_SCOPE_SUFFIX = ".scope"
37 
38 kubepods_regexp = re.compile(
39     r"(?:^/kubepods[\S]*/pod([^/]+)$)|(?:^/kubepods\.slice/kubepods-[^/]+\.slice/kubepods-[^/]+-pod([^/]+)\.slice$)"
40 )
41 
42 container_id_regexp = re.compile(
43     "^(?:[0-9a-f]{64}|[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4,})$", re.IGNORECASE
44 )
45 
46 
47 def get_cgroup_container_metadata():
48     """
49     Reads docker/kubernetes metadata (container id, pod id) from /proc/self/cgroup
50 
51     The result is a nested dictionary with the detected IDs, e.g.
52 
53         {
54             "container": {"id": "2227daf62df6694645fee5df53c1f91271546a9560e8600a525690ae252b7f63"},
55             "pod": {"uid": "90d81341_92de_11e7_8cf2_507b9d4141fa"}
56         }
57 
58     :return: a dictionary with the detected ids or {}
59     """
60     if not os.path.exists(CGROUP_PATH):
61         return {}
62     with open(CGROUP_PATH) as f:
63         return parse_cgroups(f) or {}
64 
65 
66 def parse_cgroups(filehandle):
67     """
68     Reads lines from a file handle and tries to parse docker container IDs and kubernetes Pod IDs.
69 
70     See tests.utils.docker_tests.test_cgroup_parsing for a set of test cases
71 
72     :param filehandle:
73     :return: nested dictionary or None
74     """
75     for line in filehandle:
76         parts = line.strip().split(":")
77         if len(parts) != 3:
78             continue
79         cgroup_path = parts[2]
80 
81         # Depending on the filesystem driver used for cgroup
82         # management, the paths in /proc/pid/cgroup will have
83         # one of the following formats in a Docker container:
84         #
85         #   systemd: /system.slice/docker-<container-ID>.scope
86         #   cgroupfs: /docker/<container-ID>
87         #
88         # In a Kubernetes pod, the cgroup path will look like:
89         #
90         #   systemd:/kubepods.slice/kubepods-<QoS-class>.slice/kubepods-<QoS-class>-pod<pod-UID>.slice/<container-iD>.scope
91         #   cgroupfs:/kubepods/<QoS-class>/pod<pod-UID>/<container-iD>
92 
93         directory, container_id = os.path.split(cgroup_path)
94         if container_id.endswith(SYSTEMD_SCOPE_SUFFIX):
95             container_id = container_id[: -len(SYSTEMD_SCOPE_SUFFIX)]
96             if "-" in container_id:
97                 container_id = container_id.split("-", 1)[1]
98         kubepods_match = kubepods_regexp.match(directory)
99         if kubepods_match:
100             pod_id = kubepods_match.group(1)
101             if not pod_id:
102                 pod_id = kubepods_match.group(2)
103                 if pod_id:
104                     pod_id = pod_id.replace("_", "-")
105             return {"container": {"id": container_id}, "kubernetes": {"pod": {"uid": pod_id}}}
106         elif container_id_regexp.match(container_id):
107             return {"container": {"id": container_id}}
108
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/elasticapm/utils/cgroup.py b/elasticapm/utils/cgroup.py
--- a/elasticapm/utils/cgroup.py
+++ b/elasticapm/utils/cgroup.py
@@ -36,7 +36,7 @@
 SYSTEMD_SCOPE_SUFFIX = ".scope"
 
 kubepods_regexp = re.compile(
-    r"(?:^/kubepods[\S]*/pod([^/]+)$)|(?:^/kubepods\.slice/kubepods-[^/]+\.slice/kubepods-[^/]+-pod([^/]+)\.slice$)"
+    r"(?:^/kubepods[\S]*/pod([^/]+)$)|(?:^/kubepods\.slice/(kubepods-[^/]+\.slice/)?kubepods[^/]*-pod([^/]+)\.slice$)"
 )
 
 container_id_regexp = re.compile(
@@ -97,9 +97,9 @@
                 container_id = container_id.split("-", 1)[1]
         kubepods_match = kubepods_regexp.match(directory)
         if kubepods_match:
-            pod_id = kubepods_match.group(1)
+            pod_id = kubepods_match.group(1)  # if first part of kubepods_regexp matched
             if not pod_id:
-                pod_id = kubepods_match.group(2)
+                pod_id = kubepods_match.group(3)  # if second part of kubepods_regexp matched
                 if pod_id:
                     pod_id = pod_id.replace("_", "-")
             return {"container": {"id": container_id}, "kubernetes": {"pod": {"uid": pod_id}}}

{"golden_diff": "diff --git a/elasticapm/utils/cgroup.py b/elasticapm/utils/cgroup.py\n--- a/elasticapm/utils/cgroup.py\n+++ b/elasticapm/utils/cgroup.py\n@@ -36,7 +36,7 @@\n SYSTEMD_SCOPE_SUFFIX = \".scope\"\n \n kubepods_regexp = re.compile(\n-    r\"(?:^/kubepods[\\S]*/pod([^/]+)$)|(?:^/kubepods\\.slice/kubepods-[^/]+\\.slice/kubepods-[^/]+-pod([^/]+)\\.slice$)\"\n+    r\"(?:^/kubepods[\\S]*/pod([^/]+)$)|(?:^/kubepods\\.slice/(kubepods-[^/]+\\.slice/)?kubepods[^/]*-pod([^/]+)\\.slice$)\"\n )\n \n container_id_regexp = re.compile(\n@@ -97,9 +97,9 @@\n                 container_id = container_id.split(\"-\", 1)[1]\n         kubepods_match = kubepods_regexp.match(directory)\n         if kubepods_match:\n-            pod_id = kubepods_match.group(1)\n+            pod_id = kubepods_match.group(1)  # if first part of kubepods_regexp matched\n             if not pod_id:\n-                pod_id = kubepods_match.group(2)\n+                pod_id = kubepods_match.group(3)  # if second part of kubepods_regexp matched\n                 if pod_id:\n                     pod_id = pod_id.replace(\"_\", \"-\")\n             return {\"container\": {\"id\": container_id}, \"kubernetes\": {\"pod\": {\"uid\": pod_id}}}\n", "issue": "Expand k8s pod ID discovery regex\nImplementing elastic/apm#344\n", "before_files": [{"content": "#  BSD 3-Clause License\n#\n#  Copyright (c) 2019, Elasticsearch BV\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#\n#  * Redistributions of source code must retain the above copyright notice, this\n#    list of conditions and the following disclaimer.\n#\n#  * Redistributions in binary form must reproduce the above copyright notice,\n#    this list of conditions and the following disclaimer in the documentation\n#    and/or other materials provided with the distribution.\n#\n#  * Neither the name of the copyright holder nor the names of its\n#    contributors may be used to endorse or promote products derived from\n#    this software without specific prior written permission.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n#  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\n#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n#  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n#  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\n#  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\nimport os\nimport re\n\nCGROUP_PATH = \"/proc/self/cgroup\"\n\nSYSTEMD_SCOPE_SUFFIX = \".scope\"\n\nkubepods_regexp = re.compile(\n    r\"(?:^/kubepods[\\S]*/pod([^/]+)$)|(?:^/kubepods\\.slice/kubepods-[^/]+\\.slice/kubepods-[^/]+-pod([^/]+)\\.slice$)\"\n)\n\ncontainer_id_regexp = re.compile(\n    \"^(?:[0-9a-f]{64}|[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4,})$\", re.IGNORECASE\n)\n\n\ndef get_cgroup_container_metadata():\n    \"\"\"\n    Reads docker/kubernetes metadata (container id, pod id) from /proc/self/cgroup\n\n    The result is a nested dictionary with the detected IDs, e.g.\n\n        {\n            \"container\": {\"id\": \"2227daf62df6694645fee5df53c1f91271546a9560e8600a525690ae252b7f63\"},\n            \"pod\": {\"uid\": \"90d81341_92de_11e7_8cf2_507b9d4141fa\"}\n        }\n\n    :return: a dictionary with the detected ids or {}\n    \"\"\"\n    if not os.path.exists(CGROUP_PATH):\n        return {}\n    with open(CGROUP_PATH) as f:\n        return parse_cgroups(f) or {}\n\n\ndef parse_cgroups(filehandle):\n    \"\"\"\n    Reads lines from a file handle and tries to parse docker container IDs and kubernetes Pod IDs.\n\n    See tests.utils.docker_tests.test_cgroup_parsing for a set of test cases\n\n    :param filehandle:\n    :return: nested dictionary or None\n    \"\"\"\n    for line in filehandle:\n        parts = line.strip().split(\":\")\n        if len(parts) != 3:\n            continue\n        cgroup_path = parts[2]\n\n        # Depending on the filesystem driver used for cgroup\n        # management, the paths in /proc/pid/cgroup will have\n        # one of the following formats in a Docker container:\n        #\n        #   systemd: /system.slice/docker-<container-ID>.scope\n        #   cgroupfs: /docker/<container-ID>\n        #\n        # In a Kubernetes pod, the cgroup path will look like:\n        #\n        #   systemd:/kubepods.slice/kubepods-<QoS-class>.slice/kubepods-<QoS-class>-pod<pod-UID>.slice/<container-iD>.scope\n        #   cgroupfs:/kubepods/<QoS-class>/pod<pod-UID>/<container-iD>\n\n        directory, container_id = os.path.split(cgroup_path)\n        if container_id.endswith(SYSTEMD_SCOPE_SUFFIX):\n            container_id = container_id[: -len(SYSTEMD_SCOPE_SUFFIX)]\n            if \"-\" in container_id:\n                container_id = container_id.split(\"-\", 1)[1]\n        kubepods_match = kubepods_regexp.match(directory)\n        if kubepods_match:\n            pod_id = kubepods_match.group(1)\n            if not pod_id:\n                pod_id = kubepods_match.group(2)\n                if pod_id:\n                    pod_id = pod_id.replace(\"_\", \"-\")\n            return {\"container\": {\"id\": container_id}, \"kubernetes\": {\"pod\": {\"uid\": pod_id}}}\n        elif container_id_regexp.match(container_id):\n            return {\"container\": {\"id\": container_id}}\n", "path": "elasticapm/utils/cgroup.py"}], "after_files": [{"content": "#  BSD 3-Clause License\n#\n#  Copyright (c) 2019, Elasticsearch BV\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#\n#  * Redistributions of source code must retain the above copyright notice, this\n#    list of conditions and the following disclaimer.\n#\n#  * Redistributions in binary form must reproduce the above copyright notice,\n#    this list of conditions and the following disclaimer in the documentation\n#    and/or other materials provided with the distribution.\n#\n#  * Neither the name of the copyright holder nor the names of its\n#    contributors may be used to endorse or promote products derived from\n#    this software without specific prior written permission.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n#  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\n#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n#  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n#  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\n#  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\nimport os\nimport re\n\nCGROUP_PATH = \"/proc/self/cgroup\"\n\nSYSTEMD_SCOPE_SUFFIX = \".scope\"\n\nkubepods_regexp = re.compile(\n    r\"(?:^/kubepods[\\S]*/pod([^/]+)$)|(?:^/kubepods\\.slice/(kubepods-[^/]+\\.slice/)?kubepods[^/]*-pod([^/]+)\\.slice$)\"\n)\n\ncontainer_id_regexp = re.compile(\n    \"^(?:[0-9a-f]{64}|[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4,})$\", re.IGNORECASE\n)\n\n\ndef get_cgroup_container_metadata():\n    \"\"\"\n    Reads docker/kubernetes metadata (container id, pod id) from /proc/self/cgroup\n\n    The result is a nested dictionary with the detected IDs, e.g.\n\n        {\n            \"container\": {\"id\": \"2227daf62df6694645fee5df53c1f91271546a9560e8600a525690ae252b7f63\"},\n            \"pod\": {\"uid\": \"90d81341_92de_11e7_8cf2_507b9d4141fa\"}\n        }\n\n    :return: a dictionary with the detected ids or {}\n    \"\"\"\n    if not os.path.exists(CGROUP_PATH):\n        return {}\n    with open(CGROUP_PATH) as f:\n        return parse_cgroups(f) or {}\n\n\ndef parse_cgroups(filehandle):\n    \"\"\"\n    Reads lines from a file handle and tries to parse docker container IDs and kubernetes Pod IDs.\n\n    See tests.utils.docker_tests.test_cgroup_parsing for a set of test cases\n\n    :param filehandle:\n    :return: nested dictionary or None\n    \"\"\"\n    for line in filehandle:\n        parts = line.strip().split(\":\")\n        if len(parts) != 3:\n            continue\n        cgroup_path = parts[2]\n\n        # Depending on the filesystem driver used for cgroup\n        # management, the paths in /proc/pid/cgroup will have\n        # one of the following formats in a Docker container:\n        #\n        #   systemd: /system.slice/docker-<container-ID>.scope\n        #   cgroupfs: /docker/<container-ID>\n        #\n        # In a Kubernetes pod, the cgroup path will look like:\n        #\n        #   systemd:/kubepods.slice/kubepods-<QoS-class>.slice/kubepods-<QoS-class>-pod<pod-UID>.slice/<container-iD>.scope\n        #   cgroupfs:/kubepods/<QoS-class>/pod<pod-UID>/<container-iD>\n\n        directory, container_id = os.path.split(cgroup_path)\n        if container_id.endswith(SYSTEMD_SCOPE_SUFFIX):\n            container_id = container_id[: -len(SYSTEMD_SCOPE_SUFFIX)]\n            if \"-\" in container_id:\n                container_id = container_id.split(\"-\", 1)[1]\n        kubepods_match = kubepods_regexp.match(directory)\n        if kubepods_match:\n            pod_id = kubepods_match.group(1)  # if first part of kubepods_regexp matched\n            if not pod_id:\n                pod_id = kubepods_match.group(3)  # if second part of kubepods_regexp matched\n                if pod_id:\n                    pod_id = pod_id.replace(\"_\", \"-\")\n            return {\"container\": {\"id\": container_id}, \"kubernetes\": {\"pod\": {\"uid\": pod_id}}}\n        elif container_id_regexp.match(container_id):\n            return {\"container\": {\"id\": container_id}}\n", "path": "elasticapm/utils/cgroup.py"}]}