Repository navigation
fix(dev): Send the spans of an agent in a box, and the records of box, to the dev collector - #2916
SaiAbhishek98 wants to merge 1 commit into
Conversation
…, to the dev collector ## Problem In `agentcore dev`, an agent in a box sends no span to the dev collector. Thus `agentcore/.cli/traces/otlp` stays empty, and the local trace view shows nothing. An agent that is not in a box sends its spans there. On 2026-11-24, a CodeZip agent that is not in a box wrote a trace file of 19.5 KB. A CodeZip agent in a box wrote no file. There are three causes: 1. Box makes the environment of the agent from the profile only. Thus the agent does not get the OTEL variables and the `PYTHONPATH` that dev gives to an agent that is not in a box. 2. Box sets `OTEL_EXPORTER_OTLP_ENDPOINT` of the agent to the receiver of box. The local profile has no telemetry target for the dev collector. Thus box sends the spans to no collector. 3. In a container, the command of the agent does not start with `opentelemetry-instrument`. Dev also shows the message "Traces are not collected when the agent runs in a box". ## Solution When dev collects traces, dev starts box with a dev profile. Dev makes the dev profile from the local profile of the runtime, and writes it only to a folder that git ignores. The local profile does not change. - The dev profile adds the telemetry target `[telemetry.agentcore_dev]`. The `kind` of the target is `otlp`, the destination is the dev collector, and the target has no secret. If the profile has a target with that name, dev uses `agentcore_dev_2`. - The dev profile adds to `[agent.env]` the OTEL variables that dev gives to an agent that is not in a box. The OTLP endpoints are not in that set, because box sets them. A value of the user stays. - CodeZip: dev writes the dev profile and copies of the policy files to `agentcore/.cli/box-dev/<agent>/`. The auto-instrumentation folder of the venv goes first on `PYTHONPATH`, the same as for an agent that is not in a box. Thus the auto-instrumentation operates in each Python process of the agent, and `--reload` continues to operate. - Container: the image gets the dev profile. The command starts the Python of the venv with `opentelemetry-instrument`, and dev removes `--reload` and `--reload-dir`, because the image has no live code. If the command does not start the Python of the venv, dev keeps the command, and tells the user that the agent sends no span. The container gets to the collector through `host.docker.internal`, as before. - When `instrumentation.enableOtel` is `false`, or `DISABLE_ADOT_OBSERVABILITY` is `true`, dev starts box with the local profile, as before. - CodeZip: if the venv has no auto-instrumentation, dev starts box with the local profile. Dev then shows the message that dev shows for an agent that is not in a box. - `findOtelSitecustomizeDir` moves out of `CodeZipDevRunner`, so that `BoxDevRunner` can use it. ## Tests - `boxDevTelemetry.test.ts` has 7 tests. They pin the target with no secret, the name when the user has the name, the variables with no OTLP endpoint, `PYTHONPATH` with the command and its reload kept, the container command without reload, a command that dev keeps, and the folder. - `box.test.ts` "runs a dev-time profile that sends traces to the collector, and keeps the local profile" pins the path that `box run` gets, the merge of `PYTHONPATH`, the value of the user, the copy of the policy file, and the local profile with no change. - `box.test.ts` has two more tests. They pin that `DISABLE_ADOT_OBSERVABILITY` and `enableOtel` `false` start box with the local profile, and that dev writes no dev folder. - `container.test.ts` has two tests. They pin the profile in the image for `host.docker.internal`, and the local profile for `DISABLE_ADOT_OBSERVABILITY`. - `bun test` gives `6504 pass, 9 skip, 0 fail`. `bun run typecheck`, `bun run lint:check`, `bun run format:check`, and `bun run build` give no error. - Live CodeZip test with box 0.3.0-preview.1 on macOS: after `agentcore dev`, `agentcore invoke --local` gave 200. The collector got 10 spans and 14 log records, with the service names `agent` and `strands-box`. After a change to `main.py`, uvicorn started again with the changed code, and the next invoke wrote more trace files. - Live container test with Finch: `agentcore invoke --local` gave 200. The collector got 22 spans and 12 log records, with the service names `agent` and `strands-box`. - I did not do the end-to-end tests (`bun run test:e2e`), because no change is in deploy. I did not do a test with Docker or Podman, or with a LangChain agent.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## box-integration #2916 +/- ##
=================================================
Coverage 96.89% 96.90%
=================================================
Files 776 777 +1
Lines 69346 69490 +144
=================================================
+ Hits 67194 67338 +144
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.
Approving. I found no blockers, only nits and test gaps (inline).
What I checked against box 0.3.0 (strands-agents/box tag v0.3.0: docs/user/telemetry.md, docs/user/config.md, crates/telemetry/src/config.rs):
- The
[telemetry.agentcore_dev]keys are all valid for anotlptarget in 0.3.0:kind,destination,accepts = ["traces","logs"]andinclude = ["deny","permit","trace","logs"]. Box allows a target with no secret onhttp://, because the http-or-loopback check applies only when a secret is set. No new key needs a later box. - Egress is not wider. The exporter is Box Core, which runs outside the sandbox. No
[egress]rule is added, and the workload still reaches only box's own loopback receiver.includeleaves outmcp_tool_calls, so no clear-text tool arguments or outputs reach the collector. I saw no credentials in the profile or the env that dev adds. box runs historykeeps working.box runadds its own[telemetry.records]file target (docs/design/cli/profiles.md), so the extra target does not replace the records file.- If the collector is unreachable, box drops the batch, counts it, and prints one
telemetry records were lostline when the run ends. It does not refuse to start, sodevstays up. Optional: say in the status line thatSTRANDS_BOX_TELEMETRY_DEBUG=1shows each failed send. - Container: the endpoint is already rewritten to
host.docker.internal(otelEnvForRuntime), and--add-host host.docker.internal:host-gatewayis always passed, so Linux Docker works too. The port comes from the collector's env, so a dynamic port flows through. - Dev without box is unchanged.
findOtelSitecustomizeDirwas moved without changing what it does.
Local results at b7840a8: bun run typecheck passed. bun test gave 6504 pass, 9 skip, 0 fail. I ran 9 mutations on the three test files and reverted each one. 8 were caught: the user env wins, DISABLE_ADOT gating in box and container, --reload-dir stripping, policy copies, PYTHONPATH order, the container notice, and include. 1 survived: the container notice for an agent that is not instrumented (inline).
CI: CodeQL and Analyze pass. verify (Linux, macOS, Windows) was still pending when I posted.
| join(input.projectRoot, input.runtime.codeLocation), | ||
| input.signal, | ||
| ); | ||
| if (sitecustomizeDir === undefined) { |
There was a problem hiding this comment.
When the venv has no auto-instrumentation, CodeZip falls back to the local profile. Box's own records (deny/permit, box_started) then do not reach the collector either. The container path still sends box's records in the same case (agentInstrumented: false). Should CodeZip also write the dev profile without sitecustomizeDir, so box's records still show up in the trace view? It would also make the two paths match. Not blocking.
| } | ||
|
|
||
| /** Why dev collects no traces of an agent whose venv lacks the OTEL auto-instrumentation. */ | ||
| export const NO_AUTO_INSTRUMENTATION = |
There was a problem hiding this comment.
nit: this string is copied word for word from codezip.ts:79. Could codezip.ts import NO_AUTO_INSTRUMENTATION instead, so the two can't drift apart?
| * The OTEL variables of a boxed agent under dev, as dev gives them to an agent outside a box. The | ||
| * OTLP endpoints are not here: box points the agent at its own receiver, and relays from there. | ||
| */ | ||
| export function boxDevAgentOtelEnv(serviceName: string): Record<string, string> { |
There was a problem hiding this comment.
nit: this repeats the non-endpoint part of otelEnvVars() in otel/collector.ts. If someone adds a variable there later, the boxed agent won't get it. You could derive it instead: Object.fromEntries(Object.entries(otelEnvVars(0)).filter(([k]) => !k.startsWith("OTEL_EXPORTER_OTLP_"))) plus OTEL_SERVICE_NAME. Or add a test that pins the two together.
| * | Change | Value | | ||
| * | --- | --- | | ||
| * | `[telemetry.agentcore_dev]` | an `otlp` target to the collector, for traces and logs, with no secret | | ||
| * | `[agent.env]` | the OTEL variables that dev gives an agent outside a box, except the OTLP endpoints, which box sets to its own receiver | |
There was a problem hiding this comment.
nit: "except the OTLP endpoints" is narrower than what the code does. The code leaves out every OTEL_EXPORTER_OTLP_* variable, including protocol and compression, and it has to: box 0.3.0 owns that prefix and refuses it in [agent.env] (config.md). Suggest "except every OTEL_EXPORTER_OTLP_* variable, which box owns and sets to its own receiver". The same applies to the doc comment on boxDevAgentOtelEnv.
| const args: string[] = []; | ||
| for (let index = 0; index < rest.length; index++) { | ||
| if (rest[index] === "--reload") continue; | ||
| if (rest[index] === "--reload-dir") { |
There was a problem hiding this comment.
nit: this strips only the two-word forms --reload and --reload-dir <dir>. A hand-edited command using --reload-dir=/app (which uvicorn accepts) would keep the flag in the container. The generated profiles use the two-word form, so this is fine today. A startsWith("--reload-dir=") check would cover the other form.
| const notice = dev | ||
| ? dev.agentInstrumented | ||
| ? "Traces of the agent and box's records go to the dev collector" | ||
| : "Box's records go to the dev collector. The agent's own spans do not, because its command is not the venv's Python" |
There was a problem hiding this comment.
nit: no test covers this branch. Replacing the message with any other string still passes all of box.test.ts, container.test.ts and boxDevTelemetry.test.ts. The unit test checks agentInstrumented: false, but not that the container runner yields this notice. A container.test.ts case with a non-venv command would cover it.
| expect(existsSync(join(projectRoot, "agentcore", ".cli", "box-dev"))).toBe(false); | ||
| }); | ||
|
|
||
| test("runs the local profile when dev collects no traces (enableOtel false)", async () => { |
There was a problem hiding this comment.
nit: the name says enableOtel false, but the test only passes env: {} (no collector env). The enableOtel gating is in handlers/project/dev/index.ts:414, which this test doesn't exercise. Suggest renaming it to "... when dev passes no collector endpoint".
Description
In
agentcore dev, an agent in a box sends no span to the dev collector. Thusagentcore/.cli/traces/otlpstays empty, and the local trace view shows nothing. An agent that isnot in a box sends its spans there. I did a test on 2026-11-24 with no deploy. A CodeZip agent from
agent-python-strands, which is not in a box, wrote a trace file of 19.5 KB. A CodeZip agent fromagent-python-strands-boxwrote no file.Root cause
variables and the
PYTHONPATHthat dev gives to an agent that is not in a box.OTEL_EXPORTER_OTLP_ENDPOINTof the agent to the receiver of box. The local profile hasno telemetry target for the dev collector. Thus box sends the spans to no collector.
opentelemetry-instrument.What this PR does
When dev collects traces, dev starts box with a dev profile. Dev makes the dev profile from the
local profile
.box/<agent>/local/profile.toml. The dev profile adds anotlptelemetry target tothe dev collector, with no secret, and the OTEL variables of dev for the agent.
buildagentcore/.cli/box-dev/<agent>/profile.toml, with copies of the policy filesPYTHONPATH, the same as for an agent that is not in a boxopentelemetry-instrument, without--reloadand--reload-dirDecisions that the diff does not show
ignores (
agentcore/.cli/*), or to the image. Dev writes the dev profile again at each start.httpto a loopback address. Box accepts anotlptarget with no secret onhttp. The container gets to the collector throughhost.docker.internal, as before.box runs historycontinues to operate.--reload. An agent that is not in a box gets its instrumentation throughPYTHONPATH, becauseopentelemetry-instrumentgives instrumentation only to the uvicorn processthat does the reload. The box agent uses the same procedure.
--reload. The image of container dev has no live code. If the commanddoes not start the Python of the venv, dev keeps the command, and tells the user that box sends
only its own records.
[agent.env]stays. Dev addsPYTHONPATHbefore the value of theuser.
instrumentation.enableOtelisfalse, orDISABLE_ADOT_OBSERVABILITYistrue, dev starts box with the local profile, as before.that dev shows for an agent that is not in a box.
Live results, local and with no AWS deploy
invoke --localgave 200. The collector got 10 spans and 14 log records, with the service namesagentandstrands-box. The spans includeinvoke_agent Strands Agents,chat,POST /invocations,policy net:connect, andcontrol box_started.main.pyinvoke --localgave 200. The collector got 22 spans and 12 log records, with the service namesagentandstrands-box. The agent starts with/app/.venv/bin/opentelemetry-instrument.Related Issue
A test of the Box test build on 2026-11-24 found this problem. There is no issue for it.
Documentation PR
None. No page tells the user that dev collects no trace of an agent in a box.
Type of Change
Testing
How have you tested the change?
bun test:6504 pass, 9 skip, 0 fail.bun run test:e2e, or explained why they are not applicable(CI does not run them for you). I did not do the end-to-end tests, because no change is in
deploy. The live tests in the table above use
agentcore dev.bun run typecheckbun run lint:checkbun run format:checkbun run auditbun run secrets:checkbun run buildsrc/assets/, I updated affected snapshots withbun test <test-file> --update-snapshotsand committed themby this PR
command.md(the release workflow regenerates it)The tests that pin each decision:
boxDevTelemetry.test.ts(7 tests): the target with no secret, a free target name, no OTLPendpoint in the variables, the CodeZip
PYTHONPATHwith--reloadkept, the container commandwithout
--reload, a command that dev keeps, and the folder.box.test.ts"runs a dev-time profile that sends traces to the collector, and keeps the localprofile", and two tests for
DISABLE_ADOT_OBSERVABILITYand forenableOtelfalse.container.test.ts: the profile in the image forhost.docker.internal, and the local profilefor
DISABLE_ADOT_OBSERVABILITY.I did not do a test with Docker or Podman, with a LangChain agent, or on Linux or Windows.
Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the
terms of your choice.