Skip to content

Commit d3b7d74

Browse files
committed
feat: Update kafka python to support kafka-python 3 and remove support for kafka-python-ng
1 parent ed72afb commit d3b7d74

9 files changed

Lines changed: 19 additions & 108 deletions

File tree

.changelog/4785.added

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update kafka python to support kafka-python 3 and remove support for kafka-python-ng

instrumentation/opentelemetry-instrumentation-kafka-python/pyproject.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@ dependencies = [
3131
]
3232

3333
[project.optional-dependencies]
34-
instruments = []
35-
instruments-any = [
36-
"kafka-python >= 2.0, < 3.0",
37-
"kafka-python-ng >= 2.0, < 3.0"
34+
instruments = [
35+
"kafka-python >= 2.0, < 4.0",
3836
]
3937

4038
[project.entry-points.opentelemetry_instrumentor]

instrumentation/opentelemetry-instrumentation-kafka-python/src/opentelemetry/instrumentation/kafka/__init__.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,14 @@ def process_msg(message):
8282
---
8383
"""
8484

85-
from importlib.metadata import PackageNotFoundError, distribution
8685
from typing import Collection
8786

8887
import kafka
8988
from wrapt import wrap_function_wrapper
9089

9190
from opentelemetry import trace
9291
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
93-
from opentelemetry.instrumentation.kafka.package import (
94-
_instruments_any,
95-
_instruments_kafka_python,
96-
_instruments_kafka_python_ng,
97-
)
92+
from opentelemetry.instrumentation.kafka.package import _instruments
9893
from opentelemetry.instrumentation.kafka.utils import _wrap_next, _wrap_send
9994
from opentelemetry.instrumentation.kafka.version import __version__
10095
from opentelemetry.instrumentation.utils import unwrap
@@ -106,24 +101,7 @@ class KafkaInstrumentor(BaseInstrumentor):
106101
"""
107102

108103
def instrumentation_dependencies(self) -> Collection[str]:
109-
# Determine which package of kafka-python is installed
110-
# Right now there are two packages, kafka-python and kafka-python-ng
111-
# The latter is a fork of the former because the former is connected
112-
# to a pypi namespace that the current maintainers cannot access
113-
# https://github.com/dpkp/kafka-python/issues/2431
114-
try:
115-
distribution("kafka-python-ng")
116-
return (_instruments_kafka_python_ng,)
117-
except PackageNotFoundError:
118-
pass
119-
120-
try:
121-
distribution("kafka-python")
122-
return (_instruments_kafka_python,)
123-
except PackageNotFoundError:
124-
pass
125-
126-
return _instruments_any
104+
return _instruments
127105

128106
def _instrument(self, **kwargs):
129107
"""Instruments the kafka module

instrumentation/opentelemetry-instrumentation-kafka-python/src/opentelemetry/instrumentation/kafka/package.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44

5-
_instruments_kafka_python = "kafka-python >= 2.0, < 3.0"
6-
_instruments_kafka_python_ng = "kafka-python-ng >= 2.0, < 3.0"
5+
_instruments_kafka_python = "kafka-python >= 2.0, < 4.0"
76

8-
_instruments = ()
9-
_instruments_any = (_instruments_kafka_python, _instruments_kafka_python_ng)
7+
_instruments = (_instruments_kafka_python,)

instrumentation/opentelemetry-instrumentation-kafka-python/test-requirements.txt renamed to instrumentation/opentelemetry-instrumentation-kafka-python/test-requirements-0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
kafka-python>=2.0.0,<3.0.0
12
asgiref==3.8.1
23
Deprecated==1.2.14
34
iniconfig==2.0.0

instrumentation/opentelemetry-instrumentation-kafka-python/test-requirements-ng.txt renamed to instrumentation/opentelemetry-instrumentation-kafka-python/test-requirements-1.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
kafka-python>=3.0.0,<4.0.0
12
asgiref==3.8.1
23
Deprecated==1.2.14
34
iniconfig==2.0.0
4-
kafka-python-ng==2.2.2
5+
kafka-python==2.0.2
56
packaging==24.0
67
pluggy==1.6.0
78
py-cpuinfo==9.0.0
89
pytest==7.4.4
910
tomli==2.0.1
10-
typing_extensions==4.9.0
11+
typing_extensions==4.12.2
1112
wrapt==1.16.0
1213
zipp==3.19.2
1314
-e opentelemetry-instrumentation

instrumentation/opentelemetry-instrumentation-kafka-python/tests/test_instrumentation.py

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from opentelemetry.instrumentation.kafka import KafkaInstrumentor
1111
from opentelemetry.instrumentation.kafka.package import (
1212
_instruments_kafka_python,
13-
_instruments_kafka_python_ng,
1413
)
1514

1615

@@ -54,50 +53,6 @@ def _distribution(name):
5453
)
5554
self.assertEqual(package_to_instrument, (_instruments_kafka_python,))
5655

57-
@patch("opentelemetry.instrumentation.kafka.distribution")
58-
def test_instrumentation_dependencies_kafka_python_ng_installed(
59-
self, mock_distribution
60-
) -> None:
61-
instrumentation = KafkaInstrumentor()
62-
63-
def _distribution(name):
64-
if name == "kafka-python-ng":
65-
return None
66-
raise PackageNotFoundError
67-
68-
mock_distribution.side_effect = _distribution
69-
package_to_instrument = instrumentation.instrumentation_dependencies()
70-
71-
self.assertEqual(mock_distribution.call_count, 1)
72-
self.assertEqual(
73-
mock_distribution.mock_calls, [call("kafka-python-ng")]
74-
)
75-
self.assertEqual(
76-
package_to_instrument, (_instruments_kafka_python_ng,)
77-
)
78-
79-
@patch("opentelemetry.instrumentation.kafka.distribution")
80-
def test_instrumentation_dependencies_both_installed(
81-
self, mock_distribution
82-
) -> None:
83-
instrumentation = KafkaInstrumentor()
84-
85-
def _distribution(name):
86-
# The function returns None here for all names
87-
# to simulate both packages being installed
88-
return None
89-
90-
mock_distribution.side_effect = _distribution
91-
package_to_instrument = instrumentation.instrumentation_dependencies()
92-
93-
self.assertEqual(mock_distribution.call_count, 1)
94-
self.assertEqual(
95-
mock_distribution.mock_calls, [call("kafka-python-ng")]
96-
)
97-
self.assertEqual(
98-
package_to_instrument, (_instruments_kafka_python_ng,)
99-
)
100-
10156
@patch("opentelemetry.instrumentation.kafka.distribution")
10257
def test_instrumentation_dependencies_none_installed(
10358
self, mock_distribution
@@ -122,7 +77,4 @@ def _distribution(name):
12277
call("kafka-python"),
12378
],
12479
)
125-
self.assertEqual(
126-
package_to_instrument,
127-
(_instruments_kafka_python, _instruments_kafka_python_ng),
128-
)
80+
self.assertEqual(package_to_instrument, (_instruments_kafka_python,))

tox.ini

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,8 @@ envlist =
415415
lint-instrumentation-aiokafka
416416

417417
; opentelemetry-instrumentation-kafka-python
418-
py3{10,11}-test-instrumentation-kafka-python
419-
py3{10,11,12,13,14}-test-instrumentation-kafka-pythonng
418+
py3{10,11,12,13,14}-test-instrumentation-kafka-python{0,1}
420419
pypy3-test-instrumentation-kafka-python
421-
pypy3-test-instrumentation-kafka-pythonng
422420
lint-instrumentation-kafka-python
423421

424422
; opentelemetry-instrumentation-confluent-kafka
@@ -539,10 +537,8 @@ deps =
539537
aiokafka: -r {toxinidir}/instrumentation/opentelemetry-instrumentation-aiokafka/test-requirements.txt
540538

541539
kafka-python: {[testenv]test_deps}
542-
kafka-python: -r {toxinidir}/instrumentation/opentelemetry-instrumentation-kafka-python/test-requirements.txt
543-
544-
kafka-pythonng: {[testenv]test_deps}
545-
kafka-pythonng: -r {toxinidir}/instrumentation/opentelemetry-instrumentation-kafka-python/test-requirements-ng.txt
540+
kafka-python-0: -r {toxinidir}/instrumentation/opentelemetry-instrumentation-kafka-python/test-requirements-0.txt
541+
kafka-python-1: -r {toxinidir}/instrumentation/opentelemetry-instrumentation-kafka-python/test-requirements-1.txt
546542

547543
confluent-kafka: {[testenv]test_deps}
548544
confluent-kafka: -r {toxinidir}/instrumentation/opentelemetry-instrumentation-confluent-kafka/test-requirements.txt
@@ -889,9 +885,6 @@ commands =
889885
test-instrumentation-kafka-python: pytest {toxinidir}/instrumentation/opentelemetry-instrumentation-kafka-python/tests {posargs}
890886
lint-instrumentation-kafka-python: sh -c "cd instrumentation && pylint --rcfile ../.pylintrc opentelemetry-instrumentation-kafka-python"
891887

892-
; Test only for kafka-pythonng instrumentation as the only difference between kafka-python and kafka-pythonng is the version of kafka-python
893-
test-instrumentation-kafka-pythonng: pytest {toxinidir}/instrumentation/opentelemetry-instrumentation-kafka-python/tests {posargs}
894-
895888
test-instrumentation-confluent-kafka: pytest {toxinidir}/instrumentation/opentelemetry-instrumentation-confluent-kafka/tests {posargs}
896889
lint-instrumentation-confluent-kafka: sh -c "cd instrumentation && pylint --rcfile ../.pylintrc opentelemetry-instrumentation-confluent-kafka"
897890

uv.lock

Lines changed: 4 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)