prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Switch to importlib.metadata for package versions
  Python 3.8 comes with the new standard library [module `importlib.metadata`](https://docs.python.org/3.8/library/importlib.metadata.html). This is the new de-facto way of discovering installed package versions.
  
  For older versions there's the [`importlib-metadata` backport](https://importlib-metadata.readthedocs.io/en/latest/).
  
  We currently use `pkg_resources` to do this, we'd be better off long term switching to the backport and standard library module. The backport supports down to Python 2.7 so it'll work for everything we do.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `src/scout_apm/core/metadata.py`
  Content:
  ```
  1 # coding=utf-8
  2 from __future__ import absolute_import, division, print_function, unicode_literals
  3 
  4 import datetime as dt
  5 import sys
  6 from os import getpid
  7 
  8 from scout_apm.core.commands import ApplicationEvent
  9 from scout_apm.core.config import scout_config
  10 from scout_apm.core.socket import CoreAgentSocket
  11 
  12 
  13 def report_app_metadata():
  14     CoreAgentSocket.instance().send(
  15         ApplicationEvent(
  16             event_type="scout.metadata",
  17             event_value=get_metadata(),
  18             source="Pid: " + str(getpid()),
  19             timestamp=dt.datetime.utcnow(),
  20         )
  21     )
  22 
  23 
  24 def get_metadata():
  25     data = {
  26         "language": "python",
  27         "language_version": "{}.{}.{}".format(*sys.version_info[:3]),
  28         "server_time": dt.datetime.utcnow().isoformat() + "Z",
  29         "framework": scout_config.value("framework"),
  30         "framework_version": scout_config.value("framework_version"),
  31         "environment": "",
  32         "app_server": scout_config.value("app_server"),
  33         "hostname": scout_config.value("hostname"),
  34         "database_engine": "",
  35         "database_adapter": "",
  36         "application_name": "",
  37         "libraries": get_python_packages_versions(),
  38         "paas": "",
  39         "application_root": scout_config.value("application_root"),
  40         "scm_subdirectory": scout_config.value("scm_subdirectory"),
  41         "git_sha": scout_config.value("revision_sha"),
  42     }
  43     # Deprecated - see #327:
  44     data["version"] = data["language_version"]
  45     return data
  46 
  47 
  48 def get_python_packages_versions():
  49     try:
  50         import pkg_resources
  51     except ImportError:
  52         return []
  53 
  54     return sorted(
  55         (distribution.project_name, distribution.version)
  56         for distribution in pkg_resources.working_set
  57     )
  58
  ```
  
  Path: `setup.py`
  Content:
  ```
  1 # coding=utf-8
  2 from __future__ import absolute_import, division, print_function, unicode_literals
  3 
  4 import sys
  5 
  6 from setuptools import Extension, find_packages, setup
  7 
  8 with open("README.md", "r") as fp:
  9     long_description = fp.read()
  10 
  11 packages = find_packages("src")
  12 if sys.version_info < (3, 6):
  13     packages = [p for p in packages if not p.startswith("scout_apm.async_")]
  14 
  15 compile_extensions = (
  16     # Python 3+
  17     sys.version_info >= (3,)
  18     # Not Jython
  19     and not sys.platform.startswith("java")
  20     # Not PyPy
  21     and "__pypy__" not in sys.builtin_module_names
  22 )
  23 if compile_extensions:
  24     ext_modules = [
  25         Extension(
  26             str("scout_apm.core._objtrace"), [str("src/scout_apm/core/_objtrace.c")]
  27         )
  28     ]
  29 else:
  30     ext_modules = []
  31 
  32 setup(
  33     name="scout_apm",
  34     version="2.9.1",
  35     description="Scout Application Performance Monitoring Agent",
  36     long_description=long_description,
  37     long_description_content_type="text/markdown",
  38     url="https://github.com/scoutapp/scout_apm_python",
  39     project_urls={
  40         "Documentation": "https://docs.scoutapm.com/#python-agent",
  41         "Changelog": (
  42             "https://github.com/scoutapp/scout_apm_python/blob/master/CHANGELOG.md"
  43         ),
  44     },
  45     author="Scout",
  46     author_email="support@scoutapm.com",
  47     license="MIT",
  48     zip_safe=False,
  49     python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4",
  50     packages=packages,
  51     package_dir={str(""): str("src")},
  52     ext_modules=ext_modules,
  53     entry_points={
  54         "console_scripts": [
  55             "core-agent-manager = scout_apm.core.cli.core_agent_manager:main"
  56         ]
  57     },
  58     install_requires=[
  59         'asgiref ; python_version >= "3.5"',
  60         "psutil>=5,<6",
  61         "requests>=2,<3",
  62         "wrapt>=1.10,<2.0",
  63     ],
  64     keywords="apm performance monitoring development",
  65     classifiers=[
  66         "Development Status :: 5 - Production/Stable",
  67         "Framework :: Bottle",
  68         "Framework :: Django",
  69         "Framework :: Django :: 1.8",
  70         "Framework :: Django :: 1.9",
  71         "Framework :: Django :: 1.10",
  72         "Framework :: Django :: 1.11",
  73         "Framework :: Django :: 2.0",
  74         "Framework :: Django :: 2.1",
  75         "Framework :: Django :: 2.2",
  76         "Framework :: Django :: 3.0",
  77         "Framework :: Flask",
  78         "Framework :: Pyramid",
  79         "Intended Audience :: Developers",
  80         "Topic :: System :: Monitoring",
  81         "License :: OSI Approved :: MIT License",
  82         "Operating System :: MacOS",
  83         "Operating System :: POSIX",
  84         "Operating System :: POSIX :: Linux",
  85         "Programming Language :: Python :: 2",
  86         "Programming Language :: Python :: 2.7",
  87         "Programming Language :: Python :: 3",
  88         "Programming Language :: Python :: 3.4",
  89         "Programming Language :: Python :: 3.5",
  90         "Programming Language :: Python :: 3.6",
  91         "Programming Language :: Python :: 3.7",
  92         "Programming Language :: Python :: 3.8",
  93     ],
  94 )
  95
  ```
  --- 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()
  
  ```
  
golden_diff: diff --git a/setup.py b/setup.py
  --- a/setup.py
  +++ b/setup.py
  @@ -57,6 +57,7 @@
       },
       install_requires=[
           'asgiref ; python_version >= "3.5"',
  +        'importlib-metadata ; python_version < "3.8"',
           "psutil>=5,<6",
           "requests>=2,<3",
           "wrapt>=1.10,<2.0",
  diff --git a/src/scout_apm/core/metadata.py b/src/scout_apm/core/metadata.py
  --- a/src/scout_apm/core/metadata.py
  +++ b/src/scout_apm/core/metadata.py
  @@ -47,11 +47,15 @@
   
   def get_python_packages_versions():
       try:
  -        import pkg_resources
  +        if sys.version_info >= (3, 8):
  +            from importlib.metadata import distributions
  +        else:
  +            from importlib_metadata import distributions
       except ImportError:
  +        # For some reason it is unavailable
           return []
   
       return sorted(
  -        (distribution.project_name, distribution.version)
  -        for distribution in pkg_resources.working_set
  +        (distribution.metadata["Name"], distribution.metadata["Version"])
  +        for distribution in distributions()
       )
  
verification_info: {"golden_diff": "diff --git a/setup.py b/setup.py\n--- a/setup.py\n+++ b/setup.py\n@@ -57,6 +57,7 @@\n     },\n     install_requires=[\n         'asgiref ; python_version >= \"3.5\"',\n+        'importlib-metadata ; python_version < \"3.8\"',\n         \"psutil>=5,<6\",\n         \"requests>=2,<3\",\n         \"wrapt>=1.10,<2.0\",\ndiff --git a/src/scout_apm/core/metadata.py b/src/scout_apm/core/metadata.py\n--- a/src/scout_apm/core/metadata.py\n+++ b/src/scout_apm/core/metadata.py\n@@ -47,11 +47,15 @@\n \n def get_python_packages_versions():\n     try:\n-        import pkg_resources\n+        if sys.version_info >= (3, 8):\n+            from importlib.metadata import distributions\n+        else:\n+            from importlib_metadata import distributions\n     except ImportError:\n+        # For some reason it is unavailable\n         return []\n \n     return sorted(\n-        (distribution.project_name, distribution.version)\n-        for distribution in pkg_resources.working_set\n+        (distribution.metadata[\"Name\"], distribution.metadata[\"Version\"])\n+        for distribution in distributions()\n     )\n", "issue": "Switch to importlib.metadata for package versions\nPython 3.8 comes with the new standard library [module `importlib.metadata`](https://docs.python.org/3.8/library/importlib.metadata.html). This is the new de-facto way of discovering installed package versions.\r\n\r\nFor older versions there's the [`importlib-metadata` backport](https://importlib-metadata.readthedocs.io/en/latest/).\r\n\r\nWe currently use `pkg_resources` to do this, we'd be better off long term switching to the backport and standard library module. The backport supports down to Python 2.7 so it'll work for everything we do.\n", "before_files": [{"content": "# coding=utf-8\nfrom __future__ import absolute_import, division, print_function, unicode_literals\n\nimport datetime as dt\nimport sys\nfrom os import getpid\n\nfrom scout_apm.core.commands import ApplicationEvent\nfrom scout_apm.core.config import scout_config\nfrom scout_apm.core.socket import CoreAgentSocket\n\n\ndef report_app_metadata():\n    CoreAgentSocket.instance().send(\n        ApplicationEvent(\n            event_type=\"scout.metadata\",\n            event_value=get_metadata(),\n            source=\"Pid: \" + str(getpid()),\n            timestamp=dt.datetime.utcnow(),\n        )\n    )\n\n\ndef get_metadata():\n    data = {\n        \"language\": \"python\",\n        \"language_version\": \"{}.{}.{}\".format(*sys.version_info[:3]),\n        \"server_time\": dt.datetime.utcnow().isoformat() + \"Z\",\n        \"framework\": scout_config.value(\"framework\"),\n        \"framework_version\": scout_config.value(\"framework_version\"),\n        \"environment\": \"\",\n        \"app_server\": scout_config.value(\"app_server\"),\n        \"hostname\": scout_config.value(\"hostname\"),\n        \"database_engine\": \"\",\n        \"database_adapter\": \"\",\n        \"application_name\": \"\",\n        \"libraries\": get_python_packages_versions(),\n        \"paas\": \"\",\n        \"application_root\": scout_config.value(\"application_root\"),\n        \"scm_subdirectory\": scout_config.value(\"scm_subdirectory\"),\n        \"git_sha\": scout_config.value(\"revision_sha\"),\n    }\n    # Deprecated - see #327:\n    data[\"version\"] = data[\"language_version\"]\n    return data\n\n\ndef get_python_packages_versions():\n    try:\n        import pkg_resources\n    except ImportError:\n        return []\n\n    return sorted(\n        (distribution.project_name, distribution.version)\n        for distribution in pkg_resources.working_set\n    )\n", "path": "src/scout_apm/core/metadata.py"}, {"content": "# coding=utf-8\nfrom __future__ import absolute_import, division, print_function, unicode_literals\n\nimport sys\n\nfrom setuptools import Extension, find_packages, setup\n\nwith open(\"README.md\", \"r\") as fp:\n    long_description = fp.read()\n\npackages = find_packages(\"src\")\nif sys.version_info < (3, 6):\n    packages = [p for p in packages if not p.startswith(\"scout_apm.async_\")]\n\ncompile_extensions = (\n    # Python 3+\n    sys.version_info >= (3,)\n    # Not Jython\n    and not sys.platform.startswith(\"java\")\n    # Not PyPy\n    and \"__pypy__\" not in sys.builtin_module_names\n)\nif compile_extensions:\n    ext_modules = [\n        Extension(\n            str(\"scout_apm.core._objtrace\"), [str(\"src/scout_apm/core/_objtrace.c\")]\n        )\n    ]\nelse:\n    ext_modules = []\n\nsetup(\n    name=\"scout_apm\",\n    version=\"2.9.1\",\n    description=\"Scout Application Performance Monitoring Agent\",\n    long_description=long_description,\n    long_description_content_type=\"text/markdown\",\n    url=\"https://github.com/scoutapp/scout_apm_python\",\n    project_urls={\n        \"Documentation\": \"https://docs.scoutapm.com/#python-agent\",\n        \"Changelog\": (\n            \"https://github.com/scoutapp/scout_apm_python/blob/master/CHANGELOG.md\"\n        ),\n    },\n    author=\"Scout\",\n    author_email=\"support@scoutapm.com\",\n    license=\"MIT\",\n    zip_safe=False,\n    python_requires=\">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4\",\n    packages=packages,\n    package_dir={str(\"\"): str(\"src\")},\n    ext_modules=ext_modules,\n    entry_points={\n        \"console_scripts\": [\n            \"core-agent-manager = scout_apm.core.cli.core_agent_manager:main\"\n        ]\n    },\n    install_requires=[\n        'asgiref ; python_version >= \"3.5\"',\n        \"psutil>=5,<6\",\n        \"requests>=2,<3\",\n        \"wrapt>=1.10,<2.0\",\n    ],\n    keywords=\"apm performance monitoring development\",\n    classifiers=[\n        \"Development Status :: 5 - Production/Stable\",\n        \"Framework :: Bottle\",\n        \"Framework :: Django\",\n        \"Framework :: Django :: 1.8\",\n        \"Framework :: Django :: 1.9\",\n        \"Framework :: Django :: 1.10\",\n        \"Framework :: Django :: 1.11\",\n        \"Framework :: Django :: 2.0\",\n        \"Framework :: Django :: 2.1\",\n        \"Framework :: Django :: 2.2\",\n        \"Framework :: Django :: 3.0\",\n        \"Framework :: Flask\",\n        \"Framework :: Pyramid\",\n        \"Intended Audience :: Developers\",\n        \"Topic :: System :: Monitoring\",\n        \"License :: OSI Approved :: MIT License\",\n        \"Operating System :: MacOS\",\n        \"Operating System :: POSIX\",\n        \"Operating System :: POSIX :: Linux\",\n        \"Programming Language :: Python :: 2\",\n        \"Programming Language :: Python :: 2.7\",\n        \"Programming Language :: Python :: 3\",\n        \"Programming Language :: Python :: 3.4\",\n        \"Programming Language :: Python :: 3.5\",\n        \"Programming Language :: Python :: 3.6\",\n        \"Programming Language :: Python :: 3.7\",\n        \"Programming Language :: Python :: 3.8\",\n    ],\n)\n", "path": "setup.py"}], "after_files": [{"content": "# coding=utf-8\nfrom __future__ import absolute_import, division, print_function, unicode_literals\n\nimport datetime as dt\nimport sys\nfrom os import getpid\n\nfrom scout_apm.core.commands import ApplicationEvent\nfrom scout_apm.core.config import scout_config\nfrom scout_apm.core.socket import CoreAgentSocket\n\n\ndef report_app_metadata():\n    CoreAgentSocket.instance().send(\n        ApplicationEvent(\n            event_type=\"scout.metadata\",\n            event_value=get_metadata(),\n            source=\"Pid: \" + str(getpid()),\n            timestamp=dt.datetime.utcnow(),\n        )\n    )\n\n\ndef get_metadata():\n    data = {\n        \"language\": \"python\",\n        \"language_version\": \"{}.{}.{}\".format(*sys.version_info[:3]),\n        \"server_time\": dt.datetime.utcnow().isoformat() + \"Z\",\n        \"framework\": scout_config.value(\"framework\"),\n        \"framework_version\": scout_config.value(\"framework_version\"),\n        \"environment\": \"\",\n        \"app_server\": scout_config.value(\"app_server\"),\n        \"hostname\": scout_config.value(\"hostname\"),\n        \"database_engine\": \"\",\n        \"database_adapter\": \"\",\n        \"application_name\": \"\",\n        \"libraries\": get_python_packages_versions(),\n        \"paas\": \"\",\n        \"application_root\": scout_config.value(\"application_root\"),\n        \"scm_subdirectory\": scout_config.value(\"scm_subdirectory\"),\n        \"git_sha\": scout_config.value(\"revision_sha\"),\n    }\n    # Deprecated - see #327:\n    data[\"version\"] = data[\"language_version\"]\n    return data\n\n\ndef get_python_packages_versions():\n    try:\n        if sys.version_info >= (3, 8):\n            from importlib.metadata import distributions\n        else:\n            from importlib_metadata import distributions\n    except ImportError:\n        # For some reason it is unavailable\n        return []\n\n    return sorted(\n        (distribution.metadata[\"Name\"], distribution.metadata[\"Version\"])\n        for distribution in distributions()\n    )\n", "path": "src/scout_apm/core/metadata.py"}, {"content": "# coding=utf-8\nfrom __future__ import absolute_import, division, print_function, unicode_literals\n\nimport sys\n\nfrom setuptools import Extension, find_packages, setup\n\nwith open(\"README.md\", \"r\") as fp:\n    long_description = fp.read()\n\npackages = find_packages(\"src\")\nif sys.version_info < (3, 6):\n    packages = [p for p in packages if not p.startswith(\"scout_apm.async_\")]\n\ncompile_extensions = (\n    # Python 3+\n    sys.version_info >= (3,)\n    # Not Jython\n    and not sys.platform.startswith(\"java\")\n    # Not PyPy\n    and \"__pypy__\" not in sys.builtin_module_names\n)\nif compile_extensions:\n    ext_modules = [\n        Extension(\n            str(\"scout_apm.core._objtrace\"), [str(\"src/scout_apm/core/_objtrace.c\")]\n        )\n    ]\nelse:\n    ext_modules = []\n\nsetup(\n    name=\"scout_apm\",\n    version=\"2.9.1\",\n    description=\"Scout Application Performance Monitoring Agent\",\n    long_description=long_description,\n    long_description_content_type=\"text/markdown\",\n    url=\"https://github.com/scoutapp/scout_apm_python\",\n    project_urls={\n        \"Documentation\": \"https://docs.scoutapm.com/#python-agent\",\n        \"Changelog\": (\n            \"https://github.com/scoutapp/scout_apm_python/blob/master/CHANGELOG.md\"\n        ),\n    },\n    author=\"Scout\",\n    author_email=\"support@scoutapm.com\",\n    license=\"MIT\",\n    zip_safe=False,\n    python_requires=\">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4\",\n    packages=packages,\n    package_dir={str(\"\"): str(\"src\")},\n    ext_modules=ext_modules,\n    entry_points={\n        \"console_scripts\": [\n            \"core-agent-manager = scout_apm.core.cli.core_agent_manager:main\"\n        ]\n    },\n    install_requires=[\n        'asgiref ; python_version >= \"3.5\"',\n        'importlib-metadata ; python_version < \"3.8\"',\n        \"psutil>=5,<6\",\n        \"requests>=2,<3\",\n        \"wrapt>=1.10,<2.0\",\n    ],\n    keywords=\"apm performance monitoring development\",\n    classifiers=[\n        \"Development Status :: 5 - Production/Stable\",\n        \"Framework :: Bottle\",\n        \"Framework :: Django\",\n        \"Framework :: Django :: 1.8\",\n        \"Framework :: Django :: 1.9\",\n        \"Framework :: Django :: 1.10\",\n        \"Framework :: Django :: 1.11\",\n        \"Framework :: Django :: 2.0\",\n        \"Framework :: Django :: 2.1\",\n        \"Framework :: Django :: 2.2\",\n        \"Framework :: Django :: 3.0\",\n        \"Framework :: Flask\",\n        \"Framework :: Pyramid\",\n        \"Intended Audience :: Developers\",\n        \"Topic :: System :: Monitoring\",\n        \"License :: OSI Approved :: MIT License\",\n        \"Operating System :: MacOS\",\n        \"Operating System :: POSIX\",\n        \"Operating System :: POSIX :: Linux\",\n        \"Programming Language :: Python :: 2\",\n        \"Programming Language :: Python :: 2.7\",\n        \"Programming Language :: Python :: 3\",\n        \"Programming Language :: Python :: 3.4\",\n        \"Programming Language :: Python :: 3.5\",\n        \"Programming Language :: Python :: 3.6\",\n        \"Programming Language :: Python :: 3.7\",\n        \"Programming Language :: Python :: 3.8\",\n    ],\n)\n", "path": "setup.py"}]}