Skip to content

feat(box): Send the metrics of the agent in a box to CloudWatch on AgentCore Runtime - #2967

Closed
SaiAbhishek98 wants to merge 2 commits into
aws:box-integrationfrom
SaiAbhishek98:sa/box-agent-metrics
Closed

SaiAbhishek98 wants to merge 2 commits into
aws:box-integrationfrom
SaiAbhishek98:sa/box-agent-metrics

Conversation

@SaiAbhishek98

@SaiAbhishek98 SaiAbhishek98 commented Nov 30, 2026 •

Copy link
Copy Markdown

Description

Problem. On AgentCore Runtime, an agent in a box sends no metric to CloudWatch. The @box/agentcore pack sets OTEL_METRICS_EXPORTER to none, and no default target of the Runtime profile receives metrics. An agent that is not in a box sends these metrics by default: the number of tokens, the time of each model call, and the metrics of the Strands event loop. Thus an agent that moves into a box does not send them, and nothing tells the user.

Change. With OTEL on, agentcore build writes this target into the Runtime profile (.box/<agent>/aws/profile.toml):

[telemetry.agentcore_metrics]
kind = "otlp"
destination = "https://monitoring.${{ inputs.aws.region }}.amazonaws.com"
accepts = [ "metrics" ]
include = [ "metrics" ]

[telemetry.agentcore_metrics.secret]
ref = "aws://"

The pack then sets OTEL_METRICS_EXPORTER to otlp, so the agent sends its metrics to the receiver of box. Box sends them to the CloudWatch metrics endpoint, and signs them with the execution role. agentcore/box-telemetry-policy-<runtime>.json gets this statement:

{ "Sid": "BoxTelemetryToCloudWatchMetrics", "Effect": "Allow", "Action": ["cloudwatch:PutMetricData"], "Resource": "*" }

Decisions.

  • The destination has no path. Box adds /v1/metrics to the destination of an otlp target. A destination with that path gives /v1/metrics/v1/metrics.
  • The region is the aws.region input, as for the default X-Ray target. Box 0.3.2 fills an input in a destination. The DNS suffix comes from the partition of the region of agentcore build, so China gets amazonaws.com.cn.
  • The target follows the rules of the default X-Ray target. agentcore build adds no target when enableOtel is false, or when DISABLE_ADOT_OBSERVABILITY is "true". The name is agentcore_metrics_default when a target has the name agentcore_metrics.
  • A target of the user to the CloudWatch metrics endpoint replaces the default target only with an env:// or secret:// key. CloudWatch accepts that key as a bearer token. A target with no key cannot get a signature on AgentCore Runtime, so CloudWatch refuses each send. Then agentcore build keeps the default target, and writes a Warning: line with the position of that target in the profile.
  • The grant is on "*", because PutMetricData has no resource type. agentcore build removes the statement when the target goes.
  • No change in the CLI or the L3 is necessary for "containment": "PRIVATE_VPC". With OTEL on, the private VPC of the L3 constructs adds the monitoring interface endpoint and its DNS name before this change.
  • The metrics go to CloudWatch as OpenTelemetry metrics, which PromQL reads in CloudWatch Query Studio. They do not go to the bedrock-agentcore namespace. That namespace gets the EMF records that the ADOT SDK writes for an agent that is not in a box. An EMF path is possible only with a change in box. Without that change, the agent can write records into the log group that holds the policy records of box.
  • With OTEL on, the notice of agentcore create and agentcore add runtime names the metrics and their cost.

Live tests. All the live tests on AgentCore Runtime gave the correct result. They used commit 0e7c9536 of this PR. The Testing section gives the results. The fix in 0887c3e0 has unit tests only.

Open item for review. The new statement grants cloudwatch:PutMetricData on "*" with no condition. The grant of the L3 runtime role has the condition cloudwatch:namespace = bedrock-agentcore. No test shows yet if OTLP ingestion works with a narrower condition.

Related Issue

No issue. aws/agentcore-cli#2885 set OTEL_METRICS_EXPORTER to none for the default X-Ray target.

Documentation PR

Documentation is in this PR: docs/box-local-testing.md.

Type of Change

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

Testing

Live tests on AgentCore Runtime, at 0e7c953:

  • Default network, us-west-2, a container project with two runtimes and a CodeZip project: each runtime gets the target, the otlp value of OTEL_METRICS_EXPORTER, and the PutMetricData statement. After the invokes, box sends the metrics to https://monitoring.<region>.amazonaws.com/v1/metrics with the signature of the execution role. CloudWatch answers each send with no AccessDenied and no other error. The spans, the records of box, and the Langfuse traces do not change.
  • "containment": "PRIVATE_VPC", a container in us-west-2 and CodeZip in us-east-1: the same results. The sends go through the monitoring interface endpoint of the VPC and its entry in the allowlist of the DNS firewall.
  • enableOtel set to false, and DISABLE_ADOT_OBSERVABILITY set to "true": the output of agentcore build is the same, byte for byte, as the output of the base CLI. Box sends no metric.
  • User targets: a target with a bearer key to the metrics endpoint replaces the default target. With a user target that has the name agentcore_metrics, both stay, and the default target gets the name agentcore_metrics_default. With a user target to another metrics host, both stay.
  • A project that the base CLI deployed, then a deploy with this branch: CloudFormation replaces no resource. It modifies only the runtime artifact and the IAM policy. The sends start after the deploy.
  • Projects with no box, CodeZip and container: the output is the same as the output of the base CLI. Container agentcore dev works, and dev sends no metric to CloudWatch, as before.
  • The test account cannot store OTLP metrics yet, so the read-back of a stored metric is not done.

Unit tests:

  • additions.test.ts, "the default CloudWatch metrics target": the target for CodeZip and for a container. No target with OTEL off or with DISABLE_ADOT_OBSERVABILITY. The DNS suffix of four partitions.
  • additions.test.ts: no default target and no Warning: line with a user target with a key to the metrics endpoint, for four destinations. The default target with three other destinations. The name clash, the comment at the top of the profile, the notice, and the value of OTEL_METRICS_EXPORTER.
  • additions.test.ts: the default target and a Warning: line with the position of the target, for a user target with no key to the metrics endpoint. With two user targets, the default target comes only when neither target has a key. No Warning: line with OTEL off.
  • boxNetworkSource.test.ts, "the default X-Ray target of a runtime with OTEL on": the grant with the X-Ray grant, and no grant with OTEL off. A user target with a key to the metrics endpoint gives no default target and no grant. A user target with no key gives the default target, the grant, and a Warning: line with the line number of the target. A Langfuse target.
  • boxNetworkSource.test.ts: "containment": "PRIVATE_VPC". Two runtimes, one with OTEL off.
  • boxTelemetry.test.ts: the statement, its position, and no statement without the target.
  • manager.boxNetwork.test.ts: agentcore build writes the target and the grant. A subsequent agentcore build with enableOtel set to false removes the target, the policy file, and its entry.
  • Four mutations caused errors in the tests, and then each mutation was removed: no OTEL check (161 tests with an error), no check for a user target (5), no grant (5), and no check for the key of a user target (4).

bun test, tsc, oxlint, prettier, bun run audit, bun run secrets:check, and bun run build show no error. This change has no run of the end-to-end tests of bun run test:e2e. The live tests on AgentCore Runtime replace them for this change.

  • I ran bun test (6800 pass, 12 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 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)

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.

…entCore Runtime

## Problem

On AgentCore Runtime, an agent in a box sends no metric to CloudWatch. The @box/agentcore pack sets `OTEL_METRICS_EXPORTER` to `none`, and no default target of the Runtime profile receives metrics. An agent that is not in a box sends these metrics by default: the number of tokens, the time of each model call, and the metrics of the Strands event loop. Thus an agent that moves into a box does not send them, and nothing tells the user.

## Solution

- The @box/agentcore pack sets `OTEL_METRICS_EXPORTER` to `otlp`. The agent then sends its metrics to the receiver of box, because box sets `OTEL_EXPORTER_OTLP_ENDPOINT`.
- With OTEL on, the `additions` rule adds `[telemetry.agentcore_metrics]` to the Runtime profile. It is an `otlp` target with `accepts = ["metrics"]` and `include = ["metrics"]`.
- Its destination is `https://monitoring.${{ inputs.aws.region }}`, then the DNS suffix of the partition of the region of `agentcore build`. Box adds `/v1/metrics`, so the destination has no path.
- A bare `aws://` signs the target with the execution role. The name is `agentcore_metrics_default` when a target has the name `agentcore_metrics`.
- The rule adds no target with `enableOtel` set to `false`, or with `DISABLE_ADOT_OBSERVABILITY` set to `"true"`. It also adds no target when a target of the profile sends to the CloudWatch metrics endpoint.
- `CloudWatchTelemetry` gets the value `cloudwatch_metrics` for its `kind`. `defaultMetricsTelemetry` gives the default target to the network model.
- `boxTelemetryPolicy` then grants `cloudwatch:PutMetricData` on `"*"` in `agentcore/box-telemetry-policy-<runtime>.json`. `PutMetricData` has no resource type.
- The comment at the top of the Runtime profile names the target. With OTEL on, the notice of `agentcore create` and `agentcore add runtime` names the metrics and their cost. `docs/box-local-testing.md` gives the rules.
- No change in the CLI is necessary for a PRIVATE_VPC runtime. With OTEL on, the private VPC of the L3 adds the `monitoring` interface endpoint and its DNS name.

The metrics go to CloudWatch as OpenTelemetry metrics, which PromQL reads in Query Studio. They do not go to the `bedrock-agentcore` namespace. That namespace gets the EMF records that the ADOT SDK writes for an agent that is not in a box. An EMF path is possible only with a change in box. Without that change, the agent can write records into the log group that holds the records of box.

## Tests

- `additions.test.ts`: the target for CodeZip and for a container. No target with OTEL off or with `DISABLE_ADOT_OBSERVABILITY`. The DNS suffix of four partitions, China too.
- `additions.test.ts`: no default target with a user target to the metrics endpoint, for four destinations. The default target with three other destinations. The name clash, the comment, the notice, and the value that the pack sets.
- `boxNetworkSource.test.ts`: the grant with the X-Ray grant, and no grant with OTEL off. The user target to the metrics endpoint. The default target with a Langfuse target. PRIVATE_VPC. Two runtimes, one with OTEL off.
- `boxTelemetry.test.ts`: the statement, its position, and no statement without the target.
- `manager.boxNetwork.test.ts`: `agentcore build` writes the target and the grant. A subsequent `agentcore build` with `enableOtel` set to `false` removes the target, the policy file, and its entry.
- Three mutations caused errors in the tests, and then each mutation was removed: no OTEL check (161 tests with an error), no check for a user target (5), and no grant (5).
- `bun test`, `tsc`, `oxlint`, and `prettier` show no error.
- A local run of box 0.3.2 sent the metrics of an agent to the CloudWatch metrics endpoint, with a SigV4 signature for the service `monitoring`. This test did not read the metrics from CloudWatch. That check and the live tests on AgentCore Runtime are not completed.
@codecov-commenter

codecov-commenter commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.95%. Comparing base (aaa29b1) to head (0887c3e).

Additional details and impacted files
@@               Coverage Diff                @@
##           box-integration    #2967   +/-   ##
================================================
  Coverage            96.94%   96.95%           
================================================
  Files                  780      780           
  Lines                70806    70882   +76     
================================================
+ Hits                 68645    68721   +76     
  Misses                2161     2161           

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

…arget that has no key, and tell the user (live test)

A target of the user to the CloudWatch metrics endpoint with no key cannot authenticate on AgentCore Runtime, so CloudWatch refuses each send. Before this change, `agentcore build` then left out the default metrics target, and the runtime sent no metric. After this change, only a target with an `env://` or `secret://` key replaces the default target. For a target with no key, `agentcore build` keeps the default target and writes a `Warning:` line with the position of the target.
@SaiAbhishek98
SaiAbhishek98 marked this pull request as ready for review November 30, 2026 20:29
@SaiAbhishek98
SaiAbhishek98 requested review from a team and leo237 November 30, 2026 20:29
@vvargu vvargu closed this Nov 30, 2026
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