Skip to content

Commit e296bd0

Browse files
committed
Ruff format
1 parent 1c7df3c commit e296bd0

File tree

4 files changed

+59
-188
lines changed

4 files changed

+59
-188
lines changed

newrelic/hooks/application_celery.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@
2828
from newrelic.api.message_trace import MessageTrace
2929
from newrelic.api.pre_function import wrap_pre_function
3030
from newrelic.api.transaction import current_transaction
31-
from newrelic.common.object_wrapper import FunctionWrapper, _NRBoundFunctionWrapper, wrap_function_wrapper
31+
from newrelic.common.object_wrapper import FunctionWrapper, wrap_function_wrapper
3232
from newrelic.common.signature import bind_args
3333
from newrelic.core.agent import shutdown_agent
3434

3535
UNKNOWN_TASK_NAME = "<Unknown Task>"
3636
MAPPING_TASK_NAMES = {"celery.starmap", "celery.map"}
3737

38+
3839
def task_info(instance, *args, **kwargs):
3940
# Grab the current task, which can be located in either place
4041
if instance:
@@ -129,22 +130,22 @@ def wrap_task_call(wrapped, instance, args, kwargs):
129130
except Exception:
130131
pass
131132

132-
return wrapped(*args, **kwargs)
133+
return wrapped(*args, **kwargs)
133134

134135

135136
def wrap_build_tracer(wrapped, instance, args, kwargs):
136137
class TaskWrapper(FunctionWrapper):
137138
def run(self, *args, **kwargs):
138139
return self.__call__(*args, **kwargs)
139-
140+
140141
try:
141142
bound_args = bind_args(wrapped, args, kwargs)
142143
task = bound_args.get("task", None)
143144

144145
task = TaskWrapper(task, wrap_task_call)
145146
task.__module__ = wrapped.__module__ # Ensure module is set for monkeypatching detection
146147
bound_args["task"] = task
147-
148+
148149
return wrapped(**bound_args)
149150
except:
150151
# If we can't bind the args, we just call the wrapped function
@@ -259,15 +260,15 @@ def run(self, *args, **kwargs):
259260

260261
def instrument_celery_local(module):
261262
if hasattr(module, "Proxy"):
262-
# This is used in the case where the function is
263-
# called directly on the Proxy object (rather than
263+
# This is used in the case where the function is
264+
# called directly on the Proxy object (rather than
264265
# using "delay" or "apply_async")
265266
module.Proxy.__call__ = CeleryTaskWrapper(module.Proxy.__call__)
266267

267268

268269
def instrument_celery_worker(module):
269270
if hasattr(module, "process_initializer"):
270-
# We try and force activation of the agent before
271+
# We try and force activation of the agent before
271272
# the worker process starts.
272273
_process_initializer = module.process_initializer
273274

@@ -277,8 +278,7 @@ def process_initializer(*args, **kwargs):
277278
return _process_initializer(*args, **kwargs)
278279

279280
module.process_initializer = process_initializer
280-
281-
281+
282282
if hasattr(module, "process_destructor"):
283283
# We try and force shutdown of the agent before
284284
# the worker process exits.
@@ -305,10 +305,8 @@ def force_agent_shutdown(*args, **kwargs):
305305

306306
if hasattr(module, "Worker") and hasattr(module.Worker, "_do_exit"):
307307
wrap_pre_function(module, "Worker._do_exit", force_agent_shutdown)
308-
308+
309309

310310
def instrument_celery_app_trace(module):
311311
if hasattr(module, "build_tracer"):
312312
wrap_function_wrapper(module, "build_tracer", wrap_build_tracer)
313-
314-

tests/application_celery/_target_application.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525
broker_heartbeat=0,
2626
)
2727

28+
2829
class CustomCeleryTaskWithSuper(Task):
2930
def __call__(self, *args, **kwargs):
3031
transaction = current_transaction()
3132
if transaction:
3233
transaction.add_custom_attribute("custom_task_attribute", "Called with super")
3334
return super().__call__(*args, **kwargs)
3435

36+
3537
class CustomCeleryTaskWithRun(Task):
3638
def __call__(self, *args, **kwargs):
3739
transaction = current_transaction()

tests/application_celery/test_distributed_tracing.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,17 @@ def test_celery_task_distributed_tracing_inside_background_task(dt_enabled):
4242
],
4343
background_task=True,
4444
)
45-
@validate_transaction_count(2) # One for the background task, one for the Celery task. Runs in different processes.
45+
@validate_transaction_count(2)
46+
# One for the background task, one for the Celery task. Runs in different processes.
4647
@background_task()
4748
def _test():
4849
result = add.apply_async((1, 2))
4950
result = result.get()
5051
assert result == 3
5152

5253
_test()
53-
54-
54+
55+
5556
@pytest.mark.parametrize("dt_enabled", [True, False])
5657
def test_celery_task_distributed_tracing_outside_background_task(dt_enabled):
5758
@override_application_settings({"distributed_tracing.enabled": dt_enabled})
@@ -71,10 +72,10 @@ def _test():
7172
assert result == 3
7273

7374
_test()
74-
7575

76-
# In this case, the background task creating the transaction
77-
# has not generated a distributed trace header, so the Celery
76+
77+
# In this case, the background task creating the transaction
78+
# has not generated a distributed trace header, so the Celery
7879
# task will not have a distributed trace header to accept.
7980
@pytest.mark.parametrize("dt_enabled", [True, False])
8081
def test_celery_task_distributed_tracing_inside_background_task_apply(dt_enabled):
@@ -91,8 +92,8 @@ def _test():
9192
assert result == 3
9293

9394
_test()
94-
95-
95+
96+
9697
@pytest.mark.parametrize("dt_enabled", [True, False])
9798
def test_celery_task_distributed_tracing_outside_background_task_apply(dt_enabled):
9899
@override_application_settings({"distributed_tracing.enabled": dt_enabled})
@@ -111,4 +112,4 @@ def _test():
111112
result = result.get()
112113
assert result == 3
113114

114-
_test()
115+
_test()

0 commit comments

Comments
 (0)