|
| 1 | +package com.datadog.appsec.php.integration |
| 2 | + |
| 3 | +import com.datadog.appsec.php.TelemetryHelpers |
| 4 | +import com.datadog.appsec.php.TelemetryHelpers.Metric |
| 5 | +import com.datadog.appsec.php.docker.AppSecContainer |
| 6 | +import com.datadog.appsec.php.docker.FailOnUnmatchedTraces |
| 7 | +import org.junit.jupiter.api.Test |
| 8 | +import org.junit.jupiter.api.condition.DisabledIf |
| 9 | +import org.testcontainers.containers.Container.ExecResult |
| 10 | +import org.testcontainers.junit.jupiter.Container |
| 11 | +import org.testcontainers.junit.jupiter.Testcontainers |
| 12 | + |
| 13 | +import static com.datadog.appsec.php.TelemetryHelpers.BGS_SERVICE |
| 14 | +import static com.datadog.appsec.php.integration.TestParams.getPhpVersion |
| 15 | +import static com.datadog.appsec.php.integration.TestParams.getVariant |
| 16 | + |
| 17 | +/** |
| 18 | + * What the background sender does while its process goes away: submit the counters it has |
| 19 | + * accumulated since the last flush from MSHUTDOWN (see ddtrace_mshutdown()), while the sidecar |
| 20 | + * can still address the application. |
| 21 | + * |
| 22 | + * A single-request CLI process isolates the process-exit path: its trace is queued before |
| 23 | + * telemetry finalize, and the sender is synchronously drained later in MSHUTDOWN. The FPM |
| 24 | + * workers, by contrast, are killed abruptly at the end of a run and never reach MSHUTDOWN. |
| 25 | + * |
| 26 | + * No request is ever served in this container. Since the background sender's application is |
| 27 | + * synthetic and shared by every process (see {@link TelemetryBackgroundSenderTests}), request |
| 28 | + * traffic would make any {@code trace_api} point observed here unattributable — that is what |
| 29 | + * keeps this apart from the request-path class, which has a container of its own. |
| 30 | + */ |
| 31 | +@Testcontainers |
| 32 | +@DisabledIf('isDisabled') |
| 33 | +class TelemetryBackgroundSenderShutdownTests { |
| 34 | + static boolean disabled = phpVersion != '8.2' |
| 35 | + |
| 36 | + @Container |
| 37 | + @FailOnUnmatchedTraces |
| 38 | + public static final AppSecContainer CONTAINER = |
| 39 | + new AppSecContainer( |
| 40 | + workVolume: this.name, |
| 41 | + baseTag: 'apache2-fpm-php', |
| 42 | + phpVersion: phpVersion, |
| 43 | + phpVariant: variant, |
| 44 | + www: 'base', |
| 45 | + ) |
| 46 | + |
| 47 | + private static final String FLUSH_PROBE_SERVICE = 'bgs_flush_probe' |
| 48 | + private static final long METRICS_WAIT_TIMEOUT_MS = 30_000 |
| 49 | + private static final long METRICS_POLL_INTERVAL_MS = 500 |
| 50 | + |
| 51 | + /** |
| 52 | + * The process it starts is the only one in the container that can have produced a trace_api |
| 53 | + * point: the metrics carry no process identity, so anything already queued for the synthetic |
| 54 | + * service would satisfy the assertion below. |
| 55 | + * |
| 56 | + * The sidecar buffers the points in the telemetry worker for this service/env and emits them |
| 57 | + * on its next flush (DD_TELEMETRY_HEARTBEAT_INTERVAL, 10 s here), hence the generous wait. |
| 58 | + */ |
| 59 | + @Test |
| 60 | + void 'metrics accumulated during shutdown are submitted'() { |
| 61 | + ExecResult res = CONTAINER.execInContainer('sh', '-c', |
| 62 | + "DD_SERVICE=${FLUSH_PROBE_SERVICE} php -r 'usleep(300 * 1000);'; echo status=\$?".toString()) |
| 63 | + assert res.stdout.readLines().last() == 'status=0' : "${res.stdout}\n${res.stderr}" |
| 64 | + |
| 65 | + // consume the trace this generated, or @FailOnUnmatchedTraces trips |
| 66 | + assert CONTAINER.nextCapturedTrace() != null |
| 67 | + |
| 68 | + List<Metric> series = [] |
| 69 | + long deadline = System.currentTimeMillis() + METRICS_WAIT_TIMEOUT_MS |
| 70 | + while (!series.any { it.name == 'trace_api.requests' } && |
| 71 | + System.currentTimeMillis() < deadline) { |
| 72 | + series.addAll(TelemetryHelpers.drainMetricSeries(CONTAINER, BGS_SERVICE, 0)) |
| 73 | + if (!series.any { it.name == 'trace_api.requests' }) { |
| 74 | + long remaining = deadline - System.currentTimeMillis() |
| 75 | + if (remaining > 0) { |
| 76 | + Thread.sleep(Math.min(METRICS_POLL_INTERVAL_MS, remaining)) |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + Metric requests = series.find { it.name == 'trace_api.requests' } |
| 82 | + assert requests != null : "no trace_api.requests for ${BGS_SERVICE}; got ${series*.name}" |
| 83 | + assert requests.namespace == 'tracers' |
| 84 | + assert requests.points[0][1] >= 1.0 |
| 85 | + |
| 86 | + Metric responses = series.find { it.name == 'trace_api.responses' } |
| 87 | + assert responses != null : 'trace_api.responses not reported at shutdown' |
| 88 | + assert 'status_code:2xx' in responses.tags |
| 89 | + } |
| 90 | +} |
0 commit comments