Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

perf(observability): bound the Redis command recorded on spans (#19536)

Niklas Semmler4acaf7285704
diff --git a/packages/shared/src/server/instrumentation/bootstrap/ioredisRequestHook.test.ts b/packages/shared/src/server/instrumentation/bootstrap/ioredisRequestHook.test.ts
new file mode 100644
index 0000000000..02f0894b06
--- /dev/null
+++ b/packages/shared/src/server/instrumentation/bootstrap/ioredisRequestHook.test.ts
@@ -0,0 +1,58 @@
+import { type Span } from "@opentelemetry/api";
+import { describe, expect, it } from "vitest";
+
+import { ioredisRequestHook } from "./ioredisRequestHook";
+
+const recordFullCommand = (cmdName: string, cmdArgs: unknown[]) => {
+ const attributes: Record<string, unknown> = {};
+ const span = {
+ setAttribute: (key: string, value: unknown) => {
+ attributes[key] = value;
+ },
+ } as unknown as Span;
+ ioredisRequestHook(span, { cmdName, cmdArgs });
+ return attributes["redis.full_command"] as string;
+};
+
+describe("ioredisRequestHook", () => {
+ it("records short commands verbatim", () => {
+ expect(recordFullCommand("get", ["prompt:p1:name:label:production"])).toBe(
+ "get prompt:p1:name:label:production",
+ );
+ });
+
+ it("bounds the attribute when a value is large", () => {
+ const value = "x".repeat(200_000);
+ const command = recordFullCommand("set", [
+ "prompt:p1:big",
+ value,
+ "EX",
+ 3600,
+ ]);
+
+ expect(command.length).toBeLessThan(1_000);
+ expect(command).toContain("set prompt:p1:big");
+ expect(command).toContain("200000 chars");
+ expect(command).toContain("EX 3600");
+ });
+
+ it("bounds the attribute when there are many arguments", () => {
+ const keys = Array.from({ length: 10_000 }, (_, i) => `key:${i}`);
+ const command = recordFullCommand("del", keys);
+
+ expect(command.length).toBeLessThanOrEqual(1_024 + "…[truncated]".length);
+ expect(command.endsWith("…[truncated]")).toBe(true);
+ expect(command.startsWith("del key:0 key:1")).toBe(true);
+ });
+
+ it("does not split a surrogate pair at the cut", () => {
+ const value = `${"x".repeat(127)}😀${"y".repeat(500)}`;
+ const command = recordFullCommand("set", ["k", value]);
+
+ expect(command).not.toMatch(/[\ud800-\udbff](?![\udc00-\udfff])/);
+ });
+
+ it("redacts AUTH arguments", () => {
+ expect(recordFullCommand("auth", ["secret"])).toBe("auth [REDACTED]");
+ });
+});
diff --git a/packages/shared/src/server/instrumentation/bootstrap/ioredisRequestHook.ts b/packages/shared/src/server/instrumentation/bootstrap/ioredisRequestHook.ts
index 9203237127..fcd707eb1f 100644
--- a/packages/shared/src/server/instrumentation/bootstrap/ioredisRequestHook.ts
+++ b/packages/shared/src/server/instrumentation/bootstrap/ioredisRequestHook.ts
@@ -2,7 +2,28 @@ import { type Span } from "@opentelemetry/api";
import { API_KEY_CACHE_KEY_PREFIX } from "../../auth/apiKeyCache";
-/** ioredisRequestHook records the Redis command on the span, redacting credentials and API key cache values. */
+// Values can be whole cached payloads (e.g. prompts); unbounded attributes
+// inflate every span the telemetry agent has to buffer.
+const MAX_ARG_LENGTH = 128;
+const MAX_COMMAND_LENGTH = 1_024;
+
+// Never split a surrogate pair: OTLP export requires valid UTF-8.
+function head(value: string, max: number): string {
+ const code = value.charCodeAt(max - 1);
+ return value.slice(0, code >= 0xd800 && code <= 0xdbff ? max - 1 : max);
+}
+
+function truncateArg(arg: string): string {
+ return arg.length > MAX_ARG_LENGTH
+ ? `${head(arg, MAX_ARG_LENGTH)}…[${arg.length} chars]`
+ : arg;
+}
+
+function truncate(value: string, max: number): string {
+ return value.length > max ? `${head(value, max)}…[truncated]` : value;
+}
+
+/** ioredisRequestHook records the Redis command on the span, redacting credentials and API key cache values and bounding its length. */
export function ioredisRequestHook(
span: Span,
{ cmdName, cmdArgs }: { cmdName: string; cmdArgs: unknown[] },
@@ -19,5 +40,11 @@ export function ioredisRequestHook(
args[i] = "[REDACTED]";
}
}
- span.setAttribute("redis.full_command", `${cmdName} ${args.join(" ")}`);
+ span.setAttribute(
+ "redis.full_command",
+ truncate(
+ `${cmdName} ${args.map(truncateArg).join(" ")}`,
+ MAX_COMMAND_LENGTH,
+ ),
+ );
}