Skip to content

Commit fee4fa8

Browse files
committed
Merge branch 'main' into add_more_pymongo_supported_versions
2 parents 1417dfc + ef7769c commit fee4fa8

File tree

3 files changed

+33
-21
lines changed
  • instrumentation/opentelemetry-instrumentation-kafka-python/src/opentelemetry/instrumentation/kafka
  • opentelemetry-instrumentation/src/opentelemetry/instrumentation

3 files changed

+33
-21
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4747

4848
- `opentelemetry-instrumentation-sqlite3` Instrumentation now works with `dbapi2.connect`
4949

50+
- `opentelemetry-instrumentation-kafka` Kafka: safe kafka partition extraction
51+
([#872](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/872))
52+
5053
## [1.8.0-0.27b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.8.0-0.27b0) - 2021-12-17
5154

5255
### Added

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

+29-20
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,36 @@ def extract_send_headers(args, kwargs):
5555
@staticmethod
5656
def extract_send_partition(instance, args, kwargs):
5757
"""extract partition `send` method arguments, using the `_partition` method in KafkaProducer class"""
58-
topic = KafkaPropertiesExtractor.extract_send_topic(args)
59-
key = KafkaPropertiesExtractor.extract_send_key(args, kwargs)
60-
value = KafkaPropertiesExtractor.extract_send_value(args, kwargs)
61-
partition = KafkaPropertiesExtractor._extract_argument(
62-
"partition", 4, None, args, kwargs
63-
)
64-
key_bytes = instance._serialize(
65-
instance.config["key_serializer"], topic, key
66-
)
67-
value_bytes = instance._serialize(
68-
instance.config["value_serializer"], topic, value
69-
)
70-
valid_types = (bytes, bytearray, memoryview, type(None))
71-
if (
72-
type(key_bytes) not in valid_types
73-
or type(value_bytes) not in valid_types
74-
):
58+
try:
59+
topic = KafkaPropertiesExtractor.extract_send_topic(args)
60+
key = KafkaPropertiesExtractor.extract_send_key(args, kwargs)
61+
value = KafkaPropertiesExtractor.extract_send_value(args, kwargs)
62+
partition = KafkaPropertiesExtractor._extract_argument(
63+
"partition", 4, None, args, kwargs
64+
)
65+
key_bytes = instance._serialize(
66+
instance.config["key_serializer"], topic, key
67+
)
68+
value_bytes = instance._serialize(
69+
instance.config["value_serializer"], topic, value
70+
)
71+
valid_types = (bytes, bytearray, memoryview, type(None))
72+
if (
73+
type(key_bytes) not in valid_types
74+
or type(value_bytes) not in valid_types
75+
):
76+
return None
77+
78+
all_partitions = instance._metadata.partitions_for_topic(topic)
79+
if all_partitions is None or len(all_partitions) == 0:
80+
return None
81+
82+
return instance._partition(
83+
topic, partition, key, value, key_bytes, value_bytes
84+
)
85+
except Exception as exception: # pylint: disable=W0703
86+
_LOG.debug("Unable to extract partition: %s", exception)
7587
return None
76-
return instance._partition(
77-
topic, partition, key, value, key_bytes, value_bytes
78-
)
7988

8089

8190
ProduceHookT = Optional[Callable[[Span, List, Dict], None]]

opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap_gen.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
"instrumentation": "opentelemetry-instrumentation-pymemcache==0.28b0",
9898
},
9999
"pymongo": {
100-
"library": "pymongo <= 3.1, < 5.0",
100+
"library": "pymongo >= 3.1, < 5.0",
101101
"instrumentation": "opentelemetry-instrumentation-pymongo==0.28b0",
102102
},
103103
"PyMySQL": {

0 commit comments

Comments
 (0)