|
| 1 | +--TEST-- |
| 2 | +RASP duration_ext metric accumulates across multiple calls in user requests |
| 3 | +--INI-- |
| 4 | +extension=ddtrace.so |
| 5 | +datadog.appsec.enabled=true |
| 6 | +datadog.appsec.cli_start_on_rinit=false |
| 7 | +--ENV-- |
| 8 | +DD_TRACE_GENERATE_ROOT_SPAN=0 |
| 9 | +--FILE-- |
| 10 | +<?php |
| 11 | + |
| 12 | +use function DDTrace\UserRequest\{notify_start,notify_commit}; |
| 13 | +use function DDTrace\{start_span,close_span,active_span}; |
| 14 | +use function datadog\appsec\push_addresses; |
| 15 | + |
| 16 | +include __DIR__ . '/inc/mock_helper.php'; |
| 17 | + |
| 18 | +define('NUM_CALLS', 20); |
| 19 | + |
| 20 | +$resps = array_merge( |
| 21 | + array(response_list(response_request_init([[['ok', []]], [], []]))), |
| 22 | + array_fill(0, NUM_CALLS, |
| 23 | + response_list(response_request_exec([[['ok', []]], [], [], [], false]))), |
| 24 | + array( |
| 25 | + response_list(response_request_shutdown([[['ok', []]], [], []])), |
| 26 | + |
| 27 | + // Second user request |
| 28 | + response_list(response_request_init([[['ok', []]], [], []])), |
| 29 | + response_list(response_request_exec([[['ok', []]], [], [], [], false])), |
| 30 | + response_list(response_request_shutdown([[['ok', []]], [], []])), |
| 31 | + ) |
| 32 | +); |
| 33 | + |
| 34 | +$helper = Helper::createinitedRun($resps); |
| 35 | + |
| 36 | +echo "=== First user request ===\n"; |
| 37 | +$span1 = start_span(); |
| 38 | +notify_start($span1, array()); |
| 39 | + |
| 40 | +for ($i = 0; $i < NUM_CALLS; $i++) { |
| 41 | + push_addresses(["server.request.path_params" => "test1"], "lfi"); |
| 42 | +} |
| 43 | + |
| 44 | +notify_commit($span1, 200, array()); |
| 45 | + |
| 46 | +// Get metrics for first request |
| 47 | +$metrics1 = $span1->metrics ?? []; |
| 48 | +echo "First request has duration_ext: " . (isset($metrics1['_dd.appsec.rasp.duration_ext']) ? "yes" : "no") . "\n"; |
| 49 | +if (isset($metrics1['_dd.appsec.rasp.duration_ext'])) { |
| 50 | + $duration1 = $metrics1['_dd.appsec.rasp.duration_ext']; |
| 51 | + echo "First request duration_ext is positive: " . ($duration1 > 0 ? "yes" : "no") . "\n"; |
| 52 | +} |
| 53 | + |
| 54 | +close_span(100.0); |
| 55 | + |
| 56 | +echo "\n=== Second user request ===\n"; |
| 57 | +$span2 = start_span(); |
| 58 | +notify_start($span2, array()); |
| 59 | + |
| 60 | +push_addresses(["server.request.body" => "test3"], "lfi"); |
| 61 | + |
| 62 | +notify_commit($span2, 200, array()); |
| 63 | + |
| 64 | +// Get metrics for second request |
| 65 | +$metrics2 = $span2->metrics ?? []; |
| 66 | +echo "Second request has duration_ext: " . (isset($metrics2['_dd.appsec.rasp.duration_ext']) ? "yes" : "no") . "\n"; |
| 67 | +if (isset($metrics2['_dd.appsec.rasp.duration_ext'])) { |
| 68 | + $duration2 = $metrics2['_dd.appsec.rasp.duration_ext']; |
| 69 | + echo "Second request duration_ext is positive: " . ($duration2 > 0 ? "yes" : "no") . "\n"; |
| 70 | +} |
| 71 | + |
| 72 | +close_span(100.0); |
| 73 | + |
| 74 | +if (isset($duration1) && isset($duration2)) { |
| 75 | + echo "\nBoth requests have duration_ext metrics: yes\n"; |
| 76 | + if ($duration1 > $duration2) { |
| 77 | + echo "First request has a larger duration_ext: yes\n"; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +?> |
| 83 | +--EXPECTF-- |
| 84 | +=== First user request === |
| 85 | +First request has duration_ext: yes |
| 86 | +First request duration_ext is positive: yes |
| 87 | + |
| 88 | +=== Second user request === |
| 89 | +Second request has duration_ext: yes |
| 90 | +Second request duration_ext is positive: yes |
| 91 | + |
| 92 | +Both requests have duration_ext metrics: yes |
| 93 | +First request has a larger duration_ext: yes |
0 commit comments