Skip to content

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

feat(api): track api key ids in auth telemetry (#13605)

Max Deichmann04e70d09df82
diff --git a/packages/shared/src/server/headerPropagation.ts b/packages/shared/src/server/headerPropagation.ts
index 12d7b0b035..b2070b3d0e 100644
--- a/packages/shared/src/server/headerPropagation.ts
+++ b/packages/shared/src/server/headerPropagation.ts
@@ -6,6 +6,7 @@ export type LangfuseContextProps = {
headers?: IncomingHttpHeaders;
userId?: string;
projectId?: string;
+ apiKeyId?: string;
};
/**
@@ -54,6 +55,11 @@ export const contextWithLangfuseProps = (
value: props.projectId,
});
}
+ if (props.apiKeyId) {
+ baggage = baggage.setEntry("langfuse.api_key.id", {
+ value: props.apiKeyId,
+ });
+ }
return opentelemetry.propagation.setBaggage(ctx, baggage);
};
diff --git a/packages/shared/src/server/instrumentation/index.ts b/packages/shared/src/server/instrumentation/index.ts
index faeacfbb2d..da09fa9e42 100644
--- a/packages/shared/src/server/instrumentation/index.ts
+++ b/packages/shared/src/server/instrumentation/index.ts
@@ -240,6 +240,9 @@ export const addUserToSpan = (
activeSpan.setAttribute("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);
}
diff --git a/web/src/__tests__/server/unit/langfuse-context-propagation.servertest.ts b/web/src/__tests__/server/unit/langfuse-context-propagation.servertest.ts
new file mode 100644
index 0000000000..8026497f11
--- /dev/null
+++ b/web/src/__tests__/server/unit/langfuse-context-propagation.servertest.ts
@@ -0,0 +1,50 @@
+import * as opentelemetry from "@opentelemetry/api";
+import type { Span } from "@opentelemetry/api";
+import {
+ addUserToSpan,
+ contextWithLangfuseProps,
+} from "@langfuse/shared/src/server";
+
+describe("Langfuse context propagation", () => {
+ it("adds api key id to span attributes and baggage", () => {
+ const span = {
+ setAttribute: vi.fn(),
+ } as unknown as Span;
+
+ opentelemetry.context.with(opentelemetry.ROOT_CONTEXT, () => {
+ const ctx = addUserToSpan(
+ {
+ projectId: "project-1",
+ orgId: "org-1",
+ plan: "cloud:hobby",
+ apiKeyId: "api-key-1",
+ },
+ span,
+ );
+
+ expect(span.setAttribute).toHaveBeenCalledWith(
+ "langfuse.api_key.id",
+ "api-key-1",
+ );
+ expect(
+ opentelemetry.propagation
+ .getBaggage(ctx!)
+ ?.getEntry("langfuse.api_key.id")?.value,
+ ).toBe("api-key-1");
+ });
+ });
+
+ it("adds api key id to request context baggage", () => {
+ opentelemetry.context.with(opentelemetry.ROOT_CONTEXT, () => {
+ const ctx = contextWithLangfuseProps({
+ projectId: "project-1",
+ apiKeyId: "api-key-1",
+ });
+
+ const baggage = opentelemetry.propagation.getBaggage(ctx);
+
+ expect(baggage?.getEntry("langfuse.project.id")?.value).toBe("project-1");
+ expect(baggage?.getEntry("langfuse.api_key.id")?.value).toBe("api-key-1");
+ });
+ });
+});
diff --git a/web/src/features/public-api/server/apiAuth.ts b/web/src/features/public-api/server/apiAuth.ts
index 311b7cddc8..3127d60fac 100644
--- a/web/src/features/public-api/server/apiAuth.ts
+++ b/web/src/features/public-api/server/apiAuth.ts
@@ -214,12 +214,13 @@ export class ApiAuthService {
const { orgId, cloudConfig, cloudFreeTierUsageThresholdState } =
this.extractOrgIdAndCloudConfig(dbKey);
+ const plan = getOrganizationPlanServerSide(cloudConfig);
addUserToSpan(
{
projectId: dbKey.projectId ?? undefined,
orgId,
- plan: getOrganizationPlanServerSide(cloudConfig),
+ plan,
apiKeyId: dbKey.id,
},
span,
@@ -231,7 +232,7 @@ export class ApiAuthService {
projectId: dbKey.projectId,
accessLevel: "scores",
orgId,
- plan: getOrganizationPlanServerSide(cloudConfig),
+ plan,
rateLimitOverrides: cloudConfig?.rateLimitOverrides ?? [],
apiKeyId: dbKey.id,
scope: dbKey.scope,
diff --git a/web/src/features/public-api/server/createAuthedProjectAPIRoute.ts b/web/src/features/public-api/server/createAuthedProjectAPIRoute.ts
index d989cfcd8c..55bfbf2c37 100644
--- a/web/src/features/public-api/server/createAuthedProjectAPIRoute.ts
+++ b/web/src/features/public-api/server/createAuthedProjectAPIRoute.ts
@@ -370,6 +370,7 @@ export const createAuthedProjectAPIRoute = <
const ctx = contextWithLangfuseProps({
headers: req.headers,
projectId: auth.scope.projectId,
+ apiKeyId: auth.scope.apiKeyId,
});
return opentelemetry.context.with(ctx, async () => {
const response = await routeConfig.fn({
diff --git a/web/src/pages/api/public/ingestion.ts b/web/src/pages/api/public/ingestion.ts
index 1300ad0798..7b7b5ce7f2 100644
--- a/web/src/pages/api/public/ingestion.ts
+++ b/web/src/pages/api/public/ingestion.ts
@@ -96,6 +96,7 @@ export default async function handler(
const ctx = contextWithLangfuseProps({
headers: req.headers,
projectId: authCheck.scope.projectId,
+ apiKeyId: authCheck.scope.apiKeyId,
});
// Execute the rest of the handler within the context
return opentelemetry.context.with(ctx, async () => {