Skip to content

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

test(otel): add e2e tenant isolation test for OTEL ingestion (#13622)

Niklas Semmler613995f01d4a
diff --git a/web/src/__e2e__/api.servertest.ts b/web/src/__e2e__/api.servertest.ts
index ba0b9385ed..aa27ddf4e5 100644
--- a/web/src/__e2e__/api.servertest.ts
+++ b/web/src/__e2e__/api.servertest.ts
@@ -127,8 +127,21 @@ describe("Ingestion Pipeline", () => {
expect(redis).not.toBeNull();
const redisKeys = await redis?.keys(`api-key:*`);
- expect(redisKeys?.length).toBe(1);
- const redisValue = await redis?.get(redisKeys![0]);
+ expect(redisKeys?.length ?? 0).toBeGreaterThanOrEqual(1);
+ // Concurrent e2e tests may cache additional API keys — find the seed
+ // project's key by projectId rather than assuming it is the only one.
+ let redisValue: string | null | undefined = null;
+ for (const k of redisKeys ?? []) {
+ const v = await redis?.get(k);
+ if (!v) continue;
+ try {
+ if (JSON.parse(v).projectId === projectId) {
+ redisValue = v;
+ break;
+ }
+ } catch {}
+ }
+ expect(redisValue).not.toBeNull();
const llmApiKey = OrgEnrichedApiKey.parse(JSON.parse(redisValue!));
expect(llmApiKey.projectId).toBe(
diff --git a/web/src/__e2e__/otel-tenant-isolation.servertest.ts b/web/src/__e2e__/otel-tenant-isolation.servertest.ts
new file mode 100644
index 0000000000..bd9937a91b
--- /dev/null
+++ b/web/src/__e2e__/otel-tenant-isolation.servertest.ts
@@ -0,0 +1,117 @@
+import {
+ createOrgProjectAndApiKey,
+ queryClickhouse,
+} from "@langfuse/shared/src/server";
+import { prisma } from "@langfuse/shared/src/db";
+import waitForExpect from "wait-for-expect";
+import { randomBytes } from "crypto";
+import { afterAll } from "vitest";
+
+describe("OTEL ingestion tenant isolation", () => {
+ const createdOrgIds: string[] = [];
+
+ afterAll(async () => {
+ if (createdOrgIds.length === 0) return;
+ await prisma.organization.deleteMany({
+ where: { id: { in: createdOrgIds } },
+ });
+ });
+
+ it("span posted with project A's key lands only in project A's observations", async () => {
+ const projectA = await createOrgProjectAndApiKey();
+ const projectB = await createOrgProjectAndApiKey();
+ createdOrgIds.push(projectA.orgId, projectB.orgId);
+
+ const traceId = randomBytes(16);
+ const spanId = randomBytes(8);
+ const spanIdHex = spanId.toString("hex");
+
+ const payload = {
+ resourceSpans: [
+ {
+ resource: { attributes: [] },
+ scopeSpans: [
+ {
+ scope: {
+ name: "langfuse-sdk",
+ version: "1.0.0",
+ attributes: [],
+ },
+ spans: [
+ {
+ traceId,
+ spanId,
+ name: "tenant-isolation-test-span",
+ kind: 1,
+ startTimeUnixNano: {
+ low: 466848096,
+ high: 406528574,
+ unsigned: true,
+ },
+ endTimeUnixNano: {
+ low: 467248096,
+ high: 406528574,
+ unsigned: true,
+ },
+ attributes: [],
+ status: {},
+ },
+ ],
+ },
+ ],
+ },
+ ],
+ };
+
+ const response = await fetch(
+ "http://localhost:3000/api/public/otel/v1/traces",
+ {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ Authorization: projectA.auth,
+ },
+ body: JSON.stringify(payload),
+ },
+ );
+ expect(response.status).toBe(200);
+
+ await waitForExpect(
+ async () => {
+ const rowsA = await queryClickhouse<{ count: string }>({
+ // ReplacingMergeTree: dedup with `LIMIT 1 BY id, project_id` so a
+ // retry-induced duplicate insert (the OtelIngestionQueue is
+ // configured with attempts: 6) cannot inflate the count and make
+ // the strict toBe(1) fail for a non-isolation reason.
+ query: `SELECT count() as count FROM (
+ SELECT id
+ FROM observations
+ WHERE project_id = {projectId: String} AND id = {spanId: String}
+ ORDER BY event_ts DESC
+ LIMIT 1 BY id, project_id
+ )`,
+ params: { projectId: projectA.projectId, spanId: spanIdHex },
+ });
+ expect(Number(rowsA[0]?.count)).toBe(1);
+
+ const rowsB = await queryClickhouse<{ count: string }>({
+ // ReplacingMergeTree: dedup with `LIMIT 1 BY id, project_id` so a
+ // retry-induced duplicate insert (the OtelIngestionQueue is
+ // configured with attempts: 6) cannot inflate the count and make
+ // the strict toBe(1) fail for a non-isolation reason.
+ query: `SELECT count() as count FROM (
+ SELECT id
+ FROM observations
+ WHERE project_id = {projectId: String} AND id = {spanId: String}
+ ORDER BY event_ts DESC
+ LIMIT 1 BY id, project_id
+ )`,
+ params: { projectId: projectB.projectId, spanId: spanIdHex },
+ });
+ expect(Number(rowsB[0]?.count)).toBe(0);
+ },
+ 40_000,
+ 1_000,
+ );
+ }, 60_000);
+});