Skip to content

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

fix(observability): remove user emails from Datadog traces (#15908)

Max Deichmann017ba00e5080
diff --git a/packages/shared/src/server/instrumentation/index.test.ts b/packages/shared/src/server/instrumentation/index.test.ts
index de2628f6f4..e7f1cb316c 100644
--- a/packages/shared/src/server/instrumentation/index.test.ts
+++ b/packages/shared/src/server/instrumentation/index.test.ts
@@ -1,9 +1,14 @@
-import { context } from "@opentelemetry/api";
+import {
+ context,
+ propagation,
+ ROOT_CONTEXT,
+ type Span,
+} from "@opentelemetry/api";
import { AsyncLocalStorageContextManager } from "@opentelemetry/context-async-hooks";
-import { afterAll, beforeAll, describe, expect, it } from "vitest";
+import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { normalizeClickHouseQueryTags } from "../clickhouse/queryTags";
import { contextWithLangfuseProps } from "../headerPropagation";
-import { instrumentAsync, instrumentSync } from ".";
+import { addUserToSpan, instrumentAsync, instrumentSync } from ".";
describe("instrumentation baggage propagation", () => {
// Baggage only propagates through context.with once a manager is registered.
@@ -56,4 +61,27 @@ describe("instrumentation baggage propagation", () => {
projectId: "project-1",
});
});
+
+ it("does not add user emails to span attributes or baggage", () => {
+ const span = {
+ setAttribute: vi.fn(),
+ } as unknown as Span;
+ const attributes = {
+ userId: "user-1",
+ email: "[email protected]",
+ } as Parameters<typeof addUserToSpan>[0];
+
+ const userContext = context.with(ROOT_CONTEXT, () =>
+ addUserToSpan(attributes, span),
+ );
+
+ expect(span.setAttribute).toHaveBeenCalledWith("user.id", "user-1");
+ expect(span.setAttribute).not.toHaveBeenCalledWith(
+ "user.email",
+ );
+ expect(propagation.getBaggage(userContext!)?.getEntry("user.email")).toBe(
+ undefined,
+ );
+ });
});
diff --git a/packages/shared/src/server/instrumentation/index.ts b/packages/shared/src/server/instrumentation/index.ts
index 254b41539e..7ef92ddefb 100644
--- a/packages/shared/src/server/instrumentation/index.ts
+++ b/packages/shared/src/server/instrumentation/index.ts
@@ -210,7 +210,6 @@ export const addUserToSpan = (
attributes: {
userId?: string;
projectId?: string;
- email?: string;
orgId?: string;
plan?: string;
apiKeyId?: string;
@@ -235,12 +234,6 @@ export const addUserToSpan = (
});
activeSpan.setAttribute("user.id", attributes.userId);
}
- if (attributes.email) {
- baggage = baggage.setEntry("user.email", {
- value: attributes.email,
- });
- activeSpan.setAttribute("user.email", attributes.email);
- }
if (attributes.projectId) {
baggage = baggage.setEntry("langfuse.project.id", {
value: attributes.projectId,
diff --git a/web/src/__tests__/server/in-app-agent-api-route-auth.servertest.ts b/web/src/__tests__/server/in-app-agent-api-route-auth.servertest.ts
index e8e15b4607..f1eb1a3eb1 100644
--- a/web/src/__tests__/server/in-app-agent-api-route-auth.servertest.ts
+++ b/web/src/__tests__/server/in-app-agent-api-route-auth.servertest.ts
@@ -209,7 +209,6 @@ describe("in-app agent public API route auth", () => {
expect(instrumentationMocks.addUserToSpan).toHaveBeenCalledWith({
userId: "user-1",
- email: "[email protected]",
});
});
@@ -1090,7 +1089,6 @@ describe("in-app agent public API route auth", () => {
);
expect(instrumentationMocks.addUserToSpan).toHaveBeenLastCalledWith({
userId: sessionUser.id,
- email: sessionUser.email,
projectId: project.id,
orgId: org.id,
plan: "cloud:team",
diff --git a/web/src/__tests__/server/unit/getServerAuthSession-invalid-callback-url.servertest.ts b/web/src/__tests__/server/unit/getServerAuthSession-invalid-callback-url.servertest.ts
index 4bd338df1f..eba17f3d7f 100644
--- a/web/src/__tests__/server/unit/getServerAuthSession-invalid-callback-url.servertest.ts
+++ b/web/src/__tests__/server/unit/getServerAuthSession-invalid-callback-url.servertest.ts
@@ -6,14 +6,34 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { getCookieName } from "@/src/server/utils/cookies";
-const { mockGetServerSession } = vi.hoisted(() => ({
- mockGetServerSession: vi.fn(),
-}));
+const { mockAuthSpan, mockGetServerSession, mockInstrumentAsync } = vi.hoisted(
+ () => {
+ const mockAuthSpan = {
+ setAttributes: vi.fn(),
+ };
+
+ return {
+ mockAuthSpan,
+ mockGetServerSession: vi.fn(),
+ mockInstrumentAsync: vi.fn(
+ async (
+ _options: unknown,
+ callback: (span: typeof mockAuthSpan) => unknown,
+ ) => callback(mockAuthSpan),
+ ),
+ };
+ },
+);
vi.mock("next-auth", () => ({
getServerSession: mockGetServerSession,
}));
+vi.mock("@langfuse/shared/src/server", async (importOriginal) => ({
+ ...(await importOriginal<Record<string, unknown>>()),
+ instrumentAsync: mockInstrumentAsync,
+}));
+
vi.mock("@/src/ee/features/multi-tenant-sso/utils", () => ({
findMultiTenantSsoConfig: vi.fn(),
getSsoAuthProviderIdForDomain: vi.fn(),
@@ -111,4 +131,24 @@ describe("getServerAuthSession invalid callback URL handling", () => {
}),
).toBe("http://localhost:3000");
});
+
+ it("does not attach the user email to the sign-in span", async () => {
+ const authOptions = await getAuthOptions();
+ const signIn = authOptions.callbacks?.signIn;
+ if (!signIn) throw new Error("Expected a sign-in callback");
+
+ await signIn({
+ user: { id: "user-1", email: "[email protected]" },
+ account: null,
+ profile: undefined,
+ email: undefined,
+ credentials: undefined,
+ });
+
+ expect(mockAuthSpan.setAttributes).not.toHaveBeenCalledWith(
+ expect.objectContaining({
+ "auth.email": "[email protected]",
+ }),
+ );
+ });
});
diff --git a/web/src/features/in-app-agent/server/handler.ts b/web/src/features/in-app-agent/server/handler.ts
index 3da7d4b912..008fdf46a7 100644
--- a/web/src/features/in-app-agent/server/handler.ts
+++ b/web/src/features/in-app-agent/server/handler.ts
@@ -116,7 +116,6 @@ export default async function handler(request: Request) {
addUserToSpan({
userId,
- email: user.email ?? undefined,
});
const bodyResult = await readBoundedJsonBody(
@@ -231,7 +230,6 @@ export default async function handler(request: Request) {
addUserToSpan({
userId,
- email: user.email ?? undefined,
projectId: rateLimitScope.projectId ?? undefined,
orgId: rateLimitScope.orgId,
plan: rateLimitScope.plan,
diff --git a/web/src/features/in-app-agent/server/watchHandler.ts b/web/src/features/in-app-agent/server/watchHandler.ts
index 290466a3a8..5e7475ace1 100644
--- a/web/src/features/in-app-agent/server/watchHandler.ts
+++ b/web/src/features/in-app-agent/server/watchHandler.ts
@@ -26,7 +26,7 @@ export default async function watchHandler(request: Request) {
const user = session.user;
- addUserToSpan({ userId: user.id, email: user.email ?? undefined });
+ addUserToSpan({ userId: user.id });
const url = new URL(request.url);
const query = WatchQuerySchema.safeParse({
diff --git a/web/src/server/api/trpc.ts b/web/src/server/api/trpc.ts
index 503fb5e856..6d9d49dfb6 100644
--- a/web/src/server/api/trpc.ts
+++ b/web/src/server/api/trpc.ts
@@ -65,7 +65,6 @@ export const createTRPCContext = async (opts: CreateNextContextOptions) => {
addUserToSpan({
userId: session?.user?.id,
- email: session?.user?.email ?? undefined,
});
return createInnerTRPCContext({ session, headers });
diff --git a/web/src/server/auth.ts b/web/src/server/auth.ts
index b7209499b5..281633bd5e 100644
--- a/web/src/server/auth.ts
+++ b/web/src/server/auth.ts
@@ -844,7 +844,6 @@ export async function getAuthOptions(signupAttribution?: {
},
});
- span.setAttribute("langfuse.user.email", dbUser?.email ?? "");
span.setAttribute("langfuse.user.id", dbUser?.id ?? "");
// V4 preview availability is governed by the write mode:
// - events_only: the legacy traces/observations tables are no longer
@@ -993,7 +992,7 @@ export async function getAuthOptions(signupAttribution?: {
});
},
async signIn({ user, account, profile }) {
- return instrumentAsync({ name: "next-auth-sign-in" }, async (span) => {
+ return instrumentAsync({ name: "next-auth-sign-in" }, async () => {
// Block sign in without valid user.email
const email = user.email?.toLowerCase();
if (!email) {
@@ -1005,9 +1004,6 @@ export async function getAuthOptions(signupAttribution?: {
throw new Error("Invalid email found in user object");
}
- span.setAttributes({
- "auth.email": email,
- });
// EE: Check custom SSO enforcement, enforce the specific SSO provider on email domain
// This also blocks setting a password for an email that is enforced to use SSO via password reset flow
const userDomain = email.split("@")[1].toLowerCase();