@@ -95,6 +95,10 @@ def wrap_task_call(wrapped, instance, args, kwargs):
95
95
96
96
elif transaction :
97
97
with FunctionTrace (_name , source = _source ):
98
+ # Do we accept distributed tracing headers here?
99
+ # request = wrapped.request
100
+ # headers = getattr(request, "headers", None) or vars(request)
101
+ # transaction.accept_distributed_trace_headers(headers, transport_type="AMQP")
98
102
return wrapped (* args , ** kwargs )
99
103
100
104
else :
@@ -104,14 +108,28 @@ def wrap_task_call(wrapped, instance, args, kwargs):
104
108
# Headers on earlier versions of Celery may end up as attributes
105
109
# on the request context instead of as custom headers. Handler this
106
110
# by defaulting to using vars() if headers is not available
107
- # print(f"wrapped.request: {wrapped.request}")
108
111
request = wrapped .request
109
112
headers = getattr (request , "headers" , None ) or vars (request )
110
113
111
114
settings = transaction .settings
112
115
if headers is not None and settings is not None :
113
116
if settings .distributed_tracing .enabled :
114
- transaction .accept_distributed_trace_headers (headers , transport_type = "AMQP" )
117
+ if not transaction .accept_distributed_trace_headers (headers , transport_type = "AMQP" ):
118
+ try :
119
+ dt_headers = MessageTrace .generate_request_headers (transaction )
120
+ # original_headers = kwargs.get("headers", None)
121
+ if dt_headers :
122
+ if not headers :
123
+ wrapped .request .headers = dict (dt_headers )
124
+ # kwargs["headers"] = dict(dt_headers)
125
+ else :
126
+ headers .update (dict (dt_headers ))
127
+ wrapped .request .headers = headers
128
+ # wrapped.request.headers.update(dict(dt_headers))
129
+ # kwargs["headers"] = dt_headers = dict(dt_headers)
130
+ # dt_headers.update(dict(headers))
131
+ except Exception :
132
+ pass
115
133
elif transaction .settings .cross_application_tracer .enabled :
116
134
transaction ._process_incoming_cat_headers (
117
135
headers .get (MessageTrace .cat_id_key , None ),
@@ -133,6 +151,7 @@ def run(self, *args, **kwargs):
133
151
task = bound_args .get ("task" , None )
134
152
135
153
task = TaskWrapper (task , wrap_task_call )
154
+ task .__module__ = wrapped .__module__ # Ensure module is set for monkeypatching detection
136
155
bound_args ["task" ] = task
137
156
138
157
return wrapped (** bound_args )
@@ -189,7 +208,17 @@ def wrapper(wrapped, instance, args, kwargs):
189
208
settings = transaction .settings
190
209
if headers is not None and settings is not None :
191
210
if settings .distributed_tracing .enabled :
192
- transaction .accept_distributed_trace_headers (headers , transport_type = "AMQP" )
211
+ if not transaction .accept_distributed_trace_headers (headers , transport_type = "AMQP" ):
212
+ try :
213
+ dt_headers = MessageTrace .generate_request_headers (transaction )
214
+ if dt_headers :
215
+ if not headers :
216
+ instance .request .headers = dict (dt_headers )
217
+ else :
218
+ headers .update (dict (dt_headers ))
219
+ instance .request .headers = headers
220
+ except Exception :
221
+ pass
193
222
elif transaction .settings .cross_application_tracer .enabled :
194
223
transaction ._process_incoming_cat_headers (
195
224
headers .get (MessageTrace .cat_id_key , None ),
@@ -237,29 +266,29 @@ def run(self, *args, **kwargs):
237
266
return wrapped_task
238
267
239
268
240
- # This will not work with the current version of Celery
241
- # This only gets called during the async execution of a task
242
- # and the task is wrapped later in the process to accomodate
243
- # custom task classes.
244
- def wrap_Celery_send_task (wrapped , instance , args , kwargs ):
245
- transaction = current_transaction ()
246
- if not transaction :
247
- return wrapped (* args , ** kwargs )
248
-
249
- # Merge distributed tracing headers into outgoing task headers
250
- try :
251
- dt_headers = MessageTrace .generate_request_headers (transaction )
252
- original_headers = kwargs .get ("headers" , None )
253
- if dt_headers :
254
- if not original_headers :
255
- kwargs ["headers" ] = dict (dt_headers )
256
- else :
257
- kwargs ["headers" ] = dt_headers = dict (dt_headers )
258
- dt_headers .update (dict (original_headers ))
259
- except Exception :
260
- pass
261
-
262
- return wrapped (* args , ** kwargs )
269
+ # # This will not work with the current version of Celery
270
+ # # This only gets called during the async execution of a task
271
+ # # and the task is wrapped later in the process to accomodate
272
+ # # custom task classes.
273
+ # def wrap_Celery_send_task(wrapped, instance, args, kwargs):
274
+ # transaction = current_transaction()
275
+ # if not transaction:
276
+ # return wrapped(*args, **kwargs)
277
+
278
+ # # Merge distributed tracing headers into outgoing task headers
279
+ # try:
280
+ # dt_headers = MessageTrace.generate_request_headers(transaction)
281
+ # original_headers = kwargs.get("headers", None)
282
+ # if dt_headers:
283
+ # if not original_headers:
284
+ # kwargs["headers"] = dict(dt_headers)
285
+ # else:
286
+ # kwargs["headers"] = dt_headers = dict(dt_headers)
287
+ # dt_headers.update(dict(original_headers))
288
+ # except Exception:
289
+ # pass
290
+
291
+ # return wrapped(*args, **kwargs)
263
292
264
293
265
294
def wrap_worker_optimizations (wrapped , instance , args , kwargs ):
@@ -290,9 +319,9 @@ def instrument_celery_local(module):
290
319
module .Proxy .__call__ = CeleryTaskWrapper (module .Proxy .__call__ )
291
320
292
321
293
- def instrument_celery_app_base (module ):
294
- if hasattr (module , "Celery" ) and hasattr (module .Celery , "send_task" ):
295
- wrap_function_wrapper (module , "Celery.send_task" , wrap_Celery_send_task )
322
+ # def instrument_celery_app_base(module):
323
+ # if hasattr(module, "Celery") and hasattr(module.Celery, "send_task"):
324
+ # wrap_function_wrapper(module, "Celery.send_task", wrap_Celery_send_task)
296
325
297
326
298
327
def instrument_celery_worker (module ):
@@ -347,4 +376,5 @@ def instrument_celery_app_trace(module):
347
376
348
377
if hasattr (module , "build_tracer" ):
349
378
wrap_function_wrapper (module , "build_tracer" , wrap_build_tracer )
379
+
350
380
0 commit comments