Skip to content

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

feat(api): attribute legacy GET rejections to clients (#17539)

Max Deichmannd3a277e02f85
diff --git a/web/src/__tests__/server/unit/create-authed-project-api-route-auth-errors.servertest.ts b/web/src/__tests__/server/unit/create-authed-project-api-route-auth-errors.servertest.ts
index 877073634d..77f8465c8b 100644
--- a/web/src/__tests__/server/unit/create-authed-project-api-route-auth-errors.servertest.ts
+++ b/web/src/__tests__/server/unit/create-authed-project-api-route-auth-errors.servertest.ts
@@ -11,6 +11,7 @@ const {
mockLoggerDebug,
mockLoggerInfo,
mockRecordIncrement,
+ mockExtractPublicApiCallerAttribution,
mockFindOrganization,
mockCreateStructuredPublicApiAuthError,
mockSendStructuredPublicApiErrorResponse,
@@ -23,6 +24,17 @@ const {
mockLoggerDebug: vi.fn(),
mockLoggerInfo: vi.fn(),
mockRecordIncrement: vi.fn(),
+ mockExtractPublicApiCallerAttribution: vi.fn(
+ (): {
+ userAgent?: string;
+ sdkName?: string;
+ sdkVersion?: string;
+ } => ({
+ userAgent: "langfuse-python/4.8.1",
+ sdkName: "python",
+ sdkVersion: "4.8.1",
+ }),
+ ),
mockFindOrganization: vi.fn(),
mockCreateStructuredPublicApiAuthError: vi.fn((value) => value),
mockSendStructuredPublicApiErrorResponse: vi.fn(),
@@ -64,6 +76,7 @@ vi.mock("@langfuse/shared/src/server", () => ({
error: vi.fn(),
},
recordIncrement: mockRecordIncrement,
+ extractPublicApiCallerAttribution: mockExtractPublicApiCallerAttribution,
traceException: mockTraceException,
contextWithLangfuseProps: vi.fn(() => ({})),
ClickHouseClientManager: {
@@ -173,6 +186,9 @@ describe("createAuthedProjectAPIRoute auth error handling", () => {
url: "/api/public/test",
headers: {
authorization: "Basic test",
+ "user-agent": "langfuse-python/4.8.1",
+ "x-langfuse-sdk-name": "langfuse-python",
+ "x-langfuse-sdk-version": "4.8.1",
},
query: {},
});
@@ -213,6 +229,17 @@ describe("createAuthedProjectAPIRoute auth error handling", () => {
expect(mockRecordIncrement).toHaveBeenCalledWith(
"langfuse.public_api.legacy_get_rejected",
1,
+ {
+ sdkName: "python",
+ sdkVersion: "4.8.1",
+ },
+ );
+ expect(mockExtractPublicApiCallerAttribution).toHaveBeenCalledWith(
+ expect.objectContaining({
+ "user-agent": "langfuse-python/4.8.1",
+ "x-langfuse-sdk-name": "langfuse-python",
+ "x-langfuse-sdk-version": "4.8.1",
+ }),
);
expect(mockFindOrganization).not.toHaveBeenCalled();
expect(mockLoggerInfo).toHaveBeenCalledWith(
@@ -224,10 +251,36 @@ describe("createAuthedProjectAPIRoute auth error handling", () => {
apiPath: "GET /api/public/test",
organizationCreatedAt: "2026-09-16T00:00:00.000Z",
cutoff: "2026-09-16T00:00:00.000Z",
+ userAgent: "langfuse-python/4.8.1",
+ sdkName: "python",
+ sdkVersion: "4.8.1",
},
);
});
+ it("keeps a caller-controlled user agent out of the rejection counter tags", async () => {
+ mockEnv.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = "US";
+ mockEnv.LANGFUSE_API_ORGANIZATION_CUTOFF_ENABLED = "true";
+ mockVerifyAuthHeaderAndReturnScope.mockResolvedValueOnce(validAuth);
+ const unrecognizedClient = `curl/${"x".repeat(251)}`;
+ mockExtractPublicApiCallerAttribution.mockReturnValueOnce({
+ userAgent: unrecognizedClient,
+ });
+
+ const res = await callRoute({ deprecation });
+
+ expect(res.statusCode).toBe(410);
+ expect(mockRecordIncrement).toHaveBeenCalledWith(
+ "langfuse.public_api.legacy_get_rejected",
+ 1,
+ {},
+ );
+ expect(mockLoggerInfo).toHaveBeenCalledWith(
+ "Rejected legacy GET API request for organization created at or after cutoff",
+ expect.objectContaining({ userAgent: unrecognizedClient }),
+ );
+ });
+
it("keeps deprecated GET routes available when the cutoff is disabled", async () => {
mockEnv.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = "US";
mockVerifyAuthHeaderAndReturnScope.mockResolvedValueOnce(validAuth);
diff --git a/web/src/features/public-api/server/legacyApiOrganizationCutoff.ts b/web/src/features/public-api/server/legacyApiOrganizationCutoff.ts
index 0cc4eef943..627ae50b7b 100644
--- a/web/src/features/public-api/server/legacyApiOrganizationCutoff.ts
+++ b/web/src/features/public-api/server/legacyApiOrganizationCutoff.ts
@@ -1,6 +1,7 @@
import { type NextApiRequest } from "next";
import { type ApiDeprecationInfo } from "@langfuse/shared";
import {
+ extractPublicApiCallerAttribution,
logger,
recordIncrement,
type ApiAccessScopeWithOptionalApiKeyId,
@@ -41,7 +42,21 @@ export function applyLegacyApiOrganizationCutoff(params: {
}
const apiPath = clickHouseRouteForRequest(params.req);
- recordIncrement("langfuse.public_api.legacy_get_rejected", 1);
+ const callerAttribution = extractPublicApiCallerAttribution(
+ params.req.headers,
+ );
+ // `sdkName` is canonicalized and `sdkVersion` is only set for recognized SDK
+ // releases, so both are bounded enough to tag a counter with. `userAgent` is
+ // free-form client input that would add one time series per distinct value,
+ // so it is only available on the log line below.
+ recordIncrement("langfuse.public_api.legacy_get_rejected", 1, {
+ ...(callerAttribution.sdkName
+ ? { sdkName: callerAttribution.sdkName }
+ : {}),
+ ...(callerAttribution.sdkVersion
+ ? { sdkVersion: callerAttribution.sdkVersion }
+ : {}),
+ });
logger.info(
"Rejected legacy GET API request for organization created at or after cutoff",
{
@@ -51,6 +66,7 @@ export function applyLegacyApiOrganizationCutoff(params: {
apiPath,
organizationCreatedAt: params.scope.organizationCreatedAt,
cutoff: cutoff.toISOString(),
+ ...callerAttribution,
},
);