Repository navigation
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
Closed
SaiAbhishek98 wants to merge 2 commits into
SaiAbhishek98 wants to merge 2 commits into
Conversation
…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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Problem. On AgentCore Runtime, an agent in a box sends no metric to CloudWatch. The
@box/agentcorepack setsOTEL_METRICS_EXPORTERtonone, 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 buildwrites this target into the Runtime profile (.box/<agent>/aws/profile.toml):The pack then sets
OTEL_METRICS_EXPORTERtootlp, 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>.jsongets this statement:{ "Sid": "BoxTelemetryToCloudWatchMetrics", "Effect": "Allow", "Action": ["cloudwatch:PutMetricData"], "Resource": "*" }Decisions.
/v1/metricsto the destination of anotlptarget. A destination with that path gives/v1/metrics/v1/metrics.aws.regioninput, as for the default X-Ray target. Box 0.3.2 fills an input in adestination. The DNS suffix comes from the partition of the region ofagentcore build, so China getsamazonaws.com.cn.agentcore buildadds no target whenenableOtelisfalse, or whenDISABLE_ADOT_OBSERVABILITYis"true". The name isagentcore_metrics_defaultwhen a target has the nameagentcore_metrics.env://orsecret://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. Thenagentcore buildkeeps the default target, and writes aWarning:line with the position of that target in the profile."*", becausePutMetricDatahas no resource type.agentcore buildremoves the statement when the target goes."containment": "PRIVATE_VPC". With OTEL on, the private VPC of the L3 constructs adds themonitoringinterface endpoint and its DNS name before this change.bedrock-agentcorenamespace. 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.agentcore createandagentcore add runtimenames 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:PutMetricDataon"*"with no condition. The grant of the L3 runtime role has the conditioncloudwatch: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_EXPORTERtononefor the default X-Ray target.Documentation PR
Documentation is in this PR:
docs/box-local-testing.md.Type of Change
Testing
Live tests on AgentCore Runtime, at 0e7c953:
us-west-2, a container project with two runtimes and a CodeZip project: each runtime gets the target, theotlpvalue ofOTEL_METRICS_EXPORTER, and thePutMetricDatastatement. After the invokes, box sends the metrics tohttps://monitoring.<region>.amazonaws.com/v1/metricswith the signature of the execution role. CloudWatch answers each send with noAccessDeniedand no other error. The spans, the records of box, and the Langfuse traces do not change."containment": "PRIVATE_VPC", a container inus-west-2and CodeZip inus-east-1: the same results. The sends go through themonitoringinterface endpoint of the VPC and its entry in the allowlist of the DNS firewall.enableOtelset tofalse, andDISABLE_ADOT_OBSERVABILITYset to"true": the output ofagentcore buildis the same, byte for byte, as the output of the base CLI. Box sends no metric.agentcore_metrics, both stay, and the default target gets the nameagentcore_metrics_default. With a user target to another metrics host, both stay.agentcore devworks, and dev sends no metric to CloudWatch, as before.Unit tests:
additions.test.ts, "the default CloudWatch metrics target": the target for CodeZip and for a container. No target with OTEL off or withDISABLE_ADOT_OBSERVABILITY. The DNS suffix of four partitions.additions.test.ts: no default target and noWarning: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 ofOTEL_METRICS_EXPORTER.additions.test.ts: the default target and aWarning: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. NoWarning: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 aWarning: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 buildwrites the target and the grant. A subsequentagentcore buildwithenableOtelset tofalseremoves the target, the policy file, and its entry.bun test,tsc,oxlint,prettier,bun run audit,bun run secrets:check, andbun run buildshow no error. This change has no run of the end-to-end tests ofbun run test:e2e. The live tests on AgentCore Runtime replace them for this change.bun test(6800 pass, 12 skip, 0 fail)bun run test:e2e, or explained why they are not applicable (CI does not run them for you)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 themcommand.md(the release workflow regenerates it)Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the
terms of your choice.