Skip to content

Commit bf3cb58

Browse files
authored
fix(telemetry): bucketize string length (#1972)
Only the string length needs to be bucketized. We can log the array size as-is.
1 parent 06b331f commit bf3cb58

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

src/telemetry/ClearcutLogger.ts

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

93+
const BUCKETS = [
94+
0, 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000,
95+
];
96+
97+
function bucketize(value: number): number {
98+
for (const bucket of BUCKETS) {
99+
if (bucket >= value) {
100+
return bucket;
101+
}
102+
}
103+
return BUCKETS[BUCKETS.length - 1];
104+
}
105+
93106
function transformValue(
94107
zodType: ZodType,
95108
value: unknown,
96109
): LoggedToolCallArgValue {
97110
if (zodType === 'ZodString') {
98-
return (value as string).length;
111+
return bucketize((value as string).length);
99112
} else if (zodType === 'ZodArray') {
100113
return (value as unknown[]).length;
101114
} 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)