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
37 changes: 34 additions & 3 deletions src/cli/telemetry/__tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,49 @@ describe('TelemetryClient', () => {
expect(sink.metrics[0]!.attrs.check_only).toBe('true');
});

it('silently drops invalid success payloads', async () => {
it('publishes metric with unknown defaults for incomplete success payloads', async () => {
const sink = new InMemorySink();
const client = new TelemetryClient(sink);

// Missing required attrs for 'create' — should silently drop
// Missing required attrs for 'create' — should still publish with 'unknown' defaults
await client.withCommandRun(
'create',
// @ts-expect-error — intentionally incomplete
async () => ({ language: 'python' })
);

expect(sink.metrics).toHaveLength(0);
expect(sink.metrics).toHaveLength(1);
expect(sink.metrics[0]!.attrs).toMatchObject({
exit_reason: 'success',
language: 'python',
framework: 'unknown',
model_provider: 'unknown',
});
});

it('defaults invalid attrs to unknown while preserving valid ones', async () => {
const sink = new InMemorySink();
const client = new TelemetryClient(sink);

await client.withCommandRun(
'create',
// @ts-expect-error — intentionally invalid enum value
async () => ({
language: 'rust', // invalid enum
framework: 'strands',
model_provider: 'bedrock',
memory: 'shortterm',
protocol: 'mcp',
build: 'codezip',
agent_type: 'create',
network_mode: 'public',
has_agent: true,
})
);

expect(sink.metrics).toHaveLength(1);
expect(sink.metrics[0]!.attrs.language).toBe('unknown');
expect(sink.metrics[0]!.attrs.framework).toBe('strands');
});

it('records cancel when callback returns CANCELLED', async () => {
Expand Down
17 changes: 12 additions & 5 deletions src/cli/telemetry/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { classifyError, isUserError } from './error-classification.js';
import { COMMAND_SCHEMAS, type Command, type CommandAttrs, deriveCommandGroup } from './schemas/command-run.js';
import { type CommandResult, CommandResultSchema } from './schemas/common-shapes.js';
import { type CommandResult, CommandResultSchema, resilientParse } from './schemas/common-shapes.js';
import type { MetricSink } from './sinks/metric-sink.js';
import { performance } from 'perf_hooks';

Expand Down Expand Up @@ -69,17 +69,24 @@ export class TelemetryClient {
durationMs: number
): void {
try {
// CommandResult is built internally — hard parse is intentional since
// a metric without a valid exit_reason is meaningless.
CommandResultSchema.parse(result);
if (result.exit_reason !== 'failure' && result.exit_reason !== 'cancel') {
COMMAND_SCHEMAS[command].parse(attrs);
}

// Validate command attrs resiliently: invalid fields default to 'unknown'
// instead of dropping the entire metric.
// On failure/cancel the callback attrs are empty so validation is skipped.
const validatedAttrs =
result.exit_reason !== 'failure' && result.exit_reason !== 'cancel'
? resilientParse(COMMAND_SCHEMAS[command], attrs as Record<string, unknown>)
: attrs;

const otelAttrs: Record<string, string | number> = {
command_group: deriveCommandGroup(command),
command,
};

for (const obj of [result, attrs]) {
for (const obj of [result, validatedAttrs]) {
for (const [k, v] of Object.entries(obj)) {
if (typeof v === 'boolean') {
otelAttrs[k] = String(v);
Expand Down
54 changes: 53 additions & 1 deletion src/cli/telemetry/schemas/__tests__/command-run.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { COMMAND_SCHEMAS, type Command, type CommandAttrs, deriveCommandGroup } from '../command-run';
import { ResourceAttributesSchema } from '../common-attributes';
import { CommandResultSchema } from '../common-shapes';
import { CommandResultSchema, resilientParse } from '../common-shapes';
import { describe, expect, expectTypeOf, it } from 'vitest';
import { z } from 'zod';

Expand Down Expand Up @@ -170,3 +170,55 @@ describe('type safety', () => {
}
});
});

describe('resilientParse', () => {
it('passes valid attrs through unchanged', () => {
const attrs = {
language: 'python',
framework: 'strands',
model_provider: 'bedrock',
memory: 'shortterm',
protocol: 'mcp',
build: 'codezip',
agent_type: 'create',
network_mode: 'public',
has_agent: true,
};
expect(resilientParse(COMMAND_SCHEMAS.create, attrs)).toEqual(attrs);
});

it('defaults a single invalid enum field to unknown', () => {
const attrs = {
language: 'rust', // invalid
framework: 'strands',
model_provider: 'bedrock',
memory: 'shortterm',
protocol: 'mcp',
build: 'codezip',
agent_type: 'create',
network_mode: 'public',
has_agent: true,
};
const result = resilientParse(COMMAND_SCHEMAS.create, attrs);
expect(result.language).toBe('unknown');
expect(result.framework).toBe('strands');
});

it('defaults missing required fields to unknown', () => {
const result = resilientParse(COMMAND_SCHEMAS.create, { language: 'python' });
expect(result.language).toBe('python');
expect(result.framework).toBe('unknown');
expect(result.model_provider).toBe('unknown');
});

it('defaults all fields to unknown when all are invalid', () => {
const result = resilientParse(COMMAND_SCHEMAS.create, {});
for (const value of Object.values(result)) {
expect(value).toBe('unknown');
}
});

it('returns empty object for no-attrs schemas', () => {
expect(resilientParse(COMMAND_SCHEMAS['telemetry.disable'], {})).toEqual({});
});
});
18 changes: 18 additions & 0 deletions src/cli/telemetry/schemas/common-shapes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ export function safeSchema<T extends Record<string, SafeField>>(shape: T) {
return z.object(shape);
}

/**
* Validate each field in a schema individually, defaulting to 'unknown' on failure.
* This ensures a single invalid attribute never blocks the entire metric from being published.
* Keys in attrs not present in the schema are omitted from the result.
*/
export function resilientParse(
schema: z.ZodObject<z.ZodRawShape>,
attrs: Record<string, unknown>
): Record<string, unknown> {
const result: Record<string, unknown> = {};
for (const key of Object.keys(schema.shape)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

'unknown' works well for enum fields, but some schemas have z.boolean() and z.number() fields (e.g. has_agent, runtime_count). If those are missing or invalid, the metric would carry the string "unknown" where downstream consumers expect "true"/"false" or a number.

If that's intentional (OTel attrs are all strings anyway so "unknown" is a valid sentinel for any field type), a short comment here would save a future reader from wondering. Something like:

// 'unknown' is acceptable for all field types — OTel attrs are serialized as strings,
// and downstream dashboards filter on this sentinel regardless of the original type.
result[key] = parsed.success ? parsed.data : 'unknown';

Otherwise, a type-aware default (false for booleans, 0 for numbers) would keep the data clean.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Whoops sorry, missed this before merging. The strands agent called out the same thing, and my understanding is that the OTEL client will handle this for us as long as we accept it in the backend. Agree a comment could be helpful here because its confusing that we're using a different type.

const field = schema.shape[key] as z.ZodType;
const parsed = field.safeParse(attrs[key]);
result[key] = parsed.success ? parsed.data : 'unknown';
}
return result;
}

// Primitive types
export const Count = z.number().int().nonnegative();

Expand Down
Loading