From a9336b571b9bd59cba076cd891f6039bdbc7212f Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Wed, 9 Jul 2025 15:44:31 +0200 Subject: [PATCH 1/5] Remove usage of deprecated pkg_resource in favor of importlib.metadata In setup.py just remove a check for a 2018+ release of setuptools. --- elasticapm/__init__.py | 6 ++---- setup.py | 3 --- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/elasticapm/__init__.py b/elasticapm/__init__.py index df6eee9e1..e793bfa89 100644 --- a/elasticapm/__init__.py +++ b/elasticapm/__init__.py @@ -28,6 +28,7 @@ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE import sys +import importlib.metadata from elasticapm.base import Client, get_client # noqa: F401 from elasticapm.conf import setup_logging # noqa: F401 @@ -54,10 +55,7 @@ _activation_method = None try: - try: - VERSION = __import__("importlib.metadata").metadata.version("elastic-apm") - except ImportError: - VERSION = __import__("pkg_resources").get_distribution("elastic-apm").version + VERSION = importlib.metadata.version("elastic-apm") except Exception: VERSION = "unknown" diff --git a/setup.py b/setup.py index 5dbb8f643..a4e976bfc 100644 --- a/setup.py +++ b/setup.py @@ -44,11 +44,8 @@ import codecs import os -import pkg_resources from setuptools import setup -pkg_resources.require("setuptools>=39.2") - def get_version(): """ From d260fad0b7aef8ceaf757367c43c7c9a20ee807a Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Fri, 11 Jul 2025 11:10:45 +0200 Subject: [PATCH 2/5] Add test for VERSION not being unknown --- tests/client/client_tests.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/client/client_tests.py b/tests/client/client_tests.py index 62e10d301..61f3bb9f1 100644 --- a/tests/client/client_tests.py +++ b/tests/client/client_tests.py @@ -558,3 +558,7 @@ def test_user_agent(elasticapm_client, expected): def test_label_without_client(): elasticapm.label(foo="foo") + + +def test_version_is_not_unknown(): + assert elasticapm.VERSION != "unknown" From 8e04e3c9d197edfbb5a1ee67a722faaeb9f52c01 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Fri, 11 Jul 2025 11:36:08 +0200 Subject: [PATCH 3/5] Skip unknown version on Python 3.8 as it behaves differently --- tests/client/client_tests.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/client/client_tests.py b/tests/client/client_tests.py index 61f3bb9f1..11099726d 100644 --- a/tests/client/client_tests.py +++ b/tests/client/client_tests.py @@ -560,5 +560,9 @@ def test_label_without_client(): elasticapm.label(foo="foo") +@pytest.mark.skipif( + sys.version_info < (3, 9), + reason="Python 3.8 importlib.metadata.version does not see elasticapm package in path but not installed", +) def test_version_is_not_unknown(): assert elasticapm.VERSION != "unknown" From 964383173404707a557204bda3e4c0cd6a4e26ce Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Fri, 11 Jul 2025 11:43:49 +0200 Subject: [PATCH 4/5] Keep using pkg_resources as fallback for getting version --- elasticapm/__init__.py | 6 ++++-- tests/client/client_tests.py | 4 ---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/elasticapm/__init__.py b/elasticapm/__init__.py index e793bfa89..df6eee9e1 100644 --- a/elasticapm/__init__.py +++ b/elasticapm/__init__.py @@ -28,7 +28,6 @@ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE import sys -import importlib.metadata from elasticapm.base import Client, get_client # noqa: F401 from elasticapm.conf import setup_logging # noqa: F401 @@ -55,7 +54,10 @@ _activation_method = None try: - VERSION = importlib.metadata.version("elastic-apm") + try: + VERSION = __import__("importlib.metadata").metadata.version("elastic-apm") + except ImportError: + VERSION = __import__("pkg_resources").get_distribution("elastic-apm").version except Exception: VERSION = "unknown" diff --git a/tests/client/client_tests.py b/tests/client/client_tests.py index 11099726d..61f3bb9f1 100644 --- a/tests/client/client_tests.py +++ b/tests/client/client_tests.py @@ -560,9 +560,5 @@ def test_label_without_client(): elasticapm.label(foo="foo") -@pytest.mark.skipif( - sys.version_info < (3, 9), - reason="Python 3.8 importlib.metadata.version does not see elasticapm package in path but not installed", -) def test_version_is_not_unknown(): assert elasticapm.VERSION != "unknown" From 55b9f161ea0cb59af1fd45e9eb39ba55bcdd3437 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Fri, 11 Jul 2025 11:57:41 +0200 Subject: [PATCH 5/5] No test then --- tests/client/client_tests.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/client/client_tests.py b/tests/client/client_tests.py index 61f3bb9f1..62e10d301 100644 --- a/tests/client/client_tests.py +++ b/tests/client/client_tests.py @@ -558,7 +558,3 @@ def test_user_agent(elasticapm_client, expected): def test_label_without_client(): elasticapm.label(foo="foo") - - -def test_version_is_not_unknown(): - assert elasticapm.VERSION != "unknown"