12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ import pytest
15
16
16
17
from _target_application import add , tsum
17
- from celery import chain , chord , group
18
+ from testing_support .validators .validate_code_level_metrics import (
19
+ validate_code_level_metrics ,
20
+ )
18
21
from testing_support .validators .validate_transaction_count import (
19
22
validate_transaction_count ,
20
23
)
21
24
from testing_support .validators .validate_transaction_metrics import (
22
25
validate_transaction_metrics ,
23
26
)
24
27
25
- FORGONE_TASK_METRICS = [( "Function/_target_application.add" , None ), ( "Function/_target_application.tsum" , None )]
28
+ import celery
26
29
27
30
28
- def test_task_wrapping_detection ():
29
- """
30
- Ensure celery detects our monkeypatching properly and will run our instrumentation
31
- on __call__ and runs that instead of micro-optimizing it away to a run() call.
31
+ FORGONE_TASK_METRICS = [("Function/_target_application.add" , None ), ("Function/_target_application.tsum" , None )]
32
32
33
- If this is not working, most other tests in this file will fail as the different ways
34
- of running celery tasks will not all run our instrumentation.
35
- """
36
- from celery .app .trace import task_has_custom
37
33
38
- assert task_has_custom (add , "__call__" )
34
+ @pytest .fixture (scope = "module" , autouse = True , params = [False , True ], ids = ["unpatched" , "patched" ])
35
+ def with_worker_optimizations (request , celery_worker_available ):
36
+ if request .param :
37
+ celery .app .trace .setup_worker_optimizations (celery_worker_available .app )
38
+
39
+ yield request .param
40
+ celery .app .trace .reset_worker_optimizations ()
39
41
40
42
41
43
@validate_transaction_metrics (
@@ -45,6 +47,7 @@ def test_task_wrapping_detection():
45
47
rollup_metrics = FORGONE_TASK_METRICS ,
46
48
background_task = True ,
47
49
)
50
+ @validate_code_level_metrics ("_target_application" , "add" )
48
51
@validate_transaction_count (1 )
49
52
def test_celery_task_call ():
50
53
"""
@@ -61,6 +64,7 @@ def test_celery_task_call():
61
64
rollup_metrics = FORGONE_TASK_METRICS ,
62
65
background_task = True ,
63
66
)
67
+ @validate_code_level_metrics ("_target_application" , "add" )
64
68
@validate_transaction_count (1 )
65
69
def test_celery_task_apply ():
66
70
"""
@@ -78,6 +82,7 @@ def test_celery_task_apply():
78
82
rollup_metrics = FORGONE_TASK_METRICS ,
79
83
background_task = True ,
80
84
)
85
+ @validate_code_level_metrics ("_target_application" , "add" )
81
86
@validate_transaction_count (1 )
82
87
def test_celery_task_delay ():
83
88
"""
@@ -95,6 +100,7 @@ def test_celery_task_delay():
95
100
rollup_metrics = FORGONE_TASK_METRICS ,
96
101
background_task = True ,
97
102
)
103
+ @validate_code_level_metrics ("_target_application" , "add" )
98
104
@validate_transaction_count (1 )
99
105
def test_celery_task_apply_async ():
100
106
"""
@@ -112,6 +118,7 @@ def test_celery_task_apply_async():
112
118
rollup_metrics = FORGONE_TASK_METRICS ,
113
119
background_task = True ,
114
120
)
121
+ @validate_code_level_metrics ("_target_application" , "add" )
115
122
@validate_transaction_count (1 )
116
123
def test_celery_app_send_task (celery_session_app ):
117
124
"""
@@ -129,6 +136,7 @@ def test_celery_app_send_task(celery_session_app):
129
136
rollup_metrics = FORGONE_TASK_METRICS ,
130
137
background_task = True ,
131
138
)
139
+ @validate_code_level_metrics ("_target_application" , "add" )
132
140
@validate_transaction_count (1 )
133
141
def test_celery_task_signature ():
134
142
"""
@@ -154,6 +162,8 @@ def test_celery_task_signature():
154
162
background_task = True ,
155
163
index = - 2 ,
156
164
)
165
+ @validate_code_level_metrics ("_target_application" , "add" )
166
+ @validate_code_level_metrics ("_target_application" , "add" , index = - 2 )
157
167
@validate_transaction_count (2 )
158
168
def test_celery_task_link ():
159
169
"""
@@ -179,12 +189,14 @@ def test_celery_task_link():
179
189
background_task = True ,
180
190
index = - 2 ,
181
191
)
192
+ @validate_code_level_metrics ("_target_application" , "add" )
193
+ @validate_code_level_metrics ("_target_application" , "add" , index = - 2 )
182
194
@validate_transaction_count (2 )
183
195
def test_celery_chain ():
184
196
"""
185
197
Executes multiple tasks on worker process and returns an AsyncResult.
186
198
"""
187
- result = chain (add .s (3 , 4 ), add .s (5 ))()
199
+ result = celery . chain (add .s (3 , 4 ), add .s (5 ))()
188
200
189
201
result = result .get ()
190
202
assert result == 12
@@ -205,12 +217,14 @@ def test_celery_chain():
205
217
background_task = True ,
206
218
index = - 2 ,
207
219
)
220
+ @validate_code_level_metrics ("_target_application" , "add" )
221
+ @validate_code_level_metrics ("_target_application" , "add" , index = - 2 )
208
222
@validate_transaction_count (2 )
209
223
def test_celery_group ():
210
224
"""
211
225
Executes multiple tasks on worker process and returns an AsyncResult.
212
226
"""
213
- result = group (add .s (3 , 4 ), add .s (1 , 2 ))()
227
+ result = celery . group (add .s (3 , 4 ), add .s (1 , 2 ))()
214
228
result = result .get ()
215
229
assert result == [7 , 3 ]
216
230
@@ -238,12 +252,15 @@ def test_celery_group():
238
252
background_task = True ,
239
253
index = - 3 ,
240
254
)
255
+ @validate_code_level_metrics ("_target_application" , "tsum" )
256
+ @validate_code_level_metrics ("_target_application" , "add" , index = - 2 )
257
+ @validate_code_level_metrics ("_target_application" , "add" , index = - 3 )
241
258
@validate_transaction_count (3 )
242
259
def test_celery_chord ():
243
260
"""
244
261
Executes 2 add tasks, followed by a tsum task on the worker process and returns an AsyncResult.
245
262
"""
246
- result = chord ([add .s (3 , 4 ), add .s (1 , 2 )])(tsum .s ())
263
+ result = celery . chord ([add .s (3 , 4 ), add .s (1 , 2 )])(tsum .s ())
247
264
result = result .get ()
248
265
assert result == 10
249
266
@@ -255,6 +272,7 @@ def test_celery_chord():
255
272
rollup_metrics = [("Function/_target_application.tsum" , 2 )],
256
273
background_task = True ,
257
274
)
275
+ @validate_code_level_metrics ("_target_application" , "tsum" , count = 3 )
258
276
@validate_transaction_count (1 )
259
277
def test_celery_task_map ():
260
278
"""
@@ -272,6 +290,7 @@ def test_celery_task_map():
272
290
rollup_metrics = [("Function/_target_application.add" , 2 )],
273
291
background_task = True ,
274
292
)
293
+ @validate_code_level_metrics ("_target_application" , "add" , count = 3 )
275
294
@validate_transaction_count (1 )
276
295
def test_celery_task_starmap ():
277
296
"""
@@ -297,6 +316,8 @@ def test_celery_task_starmap():
297
316
background_task = True ,
298
317
index = - 2 ,
299
318
)
319
+ @validate_code_level_metrics ("_target_application" , "add" , count = 2 )
320
+ @validate_code_level_metrics ("_target_application" , "add" , count = 2 , index = - 2 )
300
321
@validate_transaction_count (2 )
301
322
def test_celery_task_chunks ():
302
323
"""
0 commit comments