1111# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212# See the License for the specific language governing permissions and
1313# limitations under the License.
14+ from importlib .metadata import PackageNotFoundError
1415from unittest import TestCase
16+ from unittest .mock import patch , call
1517
1618from kafka import KafkaConsumer , KafkaProducer
1719from wrapt import BoundFunctionWrapper
1820
1921from opentelemetry .instrumentation .kafka import KafkaInstrumentor
20-
22+ from opentelemetry .instrumentation .kafka .package import (
23+ _instruments ,
24+ _instruments_kafka_python ,
25+ _instruments_kafka_python_ng ,
26+ )
2127
2228class TestKafka (TestCase ):
2329 def test_instrument_api (self ) -> None :
@@ -34,3 +40,80 @@ def test_instrument_api(self) -> None:
3440 self .assertFalse (
3541 isinstance (KafkaConsumer .__next__ , BoundFunctionWrapper )
3642 )
43+
44+ @patch ("opentelemetry.instrumentation.kafka.distribution" )
45+ def test_instrumentation_dependencies_kafka_python_installed (self , mock_distribution ) -> None :
46+ instrumentation = KafkaInstrumentor ()
47+
48+ def _distribution (name ):
49+ if name == "kafka-python" :
50+ return None
51+ raise PackageNotFoundError
52+
53+ mock_distribution .side_effect = _distribution
54+ package_to_instrument = instrumentation .instrumentation_dependencies ()
55+
56+ assert mock_distribution .call_count == 2
57+ assert mock_distribution .mock_calls == [
58+ call ("kafka-python-ng" ),
59+ call ("kafka-python" ),
60+ ]
61+ assert package_to_instrument == (_instruments_kafka_python ,)
62+
63+ @patch ("opentelemetry.instrumentation.kafka.distribution" )
64+ def test_instrumentation_dependencies_kafka_python_ng_installed (self , mock_distribution ) -> None :
65+ instrumentation = KafkaInstrumentor ()
66+
67+ def _distribution (name ):
68+ if name == "kafka-python-ng" :
69+ return None
70+ raise PackageNotFoundError
71+
72+ mock_distribution .side_effect = _distribution
73+ package_to_instrument = instrumentation .instrumentation_dependencies ()
74+
75+ assert mock_distribution .call_count == 1
76+ assert mock_distribution .mock_calls == [
77+ call ("kafka-python-ng" )
78+ ]
79+ assert package_to_instrument == (_instruments_kafka_python_ng ,)
80+
81+ @patch ("opentelemetry.instrumentation.kafka.distribution" )
82+ def test_instrumentation_dependencies_both_installed (self , mock_distribution ) -> None :
83+ instrumentation = KafkaInstrumentor ()
84+
85+ def _distribution (name ):
86+ # Function raises PackageNotFoundError
87+ # if name is not in the list. We will
88+ # not raise it for both names
89+ return None
90+
91+ mock_distribution .side_effect = _distribution
92+ package_to_instrument = instrumentation .instrumentation_dependencies ()
93+
94+ assert mock_distribution .call_count == 1
95+ assert mock_distribution .mock_calls == [
96+ call ("kafka-python-ng" )
97+ ]
98+ assert package_to_instrument == (_instruments_kafka_python_ng ,)
99+
100+ @patch ("opentelemetry.instrumentation.kafka.distribution" )
101+ def test_instrumentation_dependencies_none_installed (self , mock_distribution ) -> None :
102+ instrumentation = KafkaInstrumentor ()
103+
104+ def _distribution (name ):
105+ # Function raises PackageNotFoundError
106+ # if name is not in the list. We will
107+ # raise it for both names to simulate
108+ # neither being installed
109+ raise PackageNotFoundError
110+
111+ mock_distribution .side_effect = _distribution
112+ package_to_instrument = instrumentation .instrumentation_dependencies ()
113+
114+ assert mock_distribution .call_count == 2
115+ assert mock_distribution .mock_calls == [
116+ call ("kafka-python-ng" ),
117+ call ("kafka-python" ),
118+ ]
119+ assert package_to_instrument == _instruments
0 commit comments