Skip to content

fix(dev): Send the spans of an agent in a box, and the records of box, to the dev collector - #2916

Open
SaiAbhishek98 wants to merge 1 commit into
aws:box-integrationfrom
SaiAbhishek98:sa/box-dev-traces
Open

SaiAbhishek98 wants to merge 1 commit into
aws:box-integrationfrom
SaiAbhishek98:sa/box-dev-traces

Conversation

@SaiAbhishek98

Copy link
Copy Markdown

Description

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. 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 from
agent-python-strands-box wrote no file.

Root cause

  1. Box makes the environment of the agent from the profile only. 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.

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 an otlp telemetry target to
the dev collector, with no secret, and the OTEL variables of dev for the agent.

build Where dev writes the dev profile How the agent gets the instrumentation
CodeZip agentcore/.cli/box-dev/<agent>/profile.toml, with copies of the policy files The auto-instrumentation folder of the venv goes first on PYTHONPATH, the same as for an agent that is not in a box
Container The box folder of the image. The local profile is not in the image The command starts the Python of the venv with opentelemetry-instrument, without --reload and --reload-dir

Decisions that the diff does not show

  • The local profile does not change. Dev writes the dev profile only to a folder that git
    ignores (agentcore/.cli/*), or to the image. Dev writes the dev profile again at each start.
  • The telemetry target has no secret, and uses http to a loopback address. Box accepts an
    otlp target with no secret on http. The container gets to the collector through
    host.docker.internal, as before.
  • Box keeps its own records file. Thus box runs history continues to operate.
  • CodeZip keeps --reload. An agent that is not in a box gets its instrumentation through
    PYTHONPATH, because opentelemetry-instrument gives instrumentation only to the uvicorn process
    that does the reload. The box agent uses the same procedure.
  • A container drops --reload. The image of container dev has no live code. If the command
    does not start the Python of the venv, dev keeps the command, and tells the user that box sends
    only its own records.
  • A value of the user in [agent.env] stays. Dev adds PYTHONPATH before the value of the
    user.
  • The two controls of the user stay. When instrumentation.enableOtel is false, or
    DISABLE_ADOT_OBSERVABILITY is true, dev starts box with the local profile, as before.
  • A CodeZip venv with no auto-instrumentation gets the local profile. Dev then shows the message
    that dev shows for an agent that is not in a box.

Live results, local and with no AWS deploy

Test Result
CodeZip, box 0.3.0-preview.1, macOS invoke --local gave 200. The collector got 10 spans and 14 log records, with the service names agent and strands-box. The spans include invoke_agent Strands Agents, chat, POST /invocations, policy net:connect, and control box_started.
CodeZip, change to main.py uvicorn started again with the changed code. The next invoke wrote 3 more trace files.
Container, Finch invoke --local gave 200. The collector got 22 spans and 12 log records, with the service names agent and strands-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

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Other (please describe):

Testing

How have you tested the change?

  • I ran bun test: 6504 pass, 9 skip, 0 fail.
  • I ran the relevant end-to-end tests with 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.
  • I ran bun run typecheck
  • I ran bun run lint:check
  • I ran bun run format:check
  • I ran bun run audit
  • I ran bun run secrets:check
  • I ran bun run build
  • If I modified src/assets/, I updated affected snapshots with bun test <test-file> --update-snapshots and committed them
  • If I re-recorded fixtures or golden files, every change beyond account identifiers is explained
    by this PR
  • This PR does not change 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 OTLP
    endpoint in the variables, the CodeZip PYTHONPATH with --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", and two tests for DISABLE_ADOT_OBSERVABILITY and for enableOtel false.
  • container.test.ts: the profile in the image for host.docker.internal, and the local profile
    for DISABLE_ADOT_OBSERVABILITY.

I did not do a test with Docker or Podman, with a LangChain agent, or on Linux or Windows.

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the
terms of your choice.

…, 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.
@SaiAbhishek98
SaiAbhishek98 requested review from a team, leo237 and tejaskash November 25, 2026 01:15
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.90%. Comparing base (4235b7b) to head (b7840a8).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@leo237 leo237 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 an otlp target in 0.3.0: kind, destination, accepts = ["traces","logs"] and include = ["deny","permit","trace","logs"]. Box allows a target with no secret on http://, 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. include leaves out mcp_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 history keeps working. box run adds 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 lost line when the run ends. It does not refuse to start, so dev stays up. Optional: say in the status line that STRANDS_BOX_TELEMETRY_DEBUG=1 shows each failed send.
  • Container: the endpoint is already rewritten to host.docker.internal (otelEnvForRuntime), and --add-host host.docker.internal:host-gateway is 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. findOtelSitecustomizeDir was 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.

Comment thread src/core/dev/box.ts
join(input.projectRoot, input.runtime.codeLocation),
input.signal,
);
if (sitecustomizeDir === undefined) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/core/dev/box.ts
}

/** Why dev collects no traces of an agent whose venv lacks the OTEL auto-instrumentation. */
export const NO_AUTO_INSTRUMENTATION =

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 |

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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") {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/core/dev/container.ts
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"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/core/dev/box.test.ts
expect(existsSync(join(projectRoot, "agentcore", ".cli", "box-dev"))).toBe(false);
});

test("runs the local profile when dev collects no traces (enableOtel false)", async () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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".

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants