Skip to content

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

feat(telemetry): tag spans with project size classification (#18939)

tobi_kl422710577fa1
diff --git a/.env.dev.example b/.env.dev.example
index 1c5e007786..bba3a84935 100644
--- a/.env.dev.example
+++ b/.env.dev.example
@@ -71,6 +71,10 @@ CLICKHOUSE_CLUSTER_ENABLED="false"
NEXTAUTH_URL="http://localhost:3000"
NEXTAUTH_SECRET="secret"
+# Comma-separated project IDs classified as large in backend telemetry.
+# Unlisted projects are classified as unknown; unset or empty flags none as large.
+# LANGFUSE_LARGE_PROJECT_IDS=project-a,project-b
+
# Internal Topics PoC only; NOT PART OF THE SELF-HOSTED SUPPORTED ENV VARS!
# Trace and project deletion clean Topics data even when processing is disabled.
# LANGFUSE_TOPICS_ENABLED=false
diff --git a/.env.prod.example b/.env.prod.example
index d229cbc358..4788b3bfad 100644
--- a/.env.prod.example
+++ b/.env.prod.example
@@ -61,6 +61,10 @@ OTEL_SERVICE_NAME="langfuse"
# Add ECS task/cluster and container.id resource attributes to every span
# OTEL_CONTAINER_RESOURCE_DETECTORS_ENABLED=false
+# Comma-separated project IDs classified as large in backend telemetry.
+# Unlisted projects are classified as unknown; unset or empty flags none as large.
+# LANGFUSE_LARGE_PROJECT_IDS=project-a,project-b
+
# Default role for users who sign up, optional, can be org or org+project
# Supports comma-separated IDs for multiple orgs (e.g., "org1,org2,org3")
# LANGFUSE_DEFAULT_ORG_ID=
diff --git a/packages/shared/src/env.ts b/packages/shared/src/env.ts
index 84a51de536..d46b537f66 100644
--- a/packages/shared/src/env.ts
+++ b/packages/shared/src/env.ts
@@ -280,6 +280,18 @@ const EnvSchema = z.object({
.number()
.nonnegative()
.default(5_000),
+ LANGFUSE_LARGE_PROJECT_IDS: z
+ .string()
+ .default("")
+ .transform(
+ (value) =>
+ new Set(
+ value
+ .split(",")
+ .map((id) => id.trim())
+ .filter(Boolean),
+ ),
+ ),
LANGFUSE_DELETE_SKIP_PROJECT_IDS: z
.string()
.optional()
diff --git a/packages/shared/src/server/headerPropagation.ts b/packages/shared/src/server/headerPropagation.ts
index 639b1ef463..829a45fee9 100644
--- a/packages/shared/src/server/headerPropagation.ts
+++ b/packages/shared/src/server/headerPropagation.ts
@@ -1,6 +1,7 @@
import * as opentelemetry from "@opentelemetry/api";
import type { IncomingHttpHeaders } from "http";
import { env } from "../env";
+import { getProjectSize } from "./projectSize";
import {
CLICKHOUSE_QUERY_TAG_BAGGAGE_KEYS,
CLICKHOUSE_ROUTE_CONTEXT_KEY,
@@ -104,6 +105,9 @@ export const contextWithLangfuseProps = (
baggage = baggage.setEntry("langfuse.project.id", {
value: props.projectId,
});
+ baggage = baggage.setEntry("langfuse.project.size", {
+ value: getProjectSize(props.projectId),
+ });
}
if (props.apiKeyId) {
baggage = baggage.setEntry("langfuse.api_key.id", {
diff --git a/packages/shared/src/server/instrumentation/index.ts b/packages/shared/src/server/instrumentation/index.ts
index cae9ee3504..2106f8ac3a 100644
--- a/packages/shared/src/server/instrumentation/index.ts
+++ b/packages/shared/src/server/instrumentation/index.ts
@@ -7,6 +7,7 @@ import * as opentelemetry from "@opentelemetry/api";
import * as dd from "dd-trace";
import { env } from "../../env";
import { logger } from "../logger";
+import { getProjectSize } from "../projectSize";
import { CLICKHOUSE_ROUTE_CONTEXT_KEY } from "../clickhouse/queryTags";
export type TCarrier = {
@@ -206,12 +207,20 @@ export const addUserToSpan = (
},
span?: opentelemetry.Span,
) => {
- const activeSpan = span ?? getCurrentSpan();
+ const currentSpan = getCurrentSpan();
+ const activeSpan = span ?? currentSpan;
if (!activeSpan) {
return;
}
+ const setSpanAttribute = (key: string, value: string) => {
+ activeSpan.setAttribute(key, value);
+ if (currentSpan && currentSpan !== activeSpan) {
+ currentSpan.setAttribute(key, value);
+ }
+ };
+
const ctx = opentelemetry.context.active();
let baggage =
opentelemetry.propagation.getBaggage(ctx) ??
@@ -221,40 +230,40 @@ export const addUserToSpan = (
baggage = baggage.setEntry("user.id", {
value: attributes.userId,
});
- activeSpan.setAttribute("user.id", attributes.userId);
+ setSpanAttribute("user.id", attributes.userId);
}
if (attributes.projectId) {
baggage = baggage.setEntry("langfuse.project.id", {
value: attributes.projectId,
});
- activeSpan.setAttribute("langfuse.project.id", attributes.projectId);
+ setSpanAttribute("langfuse.project.id", attributes.projectId);
+ const projectSize = getProjectSize(attributes.projectId);
+ baggage = baggage.setEntry("langfuse.project.size", { value: projectSize });
+ setSpanAttribute("langfuse.project.size", projectSize);
}
if (attributes.orgId) {
baggage = baggage.setEntry("langfuse.org.id", {
value: attributes.orgId,
});
- activeSpan.setAttribute("langfuse.org.id", attributes.orgId);
+ setSpanAttribute("langfuse.org.id", attributes.orgId);
}
if (attributes.plan) {
baggage = baggage.setEntry("langfuse.org.plan", {
value: attributes.plan,
});
- activeSpan.setAttribute("langfuse.org.plan", attributes.plan);
+ setSpanAttribute("langfuse.org.plan", attributes.plan);
}
if (attributes.apiKeyId) {
baggage = baggage.setEntry("langfuse.api_key.id", {
value: attributes.apiKeyId,
});
- activeSpan.setAttribute("langfuse.api_key.id", attributes.apiKeyId);
+ setSpanAttribute("langfuse.api_key.id", attributes.apiKeyId);
}
if (attributes.publicKey) {
baggage = baggage.setEntry("langfuse.api_key.public_key", {
value: attributes.publicKey,
});
- activeSpan.setAttribute(
- "langfuse.api_key.public_key",
- attributes.publicKey,
- );
+ setSpanAttribute("langfuse.api_key.public_key", attributes.publicKey);
}
return opentelemetry.propagation.setBaggage(ctx, baggage);
diff --git a/packages/shared/src/server/projectSize.ts b/packages/shared/src/server/projectSize.ts
new file mode 100644
index 0000000000..d70c595c93
--- /dev/null
+++ b/packages/shared/src/server/projectSize.ts
@@ -0,0 +1,4 @@
+import { env } from "../env";
+
+export const getProjectSize = (projectId: string): "large" | "unknown" =>
+ env.LANGFUSE_LARGE_PROJECT_IDS?.has(projectId) ? "large" : "unknown";
diff --git a/web/src/__tests__/server/unit/public-api/enforceAuth.servertest.ts b/web/src/__tests__/server/unit/public-api/enforceAuth.servertest.ts
index e6ee0c1276..1bbe9d5dc9 100644
--- a/web/src/__tests__/server/unit/public-api/enforceAuth.servertest.ts
+++ b/web/src/__tests__/server/unit/public-api/enforceAuth.servertest.ts
@@ -245,6 +245,7 @@ describe("enforceAuth span metadata", () => {
["langfuse.api_key.id", "key_2"],
["langfuse.api_key.public_key", "pk-lf-1"],
["langfuse.project.id", PRJ],
+ ["langfuse.project.size", "unknown"],
["langfuse.org.id", ORG],
]);
});
@@ -281,6 +282,7 @@ describe("enforceAuth span metadata", () => {
["langfuse.api_key.id", "key_1"],
["langfuse.api_key.public_key", "pk-lf-1"],
["langfuse.project.id", PRJ],
+ ["langfuse.project.size", "unknown"],
["langfuse.org.id", ORG],
]);
},
@@ -310,6 +312,7 @@ describe("enforceAuth span metadata", () => {
["langfuse.api_key.id", "key_1"],
["langfuse.api_key.public_key", "pk-lf-1"],
["langfuse.project.id", "foreign"],
+ ["langfuse.project.size", "unknown"],
]);
});
@@ -364,6 +367,7 @@ describe("enforceAuth span metadata", () => {
["langfuse.api_key.id", "key_2"],
["langfuse.api_key.public_key", "pk-lf-1"],
["langfuse.project.id", PRJ],
+ ["langfuse.project.size", "unknown"],
["langfuse.org.id", ORG],
]);
});
@@ -394,6 +398,7 @@ describe("enforceAuth span metadata", () => {
expect(authSpan.setAttribute.mock.calls).toEqual([
["langfuse.authz.action", "traces:read"],
["langfuse.project.id", PRJ],
+ ["langfuse.project.size", "unknown"],
...(missing ? [] : [["langfuse.org.id", ORG]]),
]);
},