Skip to content

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

chore: Update agent guidance for repo workflows (#13642)

Max Deichmannc19b2b318120
diff --git a/packages/shared/src/server/instrumentation/index.ts b/packages/shared/src/server/instrumentation/index.ts
index 22818cd284..faeacfbb2d 100644
--- a/packages/shared/src/server/instrumentation/index.ts
+++ b/packages/shared/src/server/instrumentation/index.ts
@@ -194,6 +194,7 @@ export const addUserToSpan = (
email?: string;
orgId?: string;
plan?: string;
+ apiKeyId?: string;
},
span?: opentelemetry.Span,
) => {
@@ -238,6 +239,9 @@ export const addUserToSpan = (
});
activeSpan.setAttribute("langfuse.org.plan", attributes.plan);
}
+ if (attributes.apiKeyId) {
+ activeSpan.setAttribute("langfuse.api_key.id", attributes.apiKeyId);
+ }
return opentelemetry.propagation.setBaggage(ctx, baggage);
};
diff --git a/web/src/__tests__/server/unit/api-auth-span.servertest.ts b/web/src/__tests__/server/unit/api-auth-span.servertest.ts
new file mode 100644
index 0000000000..2ba3023bd7
--- /dev/null
+++ b/web/src/__tests__/server/unit/api-auth-span.servertest.ts
@@ -0,0 +1,179 @@
+const {
+ addUserToSpanMock,
+ createShaHashMock,
+ fakeAuthSpan,
+ instrumentAsyncMock,
+} = vi.hoisted(() => {
+ const fakeAuthSpan = {
+ end: vi.fn(),
+ recordException: vi.fn(),
+ setAttribute: vi.fn(),
+ setAttributes: vi.fn(),
+ setStatus: vi.fn(),
+ };
+
+ return {
+ addUserToSpanMock: vi.fn(),
+ createShaHashMock: vi.fn(
+ (secretKey: string, salt: string) => `hashed:${salt}:${secretKey}`,
+ ),
+ fakeAuthSpan,
+ instrumentAsyncMock: vi.fn(async (_ctx, callback) =>
+ callback(fakeAuthSpan),
+ ),
+ };
+});
+
+vi.mock("@/src/env.mjs", () => ({
+ env: {
+ LANGFUSE_CACHE_API_KEY_ENABLED: "false",
+ LANGFUSE_CACHE_API_KEY_TTL_SECONDS: 60,
+ SALT: "test-salt",
+ },
+}));
+
+vi.mock("@/src/features/entitlements/server/getPlan", () => ({
+ getOrganizationPlanServerSide: () => "oss",
+}));
+
+vi.mock("@/src/utils/exceptions", () => ({
+ isPrismaException: () => false,
+}));
+
+vi.mock("@langfuse/shared", () => ({
+ CloudConfigSchema: {
+ parse: vi.fn((value) => value),
+ },
+ isPlan: vi.fn((plan) => typeof plan === "string"),
+}));
+
+vi.mock("@langfuse/shared/src/server", () => ({
+ API_KEY_NON_EXISTENT: "api-key-non-existent",
+ CachedApiKey: {
+ safeParse: vi.fn((value) => ({ data: value, success: true })),
+ },
+ ClickHouseClientManager: {
+ getInstance: vi.fn(() => ({
+ closeAllConnections: vi.fn(),
+ })),
+ },
+ OrgEnrichedApiKey: {
+ parse: vi.fn((value) => value),
+ },
+ addUserToSpan: addUserToSpanMock,
+ createShaHash: createShaHashMock,
+ invalidateCachedApiKeys: vi.fn(),
+ invalidateCachedOrgApiKeys: vi.fn(),
+ invalidateCachedProjectApiKeys: vi.fn(),
+ instrumentAsync: instrumentAsyncMock,
+ logger: {
+ debug: vi.fn(),
+ error: vi.fn(),
+ info: vi.fn(),
+ },
+ recordIncrement: vi.fn(),
+ redis: null,
+ verifySecretKey: vi.fn(),
+}));
+
+import { ApiAuthService } from "@/src/features/public-api/server/apiAuth";
+
+const createBasicAuthHeader = (publicKey: string, secretKey: string) =>
+ `Basic ${Buffer.from(`${publicKey}:${secretKey}`).toString("base64")}`;
+
+const createProjectApiKey = () => {
+ const now = new Date("2026-01-01T00:00:00.000Z");
+ const publicKey = "pk-lf-public";
+ const secretKey = "sk-lf-secret";
+ const projectId = "project-id";
+ const orgId = "org-id";
+
+ return {
+ apiKey: {
+ id: "api-key-id",
+ createdAt: now,
+ displaySecretKey: "sk-lf-...cret",
+ expiresAt: null,
+ fastHashedSecretKey: createShaHashMock(secretKey, "test-salt"),
+ hashedSecretKey: "legacy-hash",
+ lastUsedAt: null,
+ note: null,
+ organization: null,
+ project: {
+ id: projectId,
+ organization: {
+ id: orgId,
+ cloudConfig: null,
+ cloudFreeTierUsageThresholdState: null,
+ createdAt: now,
+ name: "Org",
+ updatedAt: now,
+ },
+ },
+ projectId,
+ publicKey,
+ scope: "PROJECT",
+ },
+ orgId,
+ projectId,
+ publicKey,
+ secretKey,
+ };
+};
+
+describe("ApiAuthService span metadata", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it("adds the api key id to the auth span for Basic auth", async () => {
+ const { apiKey, orgId, projectId, publicKey, secretKey } =
+ createProjectApiKey();
+ const prisma = {
+ apiKey: {
+ findUnique: vi.fn().mockResolvedValue(apiKey),
+ },
+ };
+
+ const result = await new ApiAuthService(
+ prisma as any,
+ null,
+ ).verifyAuthHeaderAndReturnScope(
+ createBasicAuthHeader(publicKey, secretKey),
+ );
+
+ expect(result.validKey).toBe(true);
+ expect(addUserToSpanMock).toHaveBeenCalledWith(
+ expect.objectContaining({
+ apiKeyId: apiKey.id,
+ orgId,
+ projectId,
+ }),
+ fakeAuthSpan,
+ );
+ });
+
+ it("adds the api key id to the auth span for Bearer auth", async () => {
+ const { apiKey, orgId, projectId, publicKey } = createProjectApiKey();
+ const prisma = {
+ apiKey: {
+ findUnique: vi.fn().mockResolvedValue(apiKey),
+ },
+ };
+
+ const result = await new ApiAuthService(
+ prisma as any,
+ null,
+ ).verifyAuthHeaderAndReturnScope(`Bearer ${publicKey}`);
+
+ expect(result.validKey).toBe(true);
+ expect(addUserToSpanMock).toHaveBeenCalledWith(
+ expect.objectContaining({
+ apiKeyId: apiKey.id,
+ orgId,
+ projectId,
+ }),
+ fakeAuthSpan,
+ );
+ });
+});
diff --git a/web/src/features/public-api/server/apiAuth.ts b/web/src/features/public-api/server/apiAuth.ts
index 2fde58578e..311b7cddc8 100644
--- a/web/src/features/public-api/server/apiAuth.ts
+++ b/web/src/features/public-api/server/apiAuth.ts
@@ -88,7 +88,7 @@ export class ApiAuthService {
): Promise<AuthHeaderVerificationResult> {
const result: AuthHeaderVerificationResult = await instrumentAsync(
{ name: "api-auth-verify" },
- async () => {
+ async (span) => {
if (!authHeader) {
logger.debug("No authorization header");
return {
@@ -172,11 +172,15 @@ export class ApiAuthService {
throw new Error("Invalid credentials");
}
- addUserToSpan({
- projectId: finalApiKey.projectId ?? undefined,
- orgId: finalApiKey.orgId,
- plan,
- });
+ addUserToSpan(
+ {
+ projectId: finalApiKey.projectId ?? undefined,
+ orgId: finalApiKey.orgId,
+ plan,
+ apiKeyId: finalApiKey.id,
+ },
+ span,
+ );
const accessLevel =
finalApiKey.scope === "ORGANIZATION" ? "organization" : "project";
@@ -211,11 +215,15 @@ export class ApiAuthService {
const { orgId, cloudConfig, cloudFreeTierUsageThresholdState } =
this.extractOrgIdAndCloudConfig(dbKey);
- addUserToSpan({
- projectId: dbKey.projectId ?? undefined,
- orgId,
- plan: getOrganizationPlanServerSide(cloudConfig),
- });
+ addUserToSpan(
+ {
+ projectId: dbKey.projectId ?? undefined,
+ orgId,
+ plan: getOrganizationPlanServerSide(cloudConfig),
+ apiKeyId: dbKey.id,
+ },
+ span,
+ );
return {
validKey: true,