Repository navigation
feat(box): Send the spans of a boxed agent to the log group of its runtime - #2914
SaiAbhishek98 wants to merge 2 commits into
Conversation
…ntime The default X-Ray target of a Runtime profile sent the spans to X-Ray with no log group, so X-Ray put them in aws/spans, and the spans stream of the runtime's own log group stayed empty. With a box release that takes `log_group` and `log_stream` on a `cloudwatch_traces` target (strands-agents/box#629), the default target now names the log group and the log stream that the Runtime sets in OTEL_EXPORTER_OTLP_TRACES_HEADERS. The @box/agentcore pack reads the two values through the inputs `runtime.traces_log_group` and `runtime.traces_log_stream`. BOX_TRACES_LOG_GROUP_VERSION is the first box release that takes the two keys. A box.version before it gets the target without them, because an older box refuses them. Set the constant to the release that ships strands-agents/box#629. Tests: additions.test.ts and boxProfile.test.ts pin both sides of the version gate, the header line, and the grant. bun test: 6516 pass, 9 skip, 0 fail. Typecheck, lint and format pass.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## box-integration #2914 +/- ##
================================================
Coverage 96.89% 96.89%
================================================
Files 776 776
Lines 69346 69376 +30
================================================
+ Hits 67194 67224 +30
Misses 2152 2152 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
leo237
left a comment
There was a problem hiding this comment.
Reviewed at c118a51. I found no blockers. This is a draft and has no effect until the box pin moves past 0.3.0, so I'm commenting rather than approving.
What I checked
- Version gate. strands-agents/box#629 merged at a2a97ee, and that commit is 1 ahead of
v0.3.1. So v0.3.1 does not have it, and0.3.2withsemver.gteis the correct gate.0.4.0also passes it, and pre-releases are rejected, which is the safe choice. The pinned 0.3.0 gets neither the keys nor the pack inputs, so the pinned box can't refuse them. - Names. The CLI does not derive the group or stream itself. Box reads them at run time from
OTEL_EXPORTER_OTLP_TRACES_HEADERS(x-aws-log-group,x-aws-log-stream), the same way the logs target reads them. So a deploy-time runtime ID is not a problem, and no CDK token leaks in. Box enforces both-or-neither, and the CLI always writes both keys. - Local vs Runtime. The
@box/agentcorepack is staged only into the Runtime dist, soagentcore devis not affected. - User targets. If the user has their own
cloudwatch_tracestarget, build still adds no default target. WithenableOtel=false, build adds no traces target and declares no inputs (boxProfile.test.ts, the OTEL-off row). - IAM. The CLI already grants
xray:PutSpans. The L3 role already haslogs:PutResourcePolicyon/aws/bedrock-agentcore/runtimes/*for X-Ray delivery into the agent log group. This matches how a native runtime delivers its spans, and the live test (41 spans inspans, 0 inaws/spans) confirms it. - Backward compatibility.
defaultTracesTargetsmatches both forms of the target, so existing profiles keep the header line and the grant.
Results
bun run typecheck: pass.bun test: 6505 pass, 9 skip, 0 fail.- CI: Analyze, CodeQL and verify on Linux, Windows and macOS pass. e2e is skipped.
- Mutations: I tried 7 mutations (
gte→gt, droplog_stream, pack never declares the inputs, grant matches only the plain form, stream input reads the group key, target ignores the version). The tests caught each one. One more mutation, on the header text, failed to apply, so I did not test it.
Inline comments
- Missing env var stops box at start. This is a regression risk for the untested CodeZip and
PRIVATE_VPCpaths, so please run the live tests you planned before leaving draft. - The docs should name the silent-loss case and say how to debug it.
- Two nits.
Before merge: set BOX_TRACES_LOG_GROUP_VERSION to the real release and bump boxRelease.json, as your follow-ups say.
|
|
||
| # The runtime's own log group and the log stream of its spans, for the default X-Ray target. The | ||
| # Runtime names both in ${RUNTIME_TRACES_HEADERS_ENV}. | ||
| [inputs.runtime.traces_log_group] |
There was a problem hiding this comment.
These two inputs have no default, so if the Runtime does not set OTEL_EXPORTER_OTLP_TRACES_HEADERS, or sets it without one of the keys, box refuses to start the run. Box's own test (crates/cli/app/src/profile_tests.rs) shows the refusal: "inputs.cloudwatch.group has no value: UNSET is not set or is empty". So a missing env var gives no empty or garbage name. It is loud, but it stops the whole agent. Before this PR, the same runtime would run and its spans would go to aws/spans.
The log_group/log_stream inputs already depend on OTEL_EXPORTER_OTLP_LOGS_HEADERS in the same way, so this is a known pattern. But your live test shows the traces variable only on a container runtime with the default network. Before this leaves draft, please confirm that the Runtime sets OTEL_EXPORTER_OTLP_TRACES_HEADERS with both keys for CodeZip and for PRIVATE_VPC. You already list this as a follow-up. A default can't fix this, because box validates the name and an empty string fails. So I'd write the dependency in the docs instead (see the docs comment).
| permit, and trace spans, to X-Ray in the region of the Runtime. Build gives the execution role | ||
| permit, and trace spans, to X-Ray in the region of the Runtime. With box 0.3.2 or later | ||
| (`BOX_TRACES_LOG_GROUP_VERSION`), the target also names the runtime's own log group and its | ||
| `spans` stream, which box reads from `OTEL_EXPORTER_OTLP_TRACES_HEADERS`, so the spans go there |
There was a problem hiding this comment.
The text is accurate for the path you tested, but it leaves out the failure modes that the box#629 reviews raised:
- A wrong or unwritable log group loses spans silently, because the exporter checks only the HTTP status. The box#629 docs say that in one such case the spans reached neither group.
- The new requirement: the Runtime must set
OTEL_EXPORTER_OTLP_TRACES_HEADERS, or box stops at start.
Suggest one or two sentences here. Say that if no spans reach the spans stream, the user can set STRANDS_BOX_TELEMETRY_DEBUG=1 and look for telemetry export failed. That is the check you used in the live test. Also say that the delivery depends on the logs:PutResourcePolicy grant that the L3 runtime role has on /aws/bedrock-agentcore/runtimes/* (AgentCoreRuntime.ts, "PutResourcePolicy lets X-Ray deliver spans to the agent log group"). The CLI adds no grant for it. This would also back up the "No new grant" decision in the PR body.
| * Whether the box of `runtime` sends its spans to the log group that the Runtime names: its | ||
| * `box.version` is {@link BOX_TRACES_LOG_GROUP_VERSION} or later. | ||
| */ | ||
| export function tracesToRuntimeLogGroup(runtime: Pick<ProjectRuntime, "box">): boolean { |
There was a problem hiding this comment.
nit: The gate reads runtime.box.version, but the binary that build stages is BOX_RELEASE (boxContainer.ts). The two agree only because boxVersionWriter runs first and rewrites box.version to the pin. I checked that the order is right. Still, a one-line comment saying so would help, or the gate could read BOX_RELEASE.version directly. Then nobody could make the profile name keys that the staged box refuses by reordering BOX_NETWORK_WRITERS.
| }, | ||
| ]); | ||
| expect({ ...DEFAULT_TRACES_TARGET } as TomlTable).toEqual(rest); | ||
| expect([log_group, log_stream]).toEqual([ |
There was a problem hiding this comment.
nit: This assertion only compares the test's own DEFAULT_TRACES fixture with literals. The expect({ ...DEFAULT_TRACES_TARGET }).toEqual(rest) two lines up repeats the same constant check on every row. Both could go, or move out of the test.each.
A more useful test: one that renders the profile and the @box/agentcore pack for the same runtime, and checks that every ${{ inputs.runtime.traces_* }} in the profile is declared in the pack. With my mutation that stops the pack from declaring the inputs, only the 2 direct pack unit tests failed. Nothing checks that the two halves agree.
Description
Problem. On AgentCore Runtime, a native agent sends its spans to the log group of its runtime,
/aws/bedrock-agentcore/runtimes/<runtime ID>-DEFAULT, streamspans. A boxed agent does not. The default target[telemetry.agentcore_traces]that build adds to the Runtime profile has nolog_groupand nolog_stream. Thus box sends the spans to X-Ray without these headers, and X-Ray writes them toaws/spans. The spans of the agent and the decision spans of box are then not in the log group of the runtime.strands-agents/box#629 lets a
cloudwatch_tracestarget name its log group and its stream. This PR uses that change.Solution.
[telemetry.agentcore_traces]getslog_group = "${{ inputs.runtime.traces_log_group }}"andlog_stream = "${{ inputs.runtime.traces_log_stream }}".@box/agentcorepack declares the two inputs. Each input reads one key ofOTEL_EXPORTER_OTLP_TRACES_HEADERS, which the Runtime sets. This is the same method that the logs target uses forOTEL_EXPORTER_OTLP_LOGS_HEADERS.Decisions
box.versionof the runtime isBOX_TRACES_LOG_GROUP_VERSIONor later (intemplates/boxProfile.ts). A box release before strands-agents/box#629 refuses the two keys when the run starts. The value is"0.3.2". It must be equal to the first strands-agents/box release that has strands-agents/box#629. Release v0.3.1 does not have it.xray:PutSpansfrom the X-Ray grant of aws/agentcore-cli#2885. X-Ray writes into the log group with the resource policy that AgentCore puts on the log group.cloudwatch_tracestarget, build adds no default target, as before.Tests that CI does not do
The live test used a test account in us-west-2, a container runtime, the default network, and a box that I built from strands-agents/box
mainat cfff47d5, which has strands-agents/box#629. For the test only, I set the version gate to"0.3.0". The commit has"0.3.2".spansstream of the-DEFAULTlog group of the runtime got 41 spans: 23 decision spans of box and 18 spans of the agent. All spans of the agent havesession.id.aws/spansgot 0 spans of this runtime.STRANDS_BOX_TELEMETRY_DEBUG=1, box recorded 0telemetry export failedlines.agentcore traces runtime listshows the traces, with the session ID of the agent.agent-python-strands, CodeZip) sends its spans to the samespansstream, and 0 spans toaws/spans. ItsOTEL_EXPORTER_OTLP_TRACES_HEADERSnames this log group and the streamspans.I did no test of a CodeZip runtime, a
PRIVATE_VPCruntime, or the CloudWatch GenAI Observability console with spans in this stream.Follow-ups
BOX_TRACES_LOG_GROUP_VERSIONto that release, and change the box pin inboxRelease.jsonto it. Until then, the change has no effect with the pin0.3.0.PRIVATE_VPCbefore you change the PR from a draft.Related Issue
Closes #
Documentation PR
Included:
docs/box-local-testing.mdtells where the spans of a boxed agent go on AgentCore Runtime.Type of Change
Testing
How have you tested the change?
bun test: 6503 pass, 9 skip, 2 fail. The 2 failures are TUI tests (harness.screen.test.tsx,oauth.screen.test.tsx) that stop at their time limit in the full run. The two files pass alone (31 pass, 0 fail). This PR does not change them.bun run test:e2e, or explained why they are not applicable: not run. The live test above covers the Runtime.bun run typecheckbun run lint:checkbun run format:checkbun run auditbun run secrets:checkbun run buildsrc/assets/, I updated affected snapshots: not applicable.command.mdChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the
terms of your choice.