1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import pytest
1516
1617from _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+ )
1821from testing_support .validators .validate_transaction_count import (
1922 validate_transaction_count ,
2023)
2124from testing_support .validators .validate_transaction_metrics import (
2225 validate_transaction_metrics ,
2326)
2427
25- FORGONE_TASK_METRICS = [( "Function/_target_application.add" , None ), ( "Function/_target_application.tsum" , None )]
28+ import celery
2629
2730
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 )]
3232
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
3733
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 ()
3941
4042
4143@validate_transaction_metrics (
@@ -45,6 +47,7 @@ def test_task_wrapping_detection():
4547 rollup_metrics = FORGONE_TASK_METRICS ,
4648 background_task = True ,
4749)
50+ @validate_code_level_metrics ("_target_application" , "add" )
4851@validate_transaction_count (1 )
4952def test_celery_task_call ():
5053 """
@@ -61,6 +64,7 @@ def test_celery_task_call():
6164 rollup_metrics = FORGONE_TASK_METRICS ,
6265 background_task = True ,
6366)
67+ @validate_code_level_metrics ("_target_application" , "add" )
6468@validate_transaction_count (1 )
6569def test_celery_task_apply ():
6670 """
@@ -78,6 +82,7 @@ def test_celery_task_apply():
7882 rollup_metrics = FORGONE_TASK_METRICS ,
7983 background_task = True ,
8084)
85+ @validate_code_level_metrics ("_target_application" , "add" )
8186@validate_transaction_count (1 )
8287def test_celery_task_delay ():
8388 """
@@ -95,6 +100,7 @@ def test_celery_task_delay():
95100 rollup_metrics = FORGONE_TASK_METRICS ,
96101 background_task = True ,
97102)
103+ @validate_code_level_metrics ("_target_application" , "add" )
98104@validate_transaction_count (1 )
99105def test_celery_task_apply_async ():
100106 """
@@ -112,6 +118,7 @@ def test_celery_task_apply_async():
112118 rollup_metrics = FORGONE_TASK_METRICS ,
113119 background_task = True ,
114120)
121+ @validate_code_level_metrics ("_target_application" , "add" )
115122@validate_transaction_count (1 )
116123def test_celery_app_send_task (celery_session_app ):
117124 """
@@ -129,6 +136,7 @@ def test_celery_app_send_task(celery_session_app):
129136 rollup_metrics = FORGONE_TASK_METRICS ,
130137 background_task = True ,
131138)
139+ @validate_code_level_metrics ("_target_application" , "add" )
132140@validate_transaction_count (1 )
133141def test_celery_task_signature ():
134142 """
@@ -154,6 +162,8 @@ def test_celery_task_signature():
154162 background_task = True ,
155163 index = - 2 ,
156164)
165+ @validate_code_level_metrics ("_target_application" , "add" )
166+ @validate_code_level_metrics ("_target_application" , "add" , index = - 2 )
157167@validate_transaction_count (2 )
158168def test_celery_task_link ():
159169 """
@@ -179,12 +189,14 @@ def test_celery_task_link():
179189 background_task = True ,
180190 index = - 2 ,
181191)
192+ @validate_code_level_metrics ("_target_application" , "add" )
193+ @validate_code_level_metrics ("_target_application" , "add" , index = - 2 )
182194@validate_transaction_count (2 )
183195def test_celery_chain ():
184196 """
185197 Executes multiple tasks on worker process and returns an AsyncResult.
186198 """
187- result = chain (add .s (3 , 4 ), add .s (5 ))()
199+ result = celery . chain (add .s (3 , 4 ), add .s (5 ))()
188200
189201 result = result .get ()
190202 assert result == 12
@@ -205,12 +217,14 @@ def test_celery_chain():
205217 background_task = True ,
206218 index = - 2 ,
207219)
220+ @validate_code_level_metrics ("_target_application" , "add" )
221+ @validate_code_level_metrics ("_target_application" , "add" , index = - 2 )
208222@validate_transaction_count (2 )
209223def test_celery_group ():
210224 """
211225 Executes multiple tasks on worker process and returns an AsyncResult.
212226 """
213- result = group (add .s (3 , 4 ), add .s (1 , 2 ))()
227+ result = celery . group (add .s (3 , 4 ), add .s (1 , 2 ))()
214228 result = result .get ()
215229 assert result == [7 , 3 ]
216230
@@ -238,12 +252,15 @@ def test_celery_group():
238252 background_task = True ,
239253 index = - 3 ,
240254)
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 )
241258@validate_transaction_count (3 )
242259def test_celery_chord ():
243260 """
244261 Executes 2 add tasks, followed by a tsum task on the worker process and returns an AsyncResult.
245262 """
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 ())
247264 result = result .get ()
248265 assert result == 10
249266
@@ -255,6 +272,7 @@ def test_celery_chord():
255272 rollup_metrics = [("Function/_target_application.tsum" , 2 )],
256273 background_task = True ,
257274)
275+ @validate_code_level_metrics ("_target_application" , "tsum" , count = 3 )
258276@validate_transaction_count (1 )
259277def test_celery_task_map ():
260278 """
@@ -272,6 +290,7 @@ def test_celery_task_map():
272290 rollup_metrics = [("Function/_target_application.add" , 2 )],
273291 background_task = True ,
274292)
293+ @validate_code_level_metrics ("_target_application" , "add" , count = 3 )
275294@validate_transaction_count (1 )
276295def test_celery_task_starmap ():
277296 """
@@ -297,6 +316,8 @@ def test_celery_task_starmap():
297316 background_task = True ,
298317 index = - 2 ,
299318)
319+ @validate_code_level_metrics ("_target_application" , "add" , count = 2 )
320+ @validate_code_level_metrics ("_target_application" , "add" , count = 2 , index = - 2 )
300321@validate_transaction_count (2 )
301322def test_celery_task_chunks ():
302323 """
0 commit comments