Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions docs/box-local-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,17 @@ profile that names a pack `box/<name>` fails: box refuses it and names the `@box

**The default X-Ray target.** With `enableOtel` on and no `cloudwatch_traces` target, the Runtime
profile gets `[telemetry.agentcore_traces]`: box sends the spans of the agent, and its own deny,
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
Comment thread
SaiAbhishek98 marked this conversation as resolved.
Resolved
and not to `aws/spans`. The Runtime must set `OTEL_EXPORTER_OTLP_TRACES_HEADERS` with
`x-aws-log-group` and `x-aws-log-stream`, or box does not start. If the log group is incorrect,
or box cannot write to it, no span gets to a log group and box shows no error. The exporter
of box examines only the HTTP status. If no spans get to the `spans` stream, set
`STRANDS_BOX_TELEMETRY_DEBUG` to `"1"` in the `envVars` of the runtime. Then look for
`telemetry export failed` in the log group of the runtime. The spans get to the log group only
with the `logs:PutResourcePolicy` grant that the L3 runtime role has on
`/aws/bedrock-agentcore/runtimes/*`. The CLI adds no grant for it. Build gives the execution role
`xray:PutSpans` and `xray:PutTraceSegments` in `agentcore/box-telemetry-policy-<runtime>.json`. To
send the spans to a different target, add a `cloudwatch_traces` target to the local profile. To
send no spans, set `instrumentation.enableOtel` to `false` in `agentcore.json`. When `enableOtel`
Expand Down Expand Up @@ -535,8 +545,9 @@ the deny, permit, and trace records, and box signs it with the execution role.

With `instrumentation.enableOtel` on, box also sends the spans of the agent and its own spans to
X-Ray, through the target `[telemetry.agentcore_traces]`. Build gives the execution role the X-Ray
grant for it. "The default X-Ray target" in step 4 gives the rules. CloudWatch Transaction Search
keeps the spans in the log group `aws/spans`.
grant for it. "The default X-Ray target" in step 4 gives the rules. With box 0.3.2 or later, the
spans go to the stream `spans` of the runtime's own log group. With an older box, CloudWatch
Transaction Search keeps them in the log group `aws/spans`.

The CLI and the L3 constructs make no log group for box. The role that the CDK makes for the runtime
can write to the log group of the Runtime, thus build adds no grant for it. A runtime that names an
Expand Down
6 changes: 6 additions & 0 deletions src/core/project/boxNetworkSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type { BoxProfileResolver } from "./boxProfileResolver";
import { boxShowOf, FakeBoxProfileResolver } from "./boxProfileResolver-test-support";
import { telemetryPolicyWriter } from "./boxNetworkWriters";
import { boxTelemetryPolicyFile } from "./boxTelemetry";
import { BOX_RELEASE } from "./backends/boxRelease";
import { tracesToRuntimeLogGroup } from "./templates/boxProfile";

const directories: string[] = [];
afterEach(async () => {
Expand Down Expand Up @@ -476,6 +478,10 @@ describe("the default X-Ray target of a runtime with OTEL on", () => {
region: "${{ inputs.aws.region }}",
include: ["deny", "permit", "trace"],
secret: { ref: "aws://" },
...(tracesToRuntimeLogGroup(BOX_RELEASE.version) && {
log_group: "${{ inputs.runtime.traces_log_group }}",
log_stream: "${{ inputs.runtime.traces_log_stream }}",
}),
});
const [file] = policyOf(on!);
expect(JSON.parse((file as { text: string }).text)).toEqual({
Expand Down
103 changes: 96 additions & 7 deletions src/core/project/runtimeProfile/rules/additions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ import {
BOX_BEDROCK_API_KEY_ROUTE,
BOX_BEDROCK_PACK,
BOX_BEDROCK_ROUTE,
BOX_TRACES_LOG_GROUP_VERSION,
renderAgentCorePack,
renderBoxProfile,
tracesToRuntimeLogGroup,
} from "../../templates/boxProfile";
import { BOX_RELEASE } from "../../backends/boxRelease";
import { runtimeProfileContext } from "../context";
import { deriveRuntimeProfile } from "../derive";
import { serializeRuntimeProfile } from "../serialize";
Expand All @@ -29,10 +32,10 @@ import {
agentStartsUnderOtel,
apply,
DEFAULT_TELEMETRY_TARGET,
DEFAULT_TRACES_TARGET,
defaultTracesTelemetry,
MEMORY_ROUTE,
runtimeProfileHeader,
runtimeTracesTarget,
} from "./additions";

const PROFILE_FILE = ".box/agent/local/profile.toml";
Expand Down Expand Up @@ -109,11 +112,15 @@ function specOf(runtime: ProjectRuntime, memories: readonly string[] = []) {
} as unknown as ProjectSpec;
}

/** The box of {@link contextOf}: one that takes the log group of its spans. */
const BOX_VERSION = "1.0.0";

function contextOf(
runtime: ProjectRuntime,
options: {
memories?: readonly string[];
telemetry?: readonly CloudWatchTelemetry[];
boxVersion?: string;
} = {},
) {
const spec = specOf(runtime, options.memories);
Expand All @@ -122,6 +129,7 @@ function contextOf(
runtime,
profileFile: PROFILE_FILE,
region: BUILD_REGION,
boxVersion: options.boxVersion ?? BOX_VERSION,
gateway: {
name: spec.agentCoreGateways[0]!.name,
targets: spec.agentCoreGateways[0]!.targets,
Expand Down Expand Up @@ -173,14 +181,24 @@ const DEFAULT_TARGET = {
secret: { ref: "aws://" },
};

/** The default X-Ray target of a Runtime profile with OTEL on. */
const DEFAULT_TRACES = {
/** The default X-Ray target of a Runtime profile with OTEL on, for a box before 0.3.2. */
const PLAIN_TRACES = {
kind: "cloudwatch_traces",
region: "${{ inputs.aws.region }}",
include: ["deny", "permit", "trace"],
secret: { ref: "aws://" },
};

/**
* The default X-Ray target of a Runtime profile with OTEL on, for a box that takes the log group
* and stream of a `cloudwatch_traces` target, as {@link BOX_VERSION} does.
*/
const DEFAULT_TRACES = {
...PLAIN_TRACES,
log_group: "${{ inputs.runtime.traces_log_group }}",
log_stream: "${{ inputs.runtime.traces_log_stream }}",
};

/** The words that start the agent under opentelemetry-instrument on the Runtime. */
const OTEL_START: Record<Build, string[]> = {
CodeZip: ["${{ inputs.python.path }}", "${{ inputs.code.dir }}/bin/opentelemetry-instrument"],
Expand Down Expand Up @@ -1426,7 +1444,70 @@ describe("the additions rule", () => {
expect(tableOf(apply({}, contextOf(runtimeOf(build))).table)).toEqual({
agentcore: DEFAULT_TARGET,
});
expect({ ...DEFAULT_TRACES_TARGET } as TomlTable).toEqual(DEFAULT_TRACES);
expect(runtimeTracesTarget(BOX_VERSION)).toEqual(DEFAULT_TRACES);
},
);

test.each([
["0.3.0", false],
["0.3.1", false],
["0.3.0-preview.1", false],
["0.1.11-preview.10", false],
["0.3.2-preview.1", false],
["0.3.2", true],
["0.4.0", true],
["1.0.0", true],
])(
"sends the spans of the default X-Ray target to the runtime's own log group and stream only for a box that takes them (box %s)",
(version, routed) => {
const context = contextOf(OTEL_CONTAINER, { boxVersion: version });
const traces = tableOf(apply({}, context).table).agentcore_traces;

expect(traces).toEqual(routed ? DEFAULT_TRACES : PLAIN_TRACES);
expect(defaultTracesTelemetry(apply({}, context).table, BUILD_REGION)).toEqual([
{
name: "agentcore_traces",
kind: "cloudwatch_traces",
region: BUILD_REGION,
include: PLAIN_TRACES.include,
},
]);
},
);

test("takes the box that build stages, not box.version, for the default X-Ray target and its inputs", () => {
const [older, newer] = ["0.2.4", "1.0.0"].map((version) => {
const runtime = { ...OTEL_CONTAINER, box: { version } } as ProjectRuntime;
const context = { ...contextOf(runtime), boxVersion: undefined };
return {
traces: tableOf(apply({}, context).table).agentcore_traces,
pack: renderAgentCorePack(context.spec, runtime),
};
});

expect(newer).toEqual(older!);
expect(older!.traces).toEqual(
tracesToRuntimeLogGroup(BOX_RELEASE.version) ? DEFAULT_TRACES : PLAIN_TRACES,
);
});

test.each(BUILDS)(
"declares in @box/agentcore each traces input that the Runtime profile names, for the same box (%s)",
(build) => {
const runtime = otelRuntime(build);
const context = contextOf(runtime, { boxVersion: BOX_TRACES_LOG_GROUP_VERSION });
const { profile } = deriveRuntimeProfile(createdProfile(runtime), context);
const named = [
...serializeRuntimeProfile(profile).matchAll(
/\$\{\{ inputs\.runtime\.(traces_\w+) \}\}/g,
),
].map(([, name]) => name);
const { inputs } = parse(
renderAgentCorePack(context.spec, runtime, [], BOX_TRACES_LOG_GROUP_VERSION),
) as { inputs: { runtime: TomlTable } };

expect(named).not.toEqual([]);
expect(Object.keys(inputs.runtime)).toEqual(expect.arrayContaining(named));
},
);

Expand Down Expand Up @@ -1517,10 +1598,18 @@ describe("the additions rule", () => {
expect(inputs.aws.region).toMatchObject({ from: "env:AWS_REGION" });
});

test("names the default X-Ray target in the header", () => {
const context = contextOf(OTEL_CODEZIP);
test("names the default X-Ray target in the header, with the runtime's own log group when the box takes it", () => {
const routed = contextOf(OTEL_CODEZIP);
const plain = contextOf(OTEL_CODEZIP, { boxVersion: "0.3.0" });

expect(runtimeProfileHeader(context, apply({}, context).table).at(-1)).toBe(
expect(runtimeProfileHeader(routed, apply({}, routed).table).at(-1)).toBe(
"[telemetry.agentcore_traces] is the default X-Ray target: the agent's spans and box's " +
"own, in the Runtime's region, to the runtime's own log group and log stream, which " +
"box reads from OTEL_EXPORTER_OTLP_TRACES_HEADERS when the run starts. Add a " +
`cloudwatch_traces target to ${PROFILE_FILE} to use another, or set ` +
"instrumentation.enableOtel to false in agentcore.json to send none.",
);
expect(runtimeProfileHeader(plain, apply({}, plain).table).at(-1)).toBe(
"[telemetry.agentcore_traces] is the default X-Ray target: the agent's spans and box's " +
`own, in the Runtime's region. Add a cloudwatch_traces target to ${PROFILE_FILE} to ` +
"use another, or set instrumentation.enableOtel to false in agentcore.json to send none.",
Expand Down
39 changes: 33 additions & 6 deletions src/core/project/runtimeProfile/rules/additions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* | `[tool.<name>]` | a table that the template writes, as it writes it, goes; a table of the user stays |
* | `[telemetry.<name>]` | each CloudWatch target that box takes is signed with a bare `aws://`, in the deploy region when it names none; one that box refuses goes; a `file` target goes, with a warning |
* | `[telemetry.agentcore]` | added when no target is `cloudwatch_logs`: the runtime's own log group and log stream |
* | `[telemetry.agentcore_traces]` | added with OTEL on when no target is `cloudwatch_traces`: X-Ray in the Runtime's region |
* | `[telemetry.agentcore_traces]` | added with OTEL on when no target is `cloudwatch_traces`: X-Ray in the Runtime's region, to the runtime's own log group and spans stream when the box takes them |
* | `[egress.agentcore]` | added when the runtime has its default Memory and no route of the profile has the AgentCore host: that host, which box signs with a bare `aws://` and sends to directly, not through the Box gateway |
* | a route to the AgentCore host | with a default Memory, it goes to the host directly, signed with a bare `aws://`, with its name and other keys |
*
Expand All @@ -34,8 +34,11 @@ import {
boxWorkloadPort,
OBSERVABILITY_OFF_ENV,
observabilityOff,
BOX_TRACES_LOG_GROUP_VERSION,
otelOn,
RUNTIME_LOGS_HEADERS_ENV,
RUNTIME_TRACES_HEADERS_ENV,
tracesToRuntimeLogGroup,
} from "../../templates/boxProfile";
import type { CloudWatchTelemetry } from "../../boxNetwork";
import { isTable, isVenvPython } from "../projectPaths";
Expand Down Expand Up @@ -102,7 +105,7 @@ export const DEFAULT_TELEMETRY_TARGET = {
/**
* The X-Ray target of a Runtime profile with OTEL on ({@link otelOn}) that has no
* `cloudwatch_traces` target: the agent's spans and box's own, in the Runtime's region, signed with
* the execution role.
* the execution role. {@link runtimeTracesTarget} adds the runtime's own log group and stream.
*/
export const DEFAULT_TRACES_TARGET = {
kind: "cloudwatch_traces",
Expand All @@ -111,6 +114,21 @@ export const DEFAULT_TRACES_TARGET = {
secret: { ref: RUNTIME_AWS_SIGNER },
} as const;

/**
* {@link DEFAULT_TRACES_TARGET}, with the log group and log stream that the Runtime names for its
* spans when box `boxVersion`, by default the staged box, takes them
* ({@link tracesToRuntimeLogGroup}).
*/
export function runtimeTracesTarget(boxVersion?: string): TomlTable {
return {
...structuredClone(DEFAULT_TRACES_TARGET),
...(tracesToRuntimeLogGroup(boxVersion) && {
log_group: RUNTIME_INPUTS.tracesLogGroup,
log_stream: RUNTIME_INPUTS.tracesLogStream,
}),
};
}

/** The program that starts a Python command with the OpenTelemetry instrumentation. */
const OTEL_INSTRUMENT = "opentelemetry-instrument";

Expand Down Expand Up @@ -180,16 +198,25 @@ export function runtimeProfileHeader(
...defaultTracesTargets(profile).map(
(name) =>
`[telemetry.${name}] is the default X-Ray target: the agent's spans and box's own, in the ` +
`Runtime's region. Add a cloudwatch_traces target to ${context.profileFile} to use ` +
"Runtime's region" +
(Object.hasOwn(tableOf(tableOf(profile.telemetry)[name]), "log_group")
? `, to the runtime's own log group and log stream, which box reads from ` +
`${RUNTIME_TRACES_HEADERS_ENV} when the run starts`
: "") +
`. Add a cloudwatch_traces target to ${context.profileFile} to use ` +
"another, or set instrumentation.enableOtel to false in agentcore.json to send none.",
),
];
}

/** The name of each {@link DEFAULT_TRACES_TARGET} in the `[telemetry]` of `profile`. */
/**
* The name of each default X-Ray target in the `[telemetry]` of `profile`, in either form of
* {@link runtimeTracesTarget}.
*/
function defaultTracesTargets(profile: Readonly<TomlTable>): string[] {
const forms = [DEFAULT_TRACES_TARGET, runtimeTracesTarget(BOX_TRACES_LOG_GROUP_VERSION)];
return Object.entries(tableOf(profile.telemetry))
.filter(([, target]) => sameValue(target, DEFAULT_TRACES_TARGET))
.filter(([, target]) => forms.some((form) => sameValue(target, form)))
.map(([name]) => name);
}

Expand Down Expand Up @@ -755,7 +782,7 @@ function runtimeTelemetry(
const name = Object.hasOwn(result, "agentcore_traces")
? "agentcore_traces_default"
: "agentcore_traces";
result[name] = structuredClone(DEFAULT_TRACES_TARGET);
result[name] = runtimeTracesTarget(context.boxVersion);
}
return result;
}
Expand Down
6 changes: 6 additions & 0 deletions src/core/project/runtimeProfile/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export const RUNTIME_INPUTS = {
gatewayUrl: "${{ inputs.gateway.url }}",
/** The Runtime's region (@box/agentcore). */
awsRegion: "${{ inputs.aws.region }}",
/** The runtime's own log group, for its spans (@box/agentcore). */
tracesLogGroup: "${{ inputs.runtime.traces_log_group }}",
/** The log stream of the runtime's spans (@box/agentcore). */
tracesLogStream: "${{ inputs.runtime.traces_log_stream }}",
/** The unpacked zip of a CodeZip runtime (@box/agentcore-codezip). */
codeDir: "${{ inputs.code.dir }}",
/** The Python of a CodeZip Runtime (@box/agentcore-codezip). */
Expand Down Expand Up @@ -72,6 +76,8 @@ export interface RuntimeProfileContext {
readonly profileFile: string;
/** The region build builds for: of the Box gateway, its `bedrock` target, and the Memory route. */
readonly region: string;
/** The version of the box that runs the profile; absent: the box that build stages. */
readonly boxVersion?: string;
/** The runtime's Box gateway: its name, and its targets with the ones build adds. */
readonly gateway: {
readonly name: string;
Expand Down
36 changes: 36 additions & 0 deletions src/core/project/templates/boxProfile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,42 @@ describe("renderAgentCorePack", () => {
);
});

test.each([
["0.3.2", true, true],
["1.0.0", true, true],
["0.3.1", true, false],
["0.3.0", true, false],
["0.3.2", false, false],
])(
"reads the log group and stream of the runtime's spans from OTEL_EXPORTER_OTLP_TRACES_HEADERS only with OTEL on and a box that takes them (box %s, OTEL %p)",
(version, otel, declared) => {
const { runtime } = parse(
renderAgentCorePack(
{ name: "V3Demo" } as unknown as ProjectSpec,
{ name: "agent", ...(!otel && { instrumentation: { enableOtel: false } }) },
[],
version,
),
).inputs as Record<string, any>;

if (!declared) {
expect(runtime).not.toHaveProperty("traces_log_group");
expect(runtime).not.toHaveProperty("traces_log_stream");
return;
}
expect(runtime.traces_log_group).toEqual({
description: "The runtime's own CloudWatch Logs log group, for its spans.",
from: "env:OTEL_EXPORTER_OTLP_TRACES_HEADERS",
key: "x-aws-log-group",
});
expect(runtime.traces_log_stream).toEqual({
description: "The log stream of the runtime's spans.",
from: "env:OTEL_EXPORTER_OTLP_TRACES_HEADERS",
key: "x-aws-log-stream",
});
},
);

test("turns OTEL on when the runtime has no instrumentation, as create writes it", () => {
const text = renderAgentCorePack({ name: "V3Demo" } as unknown as ProjectSpec, {
name: "agent",
Expand Down
Loading
Loading