🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
chore: add env to move short-term reads to AMTs (#8367)
Steffen Schmitz10fdd9e172a9
diff --git a/packages/shared/src/env.ts b/packages/shared/src/env.tsindex 093ff15686..2a5eb1f07f 100644--- a/packages/shared/src/env.ts+++ b/packages/shared/src/env.ts@@ -148,6 +148,9 @@ const EnvSchema = z.object({LANGFUSE_EXPERIMENT_RETURN_NEW_RESULT: z.enum(["true", "false"]).default("false"),+ LANGFUSE_EXPERIMENT_RETURN_NEW_RESULT_SHORT_TERM: z+ .enum(["true", "false"])+ .default("false"),LANGFUSE_EXPERIMENT_INSERT_INTO_AGGREGATING_MERGE_TREES: z.enum(["true", "false"]).default("false"),diff --git a/packages/shared/src/server/clickhouse/measureAndReturn.ts b/packages/shared/src/server/clickhouse/measureAndReturn.tsindex 2edd05d771..509f2ad377 100644--- a/packages/shared/src/server/clickhouse/measureAndReturn.ts+++ b/packages/shared/src/server/clickhouse/measureAndReturn.ts@@ -19,12 +19,17 @@ const executionWrapper = async <T, Y>(return [res, duration];};+/**+ * Measures the execution time of two functions and returns the result based on the experiment configuration.+ * This is used to compare the execution of AggregatingMergeTrees with the existing ReplacingMergeTree execution.+ */export const measureAndReturn = async <T, Y>(args: {operationName: string;projectId: string;input: T;existingExecution: (input: T) => Promise<Y>; // eslint-disable-line no-unused-varsnewExecution: (input: T) => Promise<Y>; // eslint-disable-line no-unused-vars+ minStartTime?: Date;}): Promise<Y> => {return instrumentAsync({@@ -32,13 +37,31 @@ export const measureAndReturn = async <T, Y>(args: {spanKind: opentelemetry.SpanKind.CLIENT,},async (currentSpan) => {- const { input, existingExecution, newExecution } = args;+ const { input, existingExecution, newExecution, minStartTime } = args;if (env.LANGFUSE_EXPERIMENT_COMPARE_READ_FROM_AGGREGATING_MERGE_TREES !=="true") {currentSpan.setAttribute(`langfuse.experiment.amts.run`, "disabled");++ // Check for short-term new result experiment+ if (+ env.LANGFUSE_EXPERIMENT_RETURN_NEW_RESULT_SHORT_TERM === "true" &&+ minStartTime+ ) {+ const thirtyDaysAgo = new Date();+ thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);++ if (minStartTime >= thirtyDaysAgo) {+ currentSpan.setAttribute(+ `langfuse.experiment.amts.short-term`,+ "true",+ );+ return newExecution(input);+ }+ }+return env.LANGFUSE_EXPERIMENT_RETURN_NEW_RESULT === "true"? newExecution(input): existingExecution(input);@@ -93,6 +116,23 @@ export const measureAndReturn = async <T, Y>(args: {);}+ // Check for short-term new result experiment+ if (+ env.LANGFUSE_EXPERIMENT_RETURN_NEW_RESULT_SHORT_TERM === "true" &&+ minStartTime+ ) {+ const thirtyDaysAgo = new Date();+ thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);++ if (minStartTime >= thirtyDaysAgo) {+ currentSpan.setAttribute(+ `langfuse.experiment.amts.short-term`,+ "true",+ );+ return newResult;+ }+ }+return env.LANGFUSE_EXPERIMENT_RETURN_NEW_RESULT === "true"? newResult: existingResult;diff --git a/packages/shared/src/server/repositories/observations.ts b/packages/shared/src/server/repositories/observations.tsindex 7e31434cf1..6ea184e80d 100644--- a/packages/shared/src/server/repositories/observations.ts+++ b/packages/shared/src/server/repositories/observations.ts@@ -772,6 +772,7 @@ const getObservationsTableInternal = async <T>(return measureAndReturn({operationName: "getObservationsTableInternal",projectId,+ minStartTime: (timeFilter?.value as Date) || undefined,input: {params: {...appliedScoresFilter.params,diff --git a/packages/shared/src/server/repositories/traces.ts b/packages/shared/src/server/repositories/traces.tsindex 60705657fa..9d4b2869b7 100644--- a/packages/shared/src/server/repositories/traces.ts+++ b/packages/shared/src/server/repositories/traces.ts@@ -147,6 +147,7 @@ export const checkTraceExists = async ({return measureAndReturn({operationName: "checkTraceExists",projectId,+ minStartTime: timestamp ?? exactTimestamp,input: {params: {projectId,@@ -369,6 +370,7 @@ export const getTracesBySessionId = async (const records = await measureAndReturn({operationName: "getTracesBySessionId",projectId,+ minStartTime: timestamp,input: {params: {sessionIds,@@ -512,6 +514,7 @@ export const getTraceCountsByProjectInCreationInterval = async ({return measureAndReturn({operationName: "getTraceCountsByProjectInCreationInterval",projectId: "__CROSS_PROJECT__",+ minStartTime: start,input: {params: {start: convertDateToClickhouseDateTime(start),@@ -587,6 +590,7 @@ export const getTraceCountOfProjectsSinceCreationDate = async ({return measureAndReturn({operationName: "getTraceCountOfProjectsSinceCreationDate",projectId: "__CROSS_PROJECT__",+ minStartTime: start,input: {params: {projectIds,@@ -661,6 +665,7 @@ export const getTraceById = async ({const records = await measureAndReturn({operationName: "getTraceById",projectId,+ minStartTime: fromTimestamp ?? timestamp,input: {params: {traceId,@@ -785,6 +790,10 @@ export const getTracesGroupedByName = async (return measureAndReturn({operationName: "getTracesGroupedByName",projectId,+ minStartTime: timestampFilter?.find(+ (f) =>+ f.column === "timestamp" && (f.operator === ">=" || f.operator === ">"),+ )?.value as Date | undefined,input: {params: {projectId,@@ -880,6 +889,10 @@ export const getTracesGroupedByUsers = async (return measureAndReturn({operationName: "getTracesGroupedByUsers",projectId,+ minStartTime: filter?.find(+ (f) =>+ f.column === "timestamp" && (f.operator === ">=" || f.operator === ">"),+ )?.value as Date | undefined,input: {params: {limit,@@ -977,6 +990,10 @@ export const getTracesGroupedByTags = async (props: GroupedTracesQueryProp) => {return measureAndReturn({operationName: "getTracesGroupedByTags",projectId,+ minStartTime: filter?.find(+ (f) =>+ f.column === "timestamp" && (f.operator === ">=" || f.operator === ">"),+ )?.value as Date | undefined,input: {params: {projectId,@@ -1273,19 +1290,6 @@ export const deleteTracesOlderThanDays = async (},tags: input.tags,}),- // Delete from traces_null- commandClickhouse({- query: `- DELETE FROM traces_null- WHERE project_id = {projectId: String}- AND start_time < {cutoffDate: DateTime64(3)};- `,- params: input.params,- clickhouseConfigs: {- request_timeout: env.LANGFUSE_CLICKHOUSE_DELETION_TIMEOUT_MS,- },- tags: input.tags,- }),// Delete from traces_all_amtcommandClickhouse({query: `@@ -1502,6 +1506,10 @@ export const getTotalUserCount = async (return measureAndReturn({operationName: "getTotalUserCount",projectId,+ minStartTime: filter?.find(+ (f) =>+ f.column === "timestamp" && (f.operator === ">=" || f.operator === ">"),+ )?.value as Date | undefined,input: {params: {...tracesFilterRes.params,@@ -1651,6 +1659,10 @@ export const getUserMetrics = async (return measureAndReturn({operationName: "getUserMetrics",projectId,+ minStartTime: filter?.find(+ (f) =>+ f.column === "timestamp" && (f.operator === ">=" || f.operator === ">"),+ )?.value as Date | undefined,input: {params: {projectId,diff --git a/packages/shared/src/server/services/sessions-ui-table-service.ts b/packages/shared/src/server/services/sessions-ui-table-service.tsindex 2669dc9dea..5eed1d1d0e 100644--- a/packages/shared/src/server/services/sessions-ui-table-service.ts+++ b/packages/shared/src/server/services/sessions-ui-table-service.ts@@ -378,6 +378,11 @@ const getSessionsTableGeneric = async <T>(props: FetchSessionsTableProps) => {return measureAndReturn({operationName: "getSessionsTableGeneric",projectId,+ minStartTime: filter?.find(+ (f) =>+ f.column === "min_timestamp" &&+ (f.operator === ">=" || f.operator === ">"),+ )?.value as Date | undefined,input: {params: {projectId,diff --git a/packages/shared/src/server/services/traces-ui-table-service.ts b/packages/shared/src/server/services/traces-ui-table-service.tsindex 590b6df43a..c65551bb47 100644--- a/packages/shared/src/server/services/traces-ui-table-service.ts+++ b/packages/shared/src/server/services/traces-ui-table-service.ts@@ -350,6 +350,7 @@ async function getTracesTableGeneric(props: FetchTracesTableProps) {return measureAndReturn({operationName: "getTracesTableGeneric",projectId: props.projectId,+ minStartTime: select !== "metrics" ? timeStampFilter?.value : undefined,input: props,existingExecution: async (props) => {let sqlSelect: string;diff --git a/web/src/features/public-api/server/traces.ts b/web/src/features/public-api/server/traces.tsindex bfe5dbc857..ecb373326d 100644--- a/web/src/features/public-api/server/traces.ts+++ b/web/src/features/public-api/server/traces.ts@@ -117,6 +117,7 @@ export const generateTracesForPublicApi = async ({const result = await measureAndReturn({operationName: "getTracesForPublicApi",projectId: props.projectId,+ minStartTime: timeFilter?.value,input: {params: {...appliedEnvironmentFilter.params,diff --git a/web/src/features/query/server/queryExecutor.ts b/web/src/features/query/server/queryExecutor.tsindex 8f0cc3e8dd..07db0d284f 100644--- a/web/src/features/query/server/queryExecutor.ts+++ b/web/src/features/query/server/queryExecutor.ts@@ -48,6 +48,7 @@ export async function executeQuery(return measureAndReturn({operationName: "executeQuery",projectId,+ minStartTime: new Date(query.fromTimestamp),input: {query: compiledQuery,params: parameters,@@ -73,11 +74,7 @@ export async function executeQuery(});},newExecution: async (input) => {- const fromDate = input.fromTimestamp- ? new Date(input.fromTimestamp)- : undefined;- const traceTable = getTimeframesTracesAMT(fromDate);-+ const traceTable = getTimeframesTracesAMT(new Date(input.fromTimestamp));return queryClickhouse<Record<string, unknown>>({query: input.query.replaceAll("traces", traceTable),params: input.params,