Skip to content

Commit d76b81a

Browse files
committed
fix(telemetry): bucketize string length
1 parent 06b331f commit d76b81a

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

src/telemetry/ClearcutLogger.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,23 @@ export function transformArgType(zodType: ZodType): string {
9090
}
9191
}
9292

93+
const BUCKETS = [0, 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000];
94+
95+
function bucketize(value: number): number {
96+
for (const bucket of BUCKETS) {
97+
if (bucket >= value) {
98+
return bucket;
99+
}
100+
}
101+
return BUCKETS[BUCKETS.length - 1];
102+
}
103+
93104
function transformValue(
94105
zodType: ZodType,
95106
value: unknown,
96107
): LoggedToolCallArgValue {
97108
if (zodType === 'ZodString') {
98-
return (value as string).length;
109+
return bucketize((value as string).length);
99110
} else if (zodType === 'ZodArray') {
100111
return (value as unknown[]).length;
101112
} else {

tests/telemetry/ClearcutLogger.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,35 @@ describe('ClearcutLogger', () => {
235235
});
236236
});
237237

238+
it('bucketizes string lengths correctly', () => {
239+
const schema = {
240+
str0: zod.string(),
241+
str1: zod.string(),
242+
str3: zod.string(),
243+
str5: zod.string(),
244+
str10000: zod.string(),
245+
str10001: zod.string(),
246+
};
247+
248+
const params = {
249+
str0: '',
250+
str1: 'a',
251+
str3: 'abc',
252+
str5: 'abcde',
253+
str10000: 'a'.repeat(10000),
254+
str10001: 'a'.repeat(10001),
255+
};
256+
257+
const sanitized = sanitizeParams(params, schema);
258+
259+
assert.strictEqual(sanitized.str0_length, 0);
260+
assert.strictEqual(sanitized.str1_length, 1);
261+
assert.strictEqual(sanitized.str3_length, 5); // snaps to 5
262+
assert.strictEqual(sanitized.str5_length, 5);
263+
assert.strictEqual(sanitized.str10000_length, 10000);
264+
assert.strictEqual(sanitized.str10001_length, 10000); // snaps to 10000
265+
});
266+
238267
it('throws error for unsupported types', () => {
239268
const schema = {
240269
myObj: zod.object({foo: zod.string()}),

0 commit comments

Comments
 (0)