Skip to content

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

feat(api): add `fromTimestamp` and `toTimestamp` to `GET metrics/daily` (#1740)

Marc Klingen12a2459a1d20
diff --git a/fern/apis/server/definition/metrics.yml b/fern/apis/server/definition/metrics.yml
index b3746d88d4..38757314c3 100644
--- a/fern/apis/server/definition/metrics.yml
+++ b/fern/apis/server/definition/metrics.yml
@@ -29,6 +29,12 @@ service:
type: optional<string>
allow-multiple: true
docs: Optional filter for metrics where traces include all of these tags
+ fromTimestamp:
+ type: optional<datetime>
+ docs: Optional filter to only include traces on or after a certain timestamp
+ toTimestamp:
+ type: optional<datetime>
+ docs: Optional filter to only include traces before a certain timestamp
response: DailyMetrics
types:
DailyMetrics:
diff --git a/generated/postman/collection.json b/generated/postman/collection.json
index 8702aaadb8..77cf5a517d 100644
--- a/generated/postman/collection.json
+++ b/generated/postman/collection.json
@@ -379,7 +379,7 @@
"request": {
"description": "Get daily metrics of the Langfuse project",
"url": {
- "raw": "{{baseUrl}}/api/public/metrics/daily?page=&limit=&traceName=&userId=&tags=",
+ "raw": "{{baseUrl}}/api/public/metrics/daily?page=&limit=&traceName=&userId=&tags=&fromTimestamp=&toTimestamp=",
"host": [
"{{baseUrl}}"
],
@@ -414,6 +414,16 @@
"key": "tags",
"value": "",
"description": "Optional filter for metrics where traces include all of these tags"
+ },
+ {
+ "key": "fromTimestamp",
+ "value": "",
+ "description": "Optional filter to only include traces on or after a certain timestamp"
+ },
+ {
+ "key": "toTimestamp",
+ "value": "",
+ "description": "Optional filter to only include traces before a certain timestamp"
}
],
"variable": []
diff --git a/web/generated/openapi-server/openapi.yml b/web/generated/openapi-server/openapi.yml
index 99d1c95022..eae54ecf6e 100644
--- a/web/generated/openapi-server/openapi.yml
+++ b/web/generated/openapi-server/openapi.yml
@@ -482,6 +482,24 @@ paths:
items:
type: string
nullable: true
+ - name: fromTimestamp
+ in: query
+ description: >-
+ Optional filter to only include traces on or after a certain
+ timestamp
+ required: false
+ schema:
+ type: string
+ format: date-time
+ nullable: true
+ - name: toTimestamp
+ in: query
+ description: Optional filter to only include traces before a certain timestamp
+ required: false
+ schema:
+ type: string
+ format: date-time
+ nullable: true
responses:
'200':
description: ''
diff --git a/web/src/pages/api/public/metrics/daily.ts b/web/src/pages/api/public/metrics/daily.ts
index 818c79c992..db7508a40f 100644
--- a/web/src/pages/api/public/metrics/daily.ts
+++ b/web/src/pages/api/public/metrics/daily.ts
@@ -5,12 +5,15 @@ import { Prisma, prisma } from "@langfuse/shared/src/db";
import { verifyAuthHeaderAndReturnScope } from "@/src/features/public-api/server/apiAuth";
import { paginationZod } from "@/src/utils/zod";
import { isPrismaException } from "@/src/utils/exceptions";
+import { stringDate } from "@/src/features/public-api/server/ingestion-api-schema";
const GetUsageSchema = z.object({
...paginationZod,
traceName: z.string().nullish(),
userId: z.string().nullish(),
tags: z.union([z.array(z.string()), z.string()]).nullish(),
+ fromTimestamp: stringDate,
+ toTimestamp: stringDate,
});
export default async function handler(
@@ -52,6 +55,12 @@ export default async function handler(
", ",
)}] <@ t."tags"`
: Prisma.empty;
+ const fromTimestampCondition = obj.fromTimestamp
+ ? Prisma.sql`AND t."timestamp" >= ${obj.fromTimestamp}::timestamp with time zone at time zone 'UTC'`
+ : Prisma.empty;
+ const toTimestampCondition = obj.toTimestamp
+ ? Prisma.sql`AND t."timestamp" < ${obj.toTimestamp}::timestamp with time zone at time zone 'UTC'`
+ : Prisma.empty;
const usage = await prisma.$queryRaw`
WITH model_usage AS (
@@ -70,6 +79,8 @@ export default async function handler(
${traceNameCondition}
${userCondition}
${tagsCondition}
+ ${fromTimestampCondition}
+ ${toTimestampCondition}
GROUP BY
1,
2
@@ -104,6 +115,8 @@ export default async function handler(
${traceNameCondition}
${userCondition}
${tagsCondition}
+ ${fromTimestampCondition}
+ ${toTimestampCondition}
GROUP BY 1
)
SELECT
@@ -128,6 +141,8 @@ export default async function handler(
${traceNameCondition}
${userCondition}
${tagsCondition}
+ ${fromTimestampCondition}
+ ${toTimestampCondition}
`;
const totalItems =