diff --git a/e2eTest/project/templates.test.ts b/e2eTest/project/templates.test.ts index bd25a39b2..a03eafc66 100644 --- a/e2eTest/project/templates.test.ts +++ b/e2eTest/project/templates.test.ts @@ -5,7 +5,7 @@ import { existsSync } from "node:fs"; import { cp, mkdtemp, readdir, readFile, writeFile } from "node:fs/promises"; import { randomBytes } from "node:crypto"; import { tmpdir } from "node:os"; -import { join } from "node:path"; +import { basename, join } from "node:path"; import z from "zod"; import { E2E_PREFIX, TAGS } from "../constants"; import { CliRunner, parseResult, type RunResult } from "../helpers/run"; @@ -30,6 +30,14 @@ type RuntimeTemplateTestCase = { * concurrent Runtime tests skip it; the sequential dev tests below cover it. */ box?: boolean; + /** + * What of the agent's telemetry the test checks in CloudWatch after the deployed invoke + * (assertTelemetry): `"none"` for a template that sends none (the TypeScript templates have no + * OpenTelemetry SDK, and the FastMCP server sends no log records), and for a Box template whose + * agent makes no outbound call `"no-policy-records"`, since box then records no policy decision. + * Every check otherwise. + */ + telemetry?: "none" | "no-policy-records"; }; const MCP_INITIALIZE_PAYLOAD = { @@ -137,18 +145,21 @@ const RUNTIME_TEMPLATES: RuntimeTemplateTestCase[] = [ }, { name: "agent_ts_strands", + telemetry: "none" as const, template: "agent-typescript-strands", protocol: "HTTP" as const, payload: { prompt: "Reply with a short greeting." }, }, { name: "agent_ts_vercel", + telemetry: "none" as const, template: "agent-typescript-vercel", protocol: "HTTP" as const, payload: { prompt: "Reply with a short greeting." }, }, { name: "mcp_python_fastmcp", + telemetry: "none" as const, template: "mcp-python-fastmcp", protocol: "MCP" as const, payload: MCP_INITIALIZE_PAYLOAD, @@ -175,6 +186,7 @@ const RUNTIME_TEMPLATES: RuntimeTemplateTestCase[] = [ const BOX_TEMPLATES: RuntimeTemplateTestCase[] = [ { name: "agent_python_minimal_box", + telemetry: "no-policy-records" as const, template: "agent-python-minimal-box", protocol: "HTTP" as const, payload: { prompt: "Reply with a short greeting." }, @@ -229,6 +241,15 @@ const LocalRuntimeInvokeResponseSchema = RuntimeInvokeResponseSchema.extend({ }); const DeployResponseSchema = z.object({ message: z.string() }); const JsonObjectSchema = z.record(z.string(), z.unknown()); +const TracesSchema = z.object({ + traces: z.array( + z.object({ + traceId: z.string(), + sessionId: z.string().optional(), + spanCount: z.union([z.string(), z.number()]).optional(), + }), + ), +}); const McpResponseSchema = z.object({ jsonrpc: z.literal("2.0"), id: z.unknown(), @@ -294,10 +315,12 @@ describe( TIMEOUT_MS.PROJECT_DEV + 2 * TIMEOUT_MS.PROJECT_INVOKE + 2 * TIMEOUT_MS.PROJECT_DEPLOY + - TIMEOUT_MS.PROJECT_REMOVE, + TIMEOUT_MS.PROJECT_REMOVE + + TELEMETRY_TIMEOUT, }, async (runtime) => { const projectDir = await createTemplateProject(cli, runtime, { deploys: true }); + const session = getSessionId(runtime.name); let scenarioCompleted = false; try { if (!runtime.skipDev) { @@ -323,7 +346,7 @@ describe( "--runtime", CREATED_RUNTIME_NAME, "--session-id", - getSessionId(runtime.name), + session, "--payload", JSON.stringify(runtime.payload), "--json", @@ -341,6 +364,12 @@ describe( assertProtocolResponse(runtime, response.body); } if (runtime.box) await assertRunsInBox(cli, projectDir); + if (runtime.telemetry !== "none") { + await assertTelemetry(cli, projectDir, session, { + box: Boolean(runtime.box), + policyRecords: runtime.telemetry !== "no-policy-records", + }); + } const removed = parseResult( OperationSchema, @@ -407,10 +436,12 @@ describe.skipIf(!BOX_DEPLOYS)( 2 * BOX_TEMPLATES.length * (TIMEOUT_MS.PROJECT_DEV + TIMEOUT_MS.PROJECT_INVOKE) + 2 * TIMEOUT_MS.PROJECT_INVOKE + 3 * TIMEOUT_MS.PROJECT_DEPLOY + - TIMEOUT_MS.PROJECT_REMOVE, + TIMEOUT_MS.PROJECT_REMOVE + + TELEMETRY_TIMEOUT, }, async (runtime) => { const projectDir = await createTemplateProject(cli, runtime, { deploys: true }); + const session = getSessionId(runtime.name); const withDev = fullFlow(runtime); let scenarioCompleted = false; try { @@ -434,7 +465,7 @@ describe.skipIf(!BOX_DEPLOYS)( "--runtime", CREATED_RUNTIME_NAME, "--session-id", - getSessionId(runtime.name), + session, "--payload", JSON.stringify(runtime.payload), "--json", @@ -453,6 +484,10 @@ describe.skipIf(!BOX_DEPLOYS)( } await assertRunsInBox(cli, projectDir); + await assertTelemetry(cli, projectDir, session, { + box: true, + policyRecords: runtime.telemetry !== "no-policy-records", + }); // Dev again after build and deploy: they leave state (box's profile bindings, the // Runtime profile, .box-dist) that dev must still run beside. @@ -1363,6 +1398,95 @@ async function assertRunsInBox( expect(errors.stdout).not.toMatch(/ERROR|blocked by egress control|AccessDenied/); } +/** + * Checks that the deployed agent's telemetry reached CloudWatch after an invoke in `session`: its + * OpenTelemetry log records under the runtime's resource in the runtime's log group, and the + * invoke's trace, found by its session (`agentcore traces runtime list`), with the agent's spans. + * With `box`, also box's policy records for the agent's calls (unless `policyRecords` is false, for an + * agent that makes none), and box's spans in that trace: box + * sends both to CloudWatch (`[telemetry.agentcore]`, `[telemetry.agentcore_traces]`). Box's own + * start makes a trace of its own, with box's spans only, so the trace is picked by session. + */ +async function assertTelemetry( + cli: CliRunner, + projectDir: string, + session: string, + { box, policyRecords = box }: { box: boolean; policyRecords?: boolean }, +): Promise { + // The resource of the agent's spans and logs, which the Runtime names after the runtime. + const service = `${basename(projectDir)}_default_${CREATED_RUNTIME_NAME}.DEFAULT`; + const logs = (query: string) => + cli.run( + ["logs", "runtime", "--name", CREATED_RUNTIME_NAME, "--since", "30m", "--query", query], + projectDir, + ); + await retry( + async () => { + const found = await logs(`"${service}"`); + expect(found.exitCode).toBe(0); + expect(found.stdout).toMatch(/"resource"/); + expect(found.stdout).toContain(service); + }, + 5 * 60 * 1000, + 20 * 1000, + ); + if (box && policyRecords) { + await retry( + async () => { + const found = await logs('"strands.box.policy.verdict"'); + expect(found.exitCode).toBe(0); + expect(found.stdout).toContain("strands.box.policy.verdict"); + }, + 5 * 60 * 1000, + 20 * 1000, + ); + } + const traceId = await retry( + async () => { + const listed = parseResult( + TracesSchema, + await cli.run( + ["traces", "runtime", "list", "--name", CREATED_RUNTIME_NAME, "--since", "30m", "--json"], + projectDir, + ), + ); + const trace = listed.traces.find((t) => t.sessionId === session); + if (!trace) throw new Error(`no trace of session ${session} yet: ${JSON.stringify(listed)}`); + return trace.traceId; + }, + 10 * 60 * 1000, + 30 * 1000, + ); + const output = join(projectDir, "trace.json"); + parseResult( + JsonObjectSchema, + await cli.run( + [ + "traces", + "runtime", + "get", + traceId, + "--name", + CREATED_RUNTIME_NAME, + "--since", + "30m", + "--output", + output, + "--json", + ], + projectDir, + ), + ); + const spans = (JSON.parse(await readFile(output, "utf8")) as { "@message"?: unknown }[]).map( + (record) => JSON.stringify(record["@message"] ?? record), + ); + expect(spans.some((span) => span.includes(service))).toBe(true); + if (box) expect(spans.some((span) => span.includes("strands.box"))).toBe(true); +} + +/** How long assertTelemetry may wait for CloudWatch, for the timeouts of the tests that call it. */ +const TELEMETRY_TIMEOUT = 20 * 60 * 1000; + /** Deploys `projectDir`, runs `scenario`, and tears the stack down even when a step fails. */ async function deployed( cli: CliRunner,