Skip to content

Commit a330873

Browse files
hmstepanekTimPansinomergify[bot]
authored
Fix kombu crash when using sentry & setting parsing (#1359)
* Fix kombu crash when using sentry * Add instrumentation settings into process config function * Add test for kombu version from 2022 * Add try-except around bind args & test * [MegaLinter] Apply linters fixes * Update newrelic/hooks/messagebroker_kombu.py * [MegaLinter] Apply linters fixes * Bump tests * Fixup failing test * Remove testing of 3.11+ on kombu 5.2.4 --------- Co-authored-by: hmstepanek <[email protected]> Co-authored-by: Timothy Pansino <[email protected]> Co-authored-by: Tim Pansino <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 911aeee commit a330873

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
lines changed

newrelic/config.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,10 @@ def _process_configuration(section):
504504
_process_setting(section, "azure_operator.enabled", "getboolean", None)
505505
_process_setting(section, "package_reporting.enabled", "getboolean", None)
506506
_process_setting(section, "instrumentation.graphql.capture_introspection_queries", "getboolean", None)
507+
_process_setting(
508+
section, "instrumentation.kombu.ignored_exchanges", "get", newrelic.core.config.parse_space_separated_into_list
509+
)
510+
_process_setting(section, "instrumentation.kombu.consumer.enabled", "getboolean", None)
507511

508512

509513
# Loading of configuration from specified file and for specified
@@ -2847,12 +2851,10 @@ def _process_module_builtin_defaults():
28472851
_process_module_definition(
28482852
"kafka.coordinator.heartbeat", "newrelic.hooks.messagebroker_kafkapython", "instrument_kafka_heartbeat"
28492853
)
2850-
# Kombu instrumentation is causing crashes so until we figure out the root cause
2851-
# comment it out.
2852-
# _process_module_definition("kombu.messaging", "newrelic.hooks.messagebroker_kombu", "instrument_kombu_messaging")
2853-
# _process_module_definition(
2854-
# "kombu.serialization", "newrelic.hooks.messagebroker_kombu", "instrument_kombu_serializaion"
2855-
# )
2854+
_process_module_definition("kombu.messaging", "newrelic.hooks.messagebroker_kombu", "instrument_kombu_messaging")
2855+
_process_module_definition(
2856+
"kombu.serialization", "newrelic.hooks.messagebroker_kombu", "instrument_kombu_serializaion"
2857+
)
28562858
_process_module_definition("logging", "newrelic.hooks.logger_logging", "instrument_logging")
28572859

28582860
_process_module_definition("loguru", "newrelic.hooks.logger_loguru", "instrument_loguru")

newrelic/hooks/messagebroker_kombu.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,65 @@
4242
}
4343

4444

45+
def bind_publish(
46+
body,
47+
routing_key=None,
48+
delivery_mode=None,
49+
mandatory=False,
50+
immediate=False,
51+
priority=0,
52+
content_type=None,
53+
content_encoding=None,
54+
serializer=None,
55+
headers=None,
56+
compression=None,
57+
exchange=None,
58+
retry=False,
59+
retry_policy=None,
60+
declare=None,
61+
expiration=None,
62+
timeout=None,
63+
confirm_timeout=None,
64+
**properties,
65+
):
66+
return {
67+
"body": body,
68+
"routing_key": routing_key,
69+
"delivery_mode": delivery_mode,
70+
"mandatory": mandatory,
71+
"immediate": immediate,
72+
"priority": priority,
73+
"content_type": content_type,
74+
"content_encoding": content_encoding,
75+
"serializer": serializer,
76+
"headers": headers,
77+
"compression": compression,
78+
"exchange": exchange,
79+
"retry": retry,
80+
"retry_policy": retry_policy,
81+
"declare": declare,
82+
"expiration": expiration,
83+
"timeout": timeout,
84+
"confirm_timeout": confirm_timeout,
85+
"properties": properties,
86+
}
87+
88+
4589
def wrap_Producer_publish(wrapped, instance, args, kwargs):
4690
transaction = current_transaction()
4791

4892
if transaction is None:
4993
return wrapped(*args, **kwargs)
5094

51-
bound_args = bind_args(wrapped, args, kwargs)
95+
try:
96+
bound_args = bind_publish(*args, **kwargs)
97+
except Exception:
98+
_logger.debug(
99+
"Unable to bind arguments for kombu.messaging.Producer.publish. Report this issue to New Relic support.",
100+
record_exception=True,
101+
)
102+
return wrapped(*args, **kwargs)
103+
52104
headers = bound_args["headers"]
53105
headers = headers if headers else {}
54106
value = bound_args["body"]

tests/messagebroker_kombu/test_producer.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,14 @@ def test():
9494
producer.publish({"foo": object()}, exchange=exchange, routing_key="bar", declare=[queue])
9595

9696
test()
97+
98+
99+
def test_producer_tries_to_parse_args(exchange, producer, queue, monkeypatch):
100+
@background_task()
101+
def test():
102+
with pytest.raises(TypeError):
103+
producer.publish(
104+
{"foo": object()}, body={"foo": object()}, exchange=exchange, routing_key="bar", declare=[queue]
105+
)
106+
107+
test()

tox.ini

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ envlist =
165165
python-template_jinja2-py37-jinja2030103,
166166
python-template_mako-{py37,py38,py39,py310,py311,py312,py313},
167167
rabbitmq-messagebroker_pika-{py37,py38,py39,py310,py311,py312,py313,pypy310}-pikalatest,
168-
;; Comment out Kombu until we can root cause the crash.
169-
; rabbitmq-messagebroker_kombu-{py38,py39,py310,py311,py312,py313,pypy310}-kombulatest,
168+
rabbitmq-messagebroker_kombu-{py38,py39,py310,py311,py312,py313,pypy310}-kombulatest,
169+
rabbitmq-messagebroker_kombu-{py38,py39,py310,pypy310}-kombu050204,
170170
redis-datastore_redis-{py37,py311,pypy310}-redis04,
171171
redis-datastore_redis-{py37,py38,py39,py310,py311,py312,py313,pypy310}-redislatest,
172172
rediscluster-datastore_rediscluster-{py37,py312,py313,pypy310}-redislatest,
@@ -418,6 +418,7 @@ deps =
418418
messagebroker_confluentkafka-confluentkafka0106: confluent-kafka<1.7
419419
messagebroker_kafkapython-kafkapythonnglatest: kafka-python-ng
420420
messagebroker_kombu-kombulatest: kombu
421+
messagebroker_kombu-kombu050204: kombu<5.3.0
421422
# TODO: Pinned to 2.0 for now, fix tests later
422423
messagebroker_kafkapython-kafkapythonlatest: kafka-python<2.1
423424
template_genshi-genshilatest: genshi

0 commit comments

Comments
 (0)